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