Whamcloud - gitweb
land 0.5.20.3 b_devel onto HEAD (b_devel will remain)
[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         ptl_handle_ni_t ni;
46 };
47
48 /* FIXME: This should probably become more elegant than a global linked list */
49 static struct list_head g_uuid_list;
50 static spinlock_t       g_uuid_lock;
51
52 void class_init_uuidlist(void)
53 {
54         INIT_LIST_HEAD(&g_uuid_list);
55         spin_lock_init(&g_uuid_lock);
56 }
57
58 void class_exit_uuidlist(void)
59 {
60         struct list_head *tmp, *n;
61
62         /* Module going => sole user => don't need to lock g_uuid_list */
63         list_for_each_safe(tmp, n, &g_uuid_list) {
64                 struct uuid_nid_data *data =
65                         list_entry(tmp, struct uuid_nid_data, head);
66
67                 PORTAL_FREE(data->uuid, strlen(data->uuid) + 1);
68                 PORTAL_FREE(data, sizeof(*data));
69         }
70 }
71
72 int lustre_uuid_to_peer(char *uuid, struct lustre_peer *peer)
73 {
74         struct list_head *tmp;
75
76         spin_lock (&g_uuid_lock);
77
78         list_for_each(tmp, &g_uuid_list) {
79                 struct uuid_nid_data *data =
80                         list_entry(tmp, struct uuid_nid_data, head);
81
82                 if (strcmp(data->uuid, uuid) == 0) {
83                         peer->peer_nid = data->nid;
84                         peer->peer_ni = data->ni;
85
86                         spin_unlock (&g_uuid_lock);
87                         return 0;
88                 }
89         }
90
91         spin_unlock (&g_uuid_lock);
92         return -1;
93 }
94
95 int class_add_uuid(char *uuid, __u64 nid, __u32 nal)
96 {
97         const ptl_handle_ni_t *nip;
98         struct uuid_nid_data *data;
99         int rc;
100         int nob = strnlen (uuid, PAGE_SIZE) + 1;
101
102         if (nob > PAGE_SIZE)
103                 return -EINVAL;
104
105         nip = kportal_get_ni (nal);
106         if (nip == NULL) {
107                 CERROR("get_ni failed: is the NAL module loaded?\n");
108                 return -EIO;
109         }
110
111         rc = -ENOMEM;
112         PORTAL_ALLOC(data, sizeof(*data));
113         if (data == NULL)
114                 goto fail_0;
115
116         PORTAL_ALLOC(data->uuid, nob);
117         if (data == NULL)
118                 goto fail_1;
119
120         memcpy(data->uuid, uuid, nob);
121         data->nid = nid;
122         data->nal = nal;
123         data->ni  = *nip;
124
125         spin_lock (&g_uuid_lock);
126
127         list_add(&data->head, &g_uuid_list);
128
129         spin_unlock (&g_uuid_lock);
130
131         return 0;
132
133  fail_1:
134         PORTAL_FREE (data, sizeof (*data));
135  fail_0:
136         kportal_put_ni (nal);
137         return (rc);
138 }
139
140 /* delete only one entry if uuid is specified, otherwise delete all */
141 int class_del_uuid (char *uuid)
142 {
143         struct list_head  deathrow;
144         struct list_head *tmp;
145         struct list_head *n;
146         struct uuid_nid_data *data;
147
148         INIT_LIST_HEAD (&deathrow);
149
150         spin_lock (&g_uuid_lock);
151
152         list_for_each_safe(tmp, n, &g_uuid_list) {
153                 data = list_entry(tmp, struct uuid_nid_data, head);
154
155                 if (uuid == NULL || strcmp(data->uuid, uuid) == 0) {
156                         list_del (&data->head);
157                         list_add (&data->head, &deathrow);
158                         if (uuid)
159                                 break;
160                 }
161         }
162
163         spin_unlock (&g_uuid_lock);
164
165         if (list_empty (&deathrow))
166                 return -EINVAL;
167
168         do {
169                 data = list_entry(deathrow.next, struct uuid_nid_data, head);
170
171                 list_del (&data->head);
172
173                 kportal_put_ni (data->nal);
174                 PORTAL_FREE(data->uuid, strlen(data->uuid) + 1);
175                 PORTAL_FREE(data, sizeof(*data));
176         } while (!list_empty (&deathrow));
177
178         return 0;
179 }