X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Fptlrpc%2Fnodemap_handler.c;h=a6da068c01259e69aa3319d92be05bdeea2f64ce;hb=refs%2Fchanges%2F27%2F54227%2F13;hp=82a16b6118bf9d0e34de2bd58beb379aac97636d;hpb=2b294992edce5af7b79d4300ed3aa1ea6a8db850;p=fs%2Flustre-release.git diff --git a/lustre/ptlrpc/nodemap_handler.c b/lustre/ptlrpc/nodemap_handler.c index 82a16b6..a6da068 100644 --- a/lustre/ptlrpc/nodemap_handler.c +++ b/lustre/ptlrpc/nodemap_handler.c @@ -22,18 +22,19 @@ /* * Copyright (C) 2013, Trustees of Indiana University * - * Copyright (c) 2014, Intel Corporation. + * Copyright (c) 2017, Intel Corporation. * * Author: Joshua Walgenbach */ #include #include -#include +#include #include #include -#include #include + #include "nodemap_internal.h" +#include "ptlrpc_internal.h" #define HASH_NODEMAP_BKT_BITS 3 #define HASH_NODEMAP_CUR_BITS 3 @@ -44,29 +45,15 @@ /* nodemap proc root proc directory under fs/lustre */ struct proc_dir_entry *proc_lustre_nodemap_root; -/* Highest numerical lu_nodemap.nm_id defined */ -static atomic_t nodemap_highest_id; - -/* Simple flag to determine if nodemaps are active */ +/* Copy of config active flag to avoid locking in mapping functions */ bool nodemap_active; -/** - * pointer to default nodemap kept to keep from - * lookup it up in the hash since it is needed - * more often - */ -static struct lu_nodemap *default_nodemap; - -/** - * Lock required to access the range tree. - */ -rwlock_t nm_range_tree_lock; - -/** - * Hash keyed on nodemap name containing all - * nodemaps +/* Lock protecting the active config, useful primarily when proc and + * nodemap_hash might be replaced when loading a new config + * Any time the active config is referenced, the lock should be held. */ -static cfs_hash_t *nodemap_hash; +DEFINE_MUTEX(active_config_lock); +struct nodemap_config *active_config; /** * Nodemap destructor @@ -75,49 +62,67 @@ static cfs_hash_t *nodemap_hash; */ static void nodemap_destroy(struct lu_nodemap *nodemap) { - struct lu_nid_range *range; - struct lu_nid_range *range_temp; + ENTRY; - write_lock(&nm_range_tree_lock); - list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges, - rn_list) { - range_delete(range); - } - write_unlock(&nm_range_tree_lock); + if (nodemap->nm_pde_data != NULL) + lprocfs_nodemap_remove(nodemap->nm_pde_data); - write_lock(&nodemap->nm_idmap_lock); - idmap_delete_tree(nodemap); - write_unlock(&nodemap->nm_idmap_lock); + mutex_lock(&active_config_lock); + down_read(&active_config->nmc_range_tree_lock); nm_member_reclassify_nodemap(nodemap); - if (!cfs_hash_is_empty(nodemap->nm_member_hash)) + up_read(&active_config->nmc_range_tree_lock); + + down_write(&nodemap->nm_idmap_lock); + idmap_delete_tree(nodemap); + up_write(&nodemap->nm_idmap_lock); + + mutex_unlock(&active_config_lock); + + if (!list_empty(&nodemap->nm_member_list)) CWARN("nodemap_destroy failed to reclassify all members\n"); - nm_member_delete_hash(nodemap); + nm_member_delete_list(nodemap); OBD_FREE_PTR(nodemap); + + EXIT; } /** * Functions used for the cfs_hash */ -static void nodemap_getref(struct lu_nodemap *nodemap) +void nodemap_getref(struct lu_nodemap *nodemap) { atomic_inc(&nodemap->nm_refcount); + CDEBUG(D_INFO, "GETting nodemap %s(p=%p) : new refcount %d\n", + nodemap->nm_name, nodemap, atomic_read(&nodemap->nm_refcount)); } +/** + * Destroy nodemap if last reference is put. Should be called outside + * active_config_lock + */ void nodemap_putref(struct lu_nodemap *nodemap) { - LASSERT(nodemap != NULL); + if (!nodemap) + return; + LASSERT(atomic_read(&nodemap->nm_refcount) > 0); + CDEBUG(D_INFO, "PUTting nodemap %s(p=%p) : new refcount %d\n", + nodemap->nm_name, nodemap, + atomic_read(&nodemap->nm_refcount) - 1); + if (atomic_dec_and_test(&nodemap->nm_refcount)) nodemap_destroy(nodemap); } +EXPORT_SYMBOL(nodemap_putref); -static __u32 nodemap_hashfn(cfs_hash_t *hash_body, - const void *key, unsigned mask) +static unsigned int +nodemap_hashfn(struct cfs_hash *hash_body, + const void *key, const unsigned int bits) { - return cfs_hash_djb2_hash(key, strlen(key), mask); + return cfs_hash_djb2_hash(key, strlen(key), bits); } static void *nodemap_hs_key(struct hlist_node *hnode) @@ -144,7 +149,7 @@ static void *nodemap_hs_hashobject(struct hlist_node *hnode) return hlist_entry(hnode, struct lu_nodemap, nm_hash); } -static void nodemap_hs_get(cfs_hash_t *hs, struct hlist_node *hnode) +static void nodemap_hs_get(struct cfs_hash *hs, struct hlist_node *hnode) { struct lu_nodemap *nodemap; @@ -152,7 +157,7 @@ static void nodemap_hs_get(cfs_hash_t *hs, struct hlist_node *hnode) nodemap_getref(nodemap); } -static void nodemap_hs_put_locked(cfs_hash_t *hs, +static void nodemap_hs_put_locked(struct cfs_hash *hs, struct hlist_node *hnode) { struct lu_nodemap *nodemap; @@ -161,7 +166,7 @@ static void nodemap_hs_put_locked(cfs_hash_t *hs, nodemap_putref(nodemap); } -static cfs_hash_ops_t nodemap_hash_operations = { +static struct cfs_hash_ops nodemap_hash_operations = { .hs_hash = nodemap_hashfn, .hs_key = nodemap_hs_key, .hs_keycmp = nodemap_hs_keycmp, @@ -173,50 +178,23 @@ static cfs_hash_ops_t nodemap_hash_operations = { /* end of cfs_hash functions */ /** - * Helper iterator to clean up nodemap on module exit. - * - * \param hs hash structure - * \param bd bucket descriptor - * \param hnode hash node - * \param data not used here - */ -static int nodemap_cleanup_iter_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd, - struct hlist_node *hnode, void *data) -{ - struct lu_nodemap *nodemap; - - nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash); - nodemap_putref(nodemap); - - return 0; -} - -/** - * Walk the nodemap_hash and remove all nodemaps. - */ -void nodemap_cleanup_all(void) -{ - cfs_hash_for_each_safe(nodemap_hash, nodemap_cleanup_iter_cb, NULL); - cfs_hash_putref(nodemap_hash); -} - -/** * Initialize nodemap_hash * * \retval 0 success * \retval -ENOMEM cannot create hash */ -static int nodemap_init_hash(void) +static int nodemap_init_hash(struct nodemap_config *nmc) { - nodemap_hash = cfs_hash_create("NODEMAP", HASH_NODEMAP_CUR_BITS, - HASH_NODEMAP_MAX_BITS, - HASH_NODEMAP_BKT_BITS, 0, - CFS_HASH_MIN_THETA, - CFS_HASH_MAX_THETA, - &nodemap_hash_operations, - CFS_HASH_DEFAULT); - - if (nodemap_hash == NULL) { + nmc->nmc_nodemap_hash = cfs_hash_create("NODEMAP", + HASH_NODEMAP_CUR_BITS, + HASH_NODEMAP_MAX_BITS, + HASH_NODEMAP_BKT_BITS, 0, + CFS_HASH_MIN_THETA, + CFS_HASH_MAX_THETA, + &nodemap_hash_operations, + CFS_HASH_DEFAULT); + + if (nmc->nmc_nodemap_hash == NULL) { CERROR("cannot create nodemap_hash table\n"); return -ENOMEM; } @@ -248,48 +226,70 @@ static bool nodemap_name_is_valid(const char *name) /** * Nodemap lookup * - * Look nodemap up in the nodemap hash + * Look nodemap up in the active_config nodemap hash. Caller should hold the + * active_config_lock. * * \param name name of nodemap - * \param nodemap found nodemap or NULL - * \retval lu_nodemap named nodemap - * \retval NULL nodemap doesn't exist + * \retval nodemap pointer set to found nodemap + * \retval -EINVAL name is not valid + * \retval -ENOENT nodemap not found */ -static int nodemap_lookup(const char *name, struct lu_nodemap **nodemap) +struct lu_nodemap *nodemap_lookup(const char *name) { - int rc = 0; - - *nodemap = NULL; + struct lu_nodemap *nodemap = NULL; if (!nodemap_name_is_valid(name)) - GOTO(out, rc = -EINVAL); + return ERR_PTR(-EINVAL); - *nodemap = cfs_hash_lookup(nodemap_hash, name); - if (*nodemap == NULL) - rc = -ENOENT; + nodemap = cfs_hash_lookup(active_config->nmc_nodemap_hash, name); + if (nodemap == NULL) + return ERR_PTR(-ENOENT); -out: - return rc; + return nodemap; } /** - * classify the nid into the proper nodemap + * Classify the nid into the proper nodemap. Caller must hold active config and + * nm_range_tree_lock, and call nodemap_putref when done with nodemap. * * \param nid nid to classify * \retval nodemap nodemap containing the nid * \retval default_nodemap default nodemap + * \retval -EINVAL LO nid given without other local nid */ -struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid) +struct lu_nodemap *nodemap_classify_nid(struct lnet_nid *nid) { - struct lu_nid_range *range; + struct lu_nid_range *range; + struct lu_nodemap *nodemap; + int rc; + + ENTRY; + /* don't use 0@lo, use the first non-lo local NID instead */ + if (nid_is_lo0(nid)) { + struct lnet_processid id; + int i = 0; + + do { + rc = LNetGetId(i++, &id, true); + if (rc < 0) + RETURN(ERR_PTR(-EINVAL)); + } while (nid_is_lo0(&id.nid)); + + nid = &id.nid; + CDEBUG(D_INFO, "found nid %s\n", libcfs_nidstr(nid)); + } - range = range_search(nid); + range = range_search(active_config, nid); if (range != NULL) - return range->rn_nodemap; + nodemap = range->rn_nodemap; + else + nodemap = active_config->nmc_default_nodemap; + + LASSERT(nodemap != NULL); + nodemap_getref(nodemap); - return default_nodemap; + RETURN(nodemap); } -EXPORT_SYMBOL(nodemap_classify_nid); /** * simple check for default nodemap @@ -306,7 +306,8 @@ static bool is_default_nodemap(const struct lu_nodemap *nodemap) * \param range[2] array of two nids * \reyval 0 on success */ -int nodemap_parse_range(const char *range_str, lnet_nid_t range[2]) +int nodemap_parse_range(const char *range_str, struct lnet_nid range[2], + u8 *netmask) { char buf[LNET_NIDSTR_SIZE * 2 + 2]; char *ptr = NULL; @@ -316,15 +317,32 @@ int nodemap_parse_range(const char *range_str, lnet_nid_t range[2]) snprintf(buf, sizeof(buf), "%s", range_str); ptr = buf; - start_nidstr = strsep(&ptr, ":"); - end_nidstr = strsep(&ptr, ":"); + + /* For large NID we use netmasks. Currently we only + * support /128 which is a single NID. + */ + if (strchr(ptr, '/')) { + start_nidstr = strsep(&ptr, "/"); + + rc = kstrtou8(ptr, 10, netmask); + if (rc < 0) + GOTO(out, rc); + if (*netmask != 128) + GOTO(out, rc = -ERANGE); + end_nidstr = start_nidstr; + } else { + start_nidstr = strsep(&ptr, ":"); + end_nidstr = strsep(&ptr, ":"); + } if (start_nidstr == NULL || end_nidstr == NULL) GOTO(out, rc = -EINVAL); - range[0] = libcfs_str2nid(start_nidstr); - range[1] = libcfs_str2nid(end_nidstr); + rc = libcfs_strnid(&range[0], start_nidstr); + if (rc < 0) + GOTO(out, rc); + rc = libcfs_strnid(&range[1], end_nidstr); out: return rc; @@ -376,19 +394,34 @@ EXPORT_SYMBOL(nodemap_parse_idmap); * \param nid nid to add to the members * \param exp obd_export structure for the connection * that is being added - * \retval -EINVAL export is NULL + * \retval -EINVAL export is NULL, or has invalid NID * \retval -EEXIST export is already member of a nodemap */ -int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp) +int nodemap_add_member(struct lnet_nid *nid, struct obd_export *exp) { - struct lu_nodemap *nodemap; - int rc; + struct lu_nodemap *nodemap; + int rc = 0; + + ENTRY; + mutex_lock(&active_config_lock); + down_read(&active_config->nmc_range_tree_lock); - read_lock(&nm_range_tree_lock); nodemap = nodemap_classify_nid(nid); - rc = nm_member_add(nodemap, exp); - read_unlock(&nm_range_tree_lock); - return rc; + if (IS_ERR(nodemap)) { + CWARN("%s: error adding to nodemap, no valid NIDs found\n", + exp->exp_obd->obd_name); + rc = -EINVAL; + } else { + rc = nm_member_add(nodemap, exp); + } + + up_read(&active_config->nmc_range_tree_lock); + mutex_unlock(&active_config_lock); + + if (!IS_ERR(nodemap)) + nodemap_putref(nodemap); + + RETURN(rc); } EXPORT_SYMBOL(nodemap_add_member); @@ -399,49 +432,124 @@ EXPORT_SYMBOL(nodemap_add_member); */ void nodemap_del_member(struct obd_export *exp) { - struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap; + struct lu_nodemap *nodemap; + + ENTRY; + + /* using ac lock to prevent nodemap reclassification while deleting */ + mutex_lock(&active_config_lock); + + /* use of ted_nodemap is protected by active_config_lock. we take an + * extra reference to make sure nodemap isn't destroyed under + * active_config_lock + */ + nodemap = exp->exp_target_data.ted_nodemap; + if (nodemap == NULL) + goto out; + else + nodemap_getref(nodemap); + + mutex_lock(&nodemap->nm_member_list_lock); + nm_member_del(nodemap, exp); + mutex_unlock(&nodemap->nm_member_list_lock); + +out: + mutex_unlock(&active_config_lock); + + if (nodemap) + nodemap_putref(nodemap); - if (nodemap != NULL) - nm_member_del(nodemap, exp); + EXIT; } EXPORT_SYMBOL(nodemap_del_member); /** * add an idmap to the proper nodemap trees * - * \param name name of nodemap + * \param nodemap nodemap to add idmap to * \param id_type NODEMAP_UID or NODEMAP_GID * \param map array[2] __u32 containing the map values * map[0] is client id * map[1] is the filesystem id * - * \retval 0 on success + * \retval 0 on success + * \retval < 0 if error occurs */ -int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type, - const __u32 map[2]) +int nodemap_add_idmap_helper(struct lu_nodemap *nodemap, + enum nodemap_id_type id_type, + const __u32 map[2]) { - struct lu_nodemap *nodemap = NULL; struct lu_idmap *idmap; + struct lu_idmap *temp; int rc = 0; - rc = nodemap_lookup(name, &nodemap); - if (nodemap == NULL || is_default_nodemap(nodemap)) - GOTO(out, rc = -EINVAL); - idmap = idmap_create(map[0], map[1]); if (idmap == NULL) - GOTO(out_putref, rc = -ENOMEM); + GOTO(out, rc = -ENOMEM); - write_lock(&nodemap->nm_idmap_lock); - idmap_insert(id_type, idmap, nodemap); - write_unlock(&nodemap->nm_idmap_lock); + down_write(&nodemap->nm_idmap_lock); + temp = idmap_insert(id_type, idmap, nodemap); + /* If the new id_client or id_fs is matched, the old idmap and its + * index should be deleted according to its id_client before the new + * idmap is added again. + */ + if (IS_ERR(temp)) + GOTO(out_insert, rc = PTR_ERR(temp)); + if (temp) { + __u32 del_map[2]; + + del_map[0] = temp->id_client; + idmap_delete(id_type, temp, nodemap); + rc = nodemap_idx_idmap_del(nodemap, id_type, del_map); + /* In case there is any corrupted idmap */ + if (!rc || unlikely(rc == -ENOENT)) { + temp = idmap_insert(id_type, idmap, nodemap); + if (IS_ERR(temp)) + rc = PTR_ERR(temp); + else if (!temp) + rc = 0; + else + rc = -EPERM; + } + } +out_insert: + if (rc) + OBD_FREE_PTR(idmap); + up_write(&nodemap->nm_idmap_lock); nm_member_revoke_locks(nodemap); -out_putref: - nodemap_putref(nodemap); out: return rc; } + +int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type, + const __u32 map[2]) +{ + struct lu_nodemap *nodemap = NULL; + int rc; + + ENTRY; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + if (IS_ERR(nodemap)) { + mutex_unlock(&active_config_lock); + GOTO(out, rc = PTR_ERR(nodemap)); + } + + if (is_default_nodemap(nodemap)) { + rc = -EINVAL; + } else { + rc = nodemap_add_idmap_helper(nodemap, id_type, map); + if (rc == 0) + rc = nodemap_idx_idmap_add(nodemap, id_type, map); + } + mutex_unlock(&active_config_lock); + nodemap_putref(nodemap); + +out: + RETURN(rc); +} EXPORT_SYMBOL(nodemap_add_idmap); /** @@ -462,34 +570,89 @@ int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type, struct lu_idmap *idmap = NULL; int rc = 0; - rc = nodemap_lookup(name, &nodemap); - if (nodemap == NULL || is_default_nodemap(nodemap)) - GOTO(out, rc = -EINVAL); + ENTRY; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + if (IS_ERR(nodemap)) { + mutex_unlock(&active_config_lock); + GOTO(out, rc = PTR_ERR(nodemap)); + } + + if (is_default_nodemap(nodemap)) + GOTO(out_putref, rc = -EINVAL); - write_lock(&nodemap->nm_idmap_lock); + down_write(&nodemap->nm_idmap_lock); idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type, map[0]); if (idmap == NULL) { - write_unlock(&nodemap->nm_idmap_lock); - GOTO(out_putref, rc = -EINVAL); + rc = -EINVAL; + } else { + idmap_delete(id_type, idmap, nodemap); + rc = nodemap_idx_idmap_del(nodemap, id_type, map); } - - idmap_delete(id_type, idmap, nodemap); - write_unlock(&nodemap->nm_idmap_lock); - nm_member_revoke_locks(nodemap); + up_write(&nodemap->nm_idmap_lock); out_putref: + mutex_unlock(&active_config_lock); + if (rc == 0) + nm_member_revoke_locks(nodemap); nodemap_putref(nodemap); + out: - return rc; + RETURN(rc); } EXPORT_SYMBOL(nodemap_del_idmap); /** + * Get nodemap assigned to given export. Takes a reference on the nodemap. + * + * Note that this function may return either NULL, or an ERR_PTR() + * or a valid nodemap pointer. All of the functions accessing the + * returned nodemap can check IS_ERR(nodemap) to see if an error is + * returned. NULL is not considered an error, which is OK since this + * is a valid case if nodemap are not in use. All nodemap handling + * functions must check for nodemap == NULL and do nothing, and the + * nodemap returned from this function should not be dereferenced. + * + * \param export export to get nodemap for + * + * \retval pointer to nodemap on success + * \retval NULL nodemap subsystem disabled + * \retval -EACCES export does not have nodemap assigned + */ +struct lu_nodemap *nodemap_get_from_exp(struct obd_export *exp) +{ + struct lu_nodemap *nodemap; + + ENTRY; + + if (!nodemap_active) + RETURN(NULL); + + spin_lock(&exp->exp_target_data.ted_nodemap_lock); + nodemap = exp->exp_target_data.ted_nodemap; + if (nodemap) + nodemap_getref(nodemap); + spin_unlock(&exp->exp_target_data.ted_nodemap_lock); + + if (!nodemap) { + CDEBUG(D_INFO, "%s: nodemap null on export %s (at %s)\n", + exp->exp_obd->obd_name, + obd_uuid2str(&exp->exp_client_uuid), + obd_export_nid2str(exp)); + RETURN(ERR_PTR(-EACCES)); + } + + RETURN(nodemap); +} +EXPORT_SYMBOL(nodemap_get_from_exp); + +/** * mapping function for nodemap idmaps * * \param nodemap lu_nodemap structure defining nodemap - * \param node_type NODEMAP_UID or NODEMAP_GID + * \param node_type NODEMAP_UID or NODEMAP_GID or NODEMAP_PROJID * \param tree_type NODEMAP_CLIENT_TO_FS or * NODEMAP_FS_TO_CLIENT * \param id id to map @@ -499,10 +662,11 @@ EXPORT_SYMBOL(nodemap_del_idmap); * if the nodemap_active is false, just return the passed id without mapping * * if the id to be looked up is 0, check that root access is allowed and if it - * is, return 0. Otherwise, return the squash uid or gid. + * is, return 0. Otherwise, return the mapped uid or gid if any. + * Otherwise, return the squash uid or gid. * * if the nodemap is configured to trusted the ids from the client system, just - * return the passwd id without mapping. + * return the passed id without mapping. * * if by this point, we haven't returned and the nodemap in question is the * default nodemap, return the squash uid or gid. @@ -517,6 +681,8 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap, struct lu_idmap *idmap = NULL; __u32 found_id; + ENTRY; + if (!nodemap_active) goto out; @@ -526,20 +692,32 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap, if (id == 0) { if (nodemap->nmf_allow_root_access) goto out; - else - goto squash; + goto map; } + if (id_type == NODEMAP_UID && + !(nodemap->nmf_map_mode & NODEMAP_MAP_UID)) + goto out; + + if (id_type == NODEMAP_GID && + !(nodemap->nmf_map_mode & NODEMAP_MAP_GID)) + goto out; + + if (id_type == NODEMAP_PROJID && + !(nodemap->nmf_map_mode & NODEMAP_MAP_PROJID)) + goto out; + if (nodemap->nmf_trust_client_ids) goto out; +map: if (is_default_nodemap(nodemap)) goto squash; - read_lock(&nodemap->nm_idmap_lock); + down_read(&nodemap->nm_idmap_lock); idmap = idmap_search(nodemap, tree_type, id_type, id); if (idmap == NULL) { - read_unlock(&nodemap->nm_idmap_lock); + up_read(&nodemap->nm_idmap_lock); goto squash; } @@ -547,16 +725,18 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap, found_id = idmap->id_client; else found_id = idmap->id_fs; - read_unlock(&nodemap->nm_idmap_lock); - return found_id; + up_read(&nodemap->nm_idmap_lock); + RETURN(found_id); squash: if (id_type == NODEMAP_UID) - return nodemap->nm_squash_uid; - else - return nodemap->nm_squash_gid; + RETURN(nodemap->nm_squash_uid); + if (id_type == NODEMAP_GID) + RETURN(nodemap->nm_squash_gid); + if (id_type == NODEMAP_PROJID) + RETURN(nodemap->nm_squash_projid); out: - return id; + RETURN(id); } EXPORT_SYMBOL(nodemap_map_id); @@ -575,22 +755,25 @@ ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size, enum nodemap_tree_type tree_type) { posix_acl_xattr_header *header = buf; - posix_acl_xattr_entry *entry = &header->a_entries[0]; + posix_acl_xattr_entry *entry = GET_POSIX_ACL_XATTR_ENTRY(header); posix_acl_xattr_entry *new_entry = entry; posix_acl_xattr_entry *end; int count; + ENTRY; + if (!nodemap_active) - return size; + RETURN(size); if (unlikely(nodemap == NULL)) - return size; + RETURN(size); count = posix_acl_xattr_count(size); if (count < 0) - return -EINVAL; + RETURN(-EINVAL); if (count == 0) - return 0; + /* if not proper ACL, do nothing and return initial size */ + RETURN(size); for (end = entry + count; entry != end; entry++) { __u16 tag = le16_to_cpu(entry->e_tag); @@ -620,52 +803,88 @@ ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size, new_entry++; } - return (void *)new_entry - (void *)header; + RETURN((void *)new_entry - (void *)header); } EXPORT_SYMBOL(nodemap_map_acl); /* - * add nid range to nodemap - * \param name nodemap name - * \param range_st string containing nid range - * \retval 0 on success + * Add nid range to given nodemap + * + * \param config nodemap config to work on + * \param nodemap nodemap to add range to + * \param nid nid range to add + * \param range_id should be 0 unless loading from disk + * \retval 0 success + * \retval -ENOMEM * - * add an range to the global range tree and attached the - * range to the named nodemap. */ -int nodemap_add_range(const char *name, const lnet_nid_t nid[2]) +int nodemap_add_range_helper(struct nodemap_config *config, + struct lu_nodemap *nodemap, + const struct lnet_nid nid[2], + u8 netmask, unsigned int range_id) { - struct lu_nodemap *nodemap = NULL; - struct lu_nid_range *range; + struct lu_nid_range *range; int rc; - rc = nodemap_lookup(name, &nodemap); - if (nodemap == NULL || is_default_nodemap(nodemap)) - GOTO(out, rc = -EINVAL); - - range = range_create(nid[0], nid[1], nodemap); - if (range == NULL) - GOTO(out_putref, rc = -ENOMEM); + down_write(&config->nmc_range_tree_lock); + range = range_create(config, &nid[0], &nid[1], netmask, nodemap, + range_id); + if (range == NULL) { + up_write(&config->nmc_range_tree_lock); + GOTO(out, rc = -ENOMEM); + } - write_lock(&nm_range_tree_lock); - rc = range_insert(range); - if (rc != 0) { - CERROR("cannot insert nodemap range into '%s': rc = %d\n", - nodemap->nm_name, rc); - write_unlock(&nm_range_tree_lock); + rc = range_insert(config, range); + if (rc) { + CDEBUG_LIMIT(rc == -EEXIST ? D_INFO : D_ERROR, + "cannot insert nodemap range into '%s': rc = %d\n", + nodemap->nm_name, rc); + up_write(&config->nmc_range_tree_lock); list_del(&range->rn_list); range_destroy(range); - GOTO(out_putref, rc = -ENOMEM); + GOTO(out, rc); } list_add(&range->rn_list, &nodemap->nm_ranges); - write_unlock(&nm_range_tree_lock); - nm_member_reclassify_nodemap(default_nodemap); - nm_member_revoke_locks(default_nodemap); - nm_member_revoke_locks(nodemap); + /* nodemaps have no members if they aren't on the active config */ + if (config == active_config) + nm_member_reclassify_nodemap(config->nmc_default_nodemap); -out_putref: + up_write(&config->nmc_range_tree_lock); + + /* if range_id is non-zero, we are loading from disk */ + if (range_id == 0) + rc = nodemap_idx_range_add(range); + + if (config == active_config) { + nm_member_revoke_locks(config->nmc_default_nodemap); + nm_member_revoke_locks(nodemap); + } + +out: + return rc; +} + +int nodemap_add_range(const char *name, const struct lnet_nid nid[2], + u8 netmask) +{ + struct lu_nodemap *nodemap = NULL; + int rc; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + if (IS_ERR(nodemap)) { + mutex_unlock(&active_config_lock); + GOTO(out, rc = PTR_ERR(nodemap)); + } + + if (is_default_nodemap(nodemap)) + rc = -EINVAL; + else + rc = nodemap_add_range_helper(active_config, nodemap, nid, + netmask, 0); + mutex_unlock(&active_config_lock); nodemap_putref(nodemap); out: return rc; @@ -675,36 +894,49 @@ EXPORT_SYMBOL(nodemap_add_range); /** * delete a range * \param name nodemap name - * \param range_str string containing range + * \param nid nid range * \retval 0 on success * * Delete range from global range tree, and remove it * from the list in the associated nodemap. */ -int nodemap_del_range(const char *name, const lnet_nid_t nid[2]) +int nodemap_del_range(const char *name, const struct lnet_nid nid[2], + u8 netmask) { struct lu_nodemap *nodemap; struct lu_nid_range *range; int rc = 0; - rc = nodemap_lookup(name, &nodemap); - if (nodemap == NULL || is_default_nodemap(nodemap)) - GOTO(out, rc = -EINVAL); + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + if (IS_ERR(nodemap)) { + mutex_unlock(&active_config_lock); + GOTO(out, rc = PTR_ERR(nodemap)); + } + + if (is_default_nodemap(nodemap)) + GOTO(out_putref, rc = -EINVAL); - write_lock(&nm_range_tree_lock); - range = range_find(nid[0], nid[1]); + down_write(&active_config->nmc_range_tree_lock); + range = range_find(active_config, &nid[0], &nid[1], netmask); if (range == NULL) { - write_unlock(&nm_range_tree_lock); + up_write(&active_config->nmc_range_tree_lock); GOTO(out_putref, rc = -EINVAL); } - - range_delete(range); - write_unlock(&nm_range_tree_lock); + if (range->rn_nodemap != nodemap) { + up_write(&active_config->nmc_range_tree_lock); + GOTO(out_putref, rc = -EINVAL); + } + rc = nodemap_idx_range_del(range); + range_delete(active_config, range); nm_member_reclassify_nodemap(nodemap); - nm_member_revoke_locks(default_nodemap); + up_write(&active_config->nmc_range_tree_lock); + + nm_member_revoke_locks(active_config->nmc_default_nodemap); nm_member_revoke_locks(nodemap); out_putref: + mutex_unlock(&active_config_lock); nodemap_putref(nodemap); out: return rc; @@ -712,121 +944,356 @@ out: EXPORT_SYMBOL(nodemap_del_range); /** - * Nodemap constructor - * - * Creates an lu_nodemap structure and assigns sane default - * member values. If this is the default nodemap, the defaults - * are the most restictive in xterms of mapping behavior. Otherwise - * the default flags should be inherited from the default nodemap. - * The adds nodemap to nodemap_hash. + * set fileset on nodemap + * \param name nodemap to set fileset on + * \param fileset string containing fileset + * \retval 0 on success * - * \param name name of nodemap - * \param is_default true if default nodemap - * \retval 0 success - * \retval -EINVAL invalid nodemap name - * \retval -EEXIST nodemap already exists - * \retval -ENOMEM cannot allocate memory for nodemap + * set a fileset on the named nodemap */ -static int nodemap_create(const char *name, bool is_default) +static int nodemap_set_fileset_helper(struct nodemap_config *config, + struct lu_nodemap *nodemap, + const char *fileset) { - struct lu_nodemap *nodemap = NULL; - int rc = 0; + int rc = 0; - if (!nodemap_name_is_valid(name)) - GOTO(out, rc = -EINVAL); + /* Allow 'fileset=clear' in addition to 'fileset=""' to clear fileset + * because either command 'lctl set_param -P *.*.fileset=""' or + * 'lctl nodemap_set_fileset --fileset ""' can only work correctly + * on MGS, while on other servers, both commands will invoke upcall + * "/usr/sbin/lctl set_param nodemap.default.fileset=" by function + * process_param2_config(), which will cause "no value" error and + * won't clear fileset. + * 'fileset=""' is still kept for compatibility reason. + */ + if (fileset == NULL) + rc = -EINVAL; + else if (fileset[0] == '\0' || strcmp(fileset, "clear") == 0) + nodemap->nm_fileset[0] = '\0'; + else if (fileset[0] != '/') + rc = -EINVAL; + else if (strscpy(nodemap->nm_fileset, fileset, + sizeof(nodemap->nm_fileset)) < 0) + rc = -ENAMETOOLONG; - OBD_ALLOC_PTR(nodemap); - if (nodemap == NULL) { - CERROR("cannot allocate memory (%zu bytes)" - "for nodemap '%s'\n", sizeof(*nodemap), - name); - GOTO(out, rc = -ENOMEM); - } + return rc; +} - /* - * take an extra reference to prevent nodemap from being destroyed - * while its being created. - */ - atomic_set(&nodemap->nm_refcount, 2); - snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name); - rc = cfs_hash_add_unique(nodemap_hash, name, &nodemap->nm_hash); - if (rc != 0) { - OBD_FREE_PTR(nodemap); - GOTO(out, rc = -EEXIST); +int nodemap_set_fileset(const char *name, const char *fileset) +{ + struct lu_nodemap *nodemap = NULL; + int rc = 0; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + if (IS_ERR(nodemap)) { + mutex_unlock(&active_config_lock); + GOTO(out, rc = PTR_ERR(nodemap)); } + rc = nodemap_set_fileset_helper(active_config, nodemap, fileset); + mutex_unlock(&active_config_lock); - rc = nm_member_init_hash(nodemap); - if (rc != 0) { - OBD_FREE_PTR(nodemap); - goto out; - } + nodemap_putref(nodemap); +out: + return rc; +} +EXPORT_SYMBOL(nodemap_set_fileset); - INIT_LIST_HEAD(&nodemap->nm_ranges); +/** + * get fileset defined on nodemap + * \param nodemap nodemap to get fileset from + * \retval fileset name, or NULL if not defined or not activated + * + * get the fileset defined on the nodemap + */ +char *nodemap_get_fileset(const struct lu_nodemap *nodemap) +{ + if (!nodemap_active) + return NULL; - rwlock_init(&nodemap->nm_idmap_lock); - nodemap->nm_fs_to_client_uidmap = RB_ROOT; - nodemap->nm_client_to_fs_uidmap = RB_ROOT; - nodemap->nm_fs_to_client_gidmap = RB_ROOT; - nodemap->nm_client_to_fs_gidmap = RB_ROOT; + return (char *)nodemap->nm_fileset; +} +EXPORT_SYMBOL(nodemap_get_fileset); - if (is_default) { - nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID; - nodemap->nmf_trust_client_ids = 0; - nodemap->nmf_allow_root_access = 0; - nodemap->nmf_block_lookups = 0; +static int nodemap_validate_sepol(const char *sepol) +{ + char buf[LUSTRE_NODEMAP_SEPOL_LENGTH + 1]; + char *p = (char *)sepol; + char *q = buf; + char polname[NAME_MAX + 1] = ""; + char hash[SELINUX_POLICY_HASH_LEN + 1] = ""; + unsigned char mode; + unsigned short ver; - nodemap->nm_squash_uid = NODEMAP_NOBODY_UID; - nodemap->nm_squash_gid = NODEMAP_NOBODY_GID; + BUILD_BUG_ON(sizeof(buf) != sizeof(((struct lu_nodemap *)0)->nm_sepol)); + + if (sepol == NULL) + return -EINVAL; + + /* we allow sepol = "" which means clear SELinux policy info */ + if (sepol[0] == '\0') + return 0; + + /* make a copy of sepol, by replacing ':' with space + * so that we can use sscanf over the string + */ + while (p-sepol < sizeof(buf)) { + if (*p == ':') + *q = ' '; + else + *q = *p; + if (*p == '\0') + break; + p++; + q++; + } + if (p-sepol == sizeof(buf)) + return -ENAMETOOLONG; + + if (sscanf(buf, "%1hhu %s %hu %s", &mode, polname, &ver, hash) != 4) + return -EINVAL; + + if (mode != 0 && mode != 1) + return -EINVAL; + + return 0; +} + +/** + * set SELinux policy on nodemap + * \param name nodemap to set SELinux policy info on + * \param sepol string containing SELinux policy info + * \retval 0 on success + * + * set SELinux policy info on the named nodemap + */ +int nodemap_set_sepol(const char *name, const char *sepol) +{ + struct lu_nodemap *nodemap = NULL; + int rc; + + rc = nodemap_validate_sepol(sepol); + if (rc < 0) + GOTO(out, rc); + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + if (IS_ERR(nodemap)) { + mutex_unlock(&active_config_lock); + GOTO(out, rc = PTR_ERR(nodemap)); + } + + if (is_default_nodemap(nodemap)) { + /* We do not want nodes in the default nodemap to have + * SELinux restrictions. Sec admin should create dedicated + * nodemap entries for this. + */ + GOTO(out_putref, rc = -EINVAL); + } + + /* truncation cannot happen, as string length was checked in + * nodemap_validate_sepol() + */ + strscpy(nodemap->nm_sepol, sepol, sizeof(nodemap->nm_sepol)); + +out_putref: + mutex_unlock(&active_config_lock); + nodemap_putref(nodemap); +out: + return rc; +} +EXPORT_SYMBOL(nodemap_set_sepol); + +/** + * get SELinux policy info defined on nodemap + * \param nodemap nodemap to get SELinux policy info from + * \retval SELinux policy info, or NULL if not defined or not activated + * + * get the SELinux policy info defined on the nodemap + */ +const char *nodemap_get_sepol(const struct lu_nodemap *nodemap) +{ + if (is_default_nodemap(nodemap)) + return NULL; + else + return (char *)nodemap->nm_sepol; +} +EXPORT_SYMBOL(nodemap_get_sepol); + +/** + * Nodemap constructor + * + * Creates an lu_nodemap structure and assigns sane default + * member values. If this is the default nodemap, the defaults + * are the most restrictive in terms of mapping behavior. Otherwise + * the default flags should be inherited from the default nodemap. + * The adds nodemap to nodemap_hash. + * + * Requires that the caller take the active_config_lock + * + * \param name name of nodemap + * \param is_default true if default nodemap + * \retval nodemap success + * \retval -EINVAL invalid nodemap name + * \retval -EEXIST nodemap already exists + * \retval -ENOMEM cannot allocate memory for nodemap + */ +struct lu_nodemap *nodemap_create(const char *name, + struct nodemap_config *config, + bool is_default) +{ + struct lu_nodemap *nodemap = NULL; + struct lu_nodemap *default_nodemap; + struct cfs_hash *hash = config->nmc_nodemap_hash; + int rc = 0; + ENTRY; + + default_nodemap = config->nmc_default_nodemap; + + if (!nodemap_name_is_valid(name)) + GOTO(out, rc = -EINVAL); + + if (hash == NULL) { + CERROR("Config nodemap hash is NULL, unable to add %s\n", name); + GOTO(out, rc = -EINVAL); + } + + OBD_ALLOC_PTR(nodemap); + if (nodemap == NULL) { + CERROR("cannot allocate memory (%zu bytes) for nodemap '%s'\n", + sizeof(*nodemap), name); + GOTO(out, rc = -ENOMEM); + } + + /* + * take an extra reference to prevent nodemap from being destroyed + * while it's being created. + */ + atomic_set(&nodemap->nm_refcount, 2); + snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name); + rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash); + if (rc != 0) { + OBD_FREE_PTR(nodemap); + GOTO(out, rc = -EEXIST); + } + + INIT_LIST_HEAD(&nodemap->nm_ranges); + INIT_LIST_HEAD(&nodemap->nm_list); + INIT_LIST_HEAD(&nodemap->nm_member_list); + + mutex_init(&nodemap->nm_member_list_lock); + init_rwsem(&nodemap->nm_idmap_lock); + nodemap->nm_fs_to_client_uidmap = RB_ROOT; + nodemap->nm_client_to_fs_uidmap = RB_ROOT; + nodemap->nm_fs_to_client_gidmap = RB_ROOT; + nodemap->nm_client_to_fs_gidmap = RB_ROOT; + nodemap->nm_fs_to_client_projidmap = RB_ROOT; + nodemap->nm_client_to_fs_projidmap = RB_ROOT; + + if (is_default) { + nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID; + config->nmc_default_nodemap = nodemap; + } else { + config->nmc_nodemap_highest_id++; + nodemap->nm_id = config->nmc_nodemap_highest_id; + } - lprocfs_nodemap_register(name, is_default, nodemap); + if (is_default || default_nodemap == NULL) { + nodemap->nmf_trust_client_ids = 0; + nodemap->nmf_allow_root_access = 0; + nodemap->nmf_deny_unknown = 0; + nodemap->nmf_map_mode = NODEMAP_MAP_ALL; + nodemap->nmf_enable_audit = 1; + nodemap->nmf_forbid_encryption = 0; + nodemap->nmf_readonly_mount = 0; + nodemap->nmf_rbac = NODEMAP_RBAC_ALL; - default_nodemap = nodemap; + nodemap->nm_squash_uid = NODEMAP_NOBODY_UID; + nodemap->nm_squash_gid = NODEMAP_NOBODY_GID; + nodemap->nm_squash_projid = NODEMAP_NOBODY_PROJID; + nodemap->nm_fileset[0] = '\0'; + nodemap->nm_sepol[0] = '\0'; + if (!is_default) + CWARN("adding nodemap '%s' to config without" + " default nodemap\n", nodemap->nm_name); } else { - nodemap->nm_id = atomic_inc_return(&nodemap_highest_id); nodemap->nmf_trust_client_ids = default_nodemap->nmf_trust_client_ids; nodemap->nmf_allow_root_access = default_nodemap->nmf_allow_root_access; - nodemap->nmf_block_lookups = - default_nodemap->nmf_block_lookups; + nodemap->nmf_deny_unknown = default_nodemap->nmf_deny_unknown; + nodemap->nmf_map_mode = default_nodemap->nmf_map_mode; + nodemap->nmf_enable_audit = default_nodemap->nmf_enable_audit; + nodemap->nmf_forbid_encryption = + default_nodemap->nmf_forbid_encryption; + nodemap->nmf_readonly_mount = + default_nodemap->nmf_readonly_mount; + nodemap->nmf_rbac = default_nodemap->nmf_rbac; nodemap->nm_squash_uid = default_nodemap->nm_squash_uid; nodemap->nm_squash_gid = default_nodemap->nm_squash_gid; - - lprocfs_nodemap_register(name, is_default, nodemap); + nodemap->nm_squash_projid = default_nodemap->nm_squash_projid; + nodemap->nm_fileset[0] = '\0'; + nodemap->nm_sepol[0] = '\0'; } - if (rc == 0) { - nodemap_putref(nodemap); - goto out; - } + RETURN(nodemap); +out: CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc); - nodemap_destroy(nodemap); + RETURN(ERR_PTR(rc)); +} + +/** + * Set the nmf_deny_unknown flag to true or false. + * \param name nodemap name + * \param deny_unknown if true, squashed users will get EACCES + * \retval 0 on success + * + */ +int nodemap_set_deny_unknown(const char *name, bool deny_unknown) +{ + struct lu_nodemap *nodemap = NULL; + int rc = 0; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); + nodemap->nmf_deny_unknown = deny_unknown; + rc = nodemap_idx_nodemap_update(nodemap); + + nm_member_revoke_locks(nodemap); + nodemap_putref(nodemap); out: return rc; } +EXPORT_SYMBOL(nodemap_set_deny_unknown); /** - * update flag to turn on or off nodemap functions + * Set the nmf_allow_root_access flag to true or false. * \param name nodemap name - * \param admin_string string containing updated value + * \param allow_root if true, nodemap will not squash the root user * \retval 0 on success * - * Update admin flag to turn on or off nodemap functions. */ int nodemap_set_allow_root(const char *name, bool allow_root) { struct lu_nodemap *nodemap = NULL; int rc = 0; - rc = nodemap_lookup(name, &nodemap); - if (nodemap == NULL) - GOTO(out, rc = -ENOENT); + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); nodemap->nmf_allow_root_access = allow_root; + rc = nodemap_idx_nodemap_update(nodemap); + nm_member_revoke_locks(nodemap); nodemap_putref(nodemap); out: @@ -835,24 +1302,27 @@ out: EXPORT_SYMBOL(nodemap_set_allow_root); /** - * updated trust_client_ids flag for nodemap + * Set the nmf_trust_client_ids flag to true or false. * - * \param name nodemap name - * \param trust_string new value for trust flag + * \param name nodemap name + * \param trust_client_ids if true, nodemap will not map its IDs * \retval 0 on success * - * Update the trust_client_ids flag for a nodemap. */ int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids) { struct lu_nodemap *nodemap = NULL; int rc = 0; - rc = nodemap_lookup(name, &nodemap); - if (nodemap == NULL) - GOTO(out, rc = -ENOENT); + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); nodemap->nmf_trust_client_ids = trust_client_ids; + rc = nodemap_idx_nodemap_update(nodemap); + nm_member_revoke_locks(nodemap); nodemap_putref(nodemap); out: @@ -860,11 +1330,76 @@ out: } EXPORT_SYMBOL(nodemap_set_trust_client_ids); +int nodemap_set_mapping_mode(const char *name, + enum nodemap_mapping_modes map_mode) +{ + struct lu_nodemap *nodemap = NULL; + int rc = 0; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); + + nodemap->nmf_map_mode = map_mode; + rc = nodemap_idx_nodemap_update(nodemap); + + nm_member_revoke_locks(nodemap); + nodemap_putref(nodemap); +out: + return rc; +} +EXPORT_SYMBOL(nodemap_set_mapping_mode); + +int nodemap_set_rbac(const char *name, enum nodemap_rbac_roles rbac) +{ + struct lu_nodemap *nodemap = NULL; + enum nodemap_rbac_roles old_rbac; + int rc = 0; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); + + if (is_default_nodemap(nodemap)) + GOTO(put, rc = -EINVAL); + + old_rbac = nodemap->nmf_rbac; + /* if value does not change, do nothing */ + if (rbac == old_rbac) + GOTO(put, rc = 0); + + nodemap->nmf_rbac = rbac; + if (rbac == NODEMAP_RBAC_ALL) + /* if new value is ALL (default), just delete + * NODEMAP_CLUSTER_ROLES idx + */ + rc = nodemap_idx_cluster_roles_del(nodemap); + else if (old_rbac == NODEMAP_RBAC_ALL) + /* if old value is ALL (default), need to insert + * NODEMAP_CLUSTER_ROLES idx + */ + rc = nodemap_idx_cluster_roles_add(nodemap); + else + /* otherwise just update existing NODEMAP_CLUSTER_ROLES idx */ + rc = nodemap_idx_cluster_roles_update(nodemap); + + nm_member_revoke_locks(nodemap); +put: + nodemap_putref(nodemap); +out: + return rc; +} +EXPORT_SYMBOL(nodemap_set_rbac); + /** - * update the squash_uid for a nodemap + * Update the squash_uid for a nodemap. * * \param name nodemap name - * \param uid_string string containing new squash_uid value + * \param uid the new uid to squash unknown users to * \retval 0 on success * * Update the squash_uid for a nodemap. The squash_uid is the uid @@ -877,11 +1412,15 @@ int nodemap_set_squash_uid(const char *name, uid_t uid) struct lu_nodemap *nodemap = NULL; int rc = 0; - rc = nodemap_lookup(name, &nodemap); - if (nodemap == NULL) - GOTO(out, rc = -ENOENT); + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); nodemap->nm_squash_uid = uid; + rc = nodemap_idx_nodemap_update(nodemap); + nm_member_revoke_locks(nodemap); nodemap_putref(nodemap); out: @@ -893,10 +1432,10 @@ EXPORT_SYMBOL(nodemap_set_squash_uid); * Update the squash_gid for a nodemap. * * \param name nodemap name - * \param gid_string string containing new squash_gid value + * \param gid the new gid to squash unknown gids to * \retval 0 on success * - * Update the squash_gid for a nodemap. The squash_uid is the gid + * Update the squash_gid for a nodemap. The squash_gid is the gid * that the all client gids are mapped to if nodemap is active, * the trust_client_ids flag is not set, and the gid is not in * the idmap tree. @@ -906,11 +1445,15 @@ int nodemap_set_squash_gid(const char *name, gid_t gid) struct lu_nodemap *nodemap = NULL; int rc = 0; - rc = nodemap_lookup(name, &nodemap); - if (nodemap == NULL) - GOTO(out, rc = -ENOENT); + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); nodemap->nm_squash_gid = gid; + rc = nodemap_idx_nodemap_update(nodemap); + nm_member_revoke_locks(nodemap); nodemap_putref(nodemap); out: @@ -919,18 +1462,158 @@ out: EXPORT_SYMBOL(nodemap_set_squash_gid); /** - * Returns true if this nodemap has root user access. Always returns true if - * nodemaps are not active. + * Update the squash_projid for a nodemap. + * + * \param name nodemap name + * \param gid the new projid to squash unknown projids to + * \retval 0 on success + * + * Update the squash_projid for a nodemap. The squash_projid is the projid + * that the all client projids are mapped to if nodemap is active, + * the trust_client_ids flag is not set, and the projid is not in + * the idmap tree. + */ +int nodemap_set_squash_projid(const char *name, projid_t projid) +{ + struct lu_nodemap *nodemap = NULL; + int rc = 0; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); + + nodemap->nm_squash_projid = projid; + rc = nodemap_idx_nodemap_update(nodemap); + + nm_member_revoke_locks(nodemap); + nodemap_putref(nodemap); +out: + return rc; +} +EXPORT_SYMBOL(nodemap_set_squash_projid); + +/** + * Check if nodemap allows setting quota. + * + * If nodemap is not active, always allow. + * For user and group quota, allow if the nodemap allows root access. + * For project quota, allow if project id is not squashed or deny_unknown + * is not set. * * \param nodemap nodemap to check access for + * \param qc_type quota type + * \param id client id to map + * \retval true is setquota is allowed, false otherwise */ -bool nodemap_can_setquota(const struct lu_nodemap *nodemap) +bool nodemap_can_setquota(struct lu_nodemap *nodemap, __u32 qc_type, __u32 id) { - return !nodemap_active || nodemap->nmf_allow_root_access; + if (!nodemap_active) + return true; + + if (!nodemap || !nodemap->nmf_allow_root_access || + !(nodemap->nmf_rbac & NODEMAP_RBAC_QUOTA_OPS)) + return false; + + if (qc_type == PRJQUOTA) { + id = nodemap_map_id(nodemap, NODEMAP_PROJID, + NODEMAP_CLIENT_TO_FS, id); + + if (id == nodemap->nm_squash_projid && + nodemap->nmf_deny_unknown) + return false; + } + + return true; } EXPORT_SYMBOL(nodemap_can_setquota); /** + * Set the nmf_enable_audit flag to true or false. + * \param name nodemap name + * \param audit_mode if true, allow audit + * \retval 0 on success + * + */ +int nodemap_set_audit_mode(const char *name, bool enable_audit) +{ + struct lu_nodemap *nodemap = NULL; + int rc = 0; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); + + nodemap->nmf_enable_audit = enable_audit; + rc = nodemap_idx_nodemap_update(nodemap); + + nm_member_revoke_locks(nodemap); + nodemap_putref(nodemap); +out: + return rc; +} +EXPORT_SYMBOL(nodemap_set_audit_mode); + +/** + * Set the nmf_forbid_encryption flag to true or false. + * \param name nodemap name + * \param forbid_encryption if true, forbid encryption + * \retval 0 on success + * + */ +int nodemap_set_forbid_encryption(const char *name, bool forbid_encryption) +{ + struct lu_nodemap *nodemap = NULL; + int rc = 0; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); + + nodemap->nmf_forbid_encryption = forbid_encryption; + rc = nodemap_idx_nodemap_update(nodemap); + + nm_member_revoke_locks(nodemap); + nodemap_putref(nodemap); +out: + return rc; +} +EXPORT_SYMBOL(nodemap_set_forbid_encryption); + +/** + * Set the nmf_readonly_mount flag to true or false. + * \param name nodemap name + * \param readonly_mount if true, forbid rw mount + * \retval 0 on success + * + */ +int nodemap_set_readonly_mount(const char *name, bool readonly_mount) +{ + struct lu_nodemap *nodemap = NULL; + int rc = 0; + + mutex_lock(&active_config_lock); + nodemap = nodemap_lookup(name); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + GOTO(out, rc = PTR_ERR(nodemap)); + + nodemap->nmf_readonly_mount = readonly_mount; + rc = nodemap_idx_nodemap_update(nodemap); + + nm_member_revoke_locks(nodemap); + nodemap_putref(nodemap); +out: + return rc; +} +EXPORT_SYMBOL(nodemap_set_readonly_mount); + +/** * Add a nodemap * * \param name name of nodemap @@ -941,7 +1624,24 @@ EXPORT_SYMBOL(nodemap_can_setquota); */ int nodemap_add(const char *nodemap_name) { - return nodemap_create(nodemap_name, 0); + struct lu_nodemap *nodemap; + int rc; + + mutex_lock(&active_config_lock); + nodemap = nodemap_create(nodemap_name, active_config, 0); + if (IS_ERR(nodemap)) { + mutex_unlock(&active_config_lock); + return PTR_ERR(nodemap); + } + + rc = nodemap_idx_nodemap_add(nodemap); + if (rc == 0) + rc = lprocfs_nodemap_register(nodemap, 0); + + mutex_unlock(&active_config_lock); + nodemap_putref(nodemap); + + return rc; } EXPORT_SYMBOL(nodemap_add); @@ -955,22 +1655,58 @@ EXPORT_SYMBOL(nodemap_add); */ int nodemap_del(const char *nodemap_name) { - struct lu_nodemap *nodemap; - int rc = 0; + struct lu_nodemap *nodemap; + struct lu_nid_range *range; + struct lu_nid_range *range_temp; + int rc = 0; + int rc2 = 0; if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0) - GOTO(out, rc = -EINVAL); + RETURN(-EINVAL); - nodemap = cfs_hash_del_key(nodemap_hash, nodemap_name); - if (nodemap == NULL) + mutex_lock(&active_config_lock); + nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash, + nodemap_name); + if (nodemap == NULL) { + mutex_unlock(&active_config_lock); GOTO(out, rc = -ENOENT); + } + + /* erase nodemap from active ranges to prevent client assignment */ + down_write(&active_config->nmc_range_tree_lock); + list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges, + rn_list) { + rc2 = nodemap_idx_range_del(range); + if (rc2 < 0) + rc = rc2; + + range_delete(active_config, range); + } + up_write(&active_config->nmc_range_tree_lock); + + rc2 = nodemap_idx_nodemap_del(nodemap); + if (rc2 < 0) + rc = rc2; /* * remove procfs here in case nodemap_create called with same name - * before nodemap_destory is run. + * before nodemap_destroy is run. */ - lprocfs_remove(&nodemap->nm_proc_entry); + lprocfs_nodemap_remove(nodemap->nm_pde_data); + nodemap->nm_pde_data = NULL; + + /* reclassify all member exports from nodemap, so they put their refs */ + down_read(&active_config->nmc_range_tree_lock); + nm_member_reclassify_nodemap(nodemap); + up_read(&active_config->nmc_range_tree_lock); + + if (!list_empty(&nodemap->nm_member_list)) + CWARN("nodemap_del failed to reclassify all members\n"); + + mutex_unlock(&active_config_lock); + nodemap_putref(nodemap); + out: return rc; } @@ -983,18 +1719,185 @@ EXPORT_SYMBOL(nodemap_del); */ void nodemap_activate(const bool value) { + mutex_lock(&active_config_lock); + active_config->nmc_nodemap_is_active = value; + + /* copy active value to global to avoid locking in map functions */ nodemap_active = value; + nodemap_idx_nodemap_activate(value); + mutex_unlock(&active_config_lock); nm_member_revoke_all(); } EXPORT_SYMBOL(nodemap_activate); /** + * Helper iterator to convert nodemap hash to list. + * + * \param hs hash structure + * \param bd bucket descriptor + * \param hnode hash node + * \param nodemap_list_head list head for list of nodemaps in hash + */ +static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd, + struct hlist_node *hnode, + void *nodemap_list_head) +{ + struct lu_nodemap *nodemap; + + nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash); + list_add(&nodemap->nm_list, nodemap_list_head); + + cfs_hash_bd_del_locked(hs, bd, hnode); + + return 0; +} + +struct nodemap_config *nodemap_config_alloc(void) +{ + struct nodemap_config *config; + int rc = 0; + + OBD_ALLOC_PTR(config); + if (config == NULL) + return ERR_PTR(-ENOMEM); + + rc = nodemap_init_hash(config); + if (rc != 0) { + OBD_FREE_PTR(config); + return ERR_PTR(rc); + } + + init_rwsem(&config->nmc_range_tree_lock); + + INIT_LIST_HEAD(&config->nmc_netmask_setup); + config->nmc_range_tree.nmrt_range_interval_root = INTERVAL_TREE_ROOT; + + return config; +} +EXPORT_SYMBOL(nodemap_config_alloc); + +/** + * Walk the nodemap_hash and remove all nodemaps. + */ +void nodemap_config_dealloc(struct nodemap_config *config) +{ + struct lu_nodemap *nodemap = NULL; + struct lu_nodemap *nodemap_temp; + struct lu_nid_range *range; + struct lu_nid_range *range_temp; + LIST_HEAD(nodemap_list_head); + + cfs_hash_for_each_safe(config->nmc_nodemap_hash, + nodemap_cleanup_iter_cb, &nodemap_list_head); + cfs_hash_putref(config->nmc_nodemap_hash); + + /* Because nodemap_destroy might sleep, we can't destroy them + * in cfs_hash_for_each, so we build a list there and destroy here + */ + list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head, + nm_list) { + mutex_lock(&active_config_lock); + down_write(&config->nmc_range_tree_lock); + + /* move members to new config, requires ac lock */ + nm_member_reclassify_nodemap(nodemap); + list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges, + rn_list) + range_delete(config, range); + up_write(&config->nmc_range_tree_lock); + mutex_unlock(&active_config_lock); + + /* putref must be outside of ac lock if nm could be destroyed */ + nodemap_putref(nodemap); + } + OBD_FREE_PTR(config); +} +EXPORT_SYMBOL(nodemap_config_dealloc); + +/* + * callback for cfs_hash_for_each_safe used to convert a nodemap hash to a + * nodemap list, generally for locking purposes as a hash cb can't sleep. + */ +int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd, + struct hlist_node *hnode, + void *nodemap_list_head) +{ + struct lu_nodemap *nodemap; + + nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash); + list_add(&nodemap->nm_list, nodemap_list_head); + return 0; +} + +void nodemap_config_set_active(struct nodemap_config *config) +{ + struct nodemap_config *old_config = active_config; + struct lu_nodemap *nodemap; + struct lu_nodemap *tmp; + bool revoke_locks; + LIST_HEAD(nodemap_list_head); + + ENTRY; + + LASSERT(active_config != config); + LASSERT(config->nmc_default_nodemap); + + mutex_lock(&active_config_lock); + + /* move proc entries from already existing nms, create for new nms */ + cfs_hash_for_each_safe(config->nmc_nodemap_hash, + nm_hash_list_cb, &nodemap_list_head); + list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) { + struct lu_nodemap *old_nm = NULL; + + if (active_config != NULL) + old_nm = cfs_hash_lookup( + active_config->nmc_nodemap_hash, + nodemap->nm_name); + if (old_nm != NULL) { + nodemap->nm_pde_data = old_nm->nm_pde_data; + old_nm->nm_pde_data = NULL; + nodemap_putref(old_nm); + } else { + bool is_def = (nodemap == config->nmc_default_nodemap); + + lprocfs_nodemap_register(nodemap, is_def); + } + } + + /* + * We only need to revoke locks if old nodemap was active, and new + * config is now nodemap inactive. nodemap_config_dealloc will + * reclassify exports, triggering a lock revoke if and only if new + * nodemap is active. + */ + revoke_locks = !config->nmc_nodemap_is_active && nodemap_active; + + /* if new config is inactive, deactivate live config before switching */ + if (!config->nmc_nodemap_is_active) + nodemap_active = false; + active_config = config; + if (config->nmc_nodemap_is_active) + nodemap_active = true; + + mutex_unlock(&active_config_lock); + + if (old_config != NULL) + nodemap_config_dealloc(old_config); + + if (revoke_locks) + nm_member_revoke_all(); + + EXIT; +} + +/** * Cleanup nodemap module on exit */ void nodemap_mod_exit(void) { - nodemap_cleanup_all(); - lprocfs_remove(&proc_lustre_nodemap_root); + nodemap_config_dealloc(active_config); + nodemap_procfs_exit(); } /** @@ -1002,38 +1905,113 @@ void nodemap_mod_exit(void) */ int nodemap_mod_init(void) { + struct nodemap_config *new_config; + struct lu_nodemap *nodemap; int rc = 0; - rc = nodemap_init_hash(); + rc = nodemap_procfs_init(); if (rc != 0) - goto cleanup; + return rc; - rwlock_init(&nm_range_tree_lock); - nodemap_procfs_init(); - rc = nodemap_create(DEFAULT_NODEMAP, 1); + new_config = nodemap_config_alloc(); + if (IS_ERR(new_config)) { + nodemap_procfs_exit(); + GOTO(out, rc = PTR_ERR(new_config)); + } -cleanup: - if (rc != 0) - nodemap_mod_exit(); + nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1); + if (IS_ERR(nodemap)) { + nodemap_config_dealloc(new_config); + nodemap_procfs_exit(); + GOTO(out, rc = PTR_ERR(nodemap)); + } + nodemap_config_set_active(new_config); + nodemap_putref(nodemap); + +out: return rc; } -static int nm_member_revoke_all_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd, - struct hlist_node *hnode, void *data) +/** + * Revoke locks for all nodemaps. + */ +void nm_member_revoke_all(void) { struct lu_nodemap *nodemap; + struct lu_nodemap *tmp; + LIST_HEAD(nodemap_list_head); - nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash); - nm_member_revoke_locks(nodemap); - return 0; + mutex_lock(&active_config_lock); + cfs_hash_for_each_safe(active_config->nmc_nodemap_hash, + nm_hash_list_cb, &nodemap_list_head); + + /* revoke_locks sleeps, so can't call in cfs hash cb */ + list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) + nm_member_revoke_locks_always(nodemap); + mutex_unlock(&active_config_lock); } /** - * Revoke locks for all nodemaps. + * Returns the nodemap classification for a given nid into an ioctl buffer. + * Useful for testing the nodemap configuration to make sure it is working as + * expected. + * + * \param nid nid to classify + * \param[out] name_buf buffer to write the nodemap name to + * \param name_len length of buffer */ -void nm_member_revoke_all() +void nodemap_test_nid(struct lnet_nid *nid, char *name_buf, size_t name_len) { - cfs_hash_for_each_safe(nodemap_hash, nm_member_revoke_all_cb, NULL); + struct lu_nodemap *nodemap; + + mutex_lock(&active_config_lock); + down_read(&active_config->nmc_range_tree_lock); + nodemap = nodemap_classify_nid(nid); + up_read(&active_config->nmc_range_tree_lock); + mutex_unlock(&active_config_lock); + + if (IS_ERR(nodemap)) + return; + + strncpy(name_buf, nodemap->nm_name, name_len); + if (name_len > 0) + name_buf[name_len - 1] = '\0'; + + nodemap_putref(nodemap); } +EXPORT_SYMBOL(nodemap_test_nid); + +/** + * Passes back the id mapping for a given nid/id pair. Useful for testing the + * nodemap configuration to make sure it is working as expected. + * + * \param nid nid to classify + * \param idtype uid or gid + * \param client_id id to map to fs + * \param fs_id_buf pointer to save mapped fs_id to + * + * \retval 0 success + * \retval -EINVAL invalid NID + */ +int nodemap_test_id(struct lnet_nid *nid, enum nodemap_id_type idtype, + u32 client_id, u32 *fs_id) +{ + struct lu_nodemap *nodemap; + + mutex_lock(&active_config_lock); + down_read(&active_config->nmc_range_tree_lock); + nodemap = nodemap_classify_nid(nid); + up_read(&active_config->nmc_range_tree_lock); + mutex_unlock(&active_config_lock); + if (IS_ERR(nodemap)) + return PTR_ERR(nodemap); + + *fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS, + client_id); + nodemap_putref(nodemap); + + return 0; +} +EXPORT_SYMBOL(nodemap_test_id);