1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 #define DEBUG_SUBSYSTEM S_RPC
28 #include <obd_support.h>
29 #include <obd_class.h>
30 #include <lustre_net.h>
32 #include <liblustre.h>
35 #include "ptlrpc_internal.h"
36 #include <class_hash.h>
38 static spinlock_t conn_lock;
39 static struct list_head conn_list;
40 static struct list_head conn_unused_list;
41 static struct lustre_class_hash_body *conn_hash_body;
42 static struct lustre_class_hash_body *conn_unused_hash_body;
44 extern struct lustre_hash_operations conn_hash_operations;
46 void ptlrpc_dump_connections(void)
48 struct list_head *tmp;
49 struct ptlrpc_connection *c;
52 list_for_each(tmp, &conn_list) {
53 c = list_entry(tmp, struct ptlrpc_connection, c_link);
54 CERROR("Connection %p/%s has refcount %d (nid=%s->%s)\n",
55 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
56 libcfs_nid2str(c->c_self),
57 libcfs_nid2str(c->c_peer.nid));
62 struct ptlrpc_connection*
63 ptlrpc_lookup_conn_locked (lnet_process_id_t peer)
65 struct ptlrpc_connection *c;
67 c = lustre_hash_get_object_by_key(conn_hash_body, &peer);
71 c = lustre_hash_get_object_by_key(conn_unused_hash_body, &peer);
79 struct ptlrpc_connection *ptlrpc_get_connection(lnet_process_id_t peer,
80 lnet_nid_t self, struct obd_uuid *uuid)
82 struct ptlrpc_connection *c;
83 struct ptlrpc_connection *c2;
87 CDEBUG(D_INFO, "self %s peer %s\n",
88 libcfs_nid2str(self), libcfs_id2str(peer));
90 spin_lock(&conn_lock);
92 c = ptlrpc_lookup_conn_locked(peer);
94 spin_unlock(&conn_lock);
99 OBD_ALLOC(c, sizeof(*c));
103 atomic_set(&c->c_refcount, 1);
106 INIT_HLIST_NODE(&c->c_hash);
107 CFS_INIT_LIST_HEAD(&c->c_link);
109 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
111 spin_lock(&conn_lock);
113 c2 = ptlrpc_lookup_conn_locked(peer);
115 list_add(&c->c_link, &conn_list);
116 rc = lustre_hash_additem_unique(conn_hash_body, &peer,
119 list_del(&c->c_link);
120 CERROR("Cannot add connection to conn_hash_body\n");
127 spin_unlock(&conn_lock);
129 if (c2 == NULL && rc == 0)
133 OBD_FREE(c, sizeof(*c));
137 int ptlrpc_put_connection(struct ptlrpc_connection *c)
140 lnet_process_id_t peer;
144 CERROR("NULL connection\n");
150 CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
151 c, atomic_read(&c->c_refcount) - 1,
152 libcfs_nid2str(c->c_peer.nid));
154 spin_lock(&conn_lock);
155 LASSERT(!hlist_unhashed(&c->c_hash));
156 spin_unlock(&conn_lock);
158 if (atomic_dec_return(&c->c_refcount) == 1) {
160 spin_lock(&conn_lock);
162 lustre_hash_delitem(conn_hash_body, &peer, &c->c_hash);
163 list_del(&c->c_link);
165 list_add(&c->c_link, &conn_unused_list);
166 rc = lustre_hash_additem_unique(conn_unused_hash_body, &peer,
169 spin_unlock(&conn_lock);
170 CERROR("Cannot hash connection to conn_hash_body\n");
174 spin_unlock(&conn_lock);
179 if (atomic_read(&c->c_refcount) < 0)
180 CERROR("connection %p refcount %d!\n",
181 c, atomic_read(&c->c_refcount));
187 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
190 atomic_inc(&c->c_refcount);
191 CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
192 c, atomic_read(&c->c_refcount),
193 libcfs_nid2str(c->c_peer.nid));
197 int ptlrpc_init_connection(void)
200 CFS_INIT_LIST_HEAD(&conn_list);
201 rc = lustre_hash_init(&conn_hash_body, "CONN_HASH",
202 128, &conn_hash_operations);
206 CFS_INIT_LIST_HEAD(&conn_unused_list);
207 rc = lustre_hash_init(&conn_unused_hash_body, "CONN_UNUSED_HASH",
208 128, &conn_hash_operations);
212 spin_lock_init(&conn_lock);
215 lustre_hash_exit(&conn_hash_body);
216 lustre_hash_exit(&conn_unused_hash_body);
221 void ptlrpc_cleanup_connection(void)
223 struct list_head *tmp, *pos;
224 struct ptlrpc_connection *c;
226 spin_lock(&conn_lock);
228 lustre_hash_exit(&conn_unused_hash_body);
229 list_for_each_safe(tmp, pos, &conn_unused_list) {
230 c = list_entry(tmp, struct ptlrpc_connection, c_link);
231 list_del(&c->c_link);
232 OBD_FREE(c, sizeof(*c));
235 lustre_hash_exit(&conn_hash_body);
236 list_for_each_safe(tmp, pos, &conn_list) {
237 c = list_entry(tmp, struct ptlrpc_connection, c_link);
238 CERROR("Connection %p/%s has refcount %d (nid=%s)\n",
239 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
240 libcfs_nid2str(c->c_peer.nid));
241 list_del(&c->c_link);
242 OBD_FREE(c, sizeof(*c));
244 spin_unlock(&conn_lock);