Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * GPL HEADER START |
| 3 | * |
| 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | * |
| 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. |
| 9 | * |
| 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). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * version 2 along with this program; If not, see |
Oleg Drokin | 6a5b99a | 2016-06-14 23:33:40 -0400 | [diff] [blame] | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 19 | * |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 20 | * GPL HEADER END |
| 21 | */ |
| 22 | /* |
| 23 | * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
| 24 | * Use is subject to license terms. |
| 25 | * |
| 26 | * Copyright (c) 2011, 2012, Intel Corporation. |
| 27 | */ |
| 28 | /* |
| 29 | * This file is part of Lustre, http://www.lustre.org/ |
| 30 | * Lustre is a trademark of Sun Microsystems, Inc. |
| 31 | */ |
| 32 | |
| 33 | #define DEBUG_SUBSYSTEM S_RPC |
| 34 | |
Greg Kroah-Hartman | 610f737 | 2014-07-11 22:15:24 -0700 | [diff] [blame] | 35 | #include "../include/obd.h" |
| 36 | #include "../include/obd_support.h" |
| 37 | #include "../include/obd_class.h" |
| 38 | #include "../include/lustre_lib.h" |
| 39 | #include "../include/lustre_ha.h" |
| 40 | #include "../include/lustre_net.h" |
| 41 | #include "../include/lprocfs_status.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 42 | |
| 43 | #define NIDS_MAX 32 |
| 44 | |
| 45 | struct uuid_nid_data { |
| 46 | struct list_head un_list; |
| 47 | struct obd_uuid un_uuid; |
| 48 | int un_nid_count; |
| 49 | lnet_nid_t un_nids[NIDS_MAX]; |
| 50 | }; |
| 51 | |
| 52 | /* FIXME: This should probably become more elegant than a global linked list */ |
| 53 | static struct list_head g_uuid_list; |
| 54 | static spinlock_t g_uuid_lock; |
| 55 | |
| 56 | void class_init_uuidlist(void) |
| 57 | { |
| 58 | INIT_LIST_HEAD(&g_uuid_list); |
| 59 | spin_lock_init(&g_uuid_lock); |
| 60 | } |
| 61 | |
| 62 | void class_exit_uuidlist(void) |
| 63 | { |
| 64 | /* delete all */ |
| 65 | class_del_uuid(NULL); |
| 66 | } |
| 67 | |
| 68 | int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index) |
| 69 | { |
| 70 | struct uuid_nid_data *data; |
| 71 | struct obd_uuid tmp; |
| 72 | int rc = -ENOENT; |
| 73 | |
| 74 | obd_str2uuid(&tmp, uuid); |
| 75 | spin_lock(&g_uuid_lock); |
| 76 | list_for_each_entry(data, &g_uuid_list, un_list) { |
| 77 | if (obd_uuid_equals(&data->un_uuid, &tmp)) { |
| 78 | if (index >= data->un_nid_count) |
| 79 | break; |
| 80 | |
| 81 | rc = 0; |
| 82 | *peer_nid = data->un_nids[index]; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | spin_unlock(&g_uuid_lock); |
| 87 | return rc; |
| 88 | } |
| 89 | EXPORT_SYMBOL(lustre_uuid_to_peer); |
| 90 | |
| 91 | /* Add a nid to a niduuid. Multiple nids can be added to a single uuid; |
Oleg Drokin | 6ba5917 | 2016-02-24 22:00:35 -0500 | [diff] [blame] | 92 | * LNET will choose the best one. |
| 93 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 94 | int class_add_uuid(const char *uuid, __u64 nid) |
| 95 | { |
| 96 | struct uuid_nid_data *data, *entry; |
| 97 | int found = 0; |
| 98 | |
| 99 | LASSERT(nid != 0); /* valid newconfig NID is never zero */ |
| 100 | |
| 101 | if (strlen(uuid) > UUID_MAX - 1) |
| 102 | return -EOVERFLOW; |
| 103 | |
Julia Lawall | d727904 | 2015-05-01 17:51:15 +0200 | [diff] [blame] | 104 | data = kzalloc(sizeof(*data), GFP_NOFS); |
Julia Lawall | 485640b | 2015-06-20 18:59:07 +0200 | [diff] [blame] | 105 | if (!data) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 106 | return -ENOMEM; |
| 107 | |
| 108 | obd_str2uuid(&data->un_uuid, uuid); |
| 109 | data->un_nids[0] = nid; |
| 110 | data->un_nid_count = 1; |
| 111 | |
| 112 | spin_lock(&g_uuid_lock); |
| 113 | list_for_each_entry(entry, &g_uuid_list, un_list) { |
| 114 | if (obd_uuid_equals(&entry->un_uuid, &data->un_uuid)) { |
| 115 | int i; |
| 116 | |
| 117 | found = 1; |
| 118 | for (i = 0; i < entry->un_nid_count; i++) |
| 119 | if (nid == entry->un_nids[i]) |
| 120 | break; |
| 121 | |
| 122 | if (i == entry->un_nid_count) { |
| 123 | LASSERT(entry->un_nid_count < NIDS_MAX); |
| 124 | entry->un_nids[entry->un_nid_count++] = nid; |
| 125 | } |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | if (!found) |
| 130 | list_add(&data->un_list, &g_uuid_list); |
| 131 | spin_unlock(&g_uuid_lock); |
| 132 | |
| 133 | if (found) { |
| 134 | CDEBUG(D_INFO, "found uuid %s %s cnt=%d\n", uuid, |
| 135 | libcfs_nid2str(nid), entry->un_nid_count); |
Julia Lawall | d727904 | 2015-05-01 17:51:15 +0200 | [diff] [blame] | 136 | kfree(data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 137 | } else { |
| 138 | CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid)); |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | EXPORT_SYMBOL(class_add_uuid); |
| 143 | |
| 144 | /* Delete the nids for one uuid if specified, otherwise delete all */ |
| 145 | int class_del_uuid(const char *uuid) |
| 146 | { |
| 147 | LIST_HEAD(deathrow); |
| 148 | struct uuid_nid_data *data; |
Bhaktipriya Shridhar | 6069f75 | 2016-03-12 01:29:21 +0530 | [diff] [blame] | 149 | struct uuid_nid_data *temp; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 150 | |
| 151 | spin_lock(&g_uuid_lock); |
Oleg Drokin | cce3c2d | 2016-02-16 00:46:55 -0500 | [diff] [blame] | 152 | if (uuid) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 153 | struct obd_uuid tmp; |
| 154 | |
| 155 | obd_str2uuid(&tmp, uuid); |
| 156 | list_for_each_entry(data, &g_uuid_list, un_list) { |
| 157 | if (obd_uuid_equals(&data->un_uuid, &tmp)) { |
| 158 | list_move(&data->un_list, &deathrow); |
| 159 | break; |
| 160 | } |
| 161 | } |
Oleg Drokin | da5ecb4 | 2016-04-01 15:18:01 -0400 | [diff] [blame] | 162 | } else { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 163 | list_splice_init(&g_uuid_list, &deathrow); |
Oleg Drokin | da5ecb4 | 2016-04-01 15:18:01 -0400 | [diff] [blame] | 164 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 165 | spin_unlock(&g_uuid_lock); |
| 166 | |
Oleg Drokin | cce3c2d | 2016-02-16 00:46:55 -0500 | [diff] [blame] | 167 | if (uuid && list_empty(&deathrow)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 168 | CDEBUG(D_INFO, "Try to delete a non-existent uuid %s\n", uuid); |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
Bhaktipriya Shridhar | 6069f75 | 2016-03-12 01:29:21 +0530 | [diff] [blame] | 172 | list_for_each_entry_safe(data, temp, &deathrow, un_list) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 173 | list_del(&data->un_list); |
| 174 | |
| 175 | CDEBUG(D_INFO, "del uuid %s %s/%d\n", |
| 176 | obd_uuid2str(&data->un_uuid), |
| 177 | libcfs_nid2str(data->un_nids[0]), |
| 178 | data->un_nid_count); |
| 179 | |
Julia Lawall | d727904 | 2015-05-01 17:51:15 +0200 | [diff] [blame] | 180 | kfree(data); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* check if @nid exists in nid list of @uuid */ |
| 187 | int class_check_uuid(struct obd_uuid *uuid, __u64 nid) |
| 188 | { |
| 189 | struct uuid_nid_data *entry; |
| 190 | int found = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 191 | |
| 192 | CDEBUG(D_INFO, "check if uuid %s has %s.\n", |
| 193 | obd_uuid2str(uuid), libcfs_nid2str(nid)); |
| 194 | |
| 195 | spin_lock(&g_uuid_lock); |
| 196 | list_for_each_entry(entry, &g_uuid_list, un_list) { |
| 197 | int i; |
| 198 | |
| 199 | if (!obd_uuid_equals(&entry->un_uuid, uuid)) |
| 200 | continue; |
| 201 | |
| 202 | /* found the uuid, check if it has @nid */ |
| 203 | for (i = 0; i < entry->un_nid_count; i++) { |
| 204 | if (entry->un_nids[i] == nid) { |
| 205 | found = 1; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | break; |
| 210 | } |
| 211 | spin_unlock(&g_uuid_lock); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 212 | return found; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 213 | } |
| 214 | EXPORT_SYMBOL(class_check_uuid); |