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 spinlock_t conn_lock;
50 static struct list_head conn_list;
51 static struct lustre_class_hash_body *conn_hash_body;
52 static struct lustre_class_hash_body *conn_unused_hash_body;
54 extern struct lustre_hash_operations conn_hash_operations;
56 void ptlrpc_dump_connection(void *obj, void *data)
58 struct ptlrpc_connection *c = obj;
60 CERROR("Connection %p/%s has refcount %d (nid=%s->%s)\n",
61 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
62 libcfs_nid2str(c->c_self),
63 libcfs_nid2str(c->c_peer.nid));
66 void ptlrpc_dump_connections(void)
70 lustre_hash_iterate_all(conn_hash_body, ptlrpc_dump_connection, NULL);
75 struct ptlrpc_connection*
76 ptlrpc_lookup_conn_locked (lnet_process_id_t peer)
78 struct ptlrpc_connection *c = NULL;
81 c = lustre_hash_get_object_by_key(conn_hash_body, &peer);
85 c = lustre_hash_get_object_by_key(conn_unused_hash_body, &peer);
87 lustre_hash_delitem(conn_unused_hash_body, &peer, &c->c_hash);
88 rc = lustre_hash_additem_unique(conn_hash_body, &peer,
91 /* can't add - try with new item */
102 struct ptlrpc_connection *ptlrpc_get_connection(lnet_process_id_t peer,
103 lnet_nid_t self, struct obd_uuid *uuid)
105 struct ptlrpc_connection *c;
106 struct ptlrpc_connection *c2;
110 CDEBUG(D_INFO, "self %s peer %s\n",
111 libcfs_nid2str(self), libcfs_id2str(peer));
113 spin_lock(&conn_lock);
114 c = ptlrpc_lookup_conn_locked(peer);
115 spin_unlock(&conn_lock);
124 atomic_set(&c->c_refcount, 1);
127 INIT_HLIST_NODE(&c->c_hash);
128 CFS_INIT_LIST_HEAD(&c->c_link);
130 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
132 spin_lock(&conn_lock);
134 c2 = ptlrpc_lookup_conn_locked(peer);
136 rc = lustre_hash_additem_unique(conn_hash_body, &peer,
139 CERROR("Cannot add connection to conn_hash_body\n");
142 list_add(&c->c_link, &conn_list);
146 spin_unlock(&conn_lock);
148 if (c2 == NULL && rc == 0)
152 OBD_FREE(c, sizeof(*c));
156 int ptlrpc_put_connection(struct ptlrpc_connection *c)
162 CERROR("NULL connection\n");
166 CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
167 c, atomic_read(&c->c_refcount) - 1,
168 libcfs_nid2str(c->c_peer.nid));
170 spin_lock(&conn_lock);
171 LASSERT(!hlist_unhashed(&c->c_hash));
172 spin_unlock(&conn_lock);
174 if (atomic_dec_return(&c->c_refcount) == 1) {
176 spin_lock(&conn_lock);
177 lustre_hash_delitem(conn_hash_body, &c->c_peer, &c->c_hash);
178 rc = lustre_hash_additem_unique(conn_unused_hash_body, &c->c_peer,
180 spin_unlock(&conn_lock);
182 CERROR("Cannot hash connection to conn_hash_body\n");
189 if (atomic_read(&c->c_refcount) < 0)
190 CERROR("connection %p refcount %d!\n",
191 c, atomic_read(&c->c_refcount));
197 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
200 atomic_inc(&c->c_refcount);
201 CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
202 c, atomic_read(&c->c_refcount),
203 libcfs_nid2str(c->c_peer.nid));
207 int ptlrpc_init_connection(void)
210 CFS_INIT_LIST_HEAD(&conn_list);
211 rc = lustre_hash_init(&conn_hash_body, "CONN_HASH",
212 128, &conn_hash_operations);
216 rc = lustre_hash_init(&conn_unused_hash_body, "CONN_UNUSED_HASH",
217 128, &conn_hash_operations);
221 spin_lock_init(&conn_lock);
224 lustre_hash_exit(&conn_hash_body);
225 lustre_hash_exit(&conn_unused_hash_body);
230 void ptlrpc_cleanup_connection(void)
232 struct list_head *tmp, *pos;
233 struct ptlrpc_connection *c;
235 spin_lock(&conn_lock);
237 lustre_hash_exit(&conn_unused_hash_body);
238 lustre_hash_exit(&conn_hash_body);
240 list_for_each_safe(tmp, pos, &conn_list) {
241 c = list_entry(tmp, struct ptlrpc_connection, c_link);
242 if (atomic_read(&c->c_refcount))
243 CERROR("Connection %p/%s has refcount %d (nid=%s)\n",
244 c, c->c_remote_uuid.uuid,
245 atomic_read(&c->c_refcount),
246 libcfs_nid2str(c->c_peer.nid));
247 list_del(&c->c_link);
248 OBD_FREE(c, sizeof(*c));
250 spin_unlock(&conn_lock);