Whamcloud - gitweb
d3442226a276162af53dd76d85e540259ea121ca
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_RPC
34
35 #include <obd.h>
36 #include <obd_support.h>
37 #include <obd_class.h>
38 #include <lustre_lib.h>
39 #include <lustre_ha.h>
40 #include <lustre_net.h>
41 #include <lprocfs_status.h>
42
43 struct uuid_nid_data {
44         struct list_head        un_list;
45         struct obd_uuid         un_uuid;
46         int                     un_nid_count;
47         lnet_nid_t              un_nids[MTI_NIDS_MAX];
48 };
49
50 /* FIXME: This should probably become more elegant than a global linked list */
51 static LIST_HEAD(g_uuid_list);
52 static DEFINE_SPINLOCK(g_uuid_lock);
53
54 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index)
55 {
56         struct uuid_nid_data *data;
57         struct obd_uuid tmp;
58         int rc = -ENOENT;
59
60         obd_str2uuid(&tmp, uuid);
61         spin_lock(&g_uuid_lock);
62         list_for_each_entry(data, &g_uuid_list, un_list) {
63                 if (obd_uuid_equals(&data->un_uuid, &tmp)) {
64                         if (index >= data->un_nid_count)
65                                 break;
66
67                         rc = 0;
68                         *peer_nid = data->un_nids[index];
69                         break;
70                 }
71         }
72         spin_unlock(&g_uuid_lock);
73         return rc;
74 }
75 EXPORT_SYMBOL(lustre_uuid_to_peer);
76
77 /* Add a nid to a niduuid.  Multiple nids can be added to a single uuid;
78    LNET will choose the best one. */
79 int class_add_uuid(const char *uuid, __u64 nid)
80 {
81         struct uuid_nid_data *data, *entry;
82         int found = 0;
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                 OBD_FREE(data, sizeof(*data));
122         } else {
123                 CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid));
124         }
125         return 0;
126 }
127 EXPORT_SYMBOL(class_add_uuid);
128
129 /* Delete the nids for one uuid if specified, otherwise delete all */
130 int class_del_uuid(const char *uuid)
131 {
132         struct uuid_nid_data *data;
133         LIST_HEAD(deathrow);
134
135         spin_lock(&g_uuid_lock);
136         if (uuid != NULL) {
137                 struct obd_uuid tmp;
138
139                 obd_str2uuid(&tmp, uuid);
140                 list_for_each_entry(data, &g_uuid_list, un_list) {
141                         if (obd_uuid_equals(&data->un_uuid, &tmp)) {
142                                 list_move(&data->un_list, &deathrow);
143                                 break;
144                         }
145                 }
146         } else
147                 list_splice_init(&g_uuid_list, &deathrow);
148         spin_unlock(&g_uuid_lock);
149
150         if (uuid != NULL && list_empty(&deathrow)) {
151                 CDEBUG(D_INFO, "Try to delete a non-existent uuid %s\n", uuid);
152                 return -EINVAL;
153         }
154
155         while (!list_empty(&deathrow)) {
156                 data = list_entry(deathrow.next, struct uuid_nid_data,
157                                   un_list);
158                 list_del(&data->un_list);
159
160                 CDEBUG(D_INFO, "del uuid %s %s/%d\n",
161                        obd_uuid2str(&data->un_uuid),
162                        libcfs_nid2str(data->un_nids[0]),
163                        data->un_nid_count);
164
165                 OBD_FREE(data, sizeof(*data));
166         }
167         return 0;
168 }
169
170 int class_add_nids_to_uuid(struct obd_uuid *uuid, lnet_nid_t *nids,
171                            int nid_count)
172 {
173         struct uuid_nid_data *entry;
174         int i;
175
176         ENTRY;
177
178         if (nid_count >= MTI_NIDS_MAX) {
179                 CDEBUG(D_NET, "too many NIDs (%d) for UUID '%s'\n",
180                         nid_count, obd_uuid2str(uuid));
181                 return -ENOSPC;
182         }
183
184         spin_lock(&g_uuid_lock);
185         list_for_each_entry(entry, &g_uuid_list, un_list) {
186                 CDEBUG(D_NET, "Comparing %s with %s\n",
187                        obd_uuid2str(uuid), obd_uuid2str(&entry->un_uuid));
188
189                 if (!obd_uuid_equals(&entry->un_uuid, uuid))
190                         continue;
191                 CDEBUG(D_NET, "Updating UUID '%s'\n", obd_uuid2str(uuid));
192                 for (i = 0; i < nid_count; i++)
193                         entry->un_nids[i] = nids[i];
194                 entry->un_nid_count = nid_count;
195                 break;
196         }
197         spin_unlock(&g_uuid_lock);
198         RETURN(0);
199 }
200 EXPORT_SYMBOL(class_add_nids_to_uuid);
201
202 /* check if @nid exists in nid list of @uuid */
203 int class_check_uuid(struct obd_uuid *uuid, __u64 nid)
204 {
205         struct uuid_nid_data *entry;
206         int found = 0;
207
208         ENTRY;
209
210         CDEBUG(D_INFO, "check if uuid %s has %s.\n",
211                obd_uuid2str(uuid), libcfs_nid2str(nid));
212
213         spin_lock(&g_uuid_lock);
214         list_for_each_entry(entry, &g_uuid_list, un_list) {
215                 int i;
216
217                 if (!obd_uuid_equals(&entry->un_uuid, uuid))
218                         continue;
219
220                 /* found the uuid, check if it has @nid */
221                 for (i = 0; i < entry->un_nid_count; i++) {
222                         if (entry->un_nids[i] == nid) {
223                                 found = 1;
224                                 break;
225                         }
226                 }
227                 break;
228         }
229         spin_unlock(&g_uuid_lock);
230         RETURN(found);
231 }
232 EXPORT_SYMBOL(class_check_uuid);