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, Whamcloud, Inc.
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
39 #include <obd_support.h>
40 #include <obd_class.h>
41 #include <lustre_net.h>
43 #include <liblustre.h>
46 #include "ptlrpc_internal.h"
48 static cfs_hash_t *conn_hash = NULL;
49 static cfs_hash_ops_t conn_hash_ops;
51 struct ptlrpc_connection *
52 ptlrpc_connection_get(lnet_process_id_t peer, lnet_nid_t self,
53 struct obd_uuid *uuid)
55 struct ptlrpc_connection *conn, *conn2;
58 conn = cfs_hash_lookup(conn_hash, &peer);
68 CFS_INIT_HLIST_NODE(&conn->c_hash);
69 cfs_atomic_set(&conn->c_refcount, 1);
71 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
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.
79 conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
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));
92 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
100 LASSERT(cfs_atomic_read(&conn->c_refcount) > 1);
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
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().
114 * It will be freed later in module unload time,
115 * when ptlrpc_connection_fini()->lh_exit->conn_exit()
118 if (cfs_atomic_dec_return(&conn->c_refcount) == 1)
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));
128 struct ptlrpc_connection *
129 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
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));
141 int ptlrpc_connection_init(void)
145 conn_hash = cfs_hash_create("CONN_HASH",
148 HASH_CONN_BKT_BITS, 0,
151 &conn_hash_ops, CFS_HASH_DEFAULT);
158 void ptlrpc_connection_fini(void) {
160 cfs_hash_putref(conn_hash);
165 * Hash operations for net_peer<->connection
168 conn_hashfn(cfs_hash_t *hs, const void *key, unsigned mask)
170 return cfs_hash_djb2_hash(key, sizeof(lnet_process_id_t), mask);
174 conn_keycmp(const void *key, cfs_hlist_node_t *hnode)
176 struct ptlrpc_connection *conn;
177 const lnet_process_id_t *conn_key;
179 LASSERT(key != NULL);
180 conn_key = (lnet_process_id_t*)key;
181 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
183 return conn_key->nid == conn->c_peer.nid &&
184 conn_key->pid == conn->c_peer.pid;
188 conn_key(cfs_hlist_node_t *hnode)
190 struct ptlrpc_connection *conn;
191 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
192 return &conn->c_peer;
196 conn_object(cfs_hlist_node_t *hnode)
198 return cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
202 conn_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
204 struct ptlrpc_connection *conn;
206 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
207 cfs_atomic_inc(&conn->c_refcount);
211 conn_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
213 struct ptlrpc_connection *conn;
215 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
216 cfs_atomic_dec(&conn->c_refcount);
220 conn_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
222 struct ptlrpc_connection *conn;
224 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
226 * Nothing should be left. Connection user put it and
227 * connection also was deleted from table by this time
228 * so we should have 0 refs.
230 LASSERTF(cfs_atomic_read(&conn->c_refcount) == 0,
231 "Busy connection with %d refs\n",
232 cfs_atomic_read(&conn->c_refcount));
236 static cfs_hash_ops_t conn_hash_ops = {
237 .hs_hash = conn_hashfn,
238 .hs_keycmp = conn_keycmp,
240 .hs_object = conn_object,
242 .hs_put_locked = conn_put_locked,
243 .hs_exit = conn_exit,