4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_RPC
38 #include "../include/obd_support.h"
39 #include "../include/obd_class.h"
40 #include "../include/lustre_net.h"
42 #include "ptlrpc_internal.h"
44 static struct cfs_hash *conn_hash;
45 static cfs_hash_ops_t conn_hash_ops;
47 struct ptlrpc_connection *
48 ptlrpc_connection_get(lnet_process_id_t peer, lnet_nid_t self,
49 struct obd_uuid *uuid)
51 struct ptlrpc_connection *conn, *conn2;
53 conn = cfs_hash_lookup(conn_hash, &peer);
57 conn = kzalloc(sizeof(*conn), GFP_NOFS);
63 INIT_HLIST_NODE(&conn->c_hash);
64 atomic_set(&conn->c_refcount, 1);
66 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
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.
74 /* In the function below, .hs_keycmp resolves to
76 /* coverity[overrun-buffer-val] */
77 conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
83 CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
84 conn, atomic_read(&conn->c_refcount),
85 libcfs_nid2str(conn->c_peer.nid));
88 EXPORT_SYMBOL(ptlrpc_connection_get);
90 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
97 LASSERT(atomic_read(&conn->c_refcount) > 1);
100 * We do not remove connection from hashtable and
101 * do not free it even if last caller released ref,
102 * as we want to have it cached for the case it is
105 * Deallocating it and later creating new connection
106 * again would be wastful. This way we also avoid
107 * expensive locking to protect things from get/put
108 * race when found cached connection is freed by
109 * ptlrpc_connection_put().
111 * It will be freed later in module unload time,
112 * when ptlrpc_connection_fini()->lh_exit->conn_exit()
115 if (atomic_dec_return(&conn->c_refcount) == 1)
118 CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
119 conn, atomic_read(&conn->c_refcount),
120 libcfs_nid2str(conn->c_peer.nid));
124 EXPORT_SYMBOL(ptlrpc_connection_put);
126 struct ptlrpc_connection *
127 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
129 atomic_inc(&conn->c_refcount);
130 CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
131 conn, atomic_read(&conn->c_refcount),
132 libcfs_nid2str(conn->c_peer.nid));
136 EXPORT_SYMBOL(ptlrpc_connection_addref);
138 int ptlrpc_connection_init(void)
140 conn_hash = cfs_hash_create("CONN_HASH",
143 HASH_CONN_BKT_BITS, 0,
146 &conn_hash_ops, CFS_HASH_DEFAULT);
152 EXPORT_SYMBOL(ptlrpc_connection_init);
154 void ptlrpc_connection_fini(void)
156 cfs_hash_putref(conn_hash);
158 EXPORT_SYMBOL(ptlrpc_connection_fini);
161 * Hash operations for net_peer<->connection
164 conn_hashfn(struct cfs_hash *hs, const void *key, unsigned mask)
166 return cfs_hash_djb2_hash(key, sizeof(lnet_process_id_t), mask);
170 conn_keycmp(const void *key, struct hlist_node *hnode)
172 struct ptlrpc_connection *conn;
173 const lnet_process_id_t *conn_key;
175 LASSERT(key != NULL);
176 conn_key = (lnet_process_id_t *)key;
177 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
179 return conn_key->nid == conn->c_peer.nid &&
180 conn_key->pid == conn->c_peer.pid;
184 conn_key(struct hlist_node *hnode)
186 struct ptlrpc_connection *conn;
188 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
189 return &conn->c_peer;
193 conn_object(struct hlist_node *hnode)
195 return hlist_entry(hnode, struct ptlrpc_connection, c_hash);
199 conn_get(struct cfs_hash *hs, struct hlist_node *hnode)
201 struct ptlrpc_connection *conn;
203 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
204 atomic_inc(&conn->c_refcount);
208 conn_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
210 struct ptlrpc_connection *conn;
212 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
213 atomic_dec(&conn->c_refcount);
217 conn_exit(struct cfs_hash *hs, struct hlist_node *hnode)
219 struct ptlrpc_connection *conn;
221 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
223 * Nothing should be left. Connection user put it and
224 * connection also was deleted from table by this time
225 * so we should have 0 refs.
227 LASSERTF(atomic_read(&conn->c_refcount) == 0,
228 "Busy connection with %d refs\n",
229 atomic_read(&conn->c_refcount));
233 static cfs_hash_ops_t conn_hash_ops = {
234 .hs_hash = conn_hashfn,
235 .hs_keycmp = conn_keycmp,
237 .hs_object = conn_object,
239 .hs_put_locked = conn_put_locked,
240 .hs_exit = conn_exit,