1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
39 #define DEBUG_SUBSYSTEM S_RPC
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_net.h>
45 #include <liblustre.h>
48 #include "ptlrpc_internal.h"
50 static cfs_hash_t *conn_hash = NULL;
51 static cfs_hash_ops_t conn_hash_ops;
53 struct ptlrpc_connection *
54 ptlrpc_connection_get(lnet_process_id_t peer, lnet_nid_t self,
55 struct obd_uuid *uuid)
57 struct ptlrpc_connection *conn, *conn2;
60 conn = cfs_hash_lookup(conn_hash, &peer);
70 CFS_INIT_HLIST_NODE(&conn->c_hash);
71 cfs_atomic_set(&conn->c_refcount, 1);
73 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
76 * Add the newly created conn to the hash, on key collision we
77 * lost a racing addition and must destroy our newly allocated
78 * connection. The object which exists in the has will be
79 * returned and may be compared against out object.
81 conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
88 CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
89 conn, cfs_atomic_read(&conn->c_refcount),
90 libcfs_nid2str(conn->c_peer.nid));
94 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
102 LASSERT(cfs_atomic_read(&conn->c_refcount) > 1);
105 * We do not remove connection from hashtable and
106 * do not free it even if last caller released ref,
107 * as we want to have it cached for the case it is
110 * Deallocating it and later creating new connection
111 * again would be wastful. This way we also avoid
112 * expensive locking to protect things from get/put
113 * race when found cached connection is freed by
114 * ptlrpc_connection_put().
116 * It will be freed later in module unload time,
117 * when ptlrpc_connection_fini()->lh_exit->conn_exit()
120 if (cfs_atomic_dec_return(&conn->c_refcount) == 1)
123 CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
124 conn, cfs_atomic_read(&conn->c_refcount),
125 libcfs_nid2str(conn->c_peer.nid));
130 struct ptlrpc_connection *
131 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
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));
143 int ptlrpc_connection_init(void)
147 conn_hash = cfs_hash_create("CONN_HASH",
150 HASH_CONN_BKT_BITS, 0,
153 &conn_hash_ops, CFS_HASH_DEFAULT);
160 void ptlrpc_connection_fini(void) {
162 cfs_hash_putref(conn_hash);
167 * Hash operations for net_peer<->connection
170 conn_hashfn(cfs_hash_t *hs, const void *key, unsigned mask)
172 return cfs_hash_djb2_hash(key, sizeof(lnet_process_id_t), mask);
176 conn_keycmp(const void *key, cfs_hlist_node_t *hnode)
178 struct ptlrpc_connection *conn;
179 const lnet_process_id_t *conn_key;
181 LASSERT(key != NULL);
182 conn_key = (lnet_process_id_t*)key;
183 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
185 return conn_key->nid == conn->c_peer.nid &&
186 conn_key->pid == conn->c_peer.pid;
190 conn_key(cfs_hlist_node_t *hnode)
192 struct ptlrpc_connection *conn;
193 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
194 return &conn->c_peer;
198 conn_object(cfs_hlist_node_t *hnode)
200 return cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
204 conn_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
206 struct ptlrpc_connection *conn;
208 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
209 cfs_atomic_inc(&conn->c_refcount);
213 conn_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
215 struct ptlrpc_connection *conn;
217 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
218 cfs_atomic_dec(&conn->c_refcount);
222 conn_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
224 struct ptlrpc_connection *conn;
226 conn = cfs_hlist_entry(hnode, struct ptlrpc_connection, c_hash);
228 * Nothing should be left. Connection user put it and
229 * connection also was deleted from table by this time
230 * so we should have 0 refs.
232 LASSERTF(cfs_atomic_read(&conn->c_refcount) == 0,
233 "Busy connection with %d refs\n",
234 cfs_atomic_read(&conn->c_refcount));
238 static cfs_hash_ops_t conn_hash_ops = {
239 .hs_hash = conn_hashfn,
240 .hs_keycmp = conn_keycmp,
242 .hs_object = conn_object,
244 .hs_put_locked = conn_put_locked,
245 .hs_exit = conn_exit,