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 list_for_each(tmp, &conn_list) {
66 c = list_entry(tmp, struct ptlrpc_connection, c_link);
67 if (peer->peer_nid == c->c_peer.peer_nid &&
68 peer->peer_ni == c->c_peer.peer_ni) {
69 ptlrpc_connection_addref(c);
74 list_for_each_safe(tmp, pos, &conn_unused_list) {
75 c = list_entry(tmp, struct ptlrpc_connection, c_link);
76 if (peer->peer_nid == c->c_peer.peer_nid &&
77 peer->peer_ni == c->c_peer.peer_ni) {
78 ptlrpc_connection_addref(c);
80 list_add(&c->c_link, &conn_list);
85 /* FIXME: this should be a slab once we can validate slab addresses
87 OBD_ALLOC(c, sizeof(*c));
91 if (uuid && uuid->uuid) /* XXX ???? */
92 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
93 atomic_set(&c->c_refcount, 0);
94 memcpy(&c->c_peer, peer, sizeof(c->c_peer));
96 ptlrpc_connection_addref(c);
98 list_add(&c->c_link, &conn_list);
102 spin_unlock(&conn_lock);
106 int ptlrpc_put_connection(struct ptlrpc_connection *c)
112 CERROR("NULL connection\n");
116 CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
117 c, atomic_read(&c->c_refcount) - 1, c->c_peer.peer_nid,
118 c->c_peer.peer_ni->pni_name);
120 if (atomic_dec_and_test(&c->c_refcount)) {
121 spin_lock(&conn_lock);
122 list_del(&c->c_link);
123 list_add(&c->c_link, &conn_unused_list);
124 spin_unlock(&conn_lock);
127 if (atomic_read(&c->c_refcount) < 0)
128 CERROR("connection %p refcount %d!\n",
129 c, atomic_read(&c->c_refcount));
134 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
137 atomic_inc(&c->c_refcount);
138 CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
139 c, atomic_read(&c->c_refcount), c->c_peer.peer_nid,
140 c->c_peer.peer_ni->pni_name);
144 void ptlrpc_init_connection(void)
146 INIT_LIST_HEAD(&conn_list);
147 INIT_LIST_HEAD(&conn_unused_list);
148 conn_lock = SPIN_LOCK_UNLOCKED;
151 void ptlrpc_cleanup_connection(void)
153 struct list_head *tmp, *pos;
154 struct ptlrpc_connection *c;
156 spin_lock(&conn_lock);
157 list_for_each_safe(tmp, pos, &conn_unused_list) {
158 c = list_entry(tmp, struct ptlrpc_connection, c_link);
159 list_del(&c->c_link);
160 OBD_FREE(c, sizeof(*c));
162 list_for_each_safe(tmp, pos, &conn_list) {
163 c = list_entry(tmp, struct ptlrpc_connection, c_link);
164 CERROR("Connection %p/%s has refcount %d (nid="LPX64" on %s)\n",
165 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
166 c->c_peer.peer_nid, c->c_peer.peer_ni->pni_name);
167 list_del(&c->c_link);
168 OBD_FREE(c, sizeof(*c));
170 spin_unlock(&conn_lock);