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 static spinlock_t conn_lock;
33 static struct list_head conn_list;
34 static struct list_head conn_unused_list;
36 /* If UUID is NULL, c->c_remote_uuid must be all zeroes
37 * If UUID is non-NULL, c->c_remote_uuid must match. */
38 static int match_connection_uuid(struct ptlrpc_connection *c,
39 struct obd_uuid *uuid)
41 struct obd_uuid zero_uuid;
42 memset(&zero_uuid, 0, sizeof(zero_uuid));
45 return memcmp(c->c_remote_uuid.uuid, uuid->uuid,
48 return memcmp(c->c_remote_uuid.uuid, &zero_uuid, sizeof(zero_uuid));
51 struct ptlrpc_connection *ptlrpc_get_connection(struct ptlrpc_peer *peer,
52 struct obd_uuid *uuid)
54 struct list_head *tmp, *pos;
55 struct ptlrpc_connection *c;
58 CDEBUG(D_INFO, "peer is "LPX64" on %s\n",
59 peer->peer_nid, peer->peer_ni->pni_name);
61 spin_lock(&conn_lock);
62 list_for_each(tmp, &conn_list) {
63 c = list_entry(tmp, struct ptlrpc_connection, c_link);
64 if (peer->peer_nid == c->c_peer.peer_nid &&
65 peer->peer_ni == c->c_peer.peer_ni &&
66 !match_connection_uuid(c, uuid)) {
67 ptlrpc_connection_addref(c);
72 list_for_each_safe(tmp, pos, &conn_unused_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 !match_connection_uuid(c, uuid)) {
77 ptlrpc_connection_addref(c);
79 list_add(&c->c_link, &conn_list);
84 /* FIXME: this should be a slab once we can validate slab addresses
86 OBD_ALLOC(c, sizeof(*c));
95 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
96 INIT_LIST_HEAD(&c->c_imports);
97 INIT_LIST_HEAD(&c->c_exports);
98 INIT_LIST_HEAD(&c->c_sb_chain);
99 INIT_LIST_HEAD(&c->c_recovd_data.rd_managed_chain);
100 INIT_LIST_HEAD(&c->c_delayed_head);
101 atomic_set(&c->c_refcount, 0);
102 memcpy(&c->c_peer, peer, sizeof(c->c_peer));
103 spin_lock_init(&c->c_lock);
105 ptlrpc_connection_addref(c);
107 list_add(&c->c_link, &conn_list);
111 spin_unlock(&conn_lock);
115 int ptlrpc_put_connection(struct ptlrpc_connection *c)
121 CERROR("NULL connection\n");
125 CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
126 c, atomic_read(&c->c_refcount), c->c_peer.peer_nid,
127 c->c_peer.peer_ni->pni_name);
129 if (atomic_dec_and_test(&c->c_refcount)) {
130 recovd_conn_unmanage(c);
131 spin_lock(&conn_lock);
132 list_del(&c->c_link);
133 list_add(&c->c_link, &conn_unused_list);
134 spin_unlock(&conn_lock);
137 if (atomic_read(&c->c_refcount) < 0)
138 CERROR("connection %p refcount %d!\n",
139 c, atomic_read(&c->c_refcount));
144 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
147 atomic_inc(&c->c_refcount);
148 CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
149 c, atomic_read(&c->c_refcount), c->c_peer.peer_nid,
150 c->c_peer.peer_ni->pni_name);
154 void ptlrpc_init_connection(void)
156 INIT_LIST_HEAD(&conn_list);
157 INIT_LIST_HEAD(&conn_unused_list);
158 conn_lock = SPIN_LOCK_UNLOCKED;
161 void ptlrpc_cleanup_connection(void)
163 struct list_head *tmp, *pos;
164 struct ptlrpc_connection *c;
166 spin_lock(&conn_lock);
167 list_for_each_safe(tmp, pos, &conn_unused_list) {
168 c = list_entry(tmp, struct ptlrpc_connection, c_link);
169 list_del(&c->c_link);
170 OBD_FREE(c, sizeof(*c));
172 list_for_each_safe(tmp, pos, &conn_list) {
173 c = list_entry(tmp, struct ptlrpc_connection, c_link);
174 CERROR("Connection %p/%s has refcount %d (nid="LPX64" on %s)\n",
175 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
176 c->c_peer.peer_nid, c->c_peer.peer_ni->pni_name);
177 list_del(&c->c_link);
178 OBD_FREE(c, sizeof(*c));
180 spin_unlock(&conn_lock);