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 * Nodemap constructor
836 * Creates an lu_nodemap structure and assigns sane default
837 * member values. If this is the default nodemap, the defaults
838 * are the most restictive in xterms of mapping behavior. Otherwise
839 * the default flags should be inherited from the default nodemap.
840 * The adds nodemap to nodemap_hash.
842 * Requires that the caller take the active_config_lock
844 * \param name name of nodemap
845 * \param is_default true if default nodemap
846 * \retval nodemap success
847 * \retval -EINVAL invalid nodemap name
848 * \retval -EEXIST nodemap already exists
849 * \retval -ENOMEM cannot allocate memory for nodemap
851 struct lu_nodemap *nodemap_create(const char *name,
852 struct nodemap_config *config,
855 struct lu_nodemap *nodemap = NULL;
856 struct lu_nodemap *default_nodemap;
857 struct cfs_hash *hash = config->nmc_nodemap_hash;
860 default_nodemap = config->nmc_default_nodemap;
862 if (!nodemap_name_is_valid(name))
863 GOTO(out, rc = -EINVAL);
866 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
867 GOTO(out, rc = -EINVAL);
870 OBD_ALLOC_PTR(nodemap);
871 if (nodemap == NULL) {
872 CERROR("cannot allocate memory (%zu bytes)"
873 "for nodemap '%s'\n", sizeof(*nodemap),
875 GOTO(out, rc = -ENOMEM);
879 * take an extra reference to prevent nodemap from being destroyed
880 * while it's being created.
882 atomic_set(&nodemap->nm_refcount, 2);
883 snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
884 rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
886 OBD_FREE_PTR(nodemap);
887 GOTO(out, rc = -EEXIST);
890 INIT_LIST_HEAD(&nodemap->nm_ranges);
891 INIT_LIST_HEAD(&nodemap->nm_list);
892 INIT_LIST_HEAD(&nodemap->nm_member_list);
894 mutex_init(&nodemap->nm_member_list_lock);
895 rwlock_init(&nodemap->nm_idmap_lock);
896 nodemap->nm_fs_to_client_uidmap = RB_ROOT;
897 nodemap->nm_client_to_fs_uidmap = RB_ROOT;
898 nodemap->nm_fs_to_client_gidmap = RB_ROOT;
899 nodemap->nm_client_to_fs_gidmap = RB_ROOT;
902 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
903 config->nmc_default_nodemap = nodemap;
905 config->nmc_nodemap_highest_id++;
906 nodemap->nm_id = config->nmc_nodemap_highest_id;
909 if (is_default || default_nodemap == NULL) {
910 nodemap->nmf_trust_client_ids = 0;
911 nodemap->nmf_allow_root_access = 0;
913 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
914 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
916 CWARN("adding nodemap '%s' to config without"
917 " default nodemap\n", nodemap->nm_name);
919 nodemap->nmf_trust_client_ids =
920 default_nodemap->nmf_trust_client_ids;
921 nodemap->nmf_allow_root_access =
922 default_nodemap->nmf_allow_root_access;
924 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
925 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
931 CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
936 * update flag to turn on or off nodemap functions
937 * \param name nodemap name
938 * \param admin_string string containing updated value
939 * \retval 0 on success
941 * Update admin flag to turn on or off nodemap functions.
943 int nodemap_set_allow_root(const char *name, bool allow_root)
945 struct lu_nodemap *nodemap = NULL;
948 mutex_lock(&active_config_lock);
949 nodemap = nodemap_lookup(name);
950 mutex_unlock(&active_config_lock);
952 GOTO(out, rc = PTR_ERR(nodemap));
954 nodemap->nmf_allow_root_access = allow_root;
955 rc = nodemap_idx_nodemap_update(nodemap);
957 nm_member_revoke_locks(nodemap);
958 nodemap_putref(nodemap);
962 EXPORT_SYMBOL(nodemap_set_allow_root);
965 * updated trust_client_ids flag for nodemap
967 * \param name nodemap name
968 * \param trust_string new value for trust flag
969 * \retval 0 on success
971 * Update the trust_client_ids flag for a nodemap.
973 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
975 struct lu_nodemap *nodemap = NULL;
978 mutex_lock(&active_config_lock);
979 nodemap = nodemap_lookup(name);
980 mutex_unlock(&active_config_lock);
982 GOTO(out, rc = PTR_ERR(nodemap));
984 nodemap->nmf_trust_client_ids = trust_client_ids;
985 rc = nodemap_idx_nodemap_update(nodemap);
987 nm_member_revoke_locks(nodemap);
988 nodemap_putref(nodemap);
992 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
995 * update the squash_uid for a nodemap
997 * \param name nodemap name
998 * \param uid_string string containing new squash_uid value
999 * \retval 0 on success
1001 * Update the squash_uid for a nodemap. The squash_uid is the uid
1002 * that the all client uids are mapped to if nodemap is active,
1003 * the trust_client_ids flag is not set, and the uid is not in
1006 int nodemap_set_squash_uid(const char *name, uid_t uid)
1008 struct lu_nodemap *nodemap = NULL;
1011 mutex_lock(&active_config_lock);
1012 nodemap = nodemap_lookup(name);
1013 mutex_unlock(&active_config_lock);
1014 if (IS_ERR(nodemap))
1015 GOTO(out, rc = PTR_ERR(nodemap));
1017 nodemap->nm_squash_uid = uid;
1018 rc = nodemap_idx_nodemap_update(nodemap);
1020 nm_member_revoke_locks(nodemap);
1021 nodemap_putref(nodemap);
1025 EXPORT_SYMBOL(nodemap_set_squash_uid);
1028 * Update the squash_gid for a nodemap.
1030 * \param name nodemap name
1031 * \param gid_string string containing new squash_gid value
1032 * \retval 0 on success
1034 * Update the squash_gid for a nodemap. The squash_uid is the gid
1035 * that the all client gids are mapped to if nodemap is active,
1036 * the trust_client_ids flag is not set, and the gid is not in
1039 int nodemap_set_squash_gid(const char *name, gid_t gid)
1041 struct lu_nodemap *nodemap = NULL;
1044 mutex_lock(&active_config_lock);
1045 nodemap = nodemap_lookup(name);
1046 mutex_unlock(&active_config_lock);
1047 if (IS_ERR(nodemap))
1048 GOTO(out, rc = PTR_ERR(nodemap));
1050 nodemap->nm_squash_gid = gid;
1051 rc = nodemap_idx_nodemap_update(nodemap);
1053 nm_member_revoke_locks(nodemap);
1054 nodemap_putref(nodemap);
1058 EXPORT_SYMBOL(nodemap_set_squash_gid);
1061 * Returns true if this nodemap has root user access. Always returns true if
1062 * nodemaps are not active.
1064 * \param nodemap nodemap to check access for
1066 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
1068 return !nodemap_active || (nodemap && nodemap->nmf_allow_root_access);
1070 EXPORT_SYMBOL(nodemap_can_setquota);
1075 * \param name name of nodemap
1077 * \retval -EINVAL invalid nodemap name
1078 * \retval -EEXIST nodemap already exists
1079 * \retval -ENOMEM cannot allocate memory for nodemap
1081 int nodemap_add(const char *nodemap_name)
1083 struct lu_nodemap *nodemap;
1086 mutex_lock(&active_config_lock);
1087 nodemap = nodemap_create(nodemap_name, active_config, 0);
1088 if (IS_ERR(nodemap)) {
1089 mutex_unlock(&active_config_lock);
1090 return PTR_ERR(nodemap);
1093 rc = nodemap_idx_nodemap_add(nodemap);
1095 rc = lprocfs_nodemap_register(nodemap, 0);
1097 mutex_unlock(&active_config_lock);
1098 nodemap_putref(nodemap);
1102 EXPORT_SYMBOL(nodemap_add);
1107 * \param name name of nodemmap
1109 * \retval -EINVAL invalid input
1110 * \retval -ENOENT no existing nodemap
1112 int nodemap_del(const char *nodemap_name)
1114 struct lu_nodemap *nodemap;
1115 struct lu_nid_range *range;
1116 struct lu_nid_range *range_temp;
1120 if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1123 mutex_lock(&active_config_lock);
1124 nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1126 if (nodemap == NULL) {
1127 mutex_unlock(&active_config_lock);
1128 GOTO(out, rc = -ENOENT);
1131 /* erase nodemap from active ranges to prevent client assignment */
1132 down_write(&active_config->nmc_range_tree_lock);
1133 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1135 rc2 = nodemap_idx_range_del(range);
1139 range_delete(&active_config->nmc_range_tree, range);
1141 up_write(&active_config->nmc_range_tree_lock);
1143 rc2 = nodemap_idx_nodemap_del(nodemap);
1148 * remove procfs here in case nodemap_create called with same name
1149 * before nodemap_destroy is run.
1151 lprocfs_nodemap_remove(nodemap->nm_pde_data);
1152 nodemap->nm_pde_data = NULL;
1154 /* reclassify all member exports from nodemap, so they put their refs */
1155 down_read(&active_config->nmc_range_tree_lock);
1156 nm_member_reclassify_nodemap(nodemap);
1157 up_read(&active_config->nmc_range_tree_lock);
1159 if (!list_empty(&nodemap->nm_member_list))
1160 CWARN("nodemap_del failed to reclassify all members\n");
1162 mutex_unlock(&active_config_lock);
1164 nodemap_putref(nodemap);
1169 EXPORT_SYMBOL(nodemap_del);
1172 * activate nodemap functions
1174 * \param value 1 for on, 0 for off
1176 void nodemap_activate(const bool value)
1178 mutex_lock(&active_config_lock);
1179 active_config->nmc_nodemap_is_active = value;
1181 /* copy active value to global to avoid locking in map functions */
1182 nodemap_active = value;
1183 nodemap_idx_nodemap_activate(value);
1184 mutex_unlock(&active_config_lock);
1185 nm_member_revoke_all();
1187 EXPORT_SYMBOL(nodemap_activate);
1190 * Helper iterator to convert nodemap hash to list.
1192 * \param hs hash structure
1193 * \param bd bucket descriptor
1194 * \param hnode hash node
1195 * \param nodemap_list_head list head for list of nodemaps in hash
1197 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1198 struct hlist_node *hnode,
1199 void *nodemap_list_head)
1201 struct lu_nodemap *nodemap;
1203 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1204 list_add(&nodemap->nm_list, nodemap_list_head);
1206 cfs_hash_bd_del_locked(hs, bd, hnode);
1211 struct nodemap_config *nodemap_config_alloc(void)
1213 struct nodemap_config *config;
1216 OBD_ALLOC_PTR(config);
1218 return ERR_PTR(-ENOMEM);
1220 rc = nodemap_init_hash(config);
1222 OBD_FREE_PTR(config);
1226 init_rwsem(&config->nmc_range_tree_lock);
1230 EXPORT_SYMBOL(nodemap_config_alloc);
1233 * Walk the nodemap_hash and remove all nodemaps.
1235 void nodemap_config_dealloc(struct nodemap_config *config)
1237 struct lu_nodemap *nodemap = NULL;
1238 struct lu_nodemap *nodemap_temp;
1239 struct lu_nid_range *range;
1240 struct lu_nid_range *range_temp;
1241 LIST_HEAD(nodemap_list_head);
1243 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1244 nodemap_cleanup_iter_cb, &nodemap_list_head);
1245 cfs_hash_putref(config->nmc_nodemap_hash);
1247 /* Because nodemap_destroy might sleep, we can't destroy them
1248 * in cfs_hash_for_each, so we build a list there and destroy here
1250 list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1252 down_write(&config->nmc_range_tree_lock);
1254 /* move members to new config */
1255 nm_member_reclassify_nodemap(nodemap);
1256 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1258 range_delete(&config->nmc_range_tree, range);
1259 up_write(&config->nmc_range_tree_lock);
1261 nodemap_putref(nodemap);
1263 OBD_FREE_PTR(config);
1265 EXPORT_SYMBOL(nodemap_config_dealloc);
1267 static int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1268 struct hlist_node *hnode,
1269 void *nodemap_list_head)
1271 struct lu_nodemap *nodemap;
1273 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1274 list_add(&nodemap->nm_list, nodemap_list_head);
1278 void nodemap_config_set_active(struct nodemap_config *config)
1280 struct nodemap_config *old_config = active_config;
1281 struct lu_nodemap *nodemap;
1282 struct lu_nodemap *tmp;
1283 LIST_HEAD(nodemap_list_head);
1287 LASSERT(active_config != config);
1289 mutex_lock(&active_config_lock);
1291 /* move proc entries from already existing nms, create for new nms */
1292 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1293 nm_hash_list_cb, &nodemap_list_head);
1294 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1295 struct lu_nodemap *old_nm = NULL;
1297 if (active_config != NULL)
1298 old_nm = cfs_hash_lookup(
1299 active_config->nmc_nodemap_hash,
1301 if (old_nm != NULL) {
1302 nodemap->nm_pde_data = old_nm->nm_pde_data;
1303 old_nm->nm_pde_data = NULL;
1304 nodemap_putref(old_nm);
1306 bool is_def = (nodemap == config->nmc_default_nodemap);
1308 lprocfs_nodemap_register(nodemap, is_def);
1312 /* if new config is inactive, deactivate live config before switching */
1313 if (!config->nmc_nodemap_is_active)
1314 nodemap_active = false;
1315 active_config = config;
1316 if (config->nmc_nodemap_is_active)
1317 nodemap_active = true;
1319 mutex_unlock(&active_config_lock);
1321 if (old_config != NULL)
1322 nodemap_config_dealloc(old_config);
1324 nm_member_revoke_all();
1328 EXPORT_SYMBOL(nodemap_config_set_active);
1331 * Cleanup nodemap module on exit
1333 void nodemap_mod_exit(void)
1335 nodemap_config_dealloc(active_config);
1336 nodemap_procfs_exit();
1340 * Initialize the nodemap module
1342 int nodemap_mod_init(void)
1344 struct nodemap_config *new_config;
1345 struct lu_nodemap *nodemap;
1348 rc = nodemap_procfs_init();
1352 new_config = nodemap_config_alloc();
1353 if (IS_ERR(new_config)) {
1354 nodemap_procfs_exit();
1355 GOTO(out, rc = PTR_ERR(new_config));
1358 nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1359 if (IS_ERR(nodemap)) {
1360 nodemap_config_dealloc(new_config);
1361 nodemap_procfs_exit();
1362 GOTO(out, rc = PTR_ERR(nodemap));
1365 nodemap_config_set_active(new_config);
1366 nodemap_putref(nodemap);
1373 * Revoke locks for all nodemaps.
1375 void nm_member_revoke_all(void)
1377 struct lu_nodemap *nodemap;
1378 struct lu_nodemap *tmp;
1379 LIST_HEAD(nodemap_list_head);
1381 mutex_lock(&active_config_lock);
1382 cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1383 nm_hash_list_cb, &nodemap_list_head);
1385 /* revoke_locks sleeps, so can't call in cfs hash cb */
1386 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1387 nm_member_revoke_locks(nodemap);
1388 mutex_unlock(&active_config_lock);
1392 * Returns the nodemap classification for a given nid into an ioctl buffer.
1393 * Useful for testing the nodemap configuration to make sure it is working as
1396 * \param nid nid to classify
1397 * \param[out] name_buf buffer to write the nodemap name to
1398 * \param name_len length of buffer
1400 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1402 struct lu_nodemap *nodemap;
1404 mutex_lock(&active_config_lock);
1405 down_read(&active_config->nmc_range_tree_lock);
1406 nodemap = nodemap_classify_nid(nid);
1407 up_read(&active_config->nmc_range_tree_lock);
1408 mutex_unlock(&active_config_lock);
1410 strncpy(name_buf, nodemap->nm_name, name_len);
1412 name_buf[name_len - 1] = '\0';
1414 nodemap_putref(nodemap);
1416 EXPORT_SYMBOL(nodemap_test_nid);
1419 * Returns the id mapping for a given nid/id pair. Useful for testing the
1420 * nodemap configuration to make sure it is working as expected.
1422 * \param nid nid to classify
1423 * \param idtype uid or gid
1424 * \param client_id id to map to fs
1426 * \retval the mapped fs_id of the given client_id
1428 __u32 nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1431 struct lu_nodemap *nodemap;
1434 mutex_lock(&active_config_lock);
1435 down_read(&active_config->nmc_range_tree_lock);
1436 nodemap = nodemap_classify_nid(nid);
1437 up_read(&active_config->nmc_range_tree_lock);
1438 mutex_unlock(&active_config_lock);
1440 fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1442 nodemap_putref(nodemap);
1446 EXPORT_SYMBOL(nodemap_test_id);