Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / ptlrpc / connection.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 [sun.com URL with a
20  * copy of GPLv2].
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 #ifdef __KERNEL__
39 #include <obd_support.h>
40 #include <obd_class.h>
41 #include <lustre_net.h>
42 #else
43 #include <liblustre.h>
44 #endif
45
46 #include "ptlrpc_internal.h"
47 #include <class_hash.h>
48
49 static spinlock_t conn_lock;
50 static struct list_head conn_list;
51 static struct list_head conn_unused_list;
52 static struct lustre_class_hash_body *conn_hash_body;
53 static struct lustre_class_hash_body *conn_unused_hash_body;
54
55 extern struct lustre_hash_operations conn_hash_operations;
56
57 void ptlrpc_dump_connections(void)
58 {
59         struct list_head *tmp;
60         struct ptlrpc_connection *c;
61         ENTRY;
62
63         list_for_each(tmp, &conn_list) {
64                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
65                 CERROR("Connection %p/%s has refcount %d (nid=%s->%s)\n",
66                        c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
67                        libcfs_nid2str(c->c_self), 
68                        libcfs_nid2str(c->c_peer.nid));
69         }
70         EXIT;
71 }
72
73 struct ptlrpc_connection*
74 ptlrpc_lookup_conn_locked (lnet_process_id_t peer)
75 {
76         struct ptlrpc_connection *c;
77
78         c = lustre_hash_get_object_by_key(conn_hash_body, &peer);
79         if (c != NULL)
80                 return c;
81
82         c = lustre_hash_get_object_by_key(conn_unused_hash_body, &peer);
83         if (c != NULL)
84                 return c;
85
86         return NULL;
87 }
88
89
90 struct ptlrpc_connection *ptlrpc_get_connection(lnet_process_id_t peer,
91                                                 lnet_nid_t self, struct obd_uuid *uuid)
92 {
93         struct ptlrpc_connection *c;
94         struct ptlrpc_connection *c2;
95         int rc = 0;
96         ENTRY;
97
98         CDEBUG(D_INFO, "self %s peer %s\n", 
99                libcfs_nid2str(self), libcfs_id2str(peer));
100
101         spin_lock(&conn_lock);
102
103         c = ptlrpc_lookup_conn_locked(peer);
104         
105         spin_unlock(&conn_lock);
106
107         if (c != NULL)
108                 RETURN (c);
109         
110         OBD_ALLOC(c, sizeof(*c));
111         if (c == NULL)
112                 RETURN (NULL);
113
114         atomic_set(&c->c_refcount, 1);
115         c->c_peer = peer;
116         c->c_self = self;
117         INIT_HLIST_NODE(&c->c_hash);
118         CFS_INIT_LIST_HEAD(&c->c_link);
119         if (uuid != NULL)
120                 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
121
122         spin_lock(&conn_lock);
123
124         c2 = ptlrpc_lookup_conn_locked(peer);
125         if (c2 == NULL) {
126                 list_add(&c->c_link, &conn_list);
127                 rc = lustre_hash_additem_unique(conn_hash_body, &peer, 
128                                                 &c->c_hash);
129                 if (rc != 0) {
130                         list_del(&c->c_link);
131                         CERROR("Cannot add connection to conn_hash_body\n");
132                         goto out_conn;
133                 }
134         }
135
136 out_conn:
137
138         spin_unlock(&conn_lock);
139
140         if (c2 == NULL && rc == 0)
141                 RETURN (c);
142
143         if (c != NULL) 
144                 OBD_FREE(c, sizeof(*c));
145         RETURN (c2);
146 }
147
148 int ptlrpc_put_connection(struct ptlrpc_connection *c)
149 {
150         int rc = 0;
151         ENTRY;
152
153         if (c == NULL) {
154                 CERROR("NULL connection\n");
155                 RETURN(0);
156         }
157
158         CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
159                 c, atomic_read(&c->c_refcount) - 1, 
160                 libcfs_nid2str(c->c_peer.nid));
161
162         spin_lock(&conn_lock);
163         LASSERT(!hlist_unhashed(&c->c_hash));
164         spin_unlock(&conn_lock);
165
166         if (atomic_dec_return(&c->c_refcount) == 1) {
167
168                 spin_lock(&conn_lock);
169
170                 lustre_hash_delitem(conn_hash_body, &c->c_peer, &c->c_hash);
171                 list_del(&c->c_link);
172
173                 list_add(&c->c_link, &conn_unused_list);
174                 rc = lustre_hash_additem_unique(conn_unused_hash_body, &c->c_peer, 
175                                                 &c->c_hash);
176                 if (rc != 0) {
177                         spin_unlock(&conn_lock);
178                         CERROR("Cannot hash connection to conn_hash_body\n");
179                         GOTO(ret, rc);
180                 }
181
182                 spin_unlock(&conn_lock);
183                 rc = 1;
184  
185         } 
186
187         if (atomic_read(&c->c_refcount) < 0)
188                 CERROR("connection %p refcount %d!\n",
189                        c, atomic_read(&c->c_refcount));
190 ret :
191
192         RETURN(rc);
193 }
194
195 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
196 {
197         ENTRY;
198         atomic_inc(&c->c_refcount);
199         CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
200                 c, atomic_read(&c->c_refcount),
201                 libcfs_nid2str(c->c_peer.nid));
202         RETURN(c);
203 }
204
205 int ptlrpc_init_connection(void)
206 {
207         int rc = 0;
208         CFS_INIT_LIST_HEAD(&conn_list);
209         rc = lustre_hash_init(&conn_hash_body, "CONN_HASH", 
210                               128, &conn_hash_operations);
211         if (rc)
212                 GOTO(ret, rc);
213
214         CFS_INIT_LIST_HEAD(&conn_unused_list);
215         rc = lustre_hash_init(&conn_unused_hash_body, "CONN_UNUSED_HASH", 
216                               128, &conn_hash_operations);
217         if (rc)
218                 GOTO(ret, rc);
219
220         spin_lock_init(&conn_lock);
221 ret:
222         if (rc) {
223                 lustre_hash_exit(&conn_hash_body);
224                 lustre_hash_exit(&conn_unused_hash_body);
225         }
226         RETURN(rc);
227 }
228
229 void ptlrpc_cleanup_connection(void)
230 {
231         struct list_head *tmp, *pos;
232         struct ptlrpc_connection *c;
233
234         spin_lock(&conn_lock);
235
236         lustre_hash_exit(&conn_unused_hash_body);
237         list_for_each_safe(tmp, pos, &conn_unused_list) {
238                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
239                 list_del(&c->c_link);
240                 OBD_FREE(c, sizeof(*c));
241         }
242
243         lustre_hash_exit(&conn_hash_body);
244         list_for_each_safe(tmp, pos, &conn_list) {
245                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
246                 CERROR("Connection %p/%s has refcount %d (nid=%s)\n",
247                        c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
248                        libcfs_nid2str(c->c_peer.nid));
249                 list_del(&c->c_link);
250                 OBD_FREE(c, sizeof(*c));
251         }
252         spin_unlock(&conn_lock);
253 }