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