Whamcloud - gitweb
Branch HEAD
[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 lustre_hash_t *conn_hash = NULL;
50 static lustre_hash_ops_t conn_hash_ops;
51
52 struct ptlrpc_connection *
53 ptlrpc_connection_get(lnet_process_id_t peer, lnet_nid_t self,
54                       struct obd_uuid *uuid)
55 {
56         struct ptlrpc_connection *conn, *conn2;
57         ENTRY;
58
59         conn = lustre_hash_lookup(conn_hash, &peer);
60         if (conn)
61                 GOTO(out, conn);
62
63         OBD_ALLOC_PTR(conn);
64         if (!conn)
65                 RETURN(NULL);
66
67         conn->c_peer = peer;
68         conn->c_self = self;
69         INIT_HLIST_NODE(&conn->c_hash);
70         atomic_set(&conn->c_refcount, 1);
71         if (uuid)
72                 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
73
74         /*
75          * Add the newly created conn to the hash, on key collision we
76          * lost a racing addition and must destroy our newly allocated
77          * connection.  The object which exists in the has will be
78          * returned and may be compared against out object.
79          */
80         conn2 = lustre_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
81         if (conn != conn2) {
82                 OBD_FREE_PTR(conn);
83                 conn = conn2;
84         }
85         EXIT;
86 out:
87         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
88                conn, atomic_read(&conn->c_refcount),
89                libcfs_nid2str(conn->c_peer.nid));
90         return conn;
91 }
92
93 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
94 {
95         int rc = 0;
96         ENTRY;
97
98         if (!conn)
99                 RETURN(rc);
100
101         LASSERT(!hlist_unhashed(&conn->c_hash));
102
103         /*
104          * We do not remove connection from hashtable and
105          * do not free it even if last caller released ref,
106          * as we want to have it cached for the case it is
107          * needed again.
108          *
109          * Deallocating it and later creating new connection
110          * again would be wastful. This way we also avoid
111          * expensive locking to protect things from get/put
112          * race when found cached connection is freed by
113          * ptlrpc_connection_put().
114          *
115          * It will be freed later in module unload time,
116          * when ptlrpc_connection_fini()->lh_exit->conn_exit()
117          * path is called.
118          */
119         if (atomic_dec_return(&conn->c_refcount) == 1)
120                 rc = 1;
121
122         CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
123                conn, atomic_read(&conn->c_refcount),
124                libcfs_nid2str(conn->c_peer.nid));
125
126         RETURN(rc);
127 }
128
129 struct ptlrpc_connection *
130 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
131 {
132         ENTRY;
133
134         atomic_inc(&conn->c_refcount);
135         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
136                conn, atomic_read(&conn->c_refcount),
137                libcfs_nid2str(conn->c_peer.nid));
138
139         RETURN(conn);
140 }
141
142 int ptlrpc_connection_init(void)
143 {
144         ENTRY;
145
146         conn_hash = lustre_hash_init("CONN_HASH",
147                                      HASH_CONN_CUR_BITS,
148                                      HASH_CONN_MAX_BITS,
149                                      &conn_hash_ops, LH_REHASH);
150         if (!conn_hash)
151                 RETURN(-ENOMEM);
152
153         RETURN(0);
154 }
155
156 void ptlrpc_connection_fini(void) {
157         ENTRY;
158         lustre_hash_exit(conn_hash);
159         EXIT;
160 }
161
162 /*
163  * Hash operations for net_peer<->connection
164  */
165 static unsigned
166 conn_hashfn(lustre_hash_t *lh,  void *key, unsigned mask)
167 {
168         return lh_djb2_hash(key, sizeof(lnet_process_id_t), mask);
169 }
170
171 static int
172 conn_compare(void *key, struct hlist_node *hnode)
173 {
174         struct ptlrpc_connection *conn;
175         lnet_process_id_t *conn_key;
176
177         LASSERT(key != NULL);
178         conn_key = (lnet_process_id_t*)key;
179         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
180
181         return conn_key->nid == conn->c_peer.nid &&
182                conn_key->pid == conn->c_peer.pid;
183 }
184
185 static void *
186 conn_key(struct hlist_node *hnode)
187 {
188         struct ptlrpc_connection *conn;
189         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
190         return &conn->c_peer;
191 }
192
193 static void *
194 conn_get(struct hlist_node *hnode)
195 {
196         struct ptlrpc_connection *conn;
197
198         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
199         atomic_inc(&conn->c_refcount);
200
201         return conn;
202 }
203
204 static void *
205 conn_put(struct hlist_node *hnode)
206 {
207         struct ptlrpc_connection *conn;
208
209         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
210         atomic_dec(&conn->c_refcount);
211
212         return conn;
213 }
214
215 static void
216 conn_exit(struct hlist_node *hnode)
217 {
218         struct ptlrpc_connection *conn;
219
220         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
221         /*
222          * Nothing should be left. Connection user put it and
223          * connection also was deleted from table by this time
224          * so we should have 0 refs.
225          */
226         LASSERTF(atomic_read(&conn->c_refcount) == 0,
227                  "Busy connection with %d refs\n",
228                  atomic_read(&conn->c_refcount));
229         OBD_FREE_PTR(conn);
230 }
231
232 static lustre_hash_ops_t conn_hash_ops = {
233         .lh_hash    = conn_hashfn,
234         .lh_compare = conn_compare,
235         .lh_key     = conn_key,
236         .lh_get     = conn_get,
237         .lh_put     = conn_put,
238         .lh_exit    = conn_exit,
239 };