Whamcloud - gitweb
LU-1812 ext4: ec_type has been dropped from ext4_ext_cache
[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, Intel Corporation.
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         /* In the function below, .hs_keycmp resolves to
80          * conn_keycmp() */
81         /* coverity[overrun-buffer-val] */
82         conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
83         if (conn != conn2) {
84                 OBD_FREE_PTR(conn);
85                 conn = conn2;
86         }
87         EXIT;
88 out:
89         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
90                conn, cfs_atomic_read(&conn->c_refcount),
91                libcfs_nid2str(conn->c_peer.nid));
92         return conn;
93 }
94 EXPORT_SYMBOL(ptlrpc_connection_get);
95
96 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
97 {
98         int rc = 0;
99         ENTRY;
100
101         if (!conn)
102                 RETURN(rc);
103
104         LASSERT(cfs_atomic_read(&conn->c_refcount) > 1);
105
106         /*
107          * We do not remove connection from hashtable and
108          * do not free it even if last caller released ref,
109          * as we want to have it cached for the case it is
110          * needed again.
111          *
112          * Deallocating it and later creating new connection
113          * again would be wastful. This way we also avoid
114          * expensive locking to protect things from get/put
115          * race when found cached connection is freed by
116          * ptlrpc_connection_put().
117          *
118          * It will be freed later in module unload time,
119          * when ptlrpc_connection_fini()->lh_exit->conn_exit()
120          * path is called.
121          */
122         if (cfs_atomic_dec_return(&conn->c_refcount) == 1)
123                 rc = 1;
124
125         CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
126                conn, cfs_atomic_read(&conn->c_refcount),
127                libcfs_nid2str(conn->c_peer.nid));
128
129         RETURN(rc);
130 }
131 EXPORT_SYMBOL(ptlrpc_connection_put);
132
133 struct ptlrpc_connection *
134 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
135 {
136         ENTRY;
137
138         cfs_atomic_inc(&conn->c_refcount);
139         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
140                conn, cfs_atomic_read(&conn->c_refcount),
141                libcfs_nid2str(conn->c_peer.nid));
142
143         RETURN(conn);
144 }
145 EXPORT_SYMBOL(ptlrpc_connection_addref);
146
147 int ptlrpc_connection_init(void)
148 {
149         ENTRY;
150
151         conn_hash = cfs_hash_create("CONN_HASH",
152                                     HASH_CONN_CUR_BITS,
153                                     HASH_CONN_MAX_BITS,
154                                     HASH_CONN_BKT_BITS, 0,
155                                     CFS_HASH_MIN_THETA,
156                                     CFS_HASH_MAX_THETA,
157                                     &conn_hash_ops, CFS_HASH_DEFAULT);
158         if (!conn_hash)
159                 RETURN(-ENOMEM);
160
161         RETURN(0);
162 }
163 EXPORT_SYMBOL(ptlrpc_connection_init);
164
165 void ptlrpc_connection_fini(void) {
166         ENTRY;
167         cfs_hash_putref(conn_hash);
168         EXIT;
169 }
170 EXPORT_SYMBOL(ptlrpc_connection_fini);
171
172 /*
173  * Hash operations for net_peer<->connection
174  */
175 static unsigned
176 conn_hashfn(cfs_hash_t *hs, const void *key, unsigned mask)
177 {
178         return cfs_hash_djb2_hash(key, sizeof(lnet_process_id_t), mask);
179 }
180
181 static int
182 conn_keycmp(const void *key, cfs_hlist_node_t *hnode)
183 {
184         struct ptlrpc_connection *conn;
185         const lnet_process_id_t *conn_key;
186
187         LASSERT(key != NULL);
188         conn_key = (lnet_process_id_t*)key;
189         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
190
191         return conn_key->nid == conn->c_peer.nid &&
192                conn_key->pid == conn->c_peer.pid;
193 }
194
195 static void *
196 conn_key(cfs_hlist_node_t *hnode)
197 {
198         struct ptlrpc_connection *conn;
199         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
200         return &conn->c_peer;
201 }
202
203 static void *
204 conn_object(cfs_hlist_node_t *hnode)
205 {
206         return cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
207 }
208
209 static void
210 conn_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
211 {
212         struct ptlrpc_connection *conn;
213
214         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
215         cfs_atomic_inc(&conn->c_refcount);
216 }
217
218 static void
219 conn_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
220 {
221         struct ptlrpc_connection *conn;
222
223         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
224         cfs_atomic_dec(&conn->c_refcount);
225 }
226
227 static void
228 conn_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
229 {
230         struct ptlrpc_connection *conn;
231
232         conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
233         /*
234          * Nothing should be left. Connection user put it and
235          * connection also was deleted from table by this time
236          * so we should have 0 refs.
237          */
238         LASSERTF(cfs_atomic_read(&conn->c_refcount) == 0,
239                  "Busy connection with %d refs\n",
240                  cfs_atomic_read(&conn->c_refcount));
241         OBD_FREE_PTR(conn);
242 }
243
244 static cfs_hash_ops_t conn_hash_ops = {
245         .hs_hash        = conn_hashfn,
246         .hs_keycmp      = conn_keycmp,
247         .hs_key         = conn_key,
248         .hs_object      = conn_object,
249         .hs_get         = conn_get,
250         .hs_put_locked  = conn_put_locked,
251         .hs_exit        = conn_exit,
252 };