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