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 <uapi/linux/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);
74 write_lock(&nodemap->nm_idmap_lock);
75 idmap_delete_tree(nodemap);
76 write_unlock(&nodemap->nm_idmap_lock);
78 mutex_unlock(&active_config_lock);
80 if (!list_empty(&nodemap->nm_member_list))
81 CWARN("nodemap_destroy failed to reclassify all members\n");
83 nm_member_delete_list(nodemap);
85 OBD_FREE_PTR(nodemap);
91 * Functions used for the cfs_hash
93 void nodemap_getref(struct lu_nodemap *nodemap)
95 atomic_inc(&nodemap->nm_refcount);
96 CDEBUG(D_INFO, "GETting nodemap %s(p=%p) : new refcount %d\n",
97 nodemap->nm_name, nodemap, atomic_read(&nodemap->nm_refcount));
101 * Destroy nodemap if last reference is put. Should be called outside
104 void nodemap_putref(struct lu_nodemap *nodemap)
109 LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
111 CDEBUG(D_INFO, "PUTting nodemap %s(p=%p) : new refcount %d\n",
112 nodemap->nm_name, nodemap,
113 atomic_read(&nodemap->nm_refcount) - 1);
115 if (atomic_dec_and_test(&nodemap->nm_refcount))
116 nodemap_destroy(nodemap);
118 EXPORT_SYMBOL(nodemap_putref);
120 static __u32 nodemap_hashfn(struct cfs_hash *hash_body,
121 const void *key, unsigned mask)
123 return cfs_hash_djb2_hash(key, strlen(key), mask);
126 static void *nodemap_hs_key(struct hlist_node *hnode)
128 struct lu_nodemap *nodemap;
130 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
132 return nodemap->nm_name;
135 static int nodemap_hs_keycmp(const void *key,
136 struct hlist_node *compared_hnode)
140 nodemap_name = nodemap_hs_key(compared_hnode);
142 return !strcmp(key, nodemap_name);
145 static void *nodemap_hs_hashobject(struct hlist_node *hnode)
147 return hlist_entry(hnode, struct lu_nodemap, nm_hash);
150 static void nodemap_hs_get(struct cfs_hash *hs, struct hlist_node *hnode)
152 struct lu_nodemap *nodemap;
154 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
155 nodemap_getref(nodemap);
158 static void nodemap_hs_put_locked(struct cfs_hash *hs,
159 struct hlist_node *hnode)
161 struct lu_nodemap *nodemap;
163 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
164 nodemap_putref(nodemap);
167 static struct cfs_hash_ops nodemap_hash_operations = {
168 .hs_hash = nodemap_hashfn,
169 .hs_key = nodemap_hs_key,
170 .hs_keycmp = nodemap_hs_keycmp,
171 .hs_object = nodemap_hs_hashobject,
172 .hs_get = nodemap_hs_get,
173 .hs_put_locked = nodemap_hs_put_locked,
176 /* end of cfs_hash functions */
179 * Initialize nodemap_hash
182 * \retval -ENOMEM cannot create hash
184 static int nodemap_init_hash(struct nodemap_config *nmc)
186 nmc->nmc_nodemap_hash = cfs_hash_create("NODEMAP",
187 HASH_NODEMAP_CUR_BITS,
188 HASH_NODEMAP_MAX_BITS,
189 HASH_NODEMAP_BKT_BITS, 0,
192 &nodemap_hash_operations,
195 if (nmc->nmc_nodemap_hash == NULL) {
196 CERROR("cannot create nodemap_hash table\n");
204 * Check for valid nodemap name
206 * \param name nodemap name
208 * \retval false invalid
210 static bool nodemap_name_is_valid(const char *name)
212 if (strlen(name) > LUSTRE_NODEMAP_NAME_LENGTH ||
216 for (; *name != '\0'; name++) {
217 if (!isalnum(*name) && *name != '_')
227 * Look nodemap up in the active_config nodemap hash. Caller should hold the
228 * active_config_lock.
230 * \param name name of nodemap
231 * \retval nodemap pointer set to found nodemap
232 * \retval -EINVAL name is not valid
233 * \retval -ENOENT nodemap not found
235 struct lu_nodemap *nodemap_lookup(const char *name)
237 struct lu_nodemap *nodemap = NULL;
239 if (!nodemap_name_is_valid(name))
240 return ERR_PTR(-EINVAL);
242 nodemap = cfs_hash_lookup(active_config->nmc_nodemap_hash, name);
244 return ERR_PTR(-ENOENT);
250 * Classify the nid into the proper nodemap. Caller must hold active config and
251 * nm_range_tree_lock, and call nodemap_putref when done with nodemap.
253 * \param nid nid to classify
254 * \retval nodemap nodemap containing the nid
255 * \retval default_nodemap default nodemap
256 * \retval -EINVAL LO nid given without other local nid
258 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
260 struct lu_nid_range *range;
261 struct lu_nodemap *nodemap;
266 /* don't use 0@lo, use the first non-lo local NID instead */
267 if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND) {
268 struct lnet_process_id id;
272 rc = LNetGetId(i++, &id);
274 RETURN(ERR_PTR(-EINVAL));
275 } while (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND);
278 CDEBUG(D_INFO, "found nid %s\n", libcfs_nid2str(nid));
281 range = range_search(&active_config->nmc_range_tree, nid);
283 nodemap = range->rn_nodemap;
285 nodemap = active_config->nmc_default_nodemap;
287 LASSERT(nodemap != NULL);
288 nodemap_getref(nodemap);
294 * simple check for default nodemap
296 static bool is_default_nodemap(const struct lu_nodemap *nodemap)
298 return nodemap->nm_id == 0;
302 * parse a nodemap range string into two nids
304 * \param range_str string to parse
305 * \param range[2] array of two nids
306 * \reyval 0 on success
308 int nodemap_parse_range(const char *range_str, lnet_nid_t range[2])
310 char buf[LNET_NIDSTR_SIZE * 2 + 2];
316 snprintf(buf, sizeof(buf), "%s", range_str);
318 start_nidstr = strsep(&ptr, ":");
319 end_nidstr = strsep(&ptr, ":");
321 if (start_nidstr == NULL || end_nidstr == NULL)
322 GOTO(out, rc = -EINVAL);
324 range[0] = libcfs_str2nid(start_nidstr);
325 range[1] = libcfs_str2nid(end_nidstr);
331 EXPORT_SYMBOL(nodemap_parse_range);
334 * parse a string containing an id map of form "client_id:filesystem_id"
335 * into an array of __u32 * for use in mapping functions
337 * \param idmap_str map string
338 * \param idmap array[2] of __u32
340 * \retval 0 on success
341 * \retval -EINVAL if idmap cannot be parsed
343 int nodemap_parse_idmap(char *idmap_str, __u32 idmap[2])
346 long unsigned int idmap_buf;
349 if (idmap_str == NULL)
352 sep = strchr(idmap_str, ':');
358 rc = kstrtoul(idmap_str, 10, &idmap_buf);
361 idmap[0] = idmap_buf;
363 rc = kstrtoul(sep, 10, &idmap_buf);
366 idmap[1] = idmap_buf;
370 EXPORT_SYMBOL(nodemap_parse_idmap);
373 * add a member to a nodemap
375 * \param nid nid to add to the members
376 * \param exp obd_export structure for the connection
377 * that is being added
378 * \retval -EINVAL export is NULL, or has invalid NID
379 * \retval -EEXIST export is already member of a nodemap
381 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
383 struct lu_nodemap *nodemap;
387 mutex_lock(&active_config_lock);
388 down_read(&active_config->nmc_range_tree_lock);
390 nodemap = nodemap_classify_nid(nid);
392 if (IS_ERR(nodemap)) {
393 CWARN("%s: error adding to nodemap, no valid NIDs found\n",
394 exp->exp_obd->obd_name);
397 rc = nm_member_add(nodemap, exp);
400 up_read(&active_config->nmc_range_tree_lock);
401 mutex_unlock(&active_config_lock);
403 if (!IS_ERR(nodemap))
404 nodemap_putref(nodemap);
408 EXPORT_SYMBOL(nodemap_add_member);
411 * delete a member from a nodemap
413 * \param exp export to remove from a nodemap
415 void nodemap_del_member(struct obd_export *exp)
417 struct lu_nodemap *nodemap;
421 /* using ac lock to prevent nodemap reclassification while deleting */
422 mutex_lock(&active_config_lock);
424 /* use of ted_nodemap is protected by active_config_lock. we take an
425 * extra reference to make sure nodemap isn't destroyed under
428 nodemap = exp->exp_target_data.ted_nodemap;
432 nodemap_getref(nodemap);
434 mutex_lock(&nodemap->nm_member_list_lock);
435 nm_member_del(nodemap, exp);
436 mutex_unlock(&nodemap->nm_member_list_lock);
439 mutex_unlock(&active_config_lock);
442 nodemap_putref(nodemap);
446 EXPORT_SYMBOL(nodemap_del_member);
449 * add an idmap to the proper nodemap trees
451 * \param name name of nodemap
452 * \param id_type NODEMAP_UID or NODEMAP_GID
453 * \param map array[2] __u32 containing the map values
454 * map[0] is client id
455 * map[1] is the filesystem id
457 * \retval 0 on success
459 int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
460 enum nodemap_id_type id_type,
463 struct lu_idmap *idmap;
466 idmap = idmap_create(map[0], map[1]);
468 GOTO(out, rc = -ENOMEM);
470 write_lock(&nodemap->nm_idmap_lock);
471 idmap_insert(id_type, idmap, nodemap);
472 write_unlock(&nodemap->nm_idmap_lock);
473 nm_member_revoke_locks(nodemap);
479 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
482 struct lu_nodemap *nodemap = NULL;
485 mutex_lock(&active_config_lock);
486 nodemap = nodemap_lookup(name);
487 if (IS_ERR(nodemap)) {
488 mutex_unlock(&active_config_lock);
489 GOTO(out, rc = PTR_ERR(nodemap));
492 if (is_default_nodemap(nodemap)) {
495 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
497 rc = nodemap_idx_idmap_add(nodemap, id_type, map);
499 mutex_unlock(&active_config_lock);
500 nodemap_putref(nodemap);
505 EXPORT_SYMBOL(nodemap_add_idmap);
508 * delete idmap from proper nodemap tree
510 * \param name name of nodemap
511 * \param id_type NODEMAP_UID or NODEMAP_GID
512 * \param map array[2] __u32 containing the mapA values
513 * map[0] is client id
514 * map[1] is the filesystem id
516 * \retval 0 on success
518 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
521 struct lu_nodemap *nodemap = NULL;
522 struct lu_idmap *idmap = NULL;
525 mutex_lock(&active_config_lock);
526 nodemap = nodemap_lookup(name);
527 if (IS_ERR(nodemap)) {
528 mutex_unlock(&active_config_lock);
529 GOTO(out, rc = PTR_ERR(nodemap));
532 if (is_default_nodemap(nodemap))
533 GOTO(out_putref, rc = -EINVAL);
535 write_lock(&nodemap->nm_idmap_lock);
536 idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
541 idmap_delete(id_type, idmap, nodemap);
542 rc = nodemap_idx_idmap_del(nodemap, id_type, map);
544 write_unlock(&nodemap->nm_idmap_lock);
547 mutex_unlock(&active_config_lock);
549 nm_member_revoke_locks(nodemap);
550 nodemap_putref(nodemap);
555 EXPORT_SYMBOL(nodemap_del_idmap);
558 * Get nodemap assigned to given export. Takes a reference on the nodemap.
560 * Note that this function may return either NULL, or an ERR_PTR()
561 * or a valid nodemap pointer. All of the functions accessing the
562 * returned nodemap can check IS_ERR(nodemap) to see if an error is
563 * returned. NULL is not considered an error, which is OK since this
564 * is a valid case if nodemap are not in use. All nodemap handling
565 * functions must check for nodemap == NULL and do nothing, and the
566 * nodemap returned from this function should not be dereferenced.
568 * \param export export to get nodemap for
570 * \retval pointer to nodemap on success
571 * \retval NULL nodemap subsystem disabled
572 * \retval -EACCES export does not have nodemap assigned
574 struct lu_nodemap *nodemap_get_from_exp(struct obd_export *exp)
576 struct lu_nodemap *nodemap;
583 spin_lock(&exp->exp_target_data.ted_nodemap_lock);
584 nodemap = exp->exp_target_data.ted_nodemap;
586 nodemap_getref(nodemap);
587 spin_unlock(&exp->exp_target_data.ted_nodemap_lock);
590 CDEBUG(D_INFO, "%s: nodemap null on export %s (at %s)\n",
591 exp->exp_obd->obd_name,
592 obd_uuid2str(&exp->exp_client_uuid),
593 obd_export_nid2str(exp));
594 RETURN(ERR_PTR(-EACCES));
599 EXPORT_SYMBOL(nodemap_get_from_exp);
602 * mapping function for nodemap idmaps
604 * \param nodemap lu_nodemap structure defining nodemap
605 * \param node_type NODEMAP_UID or NODEMAP_GID
606 * \param tree_type NODEMAP_CLIENT_TO_FS or
607 * NODEMAP_FS_TO_CLIENT
608 * \param id id to map
610 * \retval mapped id according to the rules below.
612 * if the nodemap_active is false, just return the passed id without mapping
614 * if the id to be looked up is 0, check that root access is allowed and if it
615 * is, return 0. Otherwise, return the squash uid or gid.
617 * if the nodemap is configured to trusted the ids from the client system, just
618 * return the passwd id without mapping.
620 * if by this point, we haven't returned and the nodemap in question is the
621 * default nodemap, return the squash uid or gid.
623 * after these checks, search the proper tree for the mapping, and if found
624 * return the mapped value, otherwise return the squash uid or gid.
626 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
627 enum nodemap_id_type id_type,
628 enum nodemap_tree_type tree_type, __u32 id)
630 struct lu_idmap *idmap = NULL;
638 if (unlikely(nodemap == NULL))
641 if (nodemap->nmf_map_uid_only && id_type == NODEMAP_GID)
644 if (nodemap->nmf_map_gid_only && id_type == NODEMAP_UID)
648 if (nodemap->nmf_allow_root_access)
654 if (nodemap->nmf_trust_client_ids)
657 if (is_default_nodemap(nodemap))
660 read_lock(&nodemap->nm_idmap_lock);
661 idmap = idmap_search(nodemap, tree_type, id_type, id);
663 read_unlock(&nodemap->nm_idmap_lock);
667 if (tree_type == NODEMAP_FS_TO_CLIENT)
668 found_id = idmap->id_client;
670 found_id = idmap->id_fs;
671 read_unlock(&nodemap->nm_idmap_lock);
675 if (id_type == NODEMAP_UID)
676 RETURN(nodemap->nm_squash_uid);
678 RETURN(nodemap->nm_squash_gid);
682 EXPORT_SYMBOL(nodemap_map_id);
685 * Map posix ACL entries according to the nodemap membership. Removes any
688 * \param lu_nodemap nodemap
689 * \param buf buffer containing xattr encoded ACLs
690 * \param size size of ACLs in bytes
691 * \param tree_type direction of mapping
692 * \retval size new size of ACLs in bytes
693 * \retval -EINVAL bad \a size param, see posix_acl_xattr_count()
695 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
696 enum nodemap_tree_type tree_type)
698 posix_acl_xattr_header *header = buf;
699 posix_acl_xattr_entry *entry = GET_POSIX_ACL_XATTR_ENTRY(header);
700 posix_acl_xattr_entry *new_entry = entry;
701 posix_acl_xattr_entry *end;
709 if (unlikely(nodemap == NULL))
712 count = posix_acl_xattr_count(size);
718 for (end = entry + count; entry != end; entry++) {
719 __u16 tag = le16_to_cpu(entry->e_tag);
720 __u32 id = le32_to_cpu(entry->e_id);
724 id = nodemap_map_id(nodemap, NODEMAP_UID,
726 if (id == nodemap->nm_squash_uid)
728 entry->e_id = cpu_to_le32(id);
731 id = nodemap_map_id(nodemap, NODEMAP_GID,
733 if (id == nodemap->nm_squash_gid)
735 entry->e_id = cpu_to_le32(id);
739 /* if we skip an ACL, copy the following ones over it */
740 if (new_entry != entry)
746 RETURN((void *)new_entry - (void *)header);
748 EXPORT_SYMBOL(nodemap_map_acl);
751 * Add nid range to given nodemap
753 * \param config nodemap config to work on
754 * \param nodemap nodemap to add range to
755 * \param nid nid range to add
756 * \param range_id should be 0 unless loading from disk
761 int nodemap_add_range_helper(struct nodemap_config *config,
762 struct lu_nodemap *nodemap,
763 const lnet_nid_t nid[2],
764 unsigned int range_id)
766 struct lu_nid_range *range;
769 down_write(&config->nmc_range_tree_lock);
770 range = range_create(&config->nmc_range_tree, nid[0], nid[1],
773 up_write(&config->nmc_range_tree_lock);
774 GOTO(out, rc = -ENOMEM);
777 rc = range_insert(&config->nmc_range_tree, range);
779 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
780 nodemap->nm_name, rc);
781 up_write(&config->nmc_range_tree_lock);
782 list_del(&range->rn_list);
783 range_destroy(range);
784 GOTO(out, rc = -ENOMEM);
787 list_add(&range->rn_list, &nodemap->nm_ranges);
789 /* nodemaps have no members if they aren't on the active config */
790 if (config == active_config)
791 nm_member_reclassify_nodemap(config->nmc_default_nodemap);
793 up_write(&config->nmc_range_tree_lock);
795 /* if range_id is non-zero, we are loading from disk */
797 rc = nodemap_idx_range_add(range, nid);
799 if (config == active_config) {
800 nm_member_revoke_locks(config->nmc_default_nodemap);
801 nm_member_revoke_locks(nodemap);
807 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
809 struct lu_nodemap *nodemap = NULL;
812 mutex_lock(&active_config_lock);
813 nodemap = nodemap_lookup(name);
814 if (IS_ERR(nodemap)) {
815 mutex_unlock(&active_config_lock);
816 GOTO(out, rc = PTR_ERR(nodemap));
819 if (is_default_nodemap(nodemap))
822 rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
823 mutex_unlock(&active_config_lock);
824 nodemap_putref(nodemap);
828 EXPORT_SYMBOL(nodemap_add_range);
832 * \param name nodemap name
833 * \param nid nid range
834 * \retval 0 on success
836 * Delete range from global range tree, and remove it
837 * from the list in the associated nodemap.
839 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
841 struct lu_nodemap *nodemap;
842 struct lu_nid_range *range;
845 mutex_lock(&active_config_lock);
846 nodemap = nodemap_lookup(name);
847 if (IS_ERR(nodemap)) {
848 mutex_unlock(&active_config_lock);
849 GOTO(out, rc = PTR_ERR(nodemap));
852 if (is_default_nodemap(nodemap))
853 GOTO(out_putref, rc = -EINVAL);
855 down_write(&active_config->nmc_range_tree_lock);
856 range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
858 up_write(&active_config->nmc_range_tree_lock);
859 GOTO(out_putref, rc = -EINVAL);
861 if (range->rn_nodemap != nodemap) {
862 up_write(&active_config->nmc_range_tree_lock);
863 GOTO(out_putref, rc = -EINVAL);
865 rc = nodemap_idx_range_del(range);
866 range_delete(&active_config->nmc_range_tree, range);
867 nm_member_reclassify_nodemap(nodemap);
868 up_write(&active_config->nmc_range_tree_lock);
870 nm_member_revoke_locks(active_config->nmc_default_nodemap);
871 nm_member_revoke_locks(nodemap);
874 mutex_unlock(&active_config_lock);
875 nodemap_putref(nodemap);
879 EXPORT_SYMBOL(nodemap_del_range);
882 * set fileset on nodemap
883 * \param name nodemap to set fileset on
884 * \param fileset string containing fileset
885 * \retval 0 on success
887 * set a fileset on the named nodemap
889 static int nodemap_set_fileset_helper(struct nodemap_config *config,
890 struct lu_nodemap *nodemap,
895 /* we allow fileset = "" which means clear fileset info */
896 if (fileset == NULL || (fileset[0] != 0 && fileset[0] != '/'))
898 else if (strlcpy(nodemap->nm_fileset, fileset,
899 sizeof(nodemap->nm_fileset)) >=
900 sizeof(nodemap->nm_fileset))
906 int nodemap_set_fileset(const char *name, const char *fileset)
908 struct lu_nodemap *nodemap = NULL;
911 mutex_lock(&active_config_lock);
912 nodemap = nodemap_lookup(name);
913 if (IS_ERR(nodemap)) {
914 mutex_unlock(&active_config_lock);
915 GOTO(out, rc = PTR_ERR(nodemap));
918 rc = nodemap_set_fileset_helper(active_config, nodemap,
920 mutex_unlock(&active_config_lock);
922 nodemap_putref(nodemap);
926 EXPORT_SYMBOL(nodemap_set_fileset);
929 * get fileset defined on nodemap
930 * \param nodemap nodemap to get fileset from
931 * \retval fileset name, or NULL if not defined or not activated
933 * get the fileset defined on the nodemap
935 char *nodemap_get_fileset(const struct lu_nodemap *nodemap)
940 return (char *)nodemap->nm_fileset;
942 EXPORT_SYMBOL(nodemap_get_fileset);
945 * Nodemap constructor
947 * Creates an lu_nodemap structure and assigns sane default
948 * member values. If this is the default nodemap, the defaults
949 * are the most restictive in xterms of mapping behavior. Otherwise
950 * the default flags should be inherited from the default nodemap.
951 * The adds nodemap to nodemap_hash.
953 * Requires that the caller take the active_config_lock
955 * \param name name of nodemap
956 * \param is_default true if default nodemap
957 * \retval nodemap success
958 * \retval -EINVAL invalid nodemap name
959 * \retval -EEXIST nodemap already exists
960 * \retval -ENOMEM cannot allocate memory for nodemap
962 struct lu_nodemap *nodemap_create(const char *name,
963 struct nodemap_config *config,
966 struct lu_nodemap *nodemap = NULL;
967 struct lu_nodemap *default_nodemap;
968 struct cfs_hash *hash = config->nmc_nodemap_hash;
971 default_nodemap = config->nmc_default_nodemap;
973 if (!nodemap_name_is_valid(name))
974 GOTO(out, rc = -EINVAL);
977 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
978 GOTO(out, rc = -EINVAL);
981 OBD_ALLOC_PTR(nodemap);
982 if (nodemap == NULL) {
983 CERROR("cannot allocate memory (%zu bytes)"
984 "for nodemap '%s'\n", sizeof(*nodemap),
986 GOTO(out, rc = -ENOMEM);
990 * take an extra reference to prevent nodemap from being destroyed
991 * while it's being created.
993 atomic_set(&nodemap->nm_refcount, 2);
994 snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
995 rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
997 OBD_FREE_PTR(nodemap);
998 GOTO(out, rc = -EEXIST);
1001 INIT_LIST_HEAD(&nodemap->nm_ranges);
1002 INIT_LIST_HEAD(&nodemap->nm_list);
1003 INIT_LIST_HEAD(&nodemap->nm_member_list);
1005 mutex_init(&nodemap->nm_member_list_lock);
1006 rwlock_init(&nodemap->nm_idmap_lock);
1007 nodemap->nm_fs_to_client_uidmap = RB_ROOT;
1008 nodemap->nm_client_to_fs_uidmap = RB_ROOT;
1009 nodemap->nm_fs_to_client_gidmap = RB_ROOT;
1010 nodemap->nm_client_to_fs_gidmap = RB_ROOT;
1013 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
1014 config->nmc_default_nodemap = nodemap;
1016 config->nmc_nodemap_highest_id++;
1017 nodemap->nm_id = config->nmc_nodemap_highest_id;
1020 if (is_default || default_nodemap == NULL) {
1021 nodemap->nmf_trust_client_ids = 0;
1022 nodemap->nmf_allow_root_access = 0;
1023 nodemap->nmf_deny_unknown = 0;
1024 nodemap->nmf_map_uid_only = 0;
1025 nodemap->nmf_map_gid_only = 0;
1027 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
1028 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
1029 nodemap->nm_fileset[0] = '\0';
1031 CWARN("adding nodemap '%s' to config without"
1032 " default nodemap\n", nodemap->nm_name);
1034 nodemap->nmf_trust_client_ids =
1035 default_nodemap->nmf_trust_client_ids;
1036 nodemap->nmf_allow_root_access =
1037 default_nodemap->nmf_allow_root_access;
1038 nodemap->nmf_deny_unknown =
1039 default_nodemap->nmf_deny_unknown;
1040 nodemap->nmf_map_uid_only =
1041 default_nodemap->nmf_map_uid_only;
1042 nodemap->nmf_map_gid_only =
1043 default_nodemap->nmf_map_gid_only;
1045 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
1046 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
1047 nodemap->nm_fileset[0] = '\0';
1053 CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
1058 * Set the nmf_deny_unknown flag to true or false.
1059 * \param name nodemap name
1060 * \param deny_unknown if true, squashed users will get EACCES
1061 * \retval 0 on success
1064 int nodemap_set_deny_unknown(const char *name, bool deny_unknown)
1066 struct lu_nodemap *nodemap = NULL;
1069 mutex_lock(&active_config_lock);
1070 nodemap = nodemap_lookup(name);
1071 mutex_unlock(&active_config_lock);
1072 if (IS_ERR(nodemap))
1073 GOTO(out, rc = PTR_ERR(nodemap));
1075 nodemap->nmf_deny_unknown = deny_unknown;
1076 rc = nodemap_idx_nodemap_update(nodemap);
1078 nm_member_revoke_locks(nodemap);
1079 nodemap_putref(nodemap);
1083 EXPORT_SYMBOL(nodemap_set_deny_unknown);
1086 * Set the nmf_allow_root_access flag to true or false.
1087 * \param name nodemap name
1088 * \param allow_root if true, nodemap will not squash the root user
1089 * \retval 0 on success
1092 int nodemap_set_allow_root(const char *name, bool allow_root)
1094 struct lu_nodemap *nodemap = NULL;
1097 mutex_lock(&active_config_lock);
1098 nodemap = nodemap_lookup(name);
1099 mutex_unlock(&active_config_lock);
1100 if (IS_ERR(nodemap))
1101 GOTO(out, rc = PTR_ERR(nodemap));
1103 nodemap->nmf_allow_root_access = allow_root;
1104 rc = nodemap_idx_nodemap_update(nodemap);
1106 nm_member_revoke_locks(nodemap);
1107 nodemap_putref(nodemap);
1111 EXPORT_SYMBOL(nodemap_set_allow_root);
1114 * Set the nmf_trust_client_ids flag to true or false.
1116 * \param name nodemap name
1117 * \param trust_client_ids if true, nodemap will not map its IDs
1118 * \retval 0 on success
1121 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
1123 struct lu_nodemap *nodemap = NULL;
1126 mutex_lock(&active_config_lock);
1127 nodemap = nodemap_lookup(name);
1128 mutex_unlock(&active_config_lock);
1129 if (IS_ERR(nodemap))
1130 GOTO(out, rc = PTR_ERR(nodemap));
1132 nodemap->nmf_trust_client_ids = trust_client_ids;
1133 rc = nodemap_idx_nodemap_update(nodemap);
1135 nm_member_revoke_locks(nodemap);
1136 nodemap_putref(nodemap);
1140 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
1142 int nodemap_set_mapping_mode(const char *name, enum nodemap_mapping_modes mode)
1144 struct lu_nodemap *nodemap = NULL;
1147 mutex_lock(&active_config_lock);
1148 nodemap = nodemap_lookup(name);
1149 mutex_unlock(&active_config_lock);
1150 if (IS_ERR(nodemap))
1151 GOTO(out, rc = PTR_ERR(nodemap));
1154 case NODEMAP_MAP_BOTH:
1155 nodemap->nmf_map_uid_only = 0;
1156 nodemap->nmf_map_gid_only = 0;
1158 case NODEMAP_MAP_UID_ONLY:
1159 nodemap->nmf_map_uid_only = 1;
1160 nodemap->nmf_map_gid_only = 0;
1162 case NODEMAP_MAP_GID_ONLY:
1163 nodemap->nmf_map_uid_only = 0;
1164 nodemap->nmf_map_gid_only = 1;
1167 CWARN("cannot set unknown mapping mode, mode = %d\n", mode);
1169 rc = nodemap_idx_nodemap_update(nodemap);
1171 nm_member_revoke_locks(nodemap);
1172 nodemap_putref(nodemap);
1176 EXPORT_SYMBOL(nodemap_set_mapping_mode);
1179 * Update the squash_uid for a nodemap.
1181 * \param name nodemap name
1182 * \param uid the new uid to squash unknown users to
1183 * \retval 0 on success
1185 * Update the squash_uid for a nodemap. The squash_uid is the uid
1186 * that the all client uids are mapped to if nodemap is active,
1187 * the trust_client_ids flag is not set, and the uid is not in
1190 int nodemap_set_squash_uid(const char *name, uid_t uid)
1192 struct lu_nodemap *nodemap = NULL;
1195 mutex_lock(&active_config_lock);
1196 nodemap = nodemap_lookup(name);
1197 mutex_unlock(&active_config_lock);
1198 if (IS_ERR(nodemap))
1199 GOTO(out, rc = PTR_ERR(nodemap));
1201 nodemap->nm_squash_uid = uid;
1202 rc = nodemap_idx_nodemap_update(nodemap);
1204 nm_member_revoke_locks(nodemap);
1205 nodemap_putref(nodemap);
1209 EXPORT_SYMBOL(nodemap_set_squash_uid);
1212 * Update the squash_gid for a nodemap.
1214 * \param name nodemap name
1215 * \param gid the new gid to squash unknown gids to
1216 * \retval 0 on success
1218 * Update the squash_gid for a nodemap. The squash_uid is the gid
1219 * that the all client gids are mapped to if nodemap is active,
1220 * the trust_client_ids flag is not set, and the gid is not in
1223 int nodemap_set_squash_gid(const char *name, gid_t gid)
1225 struct lu_nodemap *nodemap = NULL;
1228 mutex_lock(&active_config_lock);
1229 nodemap = nodemap_lookup(name);
1230 mutex_unlock(&active_config_lock);
1231 if (IS_ERR(nodemap))
1232 GOTO(out, rc = PTR_ERR(nodemap));
1234 nodemap->nm_squash_gid = gid;
1235 rc = nodemap_idx_nodemap_update(nodemap);
1237 nm_member_revoke_locks(nodemap);
1238 nodemap_putref(nodemap);
1242 EXPORT_SYMBOL(nodemap_set_squash_gid);
1245 * Returns true if this nodemap has root user access. Always returns true if
1246 * nodemaps are not active.
1248 * \param nodemap nodemap to check access for
1250 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
1252 return !nodemap_active || (nodemap && nodemap->nmf_allow_root_access);
1254 EXPORT_SYMBOL(nodemap_can_setquota);
1259 * \param name name of nodemap
1261 * \retval -EINVAL invalid nodemap name
1262 * \retval -EEXIST nodemap already exists
1263 * \retval -ENOMEM cannot allocate memory for nodemap
1265 int nodemap_add(const char *nodemap_name)
1267 struct lu_nodemap *nodemap;
1270 mutex_lock(&active_config_lock);
1271 nodemap = nodemap_create(nodemap_name, active_config, 0);
1272 if (IS_ERR(nodemap)) {
1273 mutex_unlock(&active_config_lock);
1274 return PTR_ERR(nodemap);
1277 rc = nodemap_idx_nodemap_add(nodemap);
1279 rc = lprocfs_nodemap_register(nodemap, 0);
1281 mutex_unlock(&active_config_lock);
1282 nodemap_putref(nodemap);
1286 EXPORT_SYMBOL(nodemap_add);
1291 * \param name name of nodemmap
1293 * \retval -EINVAL invalid input
1294 * \retval -ENOENT no existing nodemap
1296 int nodemap_del(const char *nodemap_name)
1298 struct lu_nodemap *nodemap;
1299 struct lu_nid_range *range;
1300 struct lu_nid_range *range_temp;
1304 if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1307 mutex_lock(&active_config_lock);
1308 nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1310 if (nodemap == NULL) {
1311 mutex_unlock(&active_config_lock);
1312 GOTO(out, rc = -ENOENT);
1315 /* erase nodemap from active ranges to prevent client assignment */
1316 down_write(&active_config->nmc_range_tree_lock);
1317 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1319 rc2 = nodemap_idx_range_del(range);
1323 range_delete(&active_config->nmc_range_tree, range);
1325 up_write(&active_config->nmc_range_tree_lock);
1327 rc2 = nodemap_idx_nodemap_del(nodemap);
1332 * remove procfs here in case nodemap_create called with same name
1333 * before nodemap_destroy is run.
1335 lprocfs_nodemap_remove(nodemap->nm_pde_data);
1336 nodemap->nm_pde_data = NULL;
1338 /* reclassify all member exports from nodemap, so they put their refs */
1339 down_read(&active_config->nmc_range_tree_lock);
1340 nm_member_reclassify_nodemap(nodemap);
1341 up_read(&active_config->nmc_range_tree_lock);
1343 if (!list_empty(&nodemap->nm_member_list))
1344 CWARN("nodemap_del failed to reclassify all members\n");
1346 mutex_unlock(&active_config_lock);
1348 nodemap_putref(nodemap);
1353 EXPORT_SYMBOL(nodemap_del);
1356 * activate nodemap functions
1358 * \param value 1 for on, 0 for off
1360 void nodemap_activate(const bool value)
1362 mutex_lock(&active_config_lock);
1363 active_config->nmc_nodemap_is_active = value;
1365 /* copy active value to global to avoid locking in map functions */
1366 nodemap_active = value;
1367 nodemap_idx_nodemap_activate(value);
1368 mutex_unlock(&active_config_lock);
1369 nm_member_revoke_all();
1371 EXPORT_SYMBOL(nodemap_activate);
1374 * Helper iterator to convert nodemap hash to list.
1376 * \param hs hash structure
1377 * \param bd bucket descriptor
1378 * \param hnode hash node
1379 * \param nodemap_list_head list head for list of nodemaps in hash
1381 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1382 struct hlist_node *hnode,
1383 void *nodemap_list_head)
1385 struct lu_nodemap *nodemap;
1387 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1388 list_add(&nodemap->nm_list, nodemap_list_head);
1390 cfs_hash_bd_del_locked(hs, bd, hnode);
1395 struct nodemap_config *nodemap_config_alloc(void)
1397 struct nodemap_config *config;
1400 OBD_ALLOC_PTR(config);
1402 return ERR_PTR(-ENOMEM);
1404 rc = nodemap_init_hash(config);
1406 OBD_FREE_PTR(config);
1410 init_rwsem(&config->nmc_range_tree_lock);
1414 EXPORT_SYMBOL(nodemap_config_alloc);
1417 * Walk the nodemap_hash and remove all nodemaps.
1419 void nodemap_config_dealloc(struct nodemap_config *config)
1421 struct lu_nodemap *nodemap = NULL;
1422 struct lu_nodemap *nodemap_temp;
1423 struct lu_nid_range *range;
1424 struct lu_nid_range *range_temp;
1425 LIST_HEAD(nodemap_list_head);
1427 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1428 nodemap_cleanup_iter_cb, &nodemap_list_head);
1429 cfs_hash_putref(config->nmc_nodemap_hash);
1431 /* Because nodemap_destroy might sleep, we can't destroy them
1432 * in cfs_hash_for_each, so we build a list there and destroy here
1434 list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1436 mutex_lock(&active_config_lock);
1437 down_write(&config->nmc_range_tree_lock);
1439 /* move members to new config, requires ac lock */
1440 nm_member_reclassify_nodemap(nodemap);
1441 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1443 range_delete(&config->nmc_range_tree, range);
1444 up_write(&config->nmc_range_tree_lock);
1445 mutex_unlock(&active_config_lock);
1447 /* putref must be outside of ac lock if nm could be destroyed */
1448 nodemap_putref(nodemap);
1450 OBD_FREE_PTR(config);
1452 EXPORT_SYMBOL(nodemap_config_dealloc);
1455 * callback for cfs_hash_for_each_safe used to convert a nodemap hash to a
1456 * nodemap list, generally for locking purposes as a hash cb can't sleep.
1458 int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1459 struct hlist_node *hnode,
1460 void *nodemap_list_head)
1462 struct lu_nodemap *nodemap;
1464 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1465 list_add(&nodemap->nm_list, nodemap_list_head);
1469 void nodemap_config_set_active(struct nodemap_config *config)
1471 struct nodemap_config *old_config = active_config;
1472 struct lu_nodemap *nodemap;
1473 struct lu_nodemap *tmp;
1475 LIST_HEAD(nodemap_list_head);
1479 LASSERT(active_config != config);
1480 LASSERT(config->nmc_default_nodemap);
1482 mutex_lock(&active_config_lock);
1484 /* move proc entries from already existing nms, create for new nms */
1485 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1486 nm_hash_list_cb, &nodemap_list_head);
1487 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1488 struct lu_nodemap *old_nm = NULL;
1490 if (active_config != NULL)
1491 old_nm = cfs_hash_lookup(
1492 active_config->nmc_nodemap_hash,
1494 if (old_nm != NULL) {
1495 nodemap->nm_pde_data = old_nm->nm_pde_data;
1496 old_nm->nm_pde_data = NULL;
1497 nodemap_putref(old_nm);
1499 bool is_def = (nodemap == config->nmc_default_nodemap);
1501 lprocfs_nodemap_register(nodemap, is_def);
1506 * We only need to revoke locks if old nodemap was active, and new
1507 * config is now nodemap inactive. nodemap_config_dealloc will
1508 * reclassify exports, triggering a lock revoke if and only if new
1509 * nodemap is active.
1511 revoke_locks = !config->nmc_nodemap_is_active && nodemap_active;
1513 /* if new config is inactive, deactivate live config before switching */
1514 if (!config->nmc_nodemap_is_active)
1515 nodemap_active = false;
1516 active_config = config;
1517 if (config->nmc_nodemap_is_active)
1518 nodemap_active = true;
1520 mutex_unlock(&active_config_lock);
1522 if (old_config != NULL)
1523 nodemap_config_dealloc(old_config);
1526 nm_member_revoke_all();
1532 * Cleanup nodemap module on exit
1534 void nodemap_mod_exit(void)
1536 nodemap_config_dealloc(active_config);
1537 nodemap_procfs_exit();
1541 * Initialize the nodemap module
1543 int nodemap_mod_init(void)
1545 struct nodemap_config *new_config;
1546 struct lu_nodemap *nodemap;
1549 rc = nodemap_procfs_init();
1553 new_config = nodemap_config_alloc();
1554 if (IS_ERR(new_config)) {
1555 nodemap_procfs_exit();
1556 GOTO(out, rc = PTR_ERR(new_config));
1559 nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1560 if (IS_ERR(nodemap)) {
1561 nodemap_config_dealloc(new_config);
1562 nodemap_procfs_exit();
1563 GOTO(out, rc = PTR_ERR(nodemap));
1566 nodemap_config_set_active(new_config);
1567 nodemap_putref(nodemap);
1574 * Revoke locks for all nodemaps.
1576 void nm_member_revoke_all(void)
1578 struct lu_nodemap *nodemap;
1579 struct lu_nodemap *tmp;
1580 LIST_HEAD(nodemap_list_head);
1582 mutex_lock(&active_config_lock);
1583 cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1584 nm_hash_list_cb, &nodemap_list_head);
1586 /* revoke_locks sleeps, so can't call in cfs hash cb */
1587 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1588 nm_member_revoke_locks_always(nodemap);
1589 mutex_unlock(&active_config_lock);
1593 * Returns the nodemap classification for a given nid into an ioctl buffer.
1594 * Useful for testing the nodemap configuration to make sure it is working as
1597 * \param nid nid to classify
1598 * \param[out] name_buf buffer to write the nodemap name to
1599 * \param name_len length of buffer
1601 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1603 struct lu_nodemap *nodemap;
1605 mutex_lock(&active_config_lock);
1606 down_read(&active_config->nmc_range_tree_lock);
1607 nodemap = nodemap_classify_nid(nid);
1608 up_read(&active_config->nmc_range_tree_lock);
1609 mutex_unlock(&active_config_lock);
1611 if (IS_ERR(nodemap))
1614 strncpy(name_buf, nodemap->nm_name, name_len);
1616 name_buf[name_len - 1] = '\0';
1618 nodemap_putref(nodemap);
1620 EXPORT_SYMBOL(nodemap_test_nid);
1623 * Passes back the id mapping for a given nid/id pair. Useful for testing the
1624 * nodemap configuration to make sure it is working as expected.
1626 * \param nid nid to classify
1627 * \param idtype uid or gid
1628 * \param client_id id to map to fs
1629 * \param fs_id_buf pointer to save mapped fs_id to
1632 * \retval -EINVAL invalid NID
1634 int nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1635 __u32 client_id, __u32 *fs_id)
1637 struct lu_nodemap *nodemap;
1639 mutex_lock(&active_config_lock);
1640 down_read(&active_config->nmc_range_tree_lock);
1641 nodemap = nodemap_classify_nid(nid);
1642 up_read(&active_config->nmc_range_tree_lock);
1643 mutex_unlock(&active_config_lock);
1645 if (IS_ERR(nodemap))
1646 return PTR_ERR(nodemap);
1648 *fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1650 nodemap_putref(nodemap);
1654 EXPORT_SYMBOL(nodemap_test_id);