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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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"
47 #include <class_hash.h>
49 static lustre_hash_t *conn_hash = NULL;
50 static lustre_hash_ops_t conn_hash_ops;
52 struct ptlrpc_connection *
53 ptlrpc_connection_get(lnet_process_id_t peer, lnet_nid_t self,
54 struct obd_uuid *uuid)
56 struct ptlrpc_connection *conn, *conn2;
59 conn = lustre_hash_lookup(conn_hash, &peer);
69 INIT_HLIST_NODE(&conn->c_hash);
70 atomic_set(&conn->c_refcount, 1);
72 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
75 * Add the newly created conn to the hash, on key collision we
76 * lost a racing addition and must destroy our newly allocated
77 * connection. The object which exists in the has will be
78 * returned and may be compared against out object.
80 conn2 = lustre_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
87 CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
88 conn, atomic_read(&conn->c_refcount),
89 libcfs_nid2str(conn->c_peer.nid));
93 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
101 LASSERT(!hlist_unhashed(&conn->c_hash));
104 * We do not remove connection from hashtable and
105 * do not free it even if last caller released ref,
106 * as we want to have it cached for the case it is
109 * Deallocating it and later creating new connection
110 * again would be wastful. This way we also avoid
111 * expensive locking to protect things from get/put
112 * race when found cached connection is freed by
113 * ptlrpc_connection_put().
115 * It will be freed later in module unload time,
116 * when ptlrpc_connection_fini()->lh_exit->conn_exit()
119 if (atomic_dec_return(&conn->c_refcount) == 1)
122 CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
123 conn, atomic_read(&conn->c_refcount),
124 libcfs_nid2str(conn->c_peer.nid));
129 struct ptlrpc_connection *
130 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
134 atomic_inc(&conn->c_refcount);
135 CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
136 conn, atomic_read(&conn->c_refcount),
137 libcfs_nid2str(conn->c_peer.nid));
142 int ptlrpc_connection_init(void)
146 conn_hash = lustre_hash_init("CONN_HASH", 32, 32768,
147 &conn_hash_ops, LH_REHASH);
154 void ptlrpc_connection_fini(void) {
156 lustre_hash_exit(conn_hash);
161 * Hash operations for net_peer<->connection
164 conn_hashfn(lustre_hash_t *lh, void *key, unsigned mask)
166 return lh_djb2_hash(key, sizeof(lnet_process_id_t), mask);
170 conn_compare(void *key, struct hlist_node *hnode)
172 struct ptlrpc_connection *conn;
173 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;
187 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
188 return &conn->c_peer;
192 conn_get(struct hlist_node *hnode)
194 struct ptlrpc_connection *conn;
196 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
197 atomic_inc(&conn->c_refcount);
203 conn_put(struct hlist_node *hnode)
205 struct ptlrpc_connection *conn;
207 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
208 atomic_dec(&conn->c_refcount);
214 conn_exit(struct hlist_node *hnode)
216 struct ptlrpc_connection *conn;
218 conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
220 * Nothing should be left. Connection user put it and
221 * connection also was deleted from table by this time
222 * so we should have 0 refs.
224 LASSERTF(atomic_read(&conn->c_refcount) == 0,
225 "Busy connection with %d refs\n",
226 atomic_read(&conn->c_refcount));
230 static lustre_hash_ops_t conn_hash_ops = {
231 .lh_hash = conn_hashfn,
232 .lh_compare = conn_compare,
236 .lh_exit = conn_exit,