Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[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  2008 Sun Microsystems, Inc. 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 struct uuid_nid_data {
51         struct list_head un_list;
52         lnet_nid_t       un_nid;
53         char            *un_uuid;
54         int              un_count;  /* nid/uuid pair refcount */
55 };
56
57 /* FIXME: This should probably become more elegant than a global linked list */
58 static struct list_head g_uuid_list;
59 static spinlock_t       g_uuid_lock;
60
61 void class_init_uuidlist(void)
62 {
63         CFS_INIT_LIST_HEAD(&g_uuid_list);
64         spin_lock_init(&g_uuid_lock);
65 }
66
67 void class_exit_uuidlist(void)
68 {
69         /* delete all */
70         class_del_uuid(NULL);
71 }
72
73 int lustre_uuid_to_peer(char *uuid, lnet_nid_t *peer_nid, int index)
74 {
75         struct list_head *tmp;
76
77         spin_lock (&g_uuid_lock);
78
79         list_for_each(tmp, &g_uuid_list) {
80                 struct uuid_nid_data *data =
81                         list_entry(tmp, struct uuid_nid_data, un_list);
82
83                 if (!strcmp(data->un_uuid, uuid) &&
84                     index-- == 0) {
85                         *peer_nid = data->un_nid;
86
87                         spin_unlock (&g_uuid_lock);
88                         return 0;
89                 }
90         }
91
92         spin_unlock (&g_uuid_lock);
93         return -ENOENT;
94 }
95
96 /* Add a nid to a niduuid.  Multiple nids can be added to a single uuid; 
97    LNET will choose the best one. */
98 int class_add_uuid(char *uuid, __u64 nid)
99 {
100         struct uuid_nid_data *data, *entry;
101         int nob = strnlen (uuid, CFS_PAGE_SIZE) + 1;
102         int found = 0;
103
104         LASSERT(nid != 0);  /* valid newconfig NID is never zero */
105
106         if (nob > CFS_PAGE_SIZE)
107                 return -EINVAL;
108
109         OBD_ALLOC(data, sizeof(*data));
110         if (data == NULL)
111                 return -ENOMEM;
112
113         OBD_ALLOC(data->un_uuid, nob);
114         if (data == NULL) {
115                 OBD_FREE(data, sizeof(*data));
116                 return -ENOMEM;
117         }
118
119         memcpy(data->un_uuid, uuid, nob);
120         data->un_nid = nid;
121         data->un_count = 1;
122
123         spin_lock (&g_uuid_lock);
124         list_for_each_entry(entry, &g_uuid_list, un_list) {
125                 if (entry->un_nid == nid && 
126                     (strcmp(entry->un_uuid, uuid) == 0)) {
127                         found++;
128                         entry->un_count++;
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_count);
139                 OBD_FREE(data->un_uuid, nob);
140                 OBD_FREE(data, sizeof(*data));
141         } else {
142                 CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid));
143         }
144         return 0;
145 }
146
147 /* Delete the nids for one uuid if specified, otherwise delete all */
148 int class_del_uuid (char *uuid)
149 {
150         struct list_head  deathrow;
151         struct uuid_nid_data *data;
152         int found = 0;
153
154         CFS_INIT_LIST_HEAD (&deathrow);
155
156         spin_lock (&g_uuid_lock);
157         if (uuid == NULL) {
158                 list_splice_init(&g_uuid_list, &deathrow);
159                 found = 1;
160         } else {
161                 list_for_each_entry(data, &g_uuid_list, un_list) {
162                         if (strcmp(data->un_uuid, uuid))
163                                 continue;
164                         --data->un_count;
165                         LASSERT(data->un_count >= 0);
166                         if (data->un_count == 0)
167                                 list_move(&data->un_list, &deathrow);
168                         found = 1;
169                         break;
170                 }
171         }
172         spin_unlock (&g_uuid_lock);
173
174         if (!found) {
175                 if (uuid)
176                         CERROR("Try to delete a non-existent uuid %s\n", uuid);
177                 return -EINVAL;
178         }
179
180         while (!list_empty(&deathrow)) {
181                 data = list_entry(deathrow.next, struct uuid_nid_data, un_list);
182                 list_del(&data->un_list);
183
184                 CDEBUG(D_INFO, "del uuid %s %s\n", data->un_uuid,
185                        libcfs_nid2str(data->un_nid));
186
187                 OBD_FREE(data->un_uuid, strlen(data->un_uuid) + 1);
188                 OBD_FREE(data, sizeof(*data));
189         }
190
191         return 0;
192 }