Whamcloud - gitweb
LU-3963 obdclass: convert to linux list api
[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 #ifndef __KERNEL__
40 # include <liblustre.h>
41 #endif
42 #include <obd.h>
43 #include <obd_support.h>
44 #include <obd_class.h>
45 #include <lustre_lib.h>
46 #include <lustre_ha.h>
47 #include <lustre_net.h>
48 #include <lprocfs_status.h>
49
50 #define NIDS_MAX        32
51
52 struct uuid_nid_data {
53         struct list_head        un_list;
54         struct obd_uuid         un_uuid;
55         int                     un_nid_count;
56         lnet_nid_t              un_nids[NIDS_MAX];
57 };
58
59 /* FIXME: This should probably become more elegant than a global linked list */
60 static struct list_head g_uuid_list;
61 static spinlock_t       g_uuid_lock;
62
63 void class_init_uuidlist(void)
64 {
65         INIT_LIST_HEAD(&g_uuid_list);
66         spin_lock_init(&g_uuid_lock);
67 }
68
69 void class_exit_uuidlist(void)
70 {
71         /* delete all */
72         class_del_uuid(NULL);
73 }
74
75 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index)
76 {
77         struct uuid_nid_data *data;
78         struct obd_uuid tmp;
79         int rc = -ENOENT;
80
81         obd_str2uuid(&tmp, uuid);
82         spin_lock(&g_uuid_lock);
83         list_for_each_entry(data, &g_uuid_list, un_list) {
84                 if (obd_uuid_equals(&data->un_uuid, &tmp)) {
85                         if (index >= data->un_nid_count)
86                                 break;
87
88                         rc = 0;
89                         *peer_nid = data->un_nids[index];
90                         break;
91                 }
92         }
93         spin_unlock(&g_uuid_lock);
94         return rc;
95 }
96 EXPORT_SYMBOL(lustre_uuid_to_peer);
97
98 /* Add a nid to a niduuid.  Multiple nids can be added to a single uuid;
99    LNET will choose the best one. */
100 int class_add_uuid(const char *uuid, __u64 nid)
101 {
102         struct uuid_nid_data *data, *entry;
103         int found = 0;
104
105         LASSERT(nid != 0);  /* valid newconfig NID is never zero */
106
107         if (strlen(uuid) > UUID_MAX - 1)
108                 return -EOVERFLOW;
109
110         OBD_ALLOC_PTR(data);
111         if (data == NULL)
112                 return -ENOMEM;
113
114         obd_str2uuid(&data->un_uuid, uuid);
115         data->un_nids[0] = nid;
116         data->un_nid_count = 1;
117
118         spin_lock(&g_uuid_lock);
119         list_for_each_entry(entry, &g_uuid_list, un_list) {
120                 if (obd_uuid_equals(&entry->un_uuid, &data->un_uuid)) {
121                         int i;
122
123                         found = 1;
124                         for (i = 0; i < entry->un_nid_count; i++)
125                                 if (nid == entry->un_nids[i])
126                                         break;
127
128                         if (i == entry->un_nid_count) {
129                                 LASSERT(entry->un_nid_count < NIDS_MAX);
130                                 entry->un_nids[entry->un_nid_count++] = nid;
131                         }
132                         break;
133                 }
134         }
135         if (!found)
136                 list_add(&data->un_list, &g_uuid_list);
137         spin_unlock(&g_uuid_lock);
138
139         if (found) {
140                 CDEBUG(D_INFO, "found uuid %s %s cnt=%d\n", uuid,
141                        libcfs_nid2str(nid), entry->un_nid_count);
142                 OBD_FREE(data, sizeof(*data));
143         } else {
144                 CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid));
145         }
146         return 0;
147 }
148 EXPORT_SYMBOL(class_add_uuid);
149
150 /* Delete the nids for one uuid if specified, otherwise delete all */
151 int class_del_uuid(const char *uuid)
152 {
153         struct uuid_nid_data *data;
154         struct list_head deathrow;
155
156         INIT_LIST_HEAD(&deathrow);
157
158         spin_lock(&g_uuid_lock);
159         if (uuid != NULL) {
160                 struct obd_uuid tmp;
161
162                 obd_str2uuid(&tmp, uuid);
163                 list_for_each_entry(data, &g_uuid_list, un_list) {
164                         if (obd_uuid_equals(&data->un_uuid, &tmp)) {
165                                 list_move(&data->un_list, &deathrow);
166                                 break;
167                         }
168                 }
169         } else
170                 list_splice_init(&g_uuid_list, &deathrow);
171         spin_unlock(&g_uuid_lock);
172
173         if (uuid != NULL && list_empty(&deathrow)) {
174                 CDEBUG(D_INFO, "Try to delete a non-existent uuid %s\n", uuid);
175                 return -EINVAL;
176         }
177
178         while (!list_empty(&deathrow)) {
179                 data = list_entry(deathrow.next, struct uuid_nid_data,
180                                   un_list);
181                 list_del(&data->un_list);
182
183                 CDEBUG(D_INFO, "del uuid %s %s/%d\n",
184                        obd_uuid2str(&data->un_uuid),
185                        libcfs_nid2str(data->un_nids[0]),
186                        data->un_nid_count);
187
188                 OBD_FREE(data, sizeof(*data));
189         }
190         return 0;
191 }
192
193 /* check if @nid exists in nid list of @uuid */
194 int class_check_uuid(struct obd_uuid *uuid, __u64 nid)
195 {
196         struct uuid_nid_data *entry;
197         int found = 0;
198         ENTRY;
199
200         CDEBUG(D_INFO, "check if uuid %s has %s.\n",
201                obd_uuid2str(uuid), libcfs_nid2str(nid));
202
203         spin_lock(&g_uuid_lock);
204         list_for_each_entry(entry, &g_uuid_list, un_list) {
205                 int i;
206
207                 if (!obd_uuid_equals(&entry->un_uuid, uuid))
208                         continue;
209
210                 /* found the uuid, check if it has @nid */
211                 for (i = 0; i < entry->un_nid_count; i++) {
212                         if (entry->un_nids[i] == nid) {
213                                 found = 1;
214                                 break;
215                         }
216                 }
217                 break;
218         }
219         spin_unlock(&g_uuid_lock);
220         RETURN(found);
221 }
222 EXPORT_SYMBOL(class_check_uuid);