Whamcloud - gitweb
bce4efe0390f3e6c833cd4d1b9a7120ac4f09c17
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_RPC
38
39 #include <obd.h>
40 #include <obd_support.h>
41 #include <obd_class.h>
42 #include <lustre_lib.h>
43 #include <lustre_ha.h>
44 #include <lustre_net.h>
45 #include <lprocfs_status.h>
46
47 #define NIDS_MAX        32
48
49 struct uuid_nid_data {
50         struct list_head        un_list;
51         struct obd_uuid         un_uuid;
52         int                     un_nid_count;
53         lnet_nid_t              un_nids[NIDS_MAX];
54 };
55
56 /* FIXME: This should probably become more elegant than a global linked list */
57 static struct list_head g_uuid_list;
58 static spinlock_t       g_uuid_lock;
59
60 void class_init_uuidlist(void)
61 {
62         INIT_LIST_HEAD(&g_uuid_list);
63         spin_lock_init(&g_uuid_lock);
64 }
65
66 void class_exit_uuidlist(void)
67 {
68         /* delete all */
69         class_del_uuid(NULL);
70 }
71
72 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index)
73 {
74         struct uuid_nid_data *data;
75         struct obd_uuid tmp;
76         int rc = -ENOENT;
77
78         obd_str2uuid(&tmp, uuid);
79         spin_lock(&g_uuid_lock);
80         list_for_each_entry(data, &g_uuid_list, un_list) {
81                 if (obd_uuid_equals(&data->un_uuid, &tmp)) {
82                         if (index >= data->un_nid_count)
83                                 break;
84
85                         rc = 0;
86                         *peer_nid = data->un_nids[index];
87                         break;
88                 }
89         }
90         spin_unlock(&g_uuid_lock);
91         return rc;
92 }
93 EXPORT_SYMBOL(lustre_uuid_to_peer);
94
95 /* Add a nid to a niduuid.  Multiple nids can be added to a single uuid;
96    LNET will choose the best one. */
97 int class_add_uuid(const char *uuid, __u64 nid)
98 {
99         struct uuid_nid_data *data, *entry;
100         int found = 0;
101
102         LASSERT(nid != 0);  /* valid newconfig NID is never zero */
103
104         if (strlen(uuid) > UUID_MAX - 1)
105                 return -EOVERFLOW;
106
107         OBD_ALLOC_PTR(data);
108         if (data == NULL)
109                 return -ENOMEM;
110
111         obd_str2uuid(&data->un_uuid, uuid);
112         data->un_nids[0] = nid;
113         data->un_nid_count = 1;
114
115         spin_lock(&g_uuid_lock);
116         list_for_each_entry(entry, &g_uuid_list, un_list) {
117                 if (obd_uuid_equals(&entry->un_uuid, &data->un_uuid)) {
118                         int i;
119
120                         found = 1;
121                         for (i = 0; i < entry->un_nid_count; i++)
122                                 if (nid == entry->un_nids[i])
123                                         break;
124
125                         if (i == entry->un_nid_count) {
126                                 LASSERT(entry->un_nid_count < NIDS_MAX);
127                                 entry->un_nids[entry->un_nid_count++] = nid;
128                         }
129                         break;
130                 }
131         }
132         if (!found)
133                 list_add(&data->un_list, &g_uuid_list);
134         spin_unlock(&g_uuid_lock);
135
136         if (found) {
137                 CDEBUG(D_INFO, "found uuid %s %s cnt=%d\n", uuid,
138                        libcfs_nid2str(nid), entry->un_nid_count);
139                 OBD_FREE(data, sizeof(*data));
140         } else {
141                 CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid));
142         }
143         return 0;
144 }
145 EXPORT_SYMBOL(class_add_uuid);
146
147 /* Delete the nids for one uuid if specified, otherwise delete all */
148 int class_del_uuid(const char *uuid)
149 {
150         struct uuid_nid_data *data;
151         struct list_head deathrow;
152
153         INIT_LIST_HEAD(&deathrow);
154
155         spin_lock(&g_uuid_lock);
156         if (uuid != NULL) {
157                 struct obd_uuid tmp;
158
159                 obd_str2uuid(&tmp, uuid);
160                 list_for_each_entry(data, &g_uuid_list, un_list) {
161                         if (obd_uuid_equals(&data->un_uuid, &tmp)) {
162                                 list_move(&data->un_list, &deathrow);
163                                 break;
164                         }
165                 }
166         } else
167                 list_splice_init(&g_uuid_list, &deathrow);
168         spin_unlock(&g_uuid_lock);
169
170         if (uuid != NULL && list_empty(&deathrow)) {
171                 CDEBUG(D_INFO, "Try to delete a non-existent uuid %s\n", uuid);
172                 return -EINVAL;
173         }
174
175         while (!list_empty(&deathrow)) {
176                 data = list_entry(deathrow.next, struct uuid_nid_data,
177                                   un_list);
178                 list_del(&data->un_list);
179
180                 CDEBUG(D_INFO, "del uuid %s %s/%d\n",
181                        obd_uuid2str(&data->un_uuid),
182                        libcfs_nid2str(data->un_nids[0]),
183                        data->un_nid_count);
184
185                 OBD_FREE(data, sizeof(*data));
186         }
187         return 0;
188 }
189
190 /* check if @nid exists in nid list of @uuid */
191 int class_check_uuid(struct obd_uuid *uuid, __u64 nid)
192 {
193         struct uuid_nid_data *entry;
194         int found = 0;
195         ENTRY;
196
197         CDEBUG(D_INFO, "check if uuid %s has %s.\n",
198                obd_uuid2str(uuid), libcfs_nid2str(nid));
199
200         spin_lock(&g_uuid_lock);
201         list_for_each_entry(entry, &g_uuid_list, un_list) {
202                 int i;
203
204                 if (!obd_uuid_equals(&entry->un_uuid, uuid))
205                         continue;
206
207                 /* found the uuid, check if it has @nid */
208                 for (i = 0; i < entry->un_nid_count; i++) {
209                         if (entry->un_nids[i] == nid) {
210                                 found = 1;
211                                 break;
212                         }
213                 }
214                 break;
215         }
216         spin_unlock(&g_uuid_lock);
217         RETURN(found);
218 }
219 EXPORT_SYMBOL(class_check_uuid);