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 char str[PTL_NALFMT_SIZE];
41 struct list_head *tmp;
42 struct ptlrpc_connection *c;
45 list_for_each(tmp, &conn_list) {
46 c = list_entry(tmp, struct ptlrpc_connection, c_link);
47 CERROR("Connection %p/%s has refcount %d (nid=%s on %s)\n",
48 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
49 ptlrpc_peernid2str(&c->c_peer, str),
50 c->c_peer.peer_ni->pni_name);
55 struct ptlrpc_connection *ptlrpc_get_connection(struct ptlrpc_peer *peer,
56 struct obd_uuid *uuid)
58 char str[PTL_NALFMT_SIZE];
59 struct list_head *tmp, *pos;
60 struct ptlrpc_connection *c;
64 CDEBUG(D_INFO, "peer is %s on %s\n",
65 ptlrpc_peernid2str(peer, str), peer->peer_ni->pni_name);
67 spin_lock(&conn_lock);
68 list_for_each(tmp, &conn_list) {
69 c = list_entry(tmp, struct ptlrpc_connection, c_link);
70 if (peer->peer_nid == c->c_peer.peer_nid &&
71 peer->peer_ni == c->c_peer.peer_ni) {
72 ptlrpc_connection_addref(c);
77 list_for_each_safe(tmp, pos, &conn_unused_list) {
78 c = list_entry(tmp, struct ptlrpc_connection, c_link);
79 if (peer->peer_nid == c->c_peer.peer_nid &&
80 peer->peer_ni == c->c_peer.peer_ni) {
81 ptlrpc_connection_addref(c);
83 list_add(&c->c_link, &conn_list);
88 /* FIXME: this should be a slab once we can validate slab addresses
90 OBD_ALLOC_GFP(c, sizeof(*c), GFP_ATOMIC);
95 if (uuid && uuid->uuid) /* XXX ???? */
96 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
97 atomic_set(&c->c_refcount, 0);
98 memcpy(&c->c_peer, peer, sizeof(c->c_peer));
100 ptlrpc_connection_addref(c);
102 list_add(&c->c_link, &conn_list);
106 spin_unlock(&conn_lock);
110 int ptlrpc_put_connection(struct ptlrpc_connection *c)
112 char str[PTL_NALFMT_SIZE];
117 CERROR("NULL connection\n");
121 CDEBUG (D_INFO, "connection=%p refcount %d to %s on %s\n",
122 c, atomic_read(&c->c_refcount) - 1,
123 ptlrpc_peernid2str(&c->c_peer, str),
124 c->c_peer.peer_ni->pni_name);
126 if (atomic_dec_and_test(&c->c_refcount)) {
127 spin_lock(&conn_lock);
128 list_del(&c->c_link);
129 list_add(&c->c_link, &conn_unused_list);
130 spin_unlock(&conn_lock);
133 if (atomic_read(&c->c_refcount) < 0)
134 CERROR("connection %p refcount %d!\n",
135 c, atomic_read(&c->c_refcount));
140 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
142 char str[PTL_NALFMT_SIZE];
144 atomic_inc(&c->c_refcount);
145 CDEBUG (D_INFO, "connection=%p refcount %d to %s on %s\n",
146 c, atomic_read(&c->c_refcount),
147 ptlrpc_peernid2str(&c->c_peer, str),
148 c->c_peer.peer_ni->pni_name);
152 void ptlrpc_init_connection(void)
154 INIT_LIST_HEAD(&conn_list);
155 INIT_LIST_HEAD(&conn_unused_list);
156 conn_lock = SPIN_LOCK_UNLOCKED;
159 void ptlrpc_cleanup_connection(void)
161 char str[PTL_NALFMT_SIZE];
162 struct list_head *tmp, *pos;
163 struct ptlrpc_connection *c;
165 spin_lock(&conn_lock);
166 list_for_each_safe(tmp, pos, &conn_unused_list) {
167 c = list_entry(tmp, struct ptlrpc_connection, c_link);
168 list_del(&c->c_link);
169 OBD_FREE(c, sizeof(*c));
171 list_for_each_safe(tmp, pos, &conn_list) {
172 c = list_entry(tmp, struct ptlrpc_connection, c_link);
173 CERROR("Connection %p/%s has refcount %d (nid=%s on %s)\n",
174 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
175 ptlrpc_peernid2str(&c->c_peer, str),
176 c->c_peer.peer_ni->pni_name);
177 list_del(&c->c_link);
178 OBD_FREE(c, sizeof(*c));
180 spin_unlock(&conn_lock);