Whamcloud - gitweb
* Landed b_cray_portals_merge.
[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 Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24
25 #ifdef __KERNEL__
26 # include <linux/module.h>
27 # include <linux/init.h>
28 # include <linux/list.h>
29 #else
30 # include <liblustre.h>
31 #endif
32 #include <linux/obd.h>
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/lustre_lib.h>
36 #include <linux/lustre_ha.h>
37 #include <linux/lustre_net.h>
38 #include <linux/lprocfs_status.h>
39
40 struct uuid_nid_data {
41         struct list_head head;
42         ptl_nid_t nid;
43         char *uuid;
44         __u32 nal;
45 };
46
47 /* FIXME: This should probably become more elegant than a global linked list */
48 static struct list_head g_uuid_list;
49 static spinlock_t       g_uuid_lock;
50
51 void class_init_uuidlist(void)
52 {
53         INIT_LIST_HEAD(&g_uuid_list);
54         spin_lock_init(&g_uuid_lock);
55 }
56
57 void class_exit_uuidlist(void)
58 {
59         /* delete all */
60         class_del_uuid(NULL);
61 }
62
63 int lustre_uuid_to_peer(char *uuid, __u32 *peer_nal, ptl_nid_t *peer_nid)
64 {
65         struct list_head *tmp;
66
67         spin_lock (&g_uuid_lock);
68
69         list_for_each(tmp, &g_uuid_list) {
70                 struct uuid_nid_data *data =
71                         list_entry(tmp, struct uuid_nid_data, head);
72
73                 if (strcmp(data->uuid, uuid) == 0) {
74                         *peer_nid = data->nid;
75                         *peer_nal = data->nal;
76
77                         spin_unlock (&g_uuid_lock);
78                         return 0;
79                 }
80         }
81
82         spin_unlock (&g_uuid_lock);
83         return -1;
84 }
85
86 int class_add_uuid(char *uuid, __u64 nid, __u32 nal)
87 {
88         struct uuid_nid_data *data;
89         int rc;
90         int nob = strnlen (uuid, PAGE_SIZE) + 1;
91
92         if (nob > PAGE_SIZE)
93                 return -EINVAL;
94
95         rc = -ENOMEM;
96         OBD_ALLOC(data, sizeof(*data));
97         if (data == NULL)
98                 return -ENOMEM;
99
100         OBD_ALLOC(data->uuid, nob);
101         if (data == NULL) {
102                 OBD_FREE(data, sizeof(*data));
103                 return -ENOMEM;
104         }
105
106         CDEBUG(D_INFO, "add uuid %s "LPX64" %u\n", uuid, nid, nal);
107         memcpy(data->uuid, uuid, nob);
108         data->nid = nid;
109         data->nal = nal;
110
111         spin_lock (&g_uuid_lock);
112
113         list_add(&data->head, &g_uuid_list);
114
115         spin_unlock (&g_uuid_lock);
116
117         return 0;
118 }
119
120 /* delete only one entry if uuid is specified, otherwise delete all */
121 int class_del_uuid (char *uuid)
122 {
123         struct list_head  deathrow;
124         struct list_head *tmp;
125         struct list_head *n;
126         struct uuid_nid_data *data;
127
128         INIT_LIST_HEAD (&deathrow);
129
130         spin_lock (&g_uuid_lock);
131
132         list_for_each_safe(tmp, n, &g_uuid_list) {
133                 data = list_entry(tmp, struct uuid_nid_data, head);
134
135                 if (uuid == NULL || strcmp(data->uuid, uuid) == 0) {
136                         list_del (&data->head);
137                         list_add (&data->head, &deathrow);
138                         if (uuid)
139                                 break;
140                 }
141         }
142
143         spin_unlock (&g_uuid_lock);
144
145         if (list_empty (&deathrow))
146                 return -EINVAL;
147
148         do {
149                 data = list_entry(deathrow.next, struct uuid_nid_data, head);
150
151                 list_del (&data->head);
152
153                 OBD_FREE(data->uuid, strlen(data->uuid) + 1);
154                 OBD_FREE(data, sizeof(*data));
155         } while (!list_empty (&deathrow));
156
157         return 0;
158 }