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 static spinlock_t conn_lock;
30 static struct list_head conn_list;
31 static struct list_head conn_unused_list;
33 /* If UUID is NULL, c->c_remote_uuid must be all zeroes
34 * If UUID is non-NULL, c->c_remote_uuid must match. */
35 static int match_connection_uuid(struct ptlrpc_connection *c, obd_uuid_t uuid)
37 obd_uuid_t zero_uuid = {0};
40 return memcmp(c->c_remote_uuid, uuid, sizeof(uuid));
42 return memcmp(c->c_remote_uuid, zero_uuid, sizeof(zero_uuid));
45 struct ptlrpc_connection *ptlrpc_get_connection(struct lustre_peer *peer,
48 struct list_head *tmp, *pos;
49 struct ptlrpc_connection *c;
52 CDEBUG(D_INFO, "peer is %08x %08lx %08lx\n",
53 peer->peer_nid, peer->peer_ni.nal_idx, peer->peer_ni.handle_idx);
55 spin_lock(&conn_lock);
56 list_for_each(tmp, &conn_list) {
57 c = list_entry(tmp, struct ptlrpc_connection, c_link);
58 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0 &&
59 !match_connection_uuid(c, uuid)) {
60 ptlrpc_connection_addref(c);
65 list_for_each_safe(tmp, pos, &conn_unused_list) {
66 c = list_entry(tmp, struct ptlrpc_connection, c_link);
67 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0 &&
68 !match_connection_uuid(c, uuid)) {
69 ptlrpc_connection_addref(c);
71 list_add(&c->c_link, &conn_list);
76 /* FIXME: this should be a slab once we can validate slab addresses
78 OBD_ALLOC(c, sizeof(*c));
87 strcpy(c->c_remote_uuid, uuid);
88 INIT_LIST_HEAD(&c->c_imports);
89 INIT_LIST_HEAD(&c->c_exports);
90 INIT_LIST_HEAD(&c->c_sb_chain);
91 INIT_LIST_HEAD(&c->c_recovd_data.rd_managed_chain);
92 INIT_LIST_HEAD(&c->c_delayed_head);
93 atomic_set(&c->c_refcount, 0);
94 ptlrpc_connection_addref(c);
95 spin_lock_init(&c->c_lock);
97 memcpy(&c->c_peer, peer, sizeof(c->c_peer));
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\n",
117 c, atomic_read(&c->c_refcount) - 1);
118 if (atomic_dec_and_test(&c->c_refcount)) {
119 recovd_conn_unmanage(c);
120 spin_lock(&conn_lock);
121 list_del(&c->c_link);
122 list_add(&c->c_link, &conn_unused_list);
123 spin_unlock(&conn_lock);
126 if (atomic_read(&c->c_refcount) < 0)
127 CERROR("connection %p refcount %d!\n",
128 c, atomic_read(&c->c_refcount));
133 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
136 CDEBUG(D_INFO, "connection=%p refcount %d\n",
137 c, atomic_read(&c->c_refcount) + 1);
138 atomic_inc(&c->c_refcount);
142 void ptlrpc_init_connection(void)
144 INIT_LIST_HEAD(&conn_list);
145 INIT_LIST_HEAD(&conn_unused_list);
146 conn_lock = SPIN_LOCK_UNLOCKED;
149 void ptlrpc_cleanup_connection(void)
151 struct list_head *tmp, *pos;
152 struct ptlrpc_connection *c;
154 spin_lock(&conn_lock);
155 list_for_each_safe(tmp, pos, &conn_unused_list) {
156 c = list_entry(tmp, struct ptlrpc_connection, c_link);
157 list_del(&c->c_link);
158 OBD_FREE(c, sizeof(*c));
160 list_for_each_safe(tmp, pos, &conn_list) {
161 c = list_entry(tmp, struct ptlrpc_connection, c_link);
162 CERROR("Connection %p/%s has refcount %d (nid=%lu)\n",
163 c, c->c_remote_uuid, atomic_read(&c->c_refcount),
164 (unsigned long)c->c_peer.peer_nid);
165 list_del(&c->c_link);
166 OBD_FREE(c, sizeof(*c));
168 spin_unlock(&conn_lock);