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);
95 CDEBUG(D_INFO, "GETting nodemap %s(p=%p) : new refcount %d\n",
96 nodemap->nm_name, nodemap, atomic_read(&nodemap->nm_refcount));
100 * Destroy nodemap if last reference is put. Should be called outside
103 void nodemap_putref(struct lu_nodemap *nodemap)
108 LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
110 CDEBUG(D_INFO, "PUTting nodemap %s(p=%p) : new refcount %d\n",
111 nodemap->nm_name, nodemap,
112 atomic_read(&nodemap->nm_refcount) - 1);
114 if (atomic_dec_and_test(&nodemap->nm_refcount))
115 nodemap_destroy(nodemap);
117 EXPORT_SYMBOL(nodemap_putref);
119 static __u32 nodemap_hashfn(struct cfs_hash *hash_body,
120 const void *key, unsigned mask)
122 return cfs_hash_djb2_hash(key, strlen(key), mask);
125 static void *nodemap_hs_key(struct hlist_node *hnode)
127 struct lu_nodemap *nodemap;
129 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
131 return nodemap->nm_name;
134 static int nodemap_hs_keycmp(const void *key,
135 struct hlist_node *compared_hnode)
139 nodemap_name = nodemap_hs_key(compared_hnode);
141 return !strcmp(key, nodemap_name);
144 static void *nodemap_hs_hashobject(struct hlist_node *hnode)
146 return hlist_entry(hnode, struct lu_nodemap, nm_hash);
149 static void nodemap_hs_get(struct cfs_hash *hs, struct hlist_node *hnode)
151 struct lu_nodemap *nodemap;
153 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
154 nodemap_getref(nodemap);
157 static void nodemap_hs_put_locked(struct cfs_hash *hs,
158 struct hlist_node *hnode)
160 struct lu_nodemap *nodemap;
162 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
163 nodemap_putref(nodemap);
166 static struct cfs_hash_ops nodemap_hash_operations = {
167 .hs_hash = nodemap_hashfn,
168 .hs_key = nodemap_hs_key,
169 .hs_keycmp = nodemap_hs_keycmp,
170 .hs_object = nodemap_hs_hashobject,
171 .hs_get = nodemap_hs_get,
172 .hs_put_locked = nodemap_hs_put_locked,
175 /* end of cfs_hash functions */
178 * Initialize nodemap_hash
181 * \retval -ENOMEM cannot create hash
183 static int nodemap_init_hash(struct nodemap_config *nmc)
185 nmc->nmc_nodemap_hash = cfs_hash_create("NODEMAP",
186 HASH_NODEMAP_CUR_BITS,
187 HASH_NODEMAP_MAX_BITS,
188 HASH_NODEMAP_BKT_BITS, 0,
191 &nodemap_hash_operations,
194 if (nmc->nmc_nodemap_hash == NULL) {
195 CERROR("cannot create nodemap_hash table\n");
203 * Check for valid nodemap name
205 * \param name nodemap name
207 * \retval false invalid
209 static bool nodemap_name_is_valid(const char *name)
211 if (strlen(name) > LUSTRE_NODEMAP_NAME_LENGTH ||
215 for (; *name != '\0'; name++) {
216 if (!isalnum(*name) && *name != '_')
226 * Look nodemap up in the active_config nodemap hash. Caller should hold the
227 * active_config_lock.
229 * \param name name of nodemap
230 * \retval nodemap pointer set to found nodemap
231 * \retval -EINVAL name is not valid
232 * \retval -ENOENT nodemap not found
234 struct lu_nodemap *nodemap_lookup(const char *name)
236 struct lu_nodemap *nodemap = NULL;
238 if (!nodemap_name_is_valid(name))
239 return ERR_PTR(-EINVAL);
241 nodemap = cfs_hash_lookup(active_config->nmc_nodemap_hash, name);
243 return ERR_PTR(-ENOENT);
249 * Classify the nid into the proper nodemap. Caller must hold active config and
250 * nm_range_tree_lock, and call nodemap_putref when done with nodemap.
252 * \param nid nid to classify
253 * \retval nodemap nodemap containing the nid
254 * \retval default_nodemap default nodemap
256 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
258 struct lu_nid_range *range;
259 struct lu_nodemap *nodemap;
261 range = range_search(&active_config->nmc_range_tree, nid);
263 nodemap = range->rn_nodemap;
265 nodemap = active_config->nmc_default_nodemap;
267 nodemap_getref(nodemap);
273 * simple check for default nodemap
275 static bool is_default_nodemap(const struct lu_nodemap *nodemap)
277 return nodemap->nm_id == 0;
281 * parse a nodemap range string into two nids
283 * \param range_str string to parse
284 * \param range[2] array of two nids
285 * \reyval 0 on success
287 int nodemap_parse_range(const char *range_str, lnet_nid_t range[2])
289 char buf[LNET_NIDSTR_SIZE * 2 + 2];
295 snprintf(buf, sizeof(buf), "%s", range_str);
297 start_nidstr = strsep(&ptr, ":");
298 end_nidstr = strsep(&ptr, ":");
300 if (start_nidstr == NULL || end_nidstr == NULL)
301 GOTO(out, rc = -EINVAL);
303 range[0] = libcfs_str2nid(start_nidstr);
304 range[1] = libcfs_str2nid(end_nidstr);
310 EXPORT_SYMBOL(nodemap_parse_range);
313 * parse a string containing an id map of form "client_id:filesystem_id"
314 * into an array of __u32 * for use in mapping functions
316 * \param idmap_str map string
317 * \param idmap array[2] of __u32
319 * \retval 0 on success
320 * \retval -EINVAL if idmap cannot be parsed
322 int nodemap_parse_idmap(char *idmap_str, __u32 idmap[2])
325 long unsigned int idmap_buf;
328 if (idmap_str == NULL)
331 sep = strchr(idmap_str, ':');
337 rc = kstrtoul(idmap_str, 10, &idmap_buf);
340 idmap[0] = idmap_buf;
342 rc = kstrtoul(sep, 10, &idmap_buf);
345 idmap[1] = idmap_buf;
349 EXPORT_SYMBOL(nodemap_parse_idmap);
352 * add a member to a nodemap
354 * \param nid nid to add to the members
355 * \param exp obd_export structure for the connection
356 * that is being added
357 * \retval -EINVAL export is NULL
358 * \retval -EEXIST export is already member of a nodemap
360 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
362 struct lu_nodemap *nodemap;
365 mutex_lock(&active_config_lock);
366 down_read(&active_config->nmc_range_tree_lock);
368 nodemap = nodemap_classify_nid(nid);
369 rc = nm_member_add(nodemap, exp);
371 up_read(&active_config->nmc_range_tree_lock);
372 mutex_unlock(&active_config_lock);
374 nodemap_putref(nodemap);
378 EXPORT_SYMBOL(nodemap_add_member);
381 * delete a member from a nodemap
383 * \param exp export to remove from a nodemap
385 void nodemap_del_member(struct obd_export *exp)
387 struct lu_nodemap *nodemap;
391 /* using ac lock to prevent nodemap reclassification while deleting */
392 mutex_lock(&active_config_lock);
394 /* use of ted_nodemap is protected by active_config_lock. we take an
395 * extra reference to make sure nodemap isn't destroyed under
398 nodemap = exp->exp_target_data.ted_nodemap;
402 nodemap_getref(nodemap);
404 mutex_lock(&nodemap->nm_member_list_lock);
405 nm_member_del(nodemap, exp);
406 mutex_unlock(&nodemap->nm_member_list_lock);
409 mutex_unlock(&active_config_lock);
412 nodemap_putref(nodemap);
416 EXPORT_SYMBOL(nodemap_del_member);
419 * add an idmap to the proper nodemap trees
421 * \param name name of nodemap
422 * \param id_type NODEMAP_UID or NODEMAP_GID
423 * \param map array[2] __u32 containing the map values
424 * map[0] is client id
425 * map[1] is the filesystem id
427 * \retval 0 on success
429 int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
430 enum nodemap_id_type id_type,
433 struct lu_idmap *idmap;
436 idmap = idmap_create(map[0], map[1]);
438 GOTO(out, rc = -ENOMEM);
440 write_lock(&nodemap->nm_idmap_lock);
441 idmap_insert(id_type, idmap, nodemap);
442 write_unlock(&nodemap->nm_idmap_lock);
443 nm_member_revoke_locks(nodemap);
449 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
452 struct lu_nodemap *nodemap = NULL;
455 mutex_lock(&active_config_lock);
456 nodemap = nodemap_lookup(name);
457 if (IS_ERR(nodemap)) {
458 mutex_unlock(&active_config_lock);
459 GOTO(out, rc = PTR_ERR(nodemap));
462 if (is_default_nodemap(nodemap)) {
465 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
467 rc = nodemap_idx_idmap_add(nodemap, id_type, map);
469 mutex_unlock(&active_config_lock);
470 nodemap_putref(nodemap);
475 EXPORT_SYMBOL(nodemap_add_idmap);
478 * delete idmap from proper nodemap tree
480 * \param name name of nodemap
481 * \param id_type NODEMAP_UID or NODEMAP_GID
482 * \param map array[2] __u32 containing the mapA values
483 * map[0] is client id
484 * map[1] is the filesystem id
486 * \retval 0 on success
488 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
491 struct lu_nodemap *nodemap = NULL;
492 struct lu_idmap *idmap = NULL;
495 mutex_lock(&active_config_lock);
496 nodemap = nodemap_lookup(name);
497 if (IS_ERR(nodemap)) {
498 mutex_unlock(&active_config_lock);
499 GOTO(out, rc = PTR_ERR(nodemap));
502 if (is_default_nodemap(nodemap))
503 GOTO(out_putref, rc = -EINVAL);
505 write_lock(&nodemap->nm_idmap_lock);
506 idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
511 idmap_delete(id_type, idmap, nodemap);
512 rc = nodemap_idx_idmap_del(nodemap, id_type, map);
514 write_unlock(&nodemap->nm_idmap_lock);
517 mutex_unlock(&active_config_lock);
519 nm_member_revoke_locks(nodemap);
520 nodemap_putref(nodemap);
525 EXPORT_SYMBOL(nodemap_del_idmap);
528 * Get nodemap assigned to given export. Takes a reference on the nodemap.
530 * Note that this function may return either NULL, or an ERR_PTR()
531 * or a valid nodemap pointer. All of the functions accessing the
532 * returned nodemap can check IS_ERR(nodemap) to see if an error is
533 * returned. NULL is not considered an error, which is OK since this
534 * is a valid case if nodemap are not in use. All nodemap handling
535 * functions must check for nodemap == NULL and do nothing, and the
536 * nodemap returned from this function should not be dereferenced.
538 * \param export export to get nodemap for
540 * \retval pointer to nodemap on success
541 * \retval NULL nodemap subsystem disabled
542 * \retval -EACCES export does not have nodemap assigned
544 struct lu_nodemap *nodemap_get_from_exp(struct obd_export *exp)
546 struct lu_nodemap *nodemap;
553 spin_lock(&exp->exp_target_data.ted_nodemap_lock);
554 nodemap = exp->exp_target_data.ted_nodemap;
556 nodemap_getref(nodemap);
557 spin_unlock(&exp->exp_target_data.ted_nodemap_lock);
560 CDEBUG(D_INFO, "%s: nodemap null on export %s (at %s)\n",
561 exp->exp_obd->obd_name,
562 obd_uuid2str(&exp->exp_client_uuid),
563 obd_export_nid2str(exp));
564 RETURN(ERR_PTR(-EACCES));
569 EXPORT_SYMBOL(nodemap_get_from_exp);
572 * mapping function for nodemap idmaps
574 * \param nodemap lu_nodemap structure defining nodemap
575 * \param node_type NODEMAP_UID or NODEMAP_GID
576 * \param tree_type NODEMAP_CLIENT_TO_FS or
577 * NODEMAP_FS_TO_CLIENT
578 * \param id id to map
580 * \retval mapped id according to the rules below.
582 * if the nodemap_active is false, just return the passed id without mapping
584 * if the id to be looked up is 0, check that root access is allowed and if it
585 * is, return 0. Otherwise, return the squash uid or gid.
587 * if the nodemap is configured to trusted the ids from the client system, just
588 * return the passwd id without mapping.
590 * if by this point, we haven't returned and the nodemap in question is the
591 * default nodemap, return the squash uid or gid.
593 * after these checks, search the proper tree for the mapping, and if found
594 * return the mapped value, otherwise return the squash uid or gid.
596 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
597 enum nodemap_id_type id_type,
598 enum nodemap_tree_type tree_type, __u32 id)
600 struct lu_idmap *idmap = NULL;
608 if (unlikely(nodemap == NULL))
612 if (nodemap->nmf_allow_root_access)
618 if (nodemap->nmf_trust_client_ids)
621 if (is_default_nodemap(nodemap))
624 read_lock(&nodemap->nm_idmap_lock);
625 idmap = idmap_search(nodemap, tree_type, id_type, id);
627 read_unlock(&nodemap->nm_idmap_lock);
631 if (tree_type == NODEMAP_FS_TO_CLIENT)
632 found_id = idmap->id_client;
634 found_id = idmap->id_fs;
635 read_unlock(&nodemap->nm_idmap_lock);
639 if (id_type == NODEMAP_UID)
640 RETURN(nodemap->nm_squash_uid);
642 RETURN(nodemap->nm_squash_gid);
646 EXPORT_SYMBOL(nodemap_map_id);
649 * Map posix ACL entries according to the nodemap membership. Removes any
652 * \param lu_nodemap nodemap
653 * \param buf buffer containing xattr encoded ACLs
654 * \param size size of ACLs in bytes
655 * \param tree_type direction of mapping
656 * \retval size new size of ACLs in bytes
657 * \retval -EINVAL bad \a size param, see posix_acl_xattr_count()
659 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
660 enum nodemap_tree_type tree_type)
662 posix_acl_xattr_header *header = buf;
663 posix_acl_xattr_entry *entry = &header->a_entries[0];
664 posix_acl_xattr_entry *new_entry = entry;
665 posix_acl_xattr_entry *end;
671 if (unlikely(nodemap == NULL))
674 count = posix_acl_xattr_count(size);
680 for (end = entry + count; entry != end; entry++) {
681 __u16 tag = le16_to_cpu(entry->e_tag);
682 __u32 id = le32_to_cpu(entry->e_id);
686 id = nodemap_map_id(nodemap, NODEMAP_UID,
688 if (id == nodemap->nm_squash_uid)
690 entry->e_id = cpu_to_le32(id);
693 id = nodemap_map_id(nodemap, NODEMAP_GID,
695 if (id == nodemap->nm_squash_gid)
697 entry->e_id = cpu_to_le32(id);
701 /* if we skip an ACL, copy the following ones over it */
702 if (new_entry != entry)
708 return (void *)new_entry - (void *)header;
710 EXPORT_SYMBOL(nodemap_map_acl);
713 * Add nid range to given nodemap
715 * \param config nodemap config to work on
716 * \param nodemap nodemap to add range to
717 * \param nid nid range to add
718 * \param range_id should be 0 unless loading from disk
723 int nodemap_add_range_helper(struct nodemap_config *config,
724 struct lu_nodemap *nodemap,
725 const lnet_nid_t nid[2],
726 unsigned int range_id)
728 struct lu_nid_range *range;
731 down_write(&config->nmc_range_tree_lock);
732 range = range_create(&config->nmc_range_tree, nid[0], nid[1],
735 up_write(&config->nmc_range_tree_lock);
736 GOTO(out, rc = -ENOMEM);
739 rc = range_insert(&config->nmc_range_tree, range);
741 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
742 nodemap->nm_name, rc);
743 up_write(&config->nmc_range_tree_lock);
744 list_del(&range->rn_list);
745 range_destroy(range);
746 GOTO(out, rc = -ENOMEM);
749 list_add(&range->rn_list, &nodemap->nm_ranges);
750 nm_member_reclassify_nodemap(config->nmc_default_nodemap);
751 up_write(&config->nmc_range_tree_lock);
753 /* if range_id is non-zero, we are loading from disk */
755 rc = nodemap_idx_range_add(range, nid);
757 nm_member_revoke_locks(config->nmc_default_nodemap);
758 nm_member_revoke_locks(nodemap);
763 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
765 struct lu_nodemap *nodemap = NULL;
768 mutex_lock(&active_config_lock);
769 nodemap = nodemap_lookup(name);
770 if (IS_ERR(nodemap)) {
771 mutex_unlock(&active_config_lock);
772 GOTO(out, rc = PTR_ERR(nodemap));
775 if (is_default_nodemap(nodemap))
778 rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
779 mutex_unlock(&active_config_lock);
780 nodemap_putref(nodemap);
784 EXPORT_SYMBOL(nodemap_add_range);
788 * \param name nodemap name
789 * \param range_str string containing range
790 * \retval 0 on success
792 * Delete range from global range tree, and remove it
793 * from the list in the associated nodemap.
795 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
797 struct lu_nodemap *nodemap;
798 struct lu_nid_range *range;
801 mutex_lock(&active_config_lock);
802 nodemap = nodemap_lookup(name);
803 if (IS_ERR(nodemap)) {
804 mutex_unlock(&active_config_lock);
805 GOTO(out, rc = PTR_ERR(nodemap));
808 if (is_default_nodemap(nodemap))
809 GOTO(out_putref, rc = -EINVAL);
811 down_write(&active_config->nmc_range_tree_lock);
812 range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
814 up_write(&active_config->nmc_range_tree_lock);
815 GOTO(out_putref, rc = -EINVAL);
817 rc = nodemap_idx_range_del(range);
818 range_delete(&active_config->nmc_range_tree, range);
819 nm_member_reclassify_nodemap(nodemap);
820 up_write(&active_config->nmc_range_tree_lock);
822 nm_member_revoke_locks(active_config->nmc_default_nodemap);
823 nm_member_revoke_locks(nodemap);
826 mutex_unlock(&active_config_lock);
827 nodemap_putref(nodemap);
831 EXPORT_SYMBOL(nodemap_del_range);
834 * set fileset on nodemap
835 * \param name nodemap to set fileset on
836 * \param fileset string containing fileset
837 * \retval 0 on success
839 * set a fileset on the named nodemap
841 static int nodemap_set_fileset_helper(struct nodemap_config *config,
842 struct lu_nodemap *nodemap,
847 /* we allow fileset = "" which means clear fileset info */
848 if (fileset == NULL || (fileset[0] != 0 && fileset[0] != '/'))
850 else if (strlcpy(nodemap->nm_fileset, fileset,
851 sizeof(nodemap->nm_fileset)) >=
852 sizeof(nodemap->nm_fileset))
858 int nodemap_set_fileset(const char *name, const char *fileset)
860 struct lu_nodemap *nodemap = NULL;
863 mutex_lock(&active_config_lock);
864 nodemap = nodemap_lookup(name);
865 if (IS_ERR(nodemap)) {
866 mutex_unlock(&active_config_lock);
867 GOTO(out, rc = PTR_ERR(nodemap));
870 if (is_default_nodemap(nodemap))
873 rc = nodemap_set_fileset_helper(active_config, nodemap,
875 mutex_unlock(&active_config_lock);
877 nodemap_putref(nodemap);
881 EXPORT_SYMBOL(nodemap_set_fileset);
884 * get fileset defined on nodemap
885 * \param nodemap nodemap to get fileset from
886 * \retval fileset name, or NULL if not defined or not activated
888 * get the fileset defined on the nodemap
890 char *nodemap_get_fileset(const struct lu_nodemap *nodemap)
892 if (!nodemap_active || is_default_nodemap(nodemap))
895 return (char *)nodemap->nm_fileset;
897 EXPORT_SYMBOL(nodemap_get_fileset);
900 * Nodemap constructor
902 * Creates an lu_nodemap structure and assigns sane default
903 * member values. If this is the default nodemap, the defaults
904 * are the most restictive in xterms of mapping behavior. Otherwise
905 * the default flags should be inherited from the default nodemap.
906 * The adds nodemap to nodemap_hash.
908 * Requires that the caller take the active_config_lock
910 * \param name name of nodemap
911 * \param is_default true if default nodemap
912 * \retval nodemap success
913 * \retval -EINVAL invalid nodemap name
914 * \retval -EEXIST nodemap already exists
915 * \retval -ENOMEM cannot allocate memory for nodemap
917 struct lu_nodemap *nodemap_create(const char *name,
918 struct nodemap_config *config,
921 struct lu_nodemap *nodemap = NULL;
922 struct lu_nodemap *default_nodemap;
923 struct cfs_hash *hash = config->nmc_nodemap_hash;
926 default_nodemap = config->nmc_default_nodemap;
928 if (!nodemap_name_is_valid(name))
929 GOTO(out, rc = -EINVAL);
932 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
933 GOTO(out, rc = -EINVAL);
936 OBD_ALLOC_PTR(nodemap);
937 if (nodemap == NULL) {
938 CERROR("cannot allocate memory (%zu bytes)"
939 "for nodemap '%s'\n", sizeof(*nodemap),
941 GOTO(out, rc = -ENOMEM);
945 * take an extra reference to prevent nodemap from being destroyed
946 * while it's being created.
948 atomic_set(&nodemap->nm_refcount, 2);
949 snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
950 rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
952 OBD_FREE_PTR(nodemap);
953 GOTO(out, rc = -EEXIST);
956 INIT_LIST_HEAD(&nodemap->nm_ranges);
957 INIT_LIST_HEAD(&nodemap->nm_list);
958 INIT_LIST_HEAD(&nodemap->nm_member_list);
960 mutex_init(&nodemap->nm_member_list_lock);
961 rwlock_init(&nodemap->nm_idmap_lock);
962 nodemap->nm_fs_to_client_uidmap = RB_ROOT;
963 nodemap->nm_client_to_fs_uidmap = RB_ROOT;
964 nodemap->nm_fs_to_client_gidmap = RB_ROOT;
965 nodemap->nm_client_to_fs_gidmap = RB_ROOT;
968 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
969 config->nmc_default_nodemap = nodemap;
971 config->nmc_nodemap_highest_id++;
972 nodemap->nm_id = config->nmc_nodemap_highest_id;
975 if (is_default || default_nodemap == NULL) {
976 nodemap->nmf_trust_client_ids = 0;
977 nodemap->nmf_allow_root_access = 0;
979 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
980 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
982 CWARN("adding nodemap '%s' to config without"
983 " default nodemap\n", nodemap->nm_name);
985 nodemap->nmf_trust_client_ids =
986 default_nodemap->nmf_trust_client_ids;
987 nodemap->nmf_allow_root_access =
988 default_nodemap->nmf_allow_root_access;
990 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
991 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
992 nodemap->nm_fileset[0] = 0;
998 CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
1003 * update flag to turn on or off nodemap functions
1004 * \param name nodemap name
1005 * \param admin_string string containing updated value
1006 * \retval 0 on success
1008 * Update admin flag to turn on or off nodemap functions.
1010 int nodemap_set_allow_root(const char *name, bool allow_root)
1012 struct lu_nodemap *nodemap = NULL;
1015 mutex_lock(&active_config_lock);
1016 nodemap = nodemap_lookup(name);
1017 mutex_unlock(&active_config_lock);
1018 if (IS_ERR(nodemap))
1019 GOTO(out, rc = PTR_ERR(nodemap));
1021 nodemap->nmf_allow_root_access = allow_root;
1022 rc = nodemap_idx_nodemap_update(nodemap);
1024 nm_member_revoke_locks(nodemap);
1025 nodemap_putref(nodemap);
1029 EXPORT_SYMBOL(nodemap_set_allow_root);
1032 * updated trust_client_ids flag for nodemap
1034 * \param name nodemap name
1035 * \param trust_string new value for trust flag
1036 * \retval 0 on success
1038 * Update the trust_client_ids flag for a nodemap.
1040 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
1042 struct lu_nodemap *nodemap = NULL;
1045 mutex_lock(&active_config_lock);
1046 nodemap = nodemap_lookup(name);
1047 mutex_unlock(&active_config_lock);
1048 if (IS_ERR(nodemap))
1049 GOTO(out, rc = PTR_ERR(nodemap));
1051 nodemap->nmf_trust_client_ids = trust_client_ids;
1052 rc = nodemap_idx_nodemap_update(nodemap);
1054 nm_member_revoke_locks(nodemap);
1055 nodemap_putref(nodemap);
1059 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
1062 * update the squash_uid for a nodemap
1064 * \param name nodemap name
1065 * \param uid_string string containing new squash_uid value
1066 * \retval 0 on success
1068 * Update the squash_uid for a nodemap. The squash_uid is the uid
1069 * that the all client uids are mapped to if nodemap is active,
1070 * the trust_client_ids flag is not set, and the uid is not in
1073 int nodemap_set_squash_uid(const char *name, uid_t uid)
1075 struct lu_nodemap *nodemap = NULL;
1078 mutex_lock(&active_config_lock);
1079 nodemap = nodemap_lookup(name);
1080 mutex_unlock(&active_config_lock);
1081 if (IS_ERR(nodemap))
1082 GOTO(out, rc = PTR_ERR(nodemap));
1084 nodemap->nm_squash_uid = uid;
1085 rc = nodemap_idx_nodemap_update(nodemap);
1087 nm_member_revoke_locks(nodemap);
1088 nodemap_putref(nodemap);
1092 EXPORT_SYMBOL(nodemap_set_squash_uid);
1095 * Update the squash_gid for a nodemap.
1097 * \param name nodemap name
1098 * \param gid_string string containing new squash_gid value
1099 * \retval 0 on success
1101 * Update the squash_gid for a nodemap. The squash_uid is the gid
1102 * that the all client gids are mapped to if nodemap is active,
1103 * the trust_client_ids flag is not set, and the gid is not in
1106 int nodemap_set_squash_gid(const char *name, gid_t gid)
1108 struct lu_nodemap *nodemap = NULL;
1111 mutex_lock(&active_config_lock);
1112 nodemap = nodemap_lookup(name);
1113 mutex_unlock(&active_config_lock);
1114 if (IS_ERR(nodemap))
1115 GOTO(out, rc = PTR_ERR(nodemap));
1117 nodemap->nm_squash_gid = gid;
1118 rc = nodemap_idx_nodemap_update(nodemap);
1120 nm_member_revoke_locks(nodemap);
1121 nodemap_putref(nodemap);
1125 EXPORT_SYMBOL(nodemap_set_squash_gid);
1128 * Returns true if this nodemap has root user access. Always returns true if
1129 * nodemaps are not active.
1131 * \param nodemap nodemap to check access for
1133 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
1135 return !nodemap_active || (nodemap && nodemap->nmf_allow_root_access);
1137 EXPORT_SYMBOL(nodemap_can_setquota);
1142 * \param name name of nodemap
1144 * \retval -EINVAL invalid nodemap name
1145 * \retval -EEXIST nodemap already exists
1146 * \retval -ENOMEM cannot allocate memory for nodemap
1148 int nodemap_add(const char *nodemap_name)
1150 struct lu_nodemap *nodemap;
1153 mutex_lock(&active_config_lock);
1154 nodemap = nodemap_create(nodemap_name, active_config, 0);
1155 if (IS_ERR(nodemap)) {
1156 mutex_unlock(&active_config_lock);
1157 return PTR_ERR(nodemap);
1160 rc = nodemap_idx_nodemap_add(nodemap);
1162 rc = lprocfs_nodemap_register(nodemap, 0);
1164 mutex_unlock(&active_config_lock);
1165 nodemap_putref(nodemap);
1169 EXPORT_SYMBOL(nodemap_add);
1174 * \param name name of nodemmap
1176 * \retval -EINVAL invalid input
1177 * \retval -ENOENT no existing nodemap
1179 int nodemap_del(const char *nodemap_name)
1181 struct lu_nodemap *nodemap;
1182 struct lu_nid_range *range;
1183 struct lu_nid_range *range_temp;
1187 if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1190 mutex_lock(&active_config_lock);
1191 nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1193 if (nodemap == NULL) {
1194 mutex_unlock(&active_config_lock);
1195 GOTO(out, rc = -ENOENT);
1198 /* erase nodemap from active ranges to prevent client assignment */
1199 down_write(&active_config->nmc_range_tree_lock);
1200 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1202 rc2 = nodemap_idx_range_del(range);
1206 range_delete(&active_config->nmc_range_tree, range);
1208 up_write(&active_config->nmc_range_tree_lock);
1210 rc2 = nodemap_idx_nodemap_del(nodemap);
1215 * remove procfs here in case nodemap_create called with same name
1216 * before nodemap_destroy is run.
1218 lprocfs_nodemap_remove(nodemap->nm_pde_data);
1219 nodemap->nm_pde_data = NULL;
1221 /* reclassify all member exports from nodemap, so they put their refs */
1222 down_read(&active_config->nmc_range_tree_lock);
1223 nm_member_reclassify_nodemap(nodemap);
1224 up_read(&active_config->nmc_range_tree_lock);
1226 if (!list_empty(&nodemap->nm_member_list))
1227 CWARN("nodemap_del failed to reclassify all members\n");
1229 mutex_unlock(&active_config_lock);
1231 nodemap_putref(nodemap);
1236 EXPORT_SYMBOL(nodemap_del);
1239 * activate nodemap functions
1241 * \param value 1 for on, 0 for off
1243 void nodemap_activate(const bool value)
1245 mutex_lock(&active_config_lock);
1246 active_config->nmc_nodemap_is_active = value;
1248 /* copy active value to global to avoid locking in map functions */
1249 nodemap_active = value;
1250 nodemap_idx_nodemap_activate(value);
1251 mutex_unlock(&active_config_lock);
1252 nm_member_revoke_all();
1254 EXPORT_SYMBOL(nodemap_activate);
1257 * Helper iterator to convert nodemap hash to list.
1259 * \param hs hash structure
1260 * \param bd bucket descriptor
1261 * \param hnode hash node
1262 * \param nodemap_list_head list head for list of nodemaps in hash
1264 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1265 struct hlist_node *hnode,
1266 void *nodemap_list_head)
1268 struct lu_nodemap *nodemap;
1270 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1271 list_add(&nodemap->nm_list, nodemap_list_head);
1273 cfs_hash_bd_del_locked(hs, bd, hnode);
1278 struct nodemap_config *nodemap_config_alloc(void)
1280 struct nodemap_config *config;
1283 OBD_ALLOC_PTR(config);
1285 return ERR_PTR(-ENOMEM);
1287 rc = nodemap_init_hash(config);
1289 OBD_FREE_PTR(config);
1293 init_rwsem(&config->nmc_range_tree_lock);
1297 EXPORT_SYMBOL(nodemap_config_alloc);
1300 * Walk the nodemap_hash and remove all nodemaps.
1302 void nodemap_config_dealloc(struct nodemap_config *config)
1304 struct lu_nodemap *nodemap = NULL;
1305 struct lu_nodemap *nodemap_temp;
1306 struct lu_nid_range *range;
1307 struct lu_nid_range *range_temp;
1308 LIST_HEAD(nodemap_list_head);
1310 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1311 nodemap_cleanup_iter_cb, &nodemap_list_head);
1312 cfs_hash_putref(config->nmc_nodemap_hash);
1314 /* Because nodemap_destroy might sleep, we can't destroy them
1315 * in cfs_hash_for_each, so we build a list there and destroy here
1317 list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1319 down_write(&config->nmc_range_tree_lock);
1321 /* move members to new config */
1322 nm_member_reclassify_nodemap(nodemap);
1323 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1325 range_delete(&config->nmc_range_tree, range);
1326 up_write(&config->nmc_range_tree_lock);
1328 nodemap_putref(nodemap);
1330 OBD_FREE_PTR(config);
1332 EXPORT_SYMBOL(nodemap_config_dealloc);
1334 static int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1335 struct hlist_node *hnode,
1336 void *nodemap_list_head)
1338 struct lu_nodemap *nodemap;
1340 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1341 list_add(&nodemap->nm_list, nodemap_list_head);
1345 void nodemap_config_set_active(struct nodemap_config *config)
1347 struct nodemap_config *old_config = active_config;
1348 struct lu_nodemap *nodemap;
1349 struct lu_nodemap *tmp;
1350 LIST_HEAD(nodemap_list_head);
1354 LASSERT(active_config != config);
1356 mutex_lock(&active_config_lock);
1358 /* move proc entries from already existing nms, create for new nms */
1359 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1360 nm_hash_list_cb, &nodemap_list_head);
1361 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1362 struct lu_nodemap *old_nm = NULL;
1364 if (active_config != NULL)
1365 old_nm = cfs_hash_lookup(
1366 active_config->nmc_nodemap_hash,
1368 if (old_nm != NULL) {
1369 nodemap->nm_pde_data = old_nm->nm_pde_data;
1370 old_nm->nm_pde_data = NULL;
1371 nodemap_putref(old_nm);
1373 bool is_def = (nodemap == config->nmc_default_nodemap);
1375 lprocfs_nodemap_register(nodemap, is_def);
1379 /* if new config is inactive, deactivate live config before switching */
1380 if (!config->nmc_nodemap_is_active)
1381 nodemap_active = false;
1382 active_config = config;
1383 if (config->nmc_nodemap_is_active)
1384 nodemap_active = true;
1386 mutex_unlock(&active_config_lock);
1388 if (old_config != NULL)
1389 nodemap_config_dealloc(old_config);
1391 nm_member_revoke_all();
1395 EXPORT_SYMBOL(nodemap_config_set_active);
1398 * Cleanup nodemap module on exit
1400 void nodemap_mod_exit(void)
1402 nodemap_config_dealloc(active_config);
1403 nodemap_procfs_exit();
1407 * Initialize the nodemap module
1409 int nodemap_mod_init(void)
1411 struct nodemap_config *new_config;
1412 struct lu_nodemap *nodemap;
1415 rc = nodemap_procfs_init();
1419 new_config = nodemap_config_alloc();
1420 if (IS_ERR(new_config)) {
1421 nodemap_procfs_exit();
1422 GOTO(out, rc = PTR_ERR(new_config));
1425 nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1426 if (IS_ERR(nodemap)) {
1427 nodemap_config_dealloc(new_config);
1428 nodemap_procfs_exit();
1429 GOTO(out, rc = PTR_ERR(nodemap));
1432 nodemap_config_set_active(new_config);
1433 nodemap_putref(nodemap);
1440 * Revoke locks for all nodemaps.
1442 void nm_member_revoke_all(void)
1444 struct lu_nodemap *nodemap;
1445 struct lu_nodemap *tmp;
1446 LIST_HEAD(nodemap_list_head);
1448 mutex_lock(&active_config_lock);
1449 cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1450 nm_hash_list_cb, &nodemap_list_head);
1452 /* revoke_locks sleeps, so can't call in cfs hash cb */
1453 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1454 nm_member_revoke_locks(nodemap);
1455 mutex_unlock(&active_config_lock);
1459 * Returns the nodemap classification for a given nid into an ioctl buffer.
1460 * Useful for testing the nodemap configuration to make sure it is working as
1463 * \param nid nid to classify
1464 * \param[out] name_buf buffer to write the nodemap name to
1465 * \param name_len length of buffer
1467 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1469 struct lu_nodemap *nodemap;
1471 mutex_lock(&active_config_lock);
1472 down_read(&active_config->nmc_range_tree_lock);
1473 nodemap = nodemap_classify_nid(nid);
1474 up_read(&active_config->nmc_range_tree_lock);
1475 mutex_unlock(&active_config_lock);
1477 strncpy(name_buf, nodemap->nm_name, name_len);
1479 name_buf[name_len - 1] = '\0';
1481 nodemap_putref(nodemap);
1483 EXPORT_SYMBOL(nodemap_test_nid);
1486 * Returns the id mapping for a given nid/id pair. Useful for testing the
1487 * nodemap configuration to make sure it is working as expected.
1489 * \param nid nid to classify
1490 * \param idtype uid or gid
1491 * \param client_id id to map to fs
1493 * \retval the mapped fs_id of the given client_id
1495 __u32 nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1498 struct lu_nodemap *nodemap;
1501 mutex_lock(&active_config_lock);
1502 down_read(&active_config->nmc_range_tree_lock);
1503 nodemap = nodemap_classify_nid(nid);
1504 up_read(&active_config->nmc_range_tree_lock);
1505 mutex_unlock(&active_config_lock);
1507 fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1509 nodemap_putref(nodemap);
1513 EXPORT_SYMBOL(nodemap_test_id);