Whamcloud - gitweb
Revert "LU-8130 ptlrpc: convert conn_hash to rhashtable"
[fs/lustre-release.git] / lustre / ptlrpc / connection.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_RPC
34 #include <obd_support.h>
35 #include <obd_class.h>
36 #include <lustre_net.h>
37
38 #include "ptlrpc_internal.h"
39
40 static struct cfs_hash *conn_hash;
41 static struct cfs_hash_ops conn_hash_ops;
42
43 struct ptlrpc_connection *
44 ptlrpc_connection_get(struct lnet_process_id peer, lnet_nid_t self,
45                       struct obd_uuid *uuid)
46 {
47         struct ptlrpc_connection *conn, *conn2;
48         ENTRY;
49
50         peer.nid = LNetPrimaryNID(peer.nid);
51         conn = cfs_hash_lookup(conn_hash, &peer);
52         if (conn)
53                 GOTO(out, conn);
54
55         OBD_ALLOC_PTR(conn);
56         if (!conn)
57                 RETURN(NULL);
58
59         conn->c_peer = peer;
60         conn->c_self = self;
61         INIT_HLIST_NODE(&conn->c_hash);
62         atomic_set(&conn->c_refcount, 1);
63         if (uuid)
64                 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
65
66         /*
67          * Add the newly created conn to the hash, on key collision we
68          * lost a racing addition and must destroy our newly allocated
69          * connection.  The object which exists in the hash will be
70          * returned and may be compared against out object.
71          */
72         /* In the function below, .hs_keycmp resolves to
73          * conn_keycmp() */
74         /* coverity[overrun-buffer-val] */
75         conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
76         if (conn != conn2) {
77                 OBD_FREE_PTR(conn);
78                 conn = conn2;
79         }
80         EXIT;
81 out:
82         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
83                conn, atomic_read(&conn->c_refcount),
84                libcfs_nid2str(conn->c_peer.nid));
85         return conn;
86 }
87
88 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
89 {
90         int rc = 0;
91         ENTRY;
92
93         if (!conn)
94                 RETURN(rc);
95
96         LASSERT(atomic_read(&conn->c_refcount) > 1);
97
98         /*
99          * We do not remove connection from hashtable and
100          * do not free it even if last caller released ref,
101          * as we want to have it cached for the case it is
102          * needed again.
103          *
104          * Deallocating it and later creating new connection
105          * again would be wastful. This way we also avoid
106          * expensive locking to protect things from get/put
107          * race when found cached connection is freed by
108          * ptlrpc_connection_put().
109          *
110          * It will be freed later in module unload time,
111          * when ptlrpc_connection_fini()->lh_exit->conn_exit()
112          * path is called.
113          */
114         if (atomic_dec_return(&conn->c_refcount) == 1)
115                 rc = 1;
116
117         CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
118                conn, atomic_read(&conn->c_refcount),
119                libcfs_nid2str(conn->c_peer.nid));
120
121         RETURN(rc);
122 }
123
124 struct ptlrpc_connection *
125 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
126 {
127         ENTRY;
128
129         atomic_inc(&conn->c_refcount);
130         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
131                conn, atomic_read(&conn->c_refcount),
132                libcfs_nid2str(conn->c_peer.nid));
133
134         RETURN(conn);
135 }
136
137 int ptlrpc_connection_init(void)
138 {
139         ENTRY;
140
141         conn_hash = cfs_hash_create("CONN_HASH",
142                                     HASH_CONN_CUR_BITS,
143                                     HASH_CONN_MAX_BITS,
144                                     HASH_CONN_BKT_BITS, 0,
145                                     CFS_HASH_MIN_THETA,
146                                     CFS_HASH_MAX_THETA,
147                                     &conn_hash_ops, CFS_HASH_DEFAULT);
148         if (!conn_hash)
149                 RETURN(-ENOMEM);
150
151         RETURN(0);
152 }
153
154 void ptlrpc_connection_fini(void) {
155         ENTRY;
156         cfs_hash_putref(conn_hash);
157         EXIT;
158 }
159
160 /*
161  * Hash operations for net_peer<->connection
162  */
163 static unsigned
164 conn_hashfn(struct cfs_hash *hs, const void *key, unsigned mask)
165 {
166         return cfs_hash_djb2_hash(key, sizeof(struct lnet_process_id), mask);
167 }
168
169 static int
170 conn_keycmp(const void *key, struct hlist_node *hnode)
171 {
172         struct ptlrpc_connection *conn;
173         const struct lnet_process_id *conn_key;
174
175         LASSERT(key != NULL);
176         conn_key = (struct lnet_process_id *)key;
177         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
178
179         return conn_key->nid == conn->c_peer.nid &&
180                conn_key->pid == conn->c_peer.pid;
181 }
182
183 static void *
184 conn_key(struct hlist_node *hnode)
185 {
186         struct ptlrpc_connection *conn;
187         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
188         return &conn->c_peer;
189 }
190
191 static void *
192 conn_object(struct hlist_node *hnode)
193 {
194         return hlist_entry(hnode, struct ptlrpc_connection, c_hash);
195 }
196
197 static void
198 conn_get(struct cfs_hash *hs, struct hlist_node *hnode)
199 {
200         struct ptlrpc_connection *conn;
201
202         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
203         atomic_inc(&conn->c_refcount);
204 }
205
206 static void
207 conn_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
208 {
209         struct ptlrpc_connection *conn;
210
211         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
212         atomic_dec(&conn->c_refcount);
213 }
214
215 static void
216 conn_exit(struct cfs_hash *hs, 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 struct cfs_hash_ops conn_hash_ops = {
233         .hs_hash        = conn_hashfn,
234         .hs_keycmp      = conn_keycmp,
235         .hs_key         = conn_key,
236         .hs_object      = conn_object,
237         .hs_get         = conn_get,
238         .hs_put_locked  = conn_put_locked,
239         .hs_exit        = conn_exit,
240 };