Whamcloud - gitweb
Branch b1_4_mountconf
[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  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  */
25
26 #define DEBUG_SUBSYSTEM S_RPC
27
28 #ifdef __KERNEL__
29 # include <linux/module.h>
30 # include <linux/init.h>
31 # include <linux/list.h>
32 #else
33 # include <liblustre.h>
34 #endif
35 #include <linux/obd.h>
36 #include <linux/obd_support.h>
37 #include <linux/obd_class.h>
38 #include <linux/lustre_lib.h>
39 #include <linux/lustre_ha.h>
40 #include <linux/lustre_net.h>
41 #include <linux/lprocfs_status.h>
42
43 struct uuid_nid_data {
44         struct list_head un_list;
45         lnet_nid_t       un_nid;
46         char            *un_uuid;
47 };
48
49 /* FIXME: This should probably become more elegant than a global linked list */
50 static struct list_head g_uuid_list;
51 static spinlock_t       g_uuid_lock;
52
53 void class_init_uuidlist(void)
54 {
55         INIT_LIST_HEAD(&g_uuid_list);
56         spin_lock_init(&g_uuid_lock);
57 }
58
59 void class_exit_uuidlist(void)
60 {
61         /* delete all */
62         class_del_uuid(NULL);
63 }
64
65 int lustre_uuid_to_peer(char *uuid, lnet_nid_t *peer_nid, int index)
66 {
67         struct list_head *tmp;
68
69         spin_lock (&g_uuid_lock);
70
71         list_for_each(tmp, &g_uuid_list) {
72                 struct uuid_nid_data *data =
73                         list_entry(tmp, struct uuid_nid_data, un_list);
74
75                 if (!strcmp(data->un_uuid, uuid) &&
76                     index-- == 0) {
77                         *peer_nid = data->un_nid;
78
79                         spin_unlock (&g_uuid_lock);
80                         return 0;
81                 }
82         }
83
84         spin_unlock (&g_uuid_lock);
85         return -ENOENT;
86 }
87
88 int class_add_uuid(char *uuid, __u64 nid)
89 {
90         struct uuid_nid_data *data;
91         int rc;
92         int nob = strnlen (uuid, PAGE_SIZE) + 1;
93
94         LASSERT(nid != 0);  /* valid newconfig NID is never zero */
95
96         if (nob > PAGE_SIZE)
97                 return -EINVAL;
98
99         rc = -ENOMEM;
100         OBD_ALLOC(data, sizeof(*data));
101         if (data == NULL)
102                 return -ENOMEM;
103
104         OBD_ALLOC(data->un_uuid, nob);
105         if (data == NULL) {
106                 OBD_FREE(data, sizeof(*data));
107                 return -ENOMEM;
108         }
109
110         CDEBUG(D_INFO, "add uuid %s %s\n", uuid, libcfs_nid2str(nid));
111         memcpy(data->un_uuid, uuid, nob);
112         data->un_nid = nid;
113
114         spin_lock (&g_uuid_lock);
115
116         list_add(&data->un_list, &g_uuid_list);
117
118         spin_unlock (&g_uuid_lock);
119
120         return 0;
121 }
122
123 /* delete only one entry if uuid is specified, otherwise delete all */
124 int class_del_uuid (char *uuid)
125 {
126         struct list_head  deathrow;
127         struct list_head *tmp;
128         struct list_head *n;
129         struct uuid_nid_data *data;
130
131         INIT_LIST_HEAD (&deathrow);
132
133         spin_lock (&g_uuid_lock);
134
135         list_for_each_safe(tmp, n, &g_uuid_list) {
136                 data = list_entry(tmp, struct uuid_nid_data, un_list);
137
138                 if (uuid == NULL || strcmp(data->un_uuid, uuid) == 0) {
139                         list_del (&data->un_list);
140                         list_add (&data->un_list, &deathrow);
141                         if (uuid)
142                                 break;
143                 }
144         }
145
146         spin_unlock (&g_uuid_lock);
147
148         if (list_empty (&deathrow)) {
149                 if (uuid)
150                         CERROR("delete non-existent uuid %s\n", uuid);
151                 return -EINVAL;
152         }
153
154         do {
155                 data = list_entry(deathrow.next, struct uuid_nid_data, un_list);
156
157                 list_del (&data->un_list);
158
159                 OBD_FREE(data->un_uuid, strlen(data->un_uuid) + 1);
160                 OBD_FREE(data, sizeof(*data));
161         } while (!list_empty (&deathrow));
162
163         return 0;
164 }