Whamcloud - gitweb
LU-570: Add function to find connect uuid by nid
[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 /*
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         cfs_list_t       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 cfs_list_t           g_uuid_list;
61 static cfs_spinlock_t       g_uuid_lock;
62
63 void class_init_uuidlist(void)
64 {
65         CFS_INIT_LIST_HEAD(&g_uuid_list);
66         cfs_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         cfs_spin_lock(&g_uuid_lock);
83         cfs_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         cfs_spin_unlock(&g_uuid_lock);
94         return rc;
95 }
96
97 /* Add a nid to a niduuid.  Multiple nids can be added to a single uuid;
98    LNET will choose the best one. */
99 int class_add_uuid(const char *uuid, __u64 nid)
100 {
101         struct uuid_nid_data *data, *entry;
102         int found = 0;
103
104         LASSERT(nid != 0);  /* valid newconfig NID is never zero */
105
106         if (strlen(uuid) > UUID_MAX - 1)
107                 return -EOVERFLOW;
108
109         OBD_ALLOC_PTR(data);
110         if (data == NULL)
111                 return -ENOMEM;
112
113         obd_str2uuid(&data->un_uuid, uuid);
114         data->un_nids[0] = nid;
115         data->un_nid_count = 1;
116
117         cfs_spin_lock(&g_uuid_lock);
118         cfs_list_for_each_entry(entry, &g_uuid_list, un_list) {
119                 if (obd_uuid_equals(&entry->un_uuid, &data->un_uuid)) {
120                         int i;
121
122                         found = 1;
123                         for (i = 0; i < data->un_nid_count; i++)
124                                 if (nid == entry->un_nids[i])
125                                         break;
126
127                         if (i == data->un_nid_count) {
128                                 LASSERT(data->un_nid_count < NIDS_MAX);
129                                 data->un_nids[++data->un_nid_count] = nid;
130                                 break;
131                         }
132                 }
133         }
134         if (!found)
135                 cfs_list_add(&data->un_list, &g_uuid_list);
136         cfs_spin_unlock(&g_uuid_lock);
137
138         if (found) {
139                 CDEBUG(D_INFO, "found uuid %s %s cnt=%d\n", uuid,
140                        libcfs_nid2str(nid), entry->un_nid_count);
141                 OBD_FREE(data, sizeof(*data));
142         } else {
143                 CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid));
144         }
145         return 0;
146 }
147
148 /* Delete the nids for one uuid if specified, otherwise delete all */
149 int class_del_uuid(const char *uuid)
150 {
151         CFS_LIST_HEAD(deathrow);
152         struct uuid_nid_data *data;
153
154         cfs_spin_lock(&g_uuid_lock);
155         if (uuid != NULL) {
156                 struct obd_uuid tmp;
157
158                 obd_str2uuid(&tmp, uuid);
159                 cfs_list_for_each_entry(data, &g_uuid_list, un_list) {
160                         if (obd_uuid_equals(&data->un_uuid, &tmp)) {
161                                 cfs_list_move(&data->un_list, &deathrow);
162                                 break;
163                         }
164                 }
165         } else
166                 cfs_list_splice_init(&g_uuid_list, &deathrow);
167         cfs_spin_unlock(&g_uuid_lock);
168
169         if (uuid != NULL && cfs_list_empty(&deathrow)) {
170                 CDEBUG(D_INFO, "Try to delete a non-existent uuid %s\n", uuid);
171                 return -EINVAL;
172         }
173
174         while (!cfs_list_empty(&deathrow)) {
175                 data = cfs_list_entry(deathrow.next, struct uuid_nid_data,
176                                       un_list);
177                 cfs_list_del(&data->un_list);
178
179                 CDEBUG(D_INFO, "del uuid %s %s/%d\n",
180                        obd_uuid2str(&data->un_uuid),
181                        libcfs_nid2str(data->un_nids[0]),
182                        data->un_nid_count);
183
184                 OBD_FREE(data, sizeof(*data));
185         }
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         cfs_spin_lock(&g_uuid_lock);
201         cfs_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         }
215         cfs_spin_unlock (&g_uuid_lock);
216         RETURN(found);
217 }
218 EXPORT_SYMBOL(class_check_uuid);