X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Fptlrpc%2Fnodemap_handler.c;h=576779e1b9bd8f995839bc83a4a112f2dae699c3;hb=640ed6104453e912a5c7766d265a36a30a31761d;hp=e80247a965850cc375cbc0b57047fcb4ee55afc9;hpb=b18132d1201b48805503d23c4f9f403e769b27a9;p=fs%2Flustre-release.git diff --git a/lustre/ptlrpc/nodemap_handler.c b/lustre/ptlrpc/nodemap_handler.c index e80247a..576779e 100644 --- a/lustre/ptlrpc/nodemap_handler.c +++ b/lustre/ptlrpc/nodemap_handler.c @@ -22,13 +22,13 @@ /* * 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 @@ -71,9 +71,9 @@ static void nodemap_destroy(struct lu_nodemap *nodemap) nm_member_reclassify_nodemap(nodemap); up_read(&active_config->nmc_range_tree_lock); - write_lock(&nodemap->nm_idmap_lock); + down_write(&nodemap->nm_idmap_lock); idmap_delete_tree(nodemap); - write_unlock(&nodemap->nm_idmap_lock); + up_write(&nodemap->nm_idmap_lock); mutex_unlock(&active_config_lock); @@ -265,7 +265,7 @@ struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid) /* don't use 0@lo, use the first non-lo local NID instead */ if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND) { - lnet_process_id_t id; + struct lnet_process_id id; int i = 0; do { @@ -448,28 +448,56 @@ 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_helper(struct lu_nodemap *nodemap, enum nodemap_id_type id_type, const __u32 map[2]) { struct lu_idmap *idmap; + struct lu_idmap *temp; int rc = 0; idmap = idmap_create(map[0], map[1]); if (idmap == NULL) 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: @@ -482,6 +510,8 @@ int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type, struct lu_nodemap *nodemap = NULL; int rc; + ENTRY; + mutex_lock(&active_config_lock); nodemap = nodemap_lookup(name); if (IS_ERR(nodemap)) { @@ -500,7 +530,7 @@ int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type, nodemap_putref(nodemap); out: - return rc; + RETURN(rc); } EXPORT_SYMBOL(nodemap_add_idmap); @@ -522,6 +552,8 @@ int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type, struct lu_idmap *idmap = NULL; int rc = 0; + ENTRY; + mutex_lock(&active_config_lock); nodemap = nodemap_lookup(name); if (IS_ERR(nodemap)) { @@ -532,7 +564,7 @@ int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type, 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) { @@ -541,7 +573,7 @@ int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type, idmap_delete(id_type, idmap, nodemap); rc = nodemap_idx_idmap_del(nodemap, id_type, map); } - write_unlock(&nodemap->nm_idmap_lock); + up_write(&nodemap->nm_idmap_lock); out_putref: mutex_unlock(&active_config_lock); @@ -550,7 +582,7 @@ out_putref: nodemap_putref(nodemap); out: - return rc; + RETURN(rc); } EXPORT_SYMBOL(nodemap_del_idmap); @@ -615,7 +647,7 @@ EXPORT_SYMBOL(nodemap_get_from_exp); * is, return 0. 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. @@ -638,6 +670,12 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap, if (unlikely(nodemap == NULL)) goto out; + if (nodemap->nmf_map_uid_only && id_type == NODEMAP_GID) + goto out; + + if (nodemap->nmf_map_gid_only && id_type == NODEMAP_UID) + goto out; + if (id == 0) { if (nodemap->nmf_allow_root_access) goto out; @@ -651,10 +689,10 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap, 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; } @@ -662,7 +700,7 @@ __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); + up_read(&nodemap->nm_idmap_lock); RETURN(found_id); squash: @@ -690,22 +728,24 @@ 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; + RETURN(0); for (end = entry + count; entry != end; entry++) { __u16 tag = le16_to_cpu(entry->e_tag); @@ -735,7 +775,7 @@ 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); @@ -777,15 +817,21 @@ int nodemap_add_range_helper(struct nodemap_config *config, } list_add(&range->rn_list, &nodemap->nm_ranges); - nm_member_reclassify_nodemap(config->nmc_default_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); + 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, nid); - nm_member_revoke_locks(config->nmc_default_nodemap); - nm_member_revoke_locks(nodemap); + if (config == active_config) { + nm_member_revoke_locks(config->nmc_default_nodemap); + nm_member_revoke_locks(nodemap); + } out: return rc; @@ -816,7 +862,7 @@ 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 @@ -844,6 +890,10 @@ int nodemap_del_range(const char *name, const lnet_nid_t nid[2]) up_write(&active_config->nmc_range_tree_lock); GOTO(out_putref, rc = -EINVAL); } + 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->nmc_range_tree, range); nm_member_reclassify_nodemap(nodemap); @@ -874,8 +924,20 @@ static int nodemap_set_fileset_helper(struct nodemap_config *config, { int rc = 0; - /* we allow fileset = "" which means clear fileset info */ - if (fileset == NULL || (fileset[0] != 0 && fileset[0] != '/')) + /* 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 (strlcpy(nodemap->nm_fileset, fileset, sizeof(nodemap->nm_fileset)) >= @@ -897,11 +959,7 @@ int nodemap_set_fileset(const char *name, const char *fileset) GOTO(out, rc = PTR_ERR(nodemap)); } - if (is_default_nodemap(nodemap)) - rc = -EINVAL; - else - rc = nodemap_set_fileset_helper(active_config, nodemap, - fileset); + rc = nodemap_set_fileset_helper(active_config, nodemap, fileset); mutex_unlock(&active_config_lock); nodemap_putref(nodemap); @@ -919,10 +977,10 @@ EXPORT_SYMBOL(nodemap_set_fileset); */ char *nodemap_get_fileset(const struct lu_nodemap *nodemap) { - if (!nodemap_active || is_default_nodemap(nodemap)) + if (!nodemap_active) return NULL; - else - return (char *)nodemap->nm_fileset; + + return (char *)nodemap->nm_fileset; } EXPORT_SYMBOL(nodemap_get_fileset); @@ -931,7 +989,7 @@ EXPORT_SYMBOL(nodemap_get_fileset); * * 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 + * 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. * @@ -952,6 +1010,7 @@ struct lu_nodemap *nodemap_create(const char *name, struct lu_nodemap *default_nodemap; struct cfs_hash *hash = config->nmc_nodemap_hash; int rc = 0; + ENTRY; default_nodemap = config->nmc_default_nodemap; @@ -988,7 +1047,7 @@ struct lu_nodemap *nodemap_create(const char *name, INIT_LIST_HEAD(&nodemap->nm_member_list); mutex_init(&nodemap->nm_member_list_lock); - rwlock_init(&nodemap->nm_idmap_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; @@ -1006,9 +1065,13 @@ struct lu_nodemap *nodemap_create(const char *name, nodemap->nmf_trust_client_ids = 0; nodemap->nmf_allow_root_access = 0; nodemap->nmf_deny_unknown = 0; + nodemap->nmf_map_uid_only = 0; + nodemap->nmf_map_gid_only = 0; + nodemap->nmf_enable_audit = 1; nodemap->nm_squash_uid = NODEMAP_NOBODY_UID; nodemap->nm_squash_gid = NODEMAP_NOBODY_GID; + nodemap->nm_fileset[0] = '\0'; if (!is_default) CWARN("adding nodemap '%s' to config without" " default nodemap\n", nodemap->nm_name); @@ -1019,17 +1082,23 @@ struct lu_nodemap *nodemap_create(const char *name, default_nodemap->nmf_allow_root_access; nodemap->nmf_deny_unknown = default_nodemap->nmf_deny_unknown; + nodemap->nmf_map_uid_only = + default_nodemap->nmf_map_uid_only; + nodemap->nmf_map_gid_only = + default_nodemap->nmf_map_gid_only; + nodemap->nmf_enable_audit = + default_nodemap->nmf_enable_audit; nodemap->nm_squash_uid = default_nodemap->nm_squash_uid; nodemap->nm_squash_gid = default_nodemap->nm_squash_gid; - nodemap->nm_fileset[0] = 0; + nodemap->nm_fileset[0] = '\0'; } - return nodemap; + RETURN(nodemap); out: CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc); - return ERR_PTR(rc); + RETURN(ERR_PTR(rc)); } /** @@ -1117,6 +1186,42 @@ out: } EXPORT_SYMBOL(nodemap_set_trust_client_ids); +int nodemap_set_mapping_mode(const char *name, enum nodemap_mapping_modes 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)); + + switch (mode) { + case NODEMAP_MAP_BOTH: + nodemap->nmf_map_uid_only = 0; + nodemap->nmf_map_gid_only = 0; + break; + case NODEMAP_MAP_UID_ONLY: + nodemap->nmf_map_uid_only = 1; + nodemap->nmf_map_gid_only = 0; + break; + case NODEMAP_MAP_GID_ONLY: + nodemap->nmf_map_uid_only = 0; + nodemap->nmf_map_gid_only = 1; + break; + default: + CWARN("cannot set unknown mapping mode, mode = %d\n", mode); + } + rc = nodemap_idx_nodemap_update(nodemap); + + nm_member_revoke_locks(nodemap); + nodemap_putref(nodemap); +out: + return rc; +} +EXPORT_SYMBOL(nodemap_set_mapping_mode); + /** * Update the squash_uid for a nodemap. * @@ -1196,6 +1301,35 @@ bool nodemap_can_setquota(const struct lu_nodemap *nodemap) 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); + + +/** * Add a nodemap * * \param name name of nodemap @@ -1375,24 +1509,31 @@ void nodemap_config_dealloc(struct nodemap_config *config) */ 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 */ + /* 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->nmc_range_tree, 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); -static int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd, - struct hlist_node *hnode, - void *nodemap_list_head) +/* + * 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; @@ -1406,6 +1547,7 @@ 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; @@ -1436,6 +1578,14 @@ void nodemap_config_set_active(struct nodemap_config *config) } } + /* + * 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; @@ -1448,7 +1598,8 @@ void nodemap_config_set_active(struct nodemap_config *config) if (old_config != NULL) nodemap_config_dealloc(old_config); - nm_member_revoke_all(); + if (revoke_locks) + nm_member_revoke_all(); EXIT; } @@ -1510,7 +1661,7 @@ void nm_member_revoke_all(void) /* 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(nodemap); + nm_member_revoke_locks_always(nodemap); mutex_unlock(&active_config_lock); }