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 static 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)
103 LASSERT(nodemap != NULL);
104 LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
106 if (atomic_dec_and_test(&nodemap->nm_refcount))
107 nodemap_destroy(nodemap);
110 static __u32 nodemap_hashfn(struct cfs_hash *hash_body,
111 const void *key, unsigned mask)
113 return cfs_hash_djb2_hash(key, strlen(key), mask);
116 static void *nodemap_hs_key(struct hlist_node *hnode)
118 struct lu_nodemap *nodemap;
120 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
122 return nodemap->nm_name;
125 static int nodemap_hs_keycmp(const void *key,
126 struct hlist_node *compared_hnode)
130 nodemap_name = nodemap_hs_key(compared_hnode);
132 return !strcmp(key, nodemap_name);
135 static void *nodemap_hs_hashobject(struct hlist_node *hnode)
137 return hlist_entry(hnode, struct lu_nodemap, nm_hash);
140 static void nodemap_hs_get(struct cfs_hash *hs, struct hlist_node *hnode)
142 struct lu_nodemap *nodemap;
144 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
145 nodemap_getref(nodemap);
148 static void nodemap_hs_put_locked(struct cfs_hash *hs,
149 struct hlist_node *hnode)
151 struct lu_nodemap *nodemap;
153 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
154 nodemap_putref(nodemap);
157 static struct cfs_hash_ops nodemap_hash_operations = {
158 .hs_hash = nodemap_hashfn,
159 .hs_key = nodemap_hs_key,
160 .hs_keycmp = nodemap_hs_keycmp,
161 .hs_object = nodemap_hs_hashobject,
162 .hs_get = nodemap_hs_get,
163 .hs_put_locked = nodemap_hs_put_locked,
166 /* end of cfs_hash functions */
169 * Initialize nodemap_hash
172 * \retval -ENOMEM cannot create hash
174 static int nodemap_init_hash(struct nodemap_config *nmc)
176 nmc->nmc_nodemap_hash = cfs_hash_create("NODEMAP",
177 HASH_NODEMAP_CUR_BITS,
178 HASH_NODEMAP_MAX_BITS,
179 HASH_NODEMAP_BKT_BITS, 0,
182 &nodemap_hash_operations,
185 if (nmc->nmc_nodemap_hash == NULL) {
186 CERROR("cannot create nodemap_hash table\n");
194 * Check for valid nodemap name
196 * \param name nodemap name
198 * \retval false invalid
200 static bool nodemap_name_is_valid(const char *name)
202 if (strlen(name) > LUSTRE_NODEMAP_NAME_LENGTH ||
206 for (; *name != '\0'; name++) {
207 if (!isalnum(*name) && *name != '_')
217 * Look nodemap up in the active_config nodemap hash. Caller should hold the
218 * active_config_lock.
220 * \param name name of nodemap
221 * \retval nodemap pointer set to found nodemap
222 * \retval -EINVAL name is not valid
223 * \retval -ENOENT nodemap not found
225 struct lu_nodemap *nodemap_lookup(const char *name)
227 struct lu_nodemap *nodemap = NULL;
229 if (!nodemap_name_is_valid(name))
230 return ERR_PTR(-EINVAL);
232 nodemap = cfs_hash_lookup(active_config->nmc_nodemap_hash, name);
234 return ERR_PTR(-ENOENT);
240 * Classify the nid into the proper nodemap. Caller must hold active config and
241 * nm_range_tree_lock, and call nodemap_putref when done with nodemap.
243 * \param nid nid to classify
244 * \retval nodemap nodemap containing the nid
245 * \retval default_nodemap default nodemap
247 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
249 struct lu_nid_range *range;
250 struct lu_nodemap *nodemap;
252 range = range_search(&active_config->nmc_range_tree, nid);
254 nodemap = range->rn_nodemap;
256 nodemap = active_config->nmc_default_nodemap;
258 nodemap_getref(nodemap);
264 * simple check for default nodemap
266 static bool is_default_nodemap(const struct lu_nodemap *nodemap)
268 return nodemap->nm_id == 0;
272 * parse a nodemap range string into two nids
274 * \param range_str string to parse
275 * \param range[2] array of two nids
276 * \reyval 0 on success
278 int nodemap_parse_range(const char *range_str, lnet_nid_t range[2])
280 char buf[LNET_NIDSTR_SIZE * 2 + 2];
286 snprintf(buf, sizeof(buf), "%s", range_str);
288 start_nidstr = strsep(&ptr, ":");
289 end_nidstr = strsep(&ptr, ":");
291 if (start_nidstr == NULL || end_nidstr == NULL)
292 GOTO(out, rc = -EINVAL);
294 range[0] = libcfs_str2nid(start_nidstr);
295 range[1] = libcfs_str2nid(end_nidstr);
301 EXPORT_SYMBOL(nodemap_parse_range);
304 * parse a string containing an id map of form "client_id:filesystem_id"
305 * into an array of __u32 * for use in mapping functions
307 * \param idmap_str map string
308 * \param idmap array[2] of __u32
310 * \retval 0 on success
311 * \retval -EINVAL if idmap cannot be parsed
313 int nodemap_parse_idmap(char *idmap_str, __u32 idmap[2])
316 long unsigned int idmap_buf;
319 if (idmap_str == NULL)
322 sep = strchr(idmap_str, ':');
328 rc = kstrtoul(idmap_str, 10, &idmap_buf);
331 idmap[0] = idmap_buf;
333 rc = kstrtoul(sep, 10, &idmap_buf);
336 idmap[1] = idmap_buf;
340 EXPORT_SYMBOL(nodemap_parse_idmap);
343 * add a member to a nodemap
345 * \param nid nid to add to the members
346 * \param exp obd_export structure for the connection
347 * that is being added
348 * \retval -EINVAL export is NULL
349 * \retval -EEXIST export is already member of a nodemap
351 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
353 struct lu_nodemap *nodemap;
356 mutex_lock(&active_config_lock);
357 down_read(&active_config->nmc_range_tree_lock);
359 nodemap = nodemap_classify_nid(nid);
360 rc = nm_member_add(nodemap, exp);
362 up_read(&active_config->nmc_range_tree_lock);
363 mutex_unlock(&active_config_lock);
365 nodemap_putref(nodemap);
369 EXPORT_SYMBOL(nodemap_add_member);
372 * delete a member from a nodemap
374 * \param exp export to remove from a nodemap
376 void nodemap_del_member(struct obd_export *exp)
378 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
381 nm_member_del(nodemap, exp);
383 EXPORT_SYMBOL(nodemap_del_member);
386 * add an idmap to the proper nodemap trees
388 * \param name name of nodemap
389 * \param id_type NODEMAP_UID or NODEMAP_GID
390 * \param map array[2] __u32 containing the map values
391 * map[0] is client id
392 * map[1] is the filesystem id
394 * \retval 0 on success
396 int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
397 enum nodemap_id_type id_type,
400 struct lu_idmap *idmap;
403 idmap = idmap_create(map[0], map[1]);
405 GOTO(out, rc = -ENOMEM);
407 write_lock(&nodemap->nm_idmap_lock);
408 idmap_insert(id_type, idmap, nodemap);
409 write_unlock(&nodemap->nm_idmap_lock);
410 nm_member_revoke_locks(nodemap);
416 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
419 struct lu_nodemap *nodemap = NULL;
422 mutex_lock(&active_config_lock);
423 nodemap = nodemap_lookup(name);
424 if (IS_ERR(nodemap)) {
425 mutex_unlock(&active_config_lock);
426 GOTO(out, rc = PTR_ERR(nodemap));
429 if (is_default_nodemap(nodemap)) {
432 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
434 rc = nodemap_idx_idmap_add(nodemap, id_type, map);
436 mutex_unlock(&active_config_lock);
437 nodemap_putref(nodemap);
442 EXPORT_SYMBOL(nodemap_add_idmap);
445 * delete idmap from proper nodemap tree
447 * \param name name of nodemap
448 * \param id_type NODEMAP_UID or NODEMAP_GID
449 * \param map array[2] __u32 containing the mapA values
450 * map[0] is client id
451 * map[1] is the filesystem id
453 * \retval 0 on success
455 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
458 struct lu_nodemap *nodemap = NULL;
459 struct lu_idmap *idmap = NULL;
462 mutex_lock(&active_config_lock);
463 nodemap = nodemap_lookup(name);
464 if (IS_ERR(nodemap)) {
465 mutex_unlock(&active_config_lock);
466 GOTO(out, rc = PTR_ERR(nodemap));
469 if (is_default_nodemap(nodemap))
470 GOTO(out_putref, rc = -EINVAL);
472 write_lock(&nodemap->nm_idmap_lock);
473 idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
478 idmap_delete(id_type, idmap, nodemap);
479 rc = nodemap_idx_idmap_del(nodemap, id_type, map);
481 write_unlock(&nodemap->nm_idmap_lock);
484 mutex_unlock(&active_config_lock);
486 nm_member_revoke_locks(nodemap);
487 nodemap_putref(nodemap);
492 EXPORT_SYMBOL(nodemap_del_idmap);
495 * mapping function for nodemap idmaps
497 * \param nodemap lu_nodemap structure defining nodemap
498 * \param node_type NODEMAP_UID or NODEMAP_GID
499 * \param tree_type NODEMAP_CLIENT_TO_FS or
500 * NODEMAP_FS_TO_CLIENT
501 * \param id id to map
503 * \retval mapped id according to the rules below.
505 * if the nodemap_active is false, just return the passed id without mapping
507 * if the id to be looked up is 0, check that root access is allowed and if it
508 * is, return 0. Otherwise, return the squash uid or gid.
510 * if the nodemap is configured to trusted the ids from the client system, just
511 * return the passwd id without mapping.
513 * if by this point, we haven't returned and the nodemap in question is the
514 * default nodemap, return the squash uid or gid.
516 * after these checks, search the proper tree for the mapping, and if found
517 * return the mapped value, otherwise return the squash uid or gid.
519 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
520 enum nodemap_id_type id_type,
521 enum nodemap_tree_type tree_type, __u32 id)
523 struct lu_idmap *idmap = NULL;
529 if (unlikely(nodemap == NULL))
533 if (nodemap->nmf_allow_root_access)
539 if (nodemap->nmf_trust_client_ids)
542 if (is_default_nodemap(nodemap))
545 read_lock(&nodemap->nm_idmap_lock);
546 idmap = idmap_search(nodemap, tree_type, id_type, id);
548 read_unlock(&nodemap->nm_idmap_lock);
552 if (tree_type == NODEMAP_FS_TO_CLIENT)
553 found_id = idmap->id_client;
555 found_id = idmap->id_fs;
556 read_unlock(&nodemap->nm_idmap_lock);
560 if (id_type == NODEMAP_UID)
561 return nodemap->nm_squash_uid;
563 return nodemap->nm_squash_gid;
567 EXPORT_SYMBOL(nodemap_map_id);
570 * Map posix ACL entries according to the nodemap membership. Removes any
573 * \param lu_nodemap nodemap
574 * \param buf buffer containing xattr encoded ACLs
575 * \param size size of ACLs in bytes
576 * \param tree_type direction of mapping
577 * \retval size new size of ACLs in bytes
578 * \retval -EINVAL bad \a size param, see posix_acl_xattr_count()
580 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
581 enum nodemap_tree_type tree_type)
583 posix_acl_xattr_header *header = buf;
584 posix_acl_xattr_entry *entry = &header->a_entries[0];
585 posix_acl_xattr_entry *new_entry = entry;
586 posix_acl_xattr_entry *end;
592 if (unlikely(nodemap == NULL))
595 count = posix_acl_xattr_count(size);
601 for (end = entry + count; entry != end; entry++) {
602 __u16 tag = le16_to_cpu(entry->e_tag);
603 __u32 id = le32_to_cpu(entry->e_id);
607 id = nodemap_map_id(nodemap, NODEMAP_UID,
609 if (id == nodemap->nm_squash_uid)
611 entry->e_id = cpu_to_le32(id);
614 id = nodemap_map_id(nodemap, NODEMAP_GID,
616 if (id == nodemap->nm_squash_gid)
618 entry->e_id = cpu_to_le32(id);
622 /* if we skip an ACL, copy the following ones over it */
623 if (new_entry != entry)
629 return (void *)new_entry - (void *)header;
631 EXPORT_SYMBOL(nodemap_map_acl);
634 * Add nid range to given nodemap
636 * \param config nodemap config to work on
637 * \param nodemap nodemap to add range to
638 * \param nid nid range to add
639 * \param range_id should be 0 unless loading from disk
644 int nodemap_add_range_helper(struct nodemap_config *config,
645 struct lu_nodemap *nodemap,
646 const lnet_nid_t nid[2],
647 unsigned int range_id)
649 struct lu_nid_range *range;
652 down_write(&config->nmc_range_tree_lock);
653 range = range_create(&config->nmc_range_tree, nid[0], nid[1],
656 up_write(&config->nmc_range_tree_lock);
657 GOTO(out, rc = -ENOMEM);
660 rc = range_insert(&config->nmc_range_tree, range);
662 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
663 nodemap->nm_name, rc);
664 up_write(&config->nmc_range_tree_lock);
665 list_del(&range->rn_list);
666 range_destroy(range);
667 GOTO(out, rc = -ENOMEM);
670 list_add(&range->rn_list, &nodemap->nm_ranges);
671 nm_member_reclassify_nodemap(config->nmc_default_nodemap);
672 up_write(&config->nmc_range_tree_lock);
674 /* if range_id is non-zero, we are loading from disk */
676 rc = nodemap_idx_range_add(range, nid);
678 nm_member_revoke_locks(config->nmc_default_nodemap);
679 nm_member_revoke_locks(nodemap);
684 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
686 struct lu_nodemap *nodemap = NULL;
689 mutex_lock(&active_config_lock);
690 nodemap = nodemap_lookup(name);
691 if (IS_ERR(nodemap)) {
692 mutex_unlock(&active_config_lock);
693 GOTO(out, rc = PTR_ERR(nodemap));
696 if (is_default_nodemap(nodemap))
699 rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
700 mutex_unlock(&active_config_lock);
701 nodemap_putref(nodemap);
705 EXPORT_SYMBOL(nodemap_add_range);
709 * \param name nodemap name
710 * \param range_str string containing range
711 * \retval 0 on success
713 * Delete range from global range tree, and remove it
714 * from the list in the associated nodemap.
716 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
718 struct lu_nodemap *nodemap;
719 struct lu_nid_range *range;
722 mutex_lock(&active_config_lock);
723 nodemap = nodemap_lookup(name);
724 if (IS_ERR(nodemap)) {
725 mutex_unlock(&active_config_lock);
726 GOTO(out, rc = PTR_ERR(nodemap));
729 if (is_default_nodemap(nodemap))
730 GOTO(out_putref, rc = -EINVAL);
732 down_write(&active_config->nmc_range_tree_lock);
733 range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
735 up_write(&active_config->nmc_range_tree_lock);
736 GOTO(out_putref, rc = -EINVAL);
738 rc = nodemap_idx_range_del(range);
739 range_delete(&active_config->nmc_range_tree, range);
740 nm_member_reclassify_nodemap(nodemap);
741 up_write(&active_config->nmc_range_tree_lock);
743 nm_member_revoke_locks(active_config->nmc_default_nodemap);
744 nm_member_revoke_locks(nodemap);
747 mutex_unlock(&active_config_lock);
748 nodemap_putref(nodemap);
752 EXPORT_SYMBOL(nodemap_del_range);
755 * Nodemap constructor
757 * Creates an lu_nodemap structure and assigns sane default
758 * member values. If this is the default nodemap, the defaults
759 * are the most restictive in xterms of mapping behavior. Otherwise
760 * the default flags should be inherited from the default nodemap.
761 * The adds nodemap to nodemap_hash.
763 * Requires that the caller take the active_config_lock
765 * \param name name of nodemap
766 * \param is_default true if default nodemap
767 * \retval nodemap success
768 * \retval -EINVAL invalid nodemap name
769 * \retval -EEXIST nodemap already exists
770 * \retval -ENOMEM cannot allocate memory for nodemap
772 struct lu_nodemap *nodemap_create(const char *name,
773 struct nodemap_config *config,
776 struct lu_nodemap *nodemap = NULL;
777 struct lu_nodemap *default_nodemap;
778 struct cfs_hash *hash = config->nmc_nodemap_hash;
781 default_nodemap = config->nmc_default_nodemap;
783 if (!nodemap_name_is_valid(name))
784 GOTO(out, rc = -EINVAL);
787 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
788 GOTO(out, rc = -EINVAL);
791 OBD_ALLOC_PTR(nodemap);
792 if (nodemap == NULL) {
793 CERROR("cannot allocate memory (%zu bytes)"
794 "for nodemap '%s'\n", sizeof(*nodemap),
796 GOTO(out, rc = -ENOMEM);
800 * take an extra reference to prevent nodemap from being destroyed
801 * while it's being created.
803 atomic_set(&nodemap->nm_refcount, 2);
804 snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
805 rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
807 OBD_FREE_PTR(nodemap);
808 GOTO(out, rc = -EEXIST);
811 INIT_LIST_HEAD(&nodemap->nm_ranges);
812 INIT_LIST_HEAD(&nodemap->nm_list);
813 INIT_LIST_HEAD(&nodemap->nm_member_list);
815 mutex_init(&nodemap->nm_member_list_lock);
816 rwlock_init(&nodemap->nm_idmap_lock);
817 nodemap->nm_fs_to_client_uidmap = RB_ROOT;
818 nodemap->nm_client_to_fs_uidmap = RB_ROOT;
819 nodemap->nm_fs_to_client_gidmap = RB_ROOT;
820 nodemap->nm_client_to_fs_gidmap = RB_ROOT;
823 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
824 config->nmc_default_nodemap = nodemap;
826 config->nmc_nodemap_highest_id++;
827 nodemap->nm_id = config->nmc_nodemap_highest_id;
830 if (is_default || default_nodemap == NULL) {
831 nodemap->nmf_trust_client_ids = 0;
832 nodemap->nmf_allow_root_access = 0;
834 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
835 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
837 CWARN("adding nodemap '%s' to config without"
838 " default nodemap\n", nodemap->nm_name);
840 nodemap->nmf_trust_client_ids =
841 default_nodemap->nmf_trust_client_ids;
842 nodemap->nmf_allow_root_access =
843 default_nodemap->nmf_allow_root_access;
845 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
846 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
852 CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
857 * update flag to turn on or off nodemap functions
858 * \param name nodemap name
859 * \param admin_string string containing updated value
860 * \retval 0 on success
862 * Update admin flag to turn on or off nodemap functions.
864 int nodemap_set_allow_root(const char *name, bool allow_root)
866 struct lu_nodemap *nodemap = NULL;
869 mutex_lock(&active_config_lock);
870 nodemap = nodemap_lookup(name);
871 mutex_unlock(&active_config_lock);
873 GOTO(out, rc = PTR_ERR(nodemap));
875 nodemap->nmf_allow_root_access = allow_root;
876 rc = nodemap_idx_nodemap_update(nodemap);
878 nm_member_revoke_locks(nodemap);
879 nodemap_putref(nodemap);
883 EXPORT_SYMBOL(nodemap_set_allow_root);
886 * updated trust_client_ids flag for nodemap
888 * \param name nodemap name
889 * \param trust_string new value for trust flag
890 * \retval 0 on success
892 * Update the trust_client_ids flag for a nodemap.
894 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
896 struct lu_nodemap *nodemap = NULL;
899 mutex_lock(&active_config_lock);
900 nodemap = nodemap_lookup(name);
901 mutex_unlock(&active_config_lock);
903 GOTO(out, rc = PTR_ERR(nodemap));
905 nodemap->nmf_trust_client_ids = trust_client_ids;
906 rc = nodemap_idx_nodemap_update(nodemap);
908 nm_member_revoke_locks(nodemap);
909 nodemap_putref(nodemap);
913 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
916 * update the squash_uid for a nodemap
918 * \param name nodemap name
919 * \param uid_string string containing new squash_uid value
920 * \retval 0 on success
922 * Update the squash_uid for a nodemap. The squash_uid is the uid
923 * that the all client uids are mapped to if nodemap is active,
924 * the trust_client_ids flag is not set, and the uid is not in
927 int nodemap_set_squash_uid(const char *name, uid_t uid)
929 struct lu_nodemap *nodemap = NULL;
932 mutex_lock(&active_config_lock);
933 nodemap = nodemap_lookup(name);
934 mutex_unlock(&active_config_lock);
936 GOTO(out, rc = PTR_ERR(nodemap));
938 nodemap->nm_squash_uid = uid;
939 rc = nodemap_idx_nodemap_update(nodemap);
941 nm_member_revoke_locks(nodemap);
942 nodemap_putref(nodemap);
946 EXPORT_SYMBOL(nodemap_set_squash_uid);
949 * Update the squash_gid for a nodemap.
951 * \param name nodemap name
952 * \param gid_string string containing new squash_gid value
953 * \retval 0 on success
955 * Update the squash_gid for a nodemap. The squash_uid is the gid
956 * that the all client gids are mapped to if nodemap is active,
957 * the trust_client_ids flag is not set, and the gid is not in
960 int nodemap_set_squash_gid(const char *name, gid_t gid)
962 struct lu_nodemap *nodemap = NULL;
965 mutex_lock(&active_config_lock);
966 nodemap = nodemap_lookup(name);
967 mutex_unlock(&active_config_lock);
969 GOTO(out, rc = PTR_ERR(nodemap));
971 nodemap->nm_squash_gid = gid;
972 rc = nodemap_idx_nodemap_update(nodemap);
974 nm_member_revoke_locks(nodemap);
975 nodemap_putref(nodemap);
979 EXPORT_SYMBOL(nodemap_set_squash_gid);
982 * Returns true if this nodemap has root user access. Always returns true if
983 * nodemaps are not active.
985 * \param nodemap nodemap to check access for
987 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
989 return !nodemap_active || nodemap->nmf_allow_root_access;
991 EXPORT_SYMBOL(nodemap_can_setquota);
996 * \param name name of nodemap
998 * \retval -EINVAL invalid nodemap name
999 * \retval -EEXIST nodemap already exists
1000 * \retval -ENOMEM cannot allocate memory for nodemap
1002 int nodemap_add(const char *nodemap_name)
1004 struct lu_nodemap *nodemap;
1007 mutex_lock(&active_config_lock);
1008 nodemap = nodemap_create(nodemap_name, active_config, 0);
1009 if (IS_ERR(nodemap)) {
1010 mutex_unlock(&active_config_lock);
1011 return PTR_ERR(nodemap);
1014 rc = nodemap_idx_nodemap_add(nodemap);
1016 rc = lprocfs_nodemap_register(nodemap, 0);
1018 mutex_unlock(&active_config_lock);
1019 nodemap_putref(nodemap);
1023 EXPORT_SYMBOL(nodemap_add);
1028 * \param name name of nodemmap
1030 * \retval -EINVAL invalid input
1031 * \retval -ENOENT no existing nodemap
1033 int nodemap_del(const char *nodemap_name)
1035 struct lu_nodemap *nodemap;
1036 struct lu_nid_range *range;
1037 struct lu_nid_range *range_temp;
1041 if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1044 mutex_lock(&active_config_lock);
1045 nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1047 if (nodemap == NULL) {
1048 mutex_unlock(&active_config_lock);
1049 GOTO(out, rc = -ENOENT);
1052 /* erase nodemap from active ranges to prevent client assignment */
1053 down_write(&active_config->nmc_range_tree_lock);
1054 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1056 rc2 = nodemap_idx_range_del(range);
1060 range_delete(&active_config->nmc_range_tree, range);
1062 up_write(&active_config->nmc_range_tree_lock);
1064 rc2 = nodemap_idx_nodemap_del(nodemap);
1069 * remove procfs here in case nodemap_create called with same name
1070 * before nodemap_destroy is run.
1072 lprocfs_nodemap_remove(nodemap->nm_pde_data);
1073 nodemap->nm_pde_data = NULL;
1074 mutex_unlock(&active_config_lock);
1076 nodemap_putref(nodemap);
1081 EXPORT_SYMBOL(nodemap_del);
1084 * activate nodemap functions
1086 * \param value 1 for on, 0 for off
1088 void nodemap_activate(const bool value)
1090 mutex_lock(&active_config_lock);
1091 active_config->nmc_nodemap_is_active = value;
1093 /* copy active value to global to avoid locking in map functions */
1094 nodemap_active = value;
1095 nodemap_idx_nodemap_activate(value);
1096 mutex_unlock(&active_config_lock);
1097 nm_member_revoke_all();
1099 EXPORT_SYMBOL(nodemap_activate);
1102 * Helper iterator to convert nodemap hash to list.
1104 * \param hs hash structure
1105 * \param bd bucket descriptor
1106 * \param hnode hash node
1107 * \param nodemap_list_head list head for list of nodemaps in hash
1109 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1110 struct hlist_node *hnode,
1111 void *nodemap_list_head)
1113 struct lu_nodemap *nodemap;
1115 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1116 list_add(&nodemap->nm_list, nodemap_list_head);
1118 cfs_hash_bd_del_locked(hs, bd, hnode);
1124 * Walk the nodemap_hash and remove all nodemaps.
1126 void nodemap_config_cleanup(struct nodemap_config *config)
1128 struct lu_nodemap *nodemap = NULL;
1129 struct lu_nodemap *nodemap_temp;
1130 struct lu_nid_range *range;
1131 struct lu_nid_range *range_temp;
1132 LIST_HEAD(nodemap_list_head);
1134 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1135 nodemap_cleanup_iter_cb, &nodemap_list_head);
1136 cfs_hash_putref(config->nmc_nodemap_hash);
1138 /* Because nodemap_destroy might sleep, we can't destroy them
1139 * in cfs_hash_for_each, so we build a list there and destroy here
1141 list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1143 down_write(&config->nmc_range_tree_lock);
1144 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1146 range_delete(&config->nmc_range_tree, range);
1147 up_write(&config->nmc_range_tree_lock);
1149 nodemap_putref(nodemap);
1153 struct nodemap_config *nodemap_config_alloc(void)
1155 struct nodemap_config *config;
1158 OBD_ALLOC_PTR(config);
1160 return ERR_PTR(-ENOMEM);
1162 rc = nodemap_init_hash(config);
1164 OBD_FREE_PTR(config);
1168 init_rwsem(&config->nmc_range_tree_lock);
1173 void nodemap_config_dealloc(struct nodemap_config *config)
1175 nodemap_config_cleanup(config);
1176 OBD_FREE_PTR(config);
1179 static int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1180 struct hlist_node *hnode,
1181 void *nodemap_list_head)
1183 struct lu_nodemap *nodemap;
1185 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1186 list_add(&nodemap->nm_list, nodemap_list_head);
1190 void nodemap_config_set_active(struct nodemap_config *config)
1192 struct nodemap_config *old_config = active_config;
1193 struct lu_nodemap *nodemap;
1194 struct lu_nodemap *tmp;
1195 LIST_HEAD(nodemap_list_head);
1199 LASSERT(active_config != config);
1201 mutex_lock(&active_config_lock);
1203 /* move proc entries from already existing nms, create for new nms */
1204 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1205 nm_hash_list_cb, &nodemap_list_head);
1206 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1207 struct lu_nodemap *old_nm = NULL;
1209 if (active_config != NULL)
1210 old_nm = cfs_hash_lookup(
1211 active_config->nmc_nodemap_hash,
1213 if (old_nm != NULL) {
1214 nodemap->nm_pde_data = old_nm->nm_pde_data;
1215 old_nm->nm_pde_data = NULL;
1216 nodemap_putref(old_nm);
1218 bool is_def = (nodemap == config->nmc_default_nodemap);
1220 lprocfs_nodemap_register(nodemap, is_def);
1224 /* if new config is inactive, deactivate live config before switching */
1225 if (!config->nmc_nodemap_is_active)
1226 nodemap_active = false;
1227 active_config = config;
1228 if (config->nmc_nodemap_is_active)
1229 nodemap_active = true;
1231 mutex_unlock(&active_config_lock);
1233 if (old_config != NULL)
1234 nodemap_config_dealloc(old_config);
1236 nm_member_revoke_all();
1242 * Cleanup nodemap module on exit
1244 void nodemap_mod_exit(void)
1246 nodemap_config_dealloc(active_config);
1247 nodemap_procfs_exit();
1251 * Initialize the nodemap module
1253 int nodemap_mod_init(void)
1255 struct nodemap_config *new_config;
1256 struct lu_nodemap *nodemap;
1259 rc = nodemap_procfs_init();
1263 new_config = nodemap_config_alloc();
1264 if (IS_ERR(new_config)) {
1265 nodemap_procfs_exit();
1266 GOTO(out, rc = PTR_ERR(new_config));
1269 nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1270 if (IS_ERR(nodemap)) {
1271 nodemap_config_dealloc(new_config);
1272 nodemap_procfs_exit();
1273 GOTO(out, rc = PTR_ERR(nodemap));
1276 nodemap_config_set_active(new_config);
1277 nodemap_putref(nodemap);
1284 * Revoke locks for all nodemaps.
1286 void nm_member_revoke_all(void)
1288 struct lu_nodemap *nodemap;
1289 struct lu_nodemap *tmp;
1290 LIST_HEAD(nodemap_list_head);
1292 mutex_lock(&active_config_lock);
1293 cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1294 nm_hash_list_cb, &nodemap_list_head);
1296 /* revoke_locks sleeps, so can't call in cfs hash cb */
1297 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1298 nm_member_revoke_locks(nodemap);
1299 mutex_unlock(&active_config_lock);
1303 * Returns the nodemap classification for a given nid into an ioctl buffer.
1304 * Useful for testing the nodemap configuration to make sure it is working as
1307 * \param nid nid to classify
1308 * \param[out] name_buf buffer to write the nodemap name to
1309 * \param name_len length of buffer
1311 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1313 struct lu_nodemap *nodemap;
1315 mutex_lock(&active_config_lock);
1316 down_read(&active_config->nmc_range_tree_lock);
1317 nodemap = nodemap_classify_nid(nid);
1318 up_read(&active_config->nmc_range_tree_lock);
1319 mutex_unlock(&active_config_lock);
1321 strncpy(name_buf, nodemap->nm_name, name_len);
1323 name_buf[name_len - 1] = '\0';
1325 nodemap_putref(nodemap);
1327 EXPORT_SYMBOL(nodemap_test_nid);
1330 * Returns the id mapping for a given nid/id pair. Useful for testing the
1331 * nodemap configuration to make sure it is working as expected.
1333 * \param nid nid to classify
1334 * \param idtype uid or gid
1335 * \param client_id id to map to fs
1337 * \retval the mapped fs_id of the given client_id
1339 __u32 nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1342 struct lu_nodemap *nodemap;
1345 mutex_lock(&active_config_lock);
1346 down_read(&active_config->nmc_range_tree_lock);
1347 nodemap = nodemap_classify_nid(nid);
1348 up_read(&active_config->nmc_range_tree_lock);
1349 mutex_unlock(&active_config_lock);
1351 fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1353 nodemap_putref(nodemap);
1357 EXPORT_SYMBOL(nodemap_test_id);