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/lustre_net.h>
28 static spinlock_t conn_lock;
29 static struct list_head conn_list;
30 static struct list_head conn_unused_list;
32 /* If UUID is NULL, c->c_remote_uuid must be all zeroes
33 * If UUID is non-NULL, c->c_remote_uuid must match. */
34 static int match_connection_uuid(struct ptlrpc_connection *c, obd_uuid_t uuid)
36 obd_uuid_t zero_uuid = {0};
39 return memcmp(c->c_remote_uuid, uuid, sizeof(uuid));
41 return memcmp(c->c_remote_uuid, zero_uuid, sizeof(zero_uuid));
44 struct ptlrpc_connection *ptlrpc_get_connection(struct lustre_peer *peer,
47 struct list_head *tmp, *pos;
48 struct ptlrpc_connection *c;
51 CDEBUG(D_INFO, "peer is %08x %08lx %08lx\n",
52 peer->peer_nid, peer->peer_ni.nal_idx, peer->peer_ni.handle_idx);
54 spin_lock(&conn_lock);
55 list_for_each(tmp, &conn_list) {
56 c = list_entry(tmp, struct ptlrpc_connection, c_link);
57 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0 &&
58 !match_connection_uuid(c, uuid)) {
59 ptlrpc_connection_addref(c);
64 list_for_each_safe(tmp, pos, &conn_unused_list) {
65 c = list_entry(tmp, struct ptlrpc_connection, c_link);
66 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0 &&
67 !match_connection_uuid(c, uuid)) {
68 ptlrpc_connection_addref(c);
70 list_add(&c->c_link, &conn_list);
75 /* FIXME: this should be a slab once we can validate slab addresses
77 OBD_ALLOC(c, sizeof(*c));
81 c->c_level = LUSTRE_CONN_NEW;
89 strcpy(c->c_remote_uuid, uuid);
90 INIT_LIST_HEAD(&c->c_delayed_head);
91 INIT_LIST_HEAD(&c->c_sending_head);
92 INIT_LIST_HEAD(&c->c_dying_head);
93 INIT_LIST_HEAD(&c->c_imports);
94 INIT_LIST_HEAD(&c->c_exports);
95 INIT_LIST_HEAD(&c->c_sb_chain);
96 INIT_LIST_HEAD(&c->c_recovd_data.rd_managed_chain);
97 atomic_set(&c->c_refcount, 0);
98 ptlrpc_connection_addref(c);
99 spin_lock_init(&c->c_lock);
101 memcpy(&c->c_peer, peer, sizeof(c->c_peer));
102 list_add(&c->c_link, &conn_list);
106 spin_unlock(&conn_lock);
110 int ptlrpc_put_connection(struct ptlrpc_connection *c)
116 CERROR("NULL connection\n");
120 CDEBUG(D_INFO, "connection=%p refcount %d\n",
121 c, atomic_read(&c->c_refcount) - 1);
122 if (atomic_dec_and_test(&c->c_refcount)) {
123 spin_lock(&conn_lock);
124 list_del(&c->c_link);
125 list_add(&c->c_link, &conn_unused_list);
126 spin_unlock(&conn_lock);
129 if (atomic_read(&c->c_refcount) < 0)
130 CERROR("connection %p refcount %d!\n",
131 c, atomic_read(&c->c_refcount));
136 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
139 CDEBUG(D_INFO, "connection=%p refcount %d\n",
140 c, atomic_read(&c->c_refcount) + 1);
141 atomic_inc(&c->c_refcount);
145 void ptlrpc_init_connection(void)
147 INIT_LIST_HEAD(&conn_list);
148 INIT_LIST_HEAD(&conn_unused_list);
149 conn_lock = SPIN_LOCK_UNLOCKED;
152 void ptlrpc_cleanup_connection(void)
154 struct list_head *tmp, *pos;
155 struct ptlrpc_connection *c;
157 spin_lock(&conn_lock);
158 list_for_each_safe(tmp, pos, &conn_unused_list) {
159 c = list_entry(tmp, struct ptlrpc_connection, c_link);
160 list_del(&c->c_link);
161 OBD_FREE(c, sizeof(*c));
163 list_for_each_safe(tmp, pos, &conn_list) {
164 c = list_entry(tmp, struct ptlrpc_connection, c_link);
165 CERROR("Connection %p has refcount %d at cleanup (nid=%lu)!\n",
166 c, atomic_read(&c->c_refcount),
167 (unsigned long)c->c_peer.peer_nid);
168 list_del(&c->c_link);
169 OBD_FREE(c, sizeof(*c));
171 spin_unlock(&conn_lock);