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 Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_RPC
25 #include <linux/obd_support.h>
26 #include <linux/obd_class.h>
27 #include <linux/lustre_net.h>
29 #include <liblustre.h>
32 #include "ptlrpc_internal.h"
34 static spinlock_t conn_lock;
35 static struct list_head conn_list;
36 static struct list_head conn_unused_list;
38 void ptlrpc_dump_connections(void)
40 struct list_head *tmp;
41 struct ptlrpc_connection *c;
44 list_for_each(tmp, &conn_list) {
45 c = list_entry(tmp, struct ptlrpc_connection, c_link);
46 CERROR("Connection %p/%s has refcount %d (nid="LPX64" on %s)\n",
47 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
48 c->c_peer.peer_nid, c->c_peer.peer_ni->pni_name);
53 struct ptlrpc_connection *ptlrpc_get_connection(struct ptlrpc_peer *peer,
54 struct obd_uuid *uuid)
56 struct list_head *tmp, *pos;
57 struct ptlrpc_connection *c;
61 CDEBUG(D_INFO, "peer is "LPX64" on %s\n",
62 peer->peer_nid, peer->peer_ni->pni_name);
64 spin_lock(&conn_lock);
65 if (list_empty(&conn_list)) {
66 if (!ptlrpc_get_ldlm_hooks()) {
67 spin_unlock(&conn_lock);
72 list_for_each(tmp, &conn_list) {
73 c = list_entry(tmp, struct ptlrpc_connection, c_link);
74 if (peer->peer_nid == c->c_peer.peer_nid &&
75 peer->peer_ni == c->c_peer.peer_ni) {
76 ptlrpc_connection_addref(c);
81 list_for_each_safe(tmp, pos, &conn_unused_list) {
82 c = list_entry(tmp, struct ptlrpc_connection, c_link);
83 if (peer->peer_nid == c->c_peer.peer_nid &&
84 peer->peer_ni == c->c_peer.peer_ni) {
85 ptlrpc_connection_addref(c);
87 list_add(&c->c_link, &conn_list);
92 /* FIXME: this should be a slab once we can validate slab addresses
94 OBD_ALLOC(c, sizeof(*c));
102 if (uuid && uuid->uuid) /* XXX ???? */
103 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
104 atomic_set(&c->c_refcount, 0);
105 memcpy(&c->c_peer, peer, sizeof(c->c_peer));
106 spin_lock_init(&c->c_lock);
108 ptlrpc_connection_addref(c);
110 list_add(&c->c_link, &conn_list);
114 spin_unlock(&conn_lock);
118 int ptlrpc_put_connection(struct ptlrpc_connection *c)
124 CERROR("NULL connection\n");
128 CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
129 c, atomic_read(&c->c_refcount) - 1, c->c_peer.peer_nid,
130 c->c_peer.peer_ni->pni_name);
132 if (atomic_dec_and_test(&c->c_refcount)) {
133 spin_lock(&conn_lock);
134 list_del(&c->c_link);
135 list_add(&c->c_link, &conn_unused_list);
136 if (list_empty(&conn_list)) {
137 ptlrpc_put_ldlm_hooks();
139 spin_unlock(&conn_lock);
142 if (atomic_read(&c->c_refcount) < 0)
143 CERROR("connection %p refcount %d!\n",
144 c, atomic_read(&c->c_refcount));
149 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
152 atomic_inc(&c->c_refcount);
153 CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
154 c, atomic_read(&c->c_refcount), c->c_peer.peer_nid,
155 c->c_peer.peer_ni->pni_name);
159 void ptlrpc_init_connection(void)
161 INIT_LIST_HEAD(&conn_list);
162 INIT_LIST_HEAD(&conn_unused_list);
163 conn_lock = SPIN_LOCK_UNLOCKED;
166 void ptlrpc_cleanup_connection(void)
168 struct list_head *tmp, *pos;
169 struct ptlrpc_connection *c;
171 spin_lock(&conn_lock);
172 list_for_each_safe(tmp, pos, &conn_unused_list) {
173 c = list_entry(tmp, struct ptlrpc_connection, c_link);
174 list_del(&c->c_link);
175 OBD_FREE(c, sizeof(*c));
177 list_for_each_safe(tmp, pos, &conn_list) {
178 c = list_entry(tmp, struct ptlrpc_connection, c_link);
179 CERROR("Connection %p/%s has refcount %d (nid="LPX64" on %s)\n",
180 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
181 c->c_peer.peer_nid, c->c_peer.peer_ni->pni_name);
182 list_del(&c->c_link);
183 OBD_FREE(c, sizeof(*c));
185 spin_unlock(&conn_lock);