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