Whamcloud - gitweb
LU-10391 lnet: change LNetAddPeer() to take struct lnet_nid
[fs/lustre-release.git] / lustre / obdclass / lustre_peer.c
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
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_RPC
33
34 #include <obd.h>
35 #include <obd_support.h>
36 #include <obd_class.h>
37 #include <lustre_lib.h>
38 #include <lustre_ha.h>
39 #include <lustre_net.h>
40 #include <lprocfs_status.h>
41
42 struct uuid_nid_data {
43         struct list_head        un_list;
44         struct obd_uuid         un_uuid;
45         int                     un_nid_count;
46         struct lnet_nid         un_nids[MTI_NIDS_MAX];
47 };
48
49 /* FIXME: This should probably become more elegant than a global linked list */
50 static LIST_HEAD(g_uuid_list);
51 static DEFINE_SPINLOCK(g_uuid_lock);
52
53 int lustre_uuid_to_peer(const char *uuid, struct lnet_nid *peer_nid, int index)
54 {
55         struct uuid_nid_data *data;
56         struct obd_uuid tmp;
57         int rc = -ENOENT;
58
59         obd_str2uuid(&tmp, uuid);
60         spin_lock(&g_uuid_lock);
61         list_for_each_entry(data, &g_uuid_list, un_list) {
62                 if (obd_uuid_equals(&data->un_uuid, &tmp)) {
63                         if (index >= data->un_nid_count)
64                                 break;
65
66                         rc = 0;
67                         *peer_nid = data->un_nids[index];
68                         break;
69                 }
70         }
71         spin_unlock(&g_uuid_lock);
72         return rc;
73 }
74 EXPORT_SYMBOL(lustre_uuid_to_peer);
75
76 /* Add a nid to a niduuid.  Multiple nids can be added to a single uuid;
77    LNET will choose the best one. */
78 int class_add_uuid(const char *uuid, lnet_nid_t nid4)
79 {
80         struct uuid_nid_data *data, *entry;
81         struct lnet_nid nid;
82         int found = 0;
83         int rc;
84
85         LASSERT(nid4 != 0);  /* valid newconfig NID is never zero */
86         lnet_nid4_to_nid(nid4, &nid);
87
88         if (strlen(uuid) > UUID_MAX - 1)
89                 return -EOVERFLOW;
90
91         OBD_ALLOC_PTR(data);
92         if (data == NULL)
93                 return -ENOMEM;
94
95         obd_str2uuid(&data->un_uuid, uuid);
96         data->un_nids[0] = nid;
97         data->un_nid_count = 1;
98
99         spin_lock(&g_uuid_lock);
100         list_for_each_entry(entry, &g_uuid_list, un_list) {
101                 if (obd_uuid_equals(&entry->un_uuid, &data->un_uuid)) {
102                         int i;
103
104                         found = 1;
105                         for (i = 0; i < entry->un_nid_count; i++)
106                                 if (nid_same(&nid, &entry->un_nids[i]))
107                                         break;
108
109                         if (i == entry->un_nid_count) {
110                                 LASSERT(entry->un_nid_count < MTI_NIDS_MAX);
111                                 entry->un_nids[entry->un_nid_count++] = nid;
112                         }
113                         break;
114                 }
115         }
116         if (!found)
117                 list_add(&data->un_list, &g_uuid_list);
118         spin_unlock(&g_uuid_lock);
119
120         if (found) {
121                 CDEBUG(D_INFO, "found uuid %s %s cnt=%d\n", uuid,
122                        libcfs_nidstr(&nid), entry->un_nid_count);
123                 rc = LNetAddPeer(entry->un_nids, entry->un_nid_count);
124                 CDEBUG(D_INFO, "Add peer %s rc = %d\n",
125                        libcfs_nidstr(&data->un_nids[0]), rc);
126                 OBD_FREE(data, sizeof(*data));
127         } else {
128                 CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nidstr(&nid));
129                 rc = LNetAddPeer(data->un_nids, data->un_nid_count);
130                 CDEBUG(D_INFO, "Add peer %s rc = %d\n",
131                        libcfs_nidstr(&data->un_nids[0]), rc);
132         }
133
134         return 0;
135 }
136 EXPORT_SYMBOL(class_add_uuid);
137
138 /* Delete the nids for one uuid if specified, otherwise delete all */
139 int class_del_uuid(const char *uuid)
140 {
141         struct uuid_nid_data *data;
142         LIST_HEAD(deathrow);
143
144         spin_lock(&g_uuid_lock);
145         if (uuid != NULL) {
146                 struct obd_uuid tmp;
147
148                 obd_str2uuid(&tmp, uuid);
149                 list_for_each_entry(data, &g_uuid_list, un_list) {
150                         if (obd_uuid_equals(&data->un_uuid, &tmp)) {
151                                 list_move(&data->un_list, &deathrow);
152                                 break;
153                         }
154                 }
155         } else
156                 list_splice_init(&g_uuid_list, &deathrow);
157         spin_unlock(&g_uuid_lock);
158
159         if (uuid != NULL && list_empty(&deathrow)) {
160                 CDEBUG(D_INFO, "Try to delete a non-existent uuid %s\n", uuid);
161                 return -EINVAL;
162         }
163
164         while ((data = list_first_entry_or_null(&deathrow, struct uuid_nid_data,
165                                                 un_list)) != NULL) {
166                 list_del(&data->un_list);
167
168                 CDEBUG(D_INFO, "del uuid %s %s/%d\n",
169                        obd_uuid2str(&data->un_uuid),
170                        libcfs_nidstr(&data->un_nids[0]),
171                        data->un_nid_count);
172
173                 OBD_FREE(data, sizeof(*data));
174         }
175         return 0;
176 }
177
178 int class_add_nids_to_uuid(struct obd_uuid *uuid, lnet_nid_t *nids,
179                            int nid_count)
180 {
181         struct uuid_nid_data *entry;
182         int i, rc;
183         bool matched = false;
184
185         ENTRY;
186
187         if (nid_count >= MTI_NIDS_MAX) {
188                 CDEBUG(D_NET, "too many NIDs (%d) for UUID '%s'\n",
189                         nid_count, obd_uuid2str(uuid));
190                 return -ENOSPC;
191         }
192
193         spin_lock(&g_uuid_lock);
194         list_for_each_entry(entry, &g_uuid_list, un_list) {
195                 CDEBUG(D_NET, "Comparing %s with %s\n",
196                        obd_uuid2str(uuid), obd_uuid2str(&entry->un_uuid));
197
198                 if (!obd_uuid_equals(&entry->un_uuid, uuid))
199                         continue;
200
201                 matched = true;
202                 CDEBUG(D_NET, "Updating UUID '%s'\n", obd_uuid2str(uuid));
203                 for (i = 0; i < nid_count; i++)
204                         lnet_nid4_to_nid(nids[i], &entry->un_nids[i]);
205                 entry->un_nid_count = nid_count;
206                 break;
207         }
208         spin_unlock(&g_uuid_lock);
209         if (matched) {
210                 rc = LNetAddPeer(entry->un_nids, entry->un_nid_count);
211                 CDEBUG(D_INFO, "Add peer %s rc = %d\n",
212                        libcfs_nidstr(&entry->un_nids[0]), rc);
213         }
214
215         RETURN(0);
216 }
217 EXPORT_SYMBOL(class_add_nids_to_uuid);
218
219 /* check if @nid exists in nid list of @uuid */
220 int class_check_uuid(struct obd_uuid *uuid, lnet_nid_t nid4)
221 {
222         struct uuid_nid_data *entry;
223         struct lnet_nid nid;
224         int found = 0;
225
226         ENTRY;
227
228         lnet_nid4_to_nid(nid4, &nid);
229         CDEBUG(D_INFO, "check if uuid %s has %s.\n",
230                obd_uuid2str(uuid), libcfs_nidstr(&nid));
231
232         spin_lock(&g_uuid_lock);
233         list_for_each_entry(entry, &g_uuid_list, un_list) {
234                 int i;
235
236                 if (!obd_uuid_equals(&entry->un_uuid, uuid))
237                         continue;
238
239                 /* found the uuid, check if it has @nid */
240                 for (i = 0; i < entry->un_nid_count; i++) {
241                         if (nid_same(&entry->un_nids[i], &nid)) {
242                                 found = 1;
243                                 break;
244                         }
245                 }
246                 break;
247         }
248         spin_unlock(&g_uuid_lock);
249         RETURN(found);
250 }
251 EXPORT_SYMBOL(class_check_uuid);