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 struct ptlrpc_connection *ptlrpc_get_connection(struct lustre_peer *peer,
35 struct list_head *tmp, *pos;
36 struct ptlrpc_connection *c;
39 CDEBUG(D_INFO, "peer is %08x %08lx %08lx\n",
40 peer->peer_nid, peer->peer_ni.nal_idx, peer->peer_ni.handle_idx);
42 spin_lock(&conn_lock);
43 list_for_each(tmp, &conn_list) {
44 c = list_entry(tmp, struct ptlrpc_connection, c_link);
45 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0 &&
46 (!uuid || strcmp(c->c_remote_uuid, uuid) == 0)) {
47 ptlrpc_connection_addref(c);
52 list_for_each_safe(tmp, pos, &conn_unused_list) {
53 c = list_entry(tmp, struct ptlrpc_connection, c_link);
54 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0 &&
55 (!uuid || strcmp(c->c_remote_uuid, uuid) == 0)) {
56 ptlrpc_connection_addref(c);
58 list_add(&c->c_link, &conn_list);
63 /* FIXME: this should be a slab once we can validate slab addresses
65 OBD_ALLOC(c, sizeof(*c));
69 c->c_level = LUSTRE_CONN_NEW;
75 strcpy(c->c_remote_uuid, uuid);
76 INIT_LIST_HEAD(&c->c_delayed_head);
77 INIT_LIST_HEAD(&c->c_sending_head);
78 INIT_LIST_HEAD(&c->c_dying_head);
79 INIT_LIST_HEAD(&c->c_imports);
80 INIT_LIST_HEAD(&c->c_exports);
81 atomic_set(&c->c_refcount, 0);
82 ptlrpc_connection_addref(c);
83 spin_lock_init(&c->c_lock);
85 memcpy(&c->c_peer, peer, sizeof(c->c_peer));
86 list_add(&c->c_link, &conn_list);
90 spin_unlock(&conn_lock);
94 int ptlrpc_put_connection(struct ptlrpc_connection *c)
99 CDEBUG(D_INFO, "connection=%p refcount %d\n",
100 c, atomic_read(&c->c_refcount) - 1);
101 if (atomic_dec_and_test(&c->c_refcount)) {
102 spin_lock(&conn_lock);
103 list_del(&c->c_link);
104 list_add(&c->c_link, &conn_unused_list);
105 spin_unlock(&conn_lock);
108 if (atomic_read(&c->c_refcount) < 0)
109 CERROR("connection %p refcount %d!\n",
110 c, atomic_read(&c->c_refcount));
115 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
118 CDEBUG(D_INFO, "connection=%p refcount %d\n",
119 c, atomic_read(&c->c_refcount) + 1);
120 atomic_inc(&c->c_refcount);
124 void ptlrpc_init_connection(void)
126 INIT_LIST_HEAD(&conn_list);
127 INIT_LIST_HEAD(&conn_unused_list);
128 conn_lock = SPIN_LOCK_UNLOCKED;
131 void ptlrpc_cleanup_connection(void)
133 struct list_head *tmp, *pos;
134 struct ptlrpc_connection *c;
136 spin_lock(&conn_lock);
137 list_for_each_safe(tmp, pos, &conn_unused_list) {
138 c = list_entry(tmp, struct ptlrpc_connection, c_link);
139 list_del(&c->c_link);
140 OBD_FREE(c, sizeof(*c));
142 list_for_each_safe(tmp, pos, &conn_list) {
143 c = list_entry(tmp, struct ptlrpc_connection, c_link);
144 CERROR("Connection %p has refcount %d at cleanup (nid=%lu)!\n",
145 c, atomic_read(&c->c_refcount),
146 (unsigned long)c->c_peer.peer_nid);
147 list_del(&c->c_link);
148 OBD_FREE(c, sizeof(*c));
150 spin_unlock(&conn_lock);