Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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         /* delete all */
61         class_del_uuid(NULL);
62 }
63
64 int lustre_uuid_to_peer(char *uuid, 
65                         ptl_handle_ni_t *peer_ni, ptl_nid_t *peer_nid)
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, head);
74
75                 if (strcmp(data->uuid, uuid) == 0) {
76                         *peer_nid = data->nid;
77                         *peer_ni = data->ni;
78
79                         spin_unlock (&g_uuid_lock);
80                         return 0;
81                 }
82         }
83
84         spin_unlock (&g_uuid_lock);
85         return -1;
86 }
87
88 int class_add_uuid(char *uuid, __u64 nid, __u32 nal)
89 {
90         const ptl_handle_ni_t *nip;
91         struct uuid_nid_data *data;
92         int rc;
93         int nob = strnlen (uuid, PAGE_SIZE) + 1;
94
95         if (nob > PAGE_SIZE)
96                 return -EINVAL;
97
98         nip = kportal_get_ni (nal);
99         if (nip == NULL) {
100                 CERROR("get_ni failed: is the NAL module loaded?\n");
101                 return -EIO;
102         }
103
104         rc = -ENOMEM;
105         OBD_ALLOC(data, sizeof(*data));
106         if (data == NULL)
107                 goto fail_0;
108
109         OBD_ALLOC(data->uuid, nob);
110         if (data == NULL)
111                 goto fail_1;
112
113         CDEBUG(D_INFO, "add uuid %s "LPX64" %u\n", uuid, nid, nal);
114         memcpy(data->uuid, uuid, nob);
115         data->nid = nid;
116         data->nal = nal;
117         data->ni  = *nip;
118
119         spin_lock (&g_uuid_lock);
120
121         list_add(&data->head, &g_uuid_list);
122
123         spin_unlock (&g_uuid_lock);
124
125         return 0;
126
127  fail_1:
128         OBD_FREE (data, sizeof (*data));
129  fail_0:
130         kportal_put_ni (nal);
131         return (rc);
132 }
133
134 /* delete only one entry if uuid is specified, otherwise delete all */
135 int class_del_uuid (char *uuid)
136 {
137         struct list_head  deathrow;
138         struct list_head *tmp;
139         struct list_head *n;
140         struct uuid_nid_data *data;
141
142         INIT_LIST_HEAD (&deathrow);
143
144         spin_lock (&g_uuid_lock);
145
146         list_for_each_safe(tmp, n, &g_uuid_list) {
147                 data = list_entry(tmp, struct uuid_nid_data, head);
148
149                 if (uuid == NULL || strcmp(data->uuid, uuid) == 0) {
150                         list_del (&data->head);
151                         list_add (&data->head, &deathrow);
152                         if (uuid)
153                                 break;
154                 }
155         }
156
157         spin_unlock (&g_uuid_lock);
158
159         if (list_empty (&deathrow))
160                 return -EINVAL;
161
162         do {
163                 data = list_entry(deathrow.next, struct uuid_nid_data, head);
164
165                 list_del (&data->head);
166
167                 kportal_put_ni (data->nal);
168                 OBD_FREE(data->uuid, strlen(data->uuid) + 1);
169                 OBD_FREE(data, sizeof(*data));
170         } while (!list_empty (&deathrow));
171
172         return 0;
173 }