Whamcloud - gitweb
LU-1302 llog: introduce llog_open
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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 EXPORT_SYMBOL(ptlrpc_connection_get);
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(cfs_atomic_read(&conn->c_refcount) > 1);
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 (cfs_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, cfs_atomic_read(&conn->c_refcount),
124                libcfs_nid2str(conn->c_peer.nid));
125
126         RETURN(rc);
127 }
128 EXPORT_SYMBOL(ptlrpc_connection_put);
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 EXPORT_SYMBOL(ptlrpc_connection_addref);
143
144 int ptlrpc_connection_init(void)
145 {
146         ENTRY;
147
148         conn_hash = cfs_hash_create("CONN_HASH",
149                                     HASH_CONN_CUR_BITS,
150                                     HASH_CONN_MAX_BITS,
151                                     HASH_CONN_BKT_BITS, 0,
152                                     CFS_HASH_MIN_THETA,
153                                     CFS_HASH_MAX_THETA,
154                                     &conn_hash_ops, CFS_HASH_DEFAULT);
155         if (!conn_hash)
156                 RETURN(-ENOMEM);
157
158         RETURN(0);
159 }
160 EXPORT_SYMBOL(ptlrpc_connection_init);
161
162 void ptlrpc_connection_fini(void) {
163         ENTRY;
164         cfs_hash_putref(conn_hash);
165         EXIT;
166 }
167 EXPORT_SYMBOL(ptlrpc_connection_fini);
168
169 /*
170  * Hash operations for net_peer<->connection
171  */
172 static unsigned
173 conn_hashfn(cfs_hash_t *hs, const void *key, unsigned mask)
174 {
175         return cfs_hash_djb2_hash(key, sizeof(lnet_process_id_t), mask);
176 }
177
178 static int
179 conn_keycmp(const void *key, cfs_hlist_node_t *hnode)
180 {
181         struct ptlrpc_connection *conn;
182         const lnet_process_id_t *conn_key;
183
184         LASSERT(key != NULL);
185         conn_key = (lnet_process_id_t*)key;
186         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
187
188         return conn_key->nid == conn->c_peer.nid &&
189                conn_key->pid == conn->c_peer.pid;
190 }
191
192 static void *
193 conn_key(cfs_hlist_node_t *hnode)
194 {
195         struct ptlrpc_connection *conn;
196         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
197         return &conn->c_peer;
198 }
199
200 static void *
201 conn_object(cfs_hlist_node_t *hnode)
202 {
203         return cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
204 }
205
206 static void
207 conn_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
208 {
209         struct ptlrpc_connection *conn;
210
211         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
212         cfs_atomic_inc(&conn->c_refcount);
213 }
214
215 static void
216 conn_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
217 {
218         struct ptlrpc_connection *conn;
219
220         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
221         cfs_atomic_dec(&conn->c_refcount);
222 }
223
224 static void
225 conn_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
226 {
227         struct ptlrpc_connection *conn;
228
229         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
230         /*
231          * Nothing should be left. Connection user put it and
232          * connection also was deleted from table by this time
233          * so we should have 0 refs.
234          */
235         LASSERTF(cfs_atomic_read(&conn->c_refcount) == 0,
236                  "Busy connection with %d refs\n",
237                  cfs_atomic_read(&conn->c_refcount));
238         OBD_FREE_PTR(conn);
239 }
240
241 static cfs_hash_ops_t conn_hash_ops = {
242         .hs_hash        = conn_hashfn,
243         .hs_keycmp      = conn_keycmp,
244         .hs_key         = conn_key,
245         .hs_object      = conn_object,
246         .hs_get         = conn_get,
247         .hs_put_locked  = conn_put_locked,
248         .hs_exit        = conn_exit,
249 };