Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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         lnet_nid_t              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, lnet_nid_t *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, __u64 nid)
79 {
80         struct uuid_nid_data *data, *entry;
81         int found = 0;
82         int rc;
83
84         LASSERT(nid != 0);  /* valid newconfig NID is never zero */
85
86         if (strlen(uuid) > UUID_MAX - 1)
87                 return -EOVERFLOW;
88
89         OBD_ALLOC_PTR(data);
90         if (data == NULL)
91                 return -ENOMEM;
92
93         obd_str2uuid(&data->un_uuid, uuid);
94         data->un_nids[0] = nid;
95         data->un_nid_count = 1;
96
97         spin_lock(&g_uuid_lock);
98         list_for_each_entry(entry, &g_uuid_list, un_list) {
99                 if (obd_uuid_equals(&entry->un_uuid, &data->un_uuid)) {
100                         int i;
101
102                         found = 1;
103                         for (i = 0; i < entry->un_nid_count; i++)
104                                 if (nid == entry->un_nids[i])
105                                         break;
106
107                         if (i == entry->un_nid_count) {
108                                 LASSERT(entry->un_nid_count < MTI_NIDS_MAX);
109                                 entry->un_nids[entry->un_nid_count++] = nid;
110                         }
111                         break;
112                 }
113         }
114         if (!found)
115                 list_add(&data->un_list, &g_uuid_list);
116         spin_unlock(&g_uuid_lock);
117
118         if (found) {
119                 CDEBUG(D_INFO, "found uuid %s %s cnt=%d\n", uuid,
120                        libcfs_nid2str(nid), entry->un_nid_count);
121                 rc = LNetAddPeer(entry->un_nids, entry->un_nid_count);
122                 CDEBUG(D_INFO, "Add peer %s rc = %d\n",
123                        libcfs_nid2str(data->un_nids[0]), rc);
124                 OBD_FREE(data, sizeof(*data));
125         } else {
126                 CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid));
127                 rc = LNetAddPeer(data->un_nids, data->un_nid_count);
128                 CDEBUG(D_INFO, "Add peer %s rc = %d\n",
129                        libcfs_nid2str(data->un_nids[0]), rc);
130         }
131
132         return 0;
133 }
134 EXPORT_SYMBOL(class_add_uuid);
135
136 /* Delete the nids for one uuid if specified, otherwise delete all */
137 int class_del_uuid(const char *uuid)
138 {
139         struct uuid_nid_data *data;
140         LIST_HEAD(deathrow);
141
142         spin_lock(&g_uuid_lock);
143         if (uuid != NULL) {
144                 struct obd_uuid tmp;
145
146                 obd_str2uuid(&tmp, uuid);
147                 list_for_each_entry(data, &g_uuid_list, un_list) {
148                         if (obd_uuid_equals(&data->un_uuid, &tmp)) {
149                                 list_move(&data->un_list, &deathrow);
150                                 break;
151                         }
152                 }
153         } else
154                 list_splice_init(&g_uuid_list, &deathrow);
155         spin_unlock(&g_uuid_lock);
156
157         if (uuid != NULL && list_empty(&deathrow)) {
158                 CDEBUG(D_INFO, "Try to delete a non-existent uuid %s\n", uuid);
159                 return -EINVAL;
160         }
161
162         while ((data = list_first_entry_or_null(&deathrow, struct uuid_nid_data,
163                                                 un_list)) != NULL) {
164                 list_del(&data->un_list);
165
166                 CDEBUG(D_INFO, "del uuid %s %s/%d\n",
167                        obd_uuid2str(&data->un_uuid),
168                        libcfs_nid2str(data->un_nids[0]),
169                        data->un_nid_count);
170
171                 OBD_FREE(data, sizeof(*data));
172         }
173         return 0;
174 }
175
176 int class_add_nids_to_uuid(struct obd_uuid *uuid, lnet_nid_t *nids,
177                            int nid_count)
178 {
179         struct uuid_nid_data *entry;
180         int i, rc;
181         bool matched = false;
182
183         ENTRY;
184
185         if (nid_count >= MTI_NIDS_MAX) {
186                 CDEBUG(D_NET, "too many NIDs (%d) for UUID '%s'\n",
187                         nid_count, obd_uuid2str(uuid));
188                 return -ENOSPC;
189         }
190
191         spin_lock(&g_uuid_lock);
192         list_for_each_entry(entry, &g_uuid_list, un_list) {
193                 CDEBUG(D_NET, "Comparing %s with %s\n",
194                        obd_uuid2str(uuid), obd_uuid2str(&entry->un_uuid));
195
196                 if (!obd_uuid_equals(&entry->un_uuid, uuid))
197                         continue;
198
199                 matched = true;
200                 CDEBUG(D_NET, "Updating UUID '%s'\n", obd_uuid2str(uuid));
201                 for (i = 0; i < nid_count; i++)
202                         entry->un_nids[i] = nids[i];
203                 entry->un_nid_count = nid_count;
204                 break;
205         }
206         spin_unlock(&g_uuid_lock);
207         if (matched) {
208                 rc = LNetAddPeer(entry->un_nids, entry->un_nid_count);
209                 CDEBUG(D_INFO, "Add peer %s rc = %d\n",
210                        libcfs_nid2str(entry->un_nids[0]), rc);
211         }
212
213         RETURN(0);
214 }
215 EXPORT_SYMBOL(class_add_nids_to_uuid);
216
217 /* check if @nid exists in nid list of @uuid */
218 int class_check_uuid(struct obd_uuid *uuid, __u64 nid)
219 {
220         struct uuid_nid_data *entry;
221         int found = 0;
222
223         ENTRY;
224
225         CDEBUG(D_INFO, "check if uuid %s has %s.\n",
226                obd_uuid2str(uuid), libcfs_nid2str(nid));
227
228         spin_lock(&g_uuid_lock);
229         list_for_each_entry(entry, &g_uuid_list, un_list) {
230                 int i;
231
232                 if (!obd_uuid_equals(&entry->un_uuid, uuid))
233                         continue;
234
235                 /* found the uuid, check if it has @nid */
236                 for (i = 0; i < entry->un_nid_count; i++) {
237                         if (entry->un_nids[i] == nid) {
238                                 found = 1;
239                                 break;
240                         }
241                 }
242                 break;
243         }
244         spin_unlock(&g_uuid_lock);
245         RETURN(found);
246 }
247 EXPORT_SYMBOL(class_check_uuid);