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