4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2012, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_RPC
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>
45 struct uuid_nid_data {
46 struct list_head un_list;
47 struct obd_uuid un_uuid;
49 lnet_nid_t un_nids[NIDS_MAX];
52 /* FIXME: This should probably become more elegant than a global linked list */
53 static struct list_head g_uuid_list;
54 static spinlock_t g_uuid_lock;
56 void class_init_uuidlist(void)
58 INIT_LIST_HEAD(&g_uuid_list);
59 spin_lock_init(&g_uuid_lock);
62 void class_exit_uuidlist(void)
68 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index)
70 struct uuid_nid_data *data;
74 obd_str2uuid(&tmp, uuid);
75 spin_lock(&g_uuid_lock);
76 list_for_each_entry(data, &g_uuid_list, un_list) {
77 if (obd_uuid_equals(&data->un_uuid, &tmp)) {
78 if (index >= data->un_nid_count)
82 *peer_nid = data->un_nids[index];
86 spin_unlock(&g_uuid_lock);
89 EXPORT_SYMBOL(lustre_uuid_to_peer);
91 /* Add a nid to a niduuid. Multiple nids can be added to a single uuid;
92 LNET will choose the best one. */
93 int class_add_uuid(const char *uuid, __u64 nid)
95 struct uuid_nid_data *data, *entry;
98 LASSERT(nid != 0); /* valid newconfig NID is never zero */
100 if (strlen(uuid) > UUID_MAX - 1)
107 obd_str2uuid(&data->un_uuid, uuid);
108 data->un_nids[0] = nid;
109 data->un_nid_count = 1;
111 spin_lock(&g_uuid_lock);
112 list_for_each_entry(entry, &g_uuid_list, un_list) {
113 if (obd_uuid_equals(&entry->un_uuid, &data->un_uuid)) {
117 for (i = 0; i < entry->un_nid_count; i++)
118 if (nid == entry->un_nids[i])
121 if (i == entry->un_nid_count) {
122 LASSERT(entry->un_nid_count < NIDS_MAX);
123 entry->un_nids[entry->un_nid_count++] = nid;
129 list_add(&data->un_list, &g_uuid_list);
130 spin_unlock(&g_uuid_lock);
133 CDEBUG(D_INFO, "found uuid %s %s cnt=%d\n", uuid,
134 libcfs_nid2str(nid), entry->un_nid_count);
135 OBD_FREE(data, sizeof(*data));
137 CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid));
142 /* Delete the nids for one uuid if specified, otherwise delete all */
143 int class_del_uuid(const char *uuid)
145 struct uuid_nid_data *data;
146 struct list_head deathrow;
148 INIT_LIST_HEAD(&deathrow);
150 spin_lock(&g_uuid_lock);
154 obd_str2uuid(&tmp, uuid);
155 list_for_each_entry(data, &g_uuid_list, un_list) {
156 if (obd_uuid_equals(&data->un_uuid, &tmp)) {
157 list_move(&data->un_list, &deathrow);
162 list_splice_init(&g_uuid_list, &deathrow);
163 spin_unlock(&g_uuid_lock);
165 if (uuid != NULL && list_empty(&deathrow)) {
166 CDEBUG(D_INFO, "Try to delete a non-existent uuid %s\n", uuid);
170 while (!list_empty(&deathrow)) {
171 data = list_entry(deathrow.next, struct uuid_nid_data,
173 list_del(&data->un_list);
175 CDEBUG(D_INFO, "del uuid %s %s/%d\n",
176 obd_uuid2str(&data->un_uuid),
177 libcfs_nid2str(data->un_nids[0]),
180 OBD_FREE(data, sizeof(*data));
185 /* check if @nid exists in nid list of @uuid */
186 int class_check_uuid(struct obd_uuid *uuid, __u64 nid)
188 struct uuid_nid_data *entry;
192 CDEBUG(D_INFO, "check if uuid %s has %s.\n",
193 obd_uuid2str(uuid), libcfs_nid2str(nid));
195 spin_lock(&g_uuid_lock);
196 list_for_each_entry(entry, &g_uuid_list, un_list) {
199 if (!obd_uuid_equals(&entry->un_uuid, uuid))
202 /* found the uuid, check if it has @nid */
203 for (i = 0; i < entry->un_nid_count; i++) {
204 if (entry->un_nids[i] == nid) {
211 spin_unlock(&g_uuid_lock);
214 EXPORT_SYMBOL(class_check_uuid);