Whamcloud - gitweb
LU-1146 build: batch update copyright 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 (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  */
38
39 #define DEBUG_SUBSYSTEM S_RPC
40 #ifdef __KERNEL__
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_net.h>
44 #else
45 #include <liblustre.h>
46 #endif
47
48 #include "ptlrpc_internal.h"
49
50 static cfs_hash_t *conn_hash = NULL;
51 static cfs_hash_ops_t conn_hash_ops;
52
53 struct ptlrpc_connection *
54 ptlrpc_connection_get(lnet_process_id_t peer, lnet_nid_t self,
55                       struct obd_uuid *uuid)
56 {
57         struct ptlrpc_connection *conn, *conn2;
58         ENTRY;
59
60         conn = cfs_hash_lookup(conn_hash, &peer);
61         if (conn)
62                 GOTO(out, conn);
63
64         OBD_ALLOC_PTR(conn);
65         if (!conn)
66                 RETURN(NULL);
67
68         conn->c_peer = peer;
69         conn->c_self = self;
70         CFS_INIT_HLIST_NODE(&conn->c_hash);
71         cfs_atomic_set(&conn->c_refcount, 1);
72         if (uuid)
73                 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
74
75         /*
76          * Add the newly created conn to the hash, on key collision we
77          * lost a racing addition and must destroy our newly allocated
78          * connection.  The object which exists in the has will be
79          * returned and may be compared against out object.
80          */
81         conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
82         if (conn != conn2) {
83                 OBD_FREE_PTR(conn);
84                 conn = conn2;
85         }
86         EXIT;
87 out:
88         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
89                conn, cfs_atomic_read(&conn->c_refcount),
90                libcfs_nid2str(conn->c_peer.nid));
91         return conn;
92 }
93
94 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
95 {
96         int rc = 0;
97         ENTRY;
98
99         if (!conn)
100                 RETURN(rc);
101
102         LASSERT(cfs_atomic_read(&conn->c_refcount) > 1);
103
104         /*
105          * We do not remove connection from hashtable and
106          * do not free it even if last caller released ref,
107          * as we want to have it cached for the case it is
108          * needed again.
109          *
110          * Deallocating it and later creating new connection
111          * again would be wastful. This way we also avoid
112          * expensive locking to protect things from get/put
113          * race when found cached connection is freed by
114          * ptlrpc_connection_put().
115          *
116          * It will be freed later in module unload time,
117          * when ptlrpc_connection_fini()->lh_exit->conn_exit()
118          * path is called.
119          */
120         if (cfs_atomic_dec_return(&conn->c_refcount) == 1)
121                 rc = 1;
122
123         CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
124                conn, cfs_atomic_read(&conn->c_refcount),
125                libcfs_nid2str(conn->c_peer.nid));
126
127         RETURN(rc);
128 }
129
130 struct ptlrpc_connection *
131 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
132 {
133         ENTRY;
134
135         cfs_atomic_inc(&conn->c_refcount);
136         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
137                conn, cfs_atomic_read(&conn->c_refcount),
138                libcfs_nid2str(conn->c_peer.nid));
139
140         RETURN(conn);
141 }
142
143 int ptlrpc_connection_init(void)
144 {
145         ENTRY;
146
147         conn_hash = cfs_hash_create("CONN_HASH",
148                                     HASH_CONN_CUR_BITS,
149                                     HASH_CONN_MAX_BITS,
150                                     HASH_CONN_BKT_BITS, 0,
151                                     CFS_HASH_MIN_THETA,
152                                     CFS_HASH_MAX_THETA,
153                                     &conn_hash_ops, CFS_HASH_DEFAULT);
154         if (!conn_hash)
155                 RETURN(-ENOMEM);
156
157         RETURN(0);
158 }
159
160 void ptlrpc_connection_fini(void) {
161         ENTRY;
162         cfs_hash_putref(conn_hash);
163         EXIT;
164 }
165
166 /*
167  * Hash operations for net_peer<->connection
168  */
169 static unsigned
170 conn_hashfn(cfs_hash_t *hs, const void *key, unsigned mask)
171 {
172         return cfs_hash_djb2_hash(key, sizeof(lnet_process_id_t), mask);
173 }
174
175 static int
176 conn_keycmp(const void *key, cfs_hlist_node_t *hnode)
177 {
178         struct ptlrpc_connection *conn;
179         const lnet_process_id_t *conn_key;
180
181         LASSERT(key != NULL);
182         conn_key = (lnet_process_id_t*)key;
183         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
184
185         return conn_key->nid == conn->c_peer.nid &&
186                conn_key->pid == conn->c_peer.pid;
187 }
188
189 static void *
190 conn_key(cfs_hlist_node_t *hnode)
191 {
192         struct ptlrpc_connection *conn;
193         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
194         return &conn->c_peer;
195 }
196
197 static void *
198 conn_object(cfs_hlist_node_t *hnode)
199 {
200         return cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
201 }
202
203 static void
204 conn_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
205 {
206         struct ptlrpc_connection *conn;
207
208         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
209         cfs_atomic_inc(&conn->c_refcount);
210 }
211
212 static void
213 conn_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
214 {
215         struct ptlrpc_connection *conn;
216
217         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
218         cfs_atomic_dec(&conn->c_refcount);
219 }
220
221 static void
222 conn_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
223 {
224         struct ptlrpc_connection *conn;
225
226         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
227         /*
228          * Nothing should be left. Connection user put it and
229          * connection also was deleted from table by this time
230          * so we should have 0 refs.
231          */
232         LASSERTF(cfs_atomic_read(&conn->c_refcount) == 0,
233                  "Busy connection with %d refs\n",
234                  cfs_atomic_read(&conn->c_refcount));
235         OBD_FREE_PTR(conn);
236 }
237
238 static cfs_hash_ops_t conn_hash_ops = {
239         .hs_hash        = conn_hashfn,
240         .hs_keycmp      = conn_keycmp,
241         .hs_key         = conn_key,
242         .hs_object      = conn_object,
243         .hs_get         = conn_get,
244         .hs_put_locked  = conn_put_locked,
245         .hs_exit        = conn_exit,
246 };