Whamcloud - gitweb
LU-1308 Properly add multihomed nids to peer table
[fs/lustre-release.git] / lustre / obdclass / lustre_peer.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #define DEBUG_SUBSYSTEM S_RPC
40
41 #ifndef __KERNEL__
42 # include <liblustre.h>
43 #endif
44 #include <obd.h>
45 #include <obd_support.h>
46 #include <obd_class.h>
47 #include <lustre_lib.h>
48 #include <lustre_ha.h>
49 #include <lustre_net.h>
50 #include <lprocfs_status.h>
51
52 #define NIDS_MAX        32
53
54 struct uuid_nid_data {
55         cfs_list_t       un_list;
56         struct obd_uuid  un_uuid;
57         int              un_nid_count;
58         lnet_nid_t       un_nids[NIDS_MAX];
59 };
60
61 /* FIXME: This should probably become more elegant than a global linked list */
62 static cfs_list_t           g_uuid_list;
63 static cfs_spinlock_t       g_uuid_lock;
64
65 void class_init_uuidlist(void)
66 {
67         CFS_INIT_LIST_HEAD(&g_uuid_list);
68         cfs_spin_lock_init(&g_uuid_lock);
69 }
70
71 void class_exit_uuidlist(void)
72 {
73         /* delete all */
74         class_del_uuid(NULL);
75 }
76
77 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index)
78 {
79         struct uuid_nid_data *data;
80         struct obd_uuid tmp;
81         int rc = -ENOENT;
82
83         obd_str2uuid(&tmp, uuid);
84         cfs_spin_lock(&g_uuid_lock);
85         cfs_list_for_each_entry(data, &g_uuid_list, un_list) {
86                 if (obd_uuid_equals(&data->un_uuid, &tmp)) {
87                         if (index >= data->un_nid_count)
88                                 break;
89
90                         rc = 0;
91                         *peer_nid = data->un_nids[index];
92                         break;
93                 }
94         }
95         cfs_spin_unlock(&g_uuid_lock);
96         return rc;
97 }
98
99 /* Add a nid to a niduuid.  Multiple nids can be added to a single uuid;
100    LNET will choose the best one. */
101 int class_add_uuid(const char *uuid, __u64 nid)
102 {
103         struct uuid_nid_data *data, *entry;
104         int found = 0;
105
106         LASSERT(nid != 0);  /* valid newconfig NID is never zero */
107
108         if (strlen(uuid) > UUID_MAX - 1)
109                 return -EOVERFLOW;
110
111         OBD_ALLOC_PTR(data);
112         if (data == NULL)
113                 return -ENOMEM;
114
115         obd_str2uuid(&data->un_uuid, uuid);
116         data->un_nids[0] = nid;
117         data->un_nid_count = 1;
118
119         cfs_spin_lock(&g_uuid_lock);
120         cfs_list_for_each_entry(entry, &g_uuid_list, un_list) {
121                 if (obd_uuid_equals(&entry->un_uuid, &data->un_uuid)) {
122                         int i;
123
124                         found = 1;
125                         for (i = 0; i < entry->un_nid_count; i++)
126                                 if (nid == entry->un_nids[i])
127                                         break;
128
129                         if (i == entry->un_nid_count) {
130                                 LASSERT(entry->un_nid_count < NIDS_MAX);
131                                 entry->un_nids[++entry->un_nid_count] = nid;
132                         }
133                         break;
134                 }
135         }
136         if (!found)
137                 cfs_list_add(&data->un_list, &g_uuid_list);
138         cfs_spin_unlock(&g_uuid_lock);
139
140         if (found) {
141                 CDEBUG(D_INFO, "found uuid %s %s cnt=%d\n", uuid,
142                        libcfs_nid2str(nid), entry->un_nid_count);
143                 OBD_FREE(data, sizeof(*data));
144         } else {
145                 CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid));
146         }
147         return 0;
148 }
149
150 /* Delete the nids for one uuid if specified, otherwise delete all */
151 int class_del_uuid(const char *uuid)
152 {
153         CFS_LIST_HEAD(deathrow);
154         struct uuid_nid_data *data;
155
156         cfs_spin_lock(&g_uuid_lock);
157         if (uuid != NULL) {
158                 struct obd_uuid tmp;
159
160                 obd_str2uuid(&tmp, uuid);
161                 cfs_list_for_each_entry(data, &g_uuid_list, un_list) {
162                         if (obd_uuid_equals(&data->un_uuid, &tmp)) {
163                                 cfs_list_move(&data->un_list, &deathrow);
164                                 break;
165                         }
166                 }
167         } else
168                 cfs_list_splice_init(&g_uuid_list, &deathrow);
169         cfs_spin_unlock(&g_uuid_lock);
170
171         if (uuid != NULL && cfs_list_empty(&deathrow)) {
172                 CDEBUG(D_INFO, "Try to delete a non-existent uuid %s\n", uuid);
173                 return -EINVAL;
174         }
175
176         while (!cfs_list_empty(&deathrow)) {
177                 data = cfs_list_entry(deathrow.next, struct uuid_nid_data,
178                                       un_list);
179                 cfs_list_del(&data->un_list);
180
181                 CDEBUG(D_INFO, "del uuid %s %s/%d\n",
182                        obd_uuid2str(&data->un_uuid),
183                        libcfs_nid2str(data->un_nids[0]),
184                        data->un_nid_count);
185
186                 OBD_FREE(data, sizeof(*data));
187         }
188
189         return 0;
190 }
191
192 /* check if @nid exists in nid list of @uuid */
193 int class_check_uuid(struct obd_uuid *uuid, __u64 nid)
194 {
195         struct uuid_nid_data *entry;
196         int found = 0;
197         ENTRY;
198
199         CDEBUG(D_INFO, "check if uuid %s has %s.\n",
200                obd_uuid2str(uuid), libcfs_nid2str(nid));
201
202         cfs_spin_lock(&g_uuid_lock);
203         cfs_list_for_each_entry(entry, &g_uuid_list, un_list) {
204                 int i;
205
206                 if (!obd_uuid_equals(&entry->un_uuid, uuid))
207                         continue;
208
209                 /* found the uuid, check if it has @nid */
210                 for (i = 0; i < entry->un_nid_count; i++) {
211                         if (entry->un_nids[i] == nid) {
212                                 found = 1;
213                                 break;
214                         }
215                 }
216                 break;
217         }
218         cfs_spin_unlock (&g_uuid_lock);
219         RETURN(found);
220 }
221 EXPORT_SYMBOL(class_check_uuid);