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)
34 struct list_head *tmp, *pos;
35 struct ptlrpc_connection *c;
38 spin_lock(&conn_lock);
39 list_for_each(tmp, &conn_list) {
40 c = list_entry(tmp, struct ptlrpc_connection, c_link);
41 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0) {
42 ptlrpc_connection_addref(c);
47 list_for_each_safe(tmp, pos, &conn_unused_list) {
48 c = list_entry(tmp, struct ptlrpc_connection, c_link);
49 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0) {
50 ptlrpc_connection_addref(c);
52 list_add(&c->c_link, &conn_list);
57 /* FIXME: this should be a slab once we can validate slab addresses
59 OBD_ALLOC(c, sizeof(*c));
63 c->c_level = LUSTRE_CONN_NEW;
69 atomic_set(&c->c_refcount, 0);
70 ptlrpc_connection_addref(c);
71 spin_lock_init(&c->c_lock);
73 memcpy(&c->c_peer, peer, sizeof(c->c_peer));
74 list_add(&c->c_link, &conn_list);
78 spin_unlock(&conn_lock);
82 int ptlrpc_put_connection(struct ptlrpc_connection *c)
87 CDEBUG(D_INFO, "connection=%p\n", c);
88 if (atomic_dec_and_test(&c->c_refcount)) {
89 spin_lock(&conn_lock);
91 list_add(&c->c_link, &conn_unused_list);
92 spin_unlock(&conn_lock);
95 if (atomic_read(&c->c_refcount) < 0)
96 CDEBUG(D_INFO, "refcount < 0 for connection %p!\n", c);
101 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
104 CDEBUG(D_INFO, "connection=%p\n", c);
105 atomic_inc(&c->c_refcount);
109 void ptlrpc_init_connection(void)
111 INIT_LIST_HEAD(&conn_list);
112 INIT_LIST_HEAD(&conn_unused_list);
113 conn_lock = SPIN_LOCK_UNLOCKED;
116 void ptlrpc_cleanup_connection(void)
118 struct list_head *tmp, *pos;
119 struct ptlrpc_connection *c;
121 spin_lock(&conn_lock);
122 list_for_each_safe(tmp, pos, &conn_unused_list) {
123 c = list_entry(tmp, struct ptlrpc_connection, c_link);
124 list_del(&c->c_link);
125 OBD_FREE(c, sizeof(*c));
127 list_for_each_safe(tmp, pos, &conn_list) {
128 c = list_entry(tmp, struct ptlrpc_connection, c_link);
129 CERROR("Connection %p has refcount %d at cleanup (nid=%lu)!\n",
130 c, atomic_read(&c->c_refcount),
131 (unsigned long)c->c_peer.peer_nid);
132 list_del(&c->c_link);
133 OBD_FREE(c, sizeof(*c));
135 spin_unlock(&conn_lock);