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 [sun.com URL with a
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 spinlock_t conn_lock;
50 static struct list_head conn_list;
51 static struct list_head conn_unused_list;
52 static struct lustre_class_hash_body *conn_hash_body;
53 static struct lustre_class_hash_body *conn_unused_hash_body;
55 extern struct lustre_hash_operations conn_hash_operations;
57 void ptlrpc_dump_connections(void)
59 struct list_head *tmp;
60 struct ptlrpc_connection *c;
63 list_for_each(tmp, &conn_list) {
64 c = list_entry(tmp, struct ptlrpc_connection, c_link);
65 CERROR("Connection %p/%s has refcount %d (nid=%s->%s)\n",
66 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
67 libcfs_nid2str(c->c_self),
68 libcfs_nid2str(c->c_peer.nid));
73 struct ptlrpc_connection*
74 ptlrpc_lookup_conn_locked (lnet_process_id_t peer)
76 struct ptlrpc_connection *c;
78 c = lustre_hash_get_object_by_key(conn_hash_body, &peer);
82 c = lustre_hash_get_object_by_key(conn_unused_hash_body, &peer);
90 struct ptlrpc_connection *ptlrpc_get_connection(lnet_process_id_t peer,
91 lnet_nid_t self, struct obd_uuid *uuid)
93 struct ptlrpc_connection *c;
94 struct ptlrpc_connection *c2;
98 CDEBUG(D_INFO, "self %s peer %s\n",
99 libcfs_nid2str(self), libcfs_id2str(peer));
101 spin_lock(&conn_lock);
103 c = ptlrpc_lookup_conn_locked(peer);
105 spin_unlock(&conn_lock);
110 OBD_ALLOC(c, sizeof(*c));
114 atomic_set(&c->c_refcount, 1);
117 INIT_HLIST_NODE(&c->c_hash);
118 CFS_INIT_LIST_HEAD(&c->c_link);
120 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
122 spin_lock(&conn_lock);
124 c2 = ptlrpc_lookup_conn_locked(peer);
126 list_add(&c->c_link, &conn_list);
127 rc = lustre_hash_additem_unique(conn_hash_body, &peer,
130 list_del(&c->c_link);
131 CERROR("Cannot add connection to conn_hash_body\n");
138 spin_unlock(&conn_lock);
140 if (c2 == NULL && rc == 0)
144 OBD_FREE(c, sizeof(*c));
148 int ptlrpc_put_connection(struct ptlrpc_connection *c)
154 CERROR("NULL connection\n");
158 CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
159 c, atomic_read(&c->c_refcount) - 1,
160 libcfs_nid2str(c->c_peer.nid));
162 spin_lock(&conn_lock);
163 LASSERT(!hlist_unhashed(&c->c_hash));
164 spin_unlock(&conn_lock);
166 if (atomic_dec_return(&c->c_refcount) == 1) {
168 spin_lock(&conn_lock);
170 lustre_hash_delitem(conn_hash_body, &c->c_peer, &c->c_hash);
171 list_del(&c->c_link);
173 list_add(&c->c_link, &conn_unused_list);
174 rc = lustre_hash_additem_unique(conn_unused_hash_body, &c->c_peer,
177 spin_unlock(&conn_lock);
178 CERROR("Cannot hash connection to conn_hash_body\n");
182 spin_unlock(&conn_lock);
187 if (atomic_read(&c->c_refcount) < 0)
188 CERROR("connection %p refcount %d!\n",
189 c, atomic_read(&c->c_refcount));
195 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
198 atomic_inc(&c->c_refcount);
199 CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
200 c, atomic_read(&c->c_refcount),
201 libcfs_nid2str(c->c_peer.nid));
205 int ptlrpc_init_connection(void)
208 CFS_INIT_LIST_HEAD(&conn_list);
209 rc = lustre_hash_init(&conn_hash_body, "CONN_HASH",
210 128, &conn_hash_operations);
214 CFS_INIT_LIST_HEAD(&conn_unused_list);
215 rc = lustre_hash_init(&conn_unused_hash_body, "CONN_UNUSED_HASH",
216 128, &conn_hash_operations);
220 spin_lock_init(&conn_lock);
223 lustre_hash_exit(&conn_hash_body);
224 lustre_hash_exit(&conn_unused_hash_body);
229 void ptlrpc_cleanup_connection(void)
231 struct list_head *tmp, *pos;
232 struct ptlrpc_connection *c;
234 spin_lock(&conn_lock);
236 lustre_hash_exit(&conn_unused_hash_body);
237 list_for_each_safe(tmp, pos, &conn_unused_list) {
238 c = list_entry(tmp, struct ptlrpc_connection, c_link);
239 list_del(&c->c_link);
240 OBD_FREE(c, sizeof(*c));
243 lustre_hash_exit(&conn_hash_body);
244 list_for_each_safe(tmp, pos, &conn_list) {
245 c = list_entry(tmp, struct ptlrpc_connection, c_link);
246 CERROR("Connection %p/%s has refcount %d (nid=%s)\n",
247 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
248 libcfs_nid2str(c->c_peer.nid));
249 list_del(&c->c_link);
250 OBD_FREE(c, sizeof(*c));
252 spin_unlock(&conn_lock);