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 #include "ptlrpc_internal.h"
34 static spinlock_t conn_lock;
35 static struct list_head conn_list;
36 static struct list_head conn_unused_list;
38 void ptlrpc_dump_connections(void)
40 struct list_head *tmp;
41 struct ptlrpc_connection *c;
44 list_for_each(tmp, &conn_list) {
45 c = list_entry(tmp, struct ptlrpc_connection, c_link);
46 CERROR("Connection %p/%s has refcount %d (nid=%s)\n",
47 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
48 libcfs_nid2str(c->c_peer.nid));
53 struct ptlrpc_connection *ptlrpc_get_connection(ptl_process_id_t peer,
54 struct obd_uuid *uuid)
56 struct list_head *tmp, *pos;
57 struct ptlrpc_connection *c;
60 CDEBUG(D_INFO, "peer is %s\n", libcfs_id2str(peer));
62 spin_lock(&conn_lock);
63 list_for_each(tmp, &conn_list) {
64 c = list_entry(tmp, struct ptlrpc_connection, c_link);
65 if (memcmp(&peer, &c->c_peer, sizeof(peer)) == 0) {
66 ptlrpc_connection_addref(c);
71 list_for_each_safe(tmp, pos, &conn_unused_list) {
72 c = list_entry(tmp, struct ptlrpc_connection, c_link);
73 if (memcmp(&peer, &c->c_peer, sizeof(peer)) == 0) {
74 ptlrpc_connection_addref(c);
76 list_add(&c->c_link, &conn_list);
81 /* FIXME: this should be a slab once we can validate slab addresses
83 OBD_ALLOC_GFP(c, sizeof(*c), GFP_ATOMIC);
88 if (uuid && uuid->uuid) /* XXX ???? */
89 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
90 atomic_set(&c->c_refcount, 0);
91 memcpy(&c->c_peer, &peer, sizeof(c->c_peer));
93 ptlrpc_connection_addref(c);
95 list_add(&c->c_link, &conn_list);
99 spin_unlock(&conn_lock);
103 int ptlrpc_put_connection(struct ptlrpc_connection *c)
109 CERROR("NULL connection\n");
113 CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
114 c, atomic_read(&c->c_refcount) - 1,
115 libcfs_nid2str(c->c_peer.nid));
117 if (atomic_dec_and_test(&c->c_refcount)) {
118 spin_lock(&conn_lock);
119 list_del(&c->c_link);
120 list_add(&c->c_link, &conn_unused_list);
121 spin_unlock(&conn_lock);
124 if (atomic_read(&c->c_refcount) < 0)
125 CERROR("connection %p refcount %d!\n",
126 c, atomic_read(&c->c_refcount));
131 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
134 atomic_inc(&c->c_refcount);
135 CDEBUG (D_INFO, "connection=%p refcount %d to %s\n",
136 c, atomic_read(&c->c_refcount),
137 libcfs_nid2str(c->c_peer.nid));
141 void ptlrpc_init_connection(void)
143 INIT_LIST_HEAD(&conn_list);
144 INIT_LIST_HEAD(&conn_unused_list);
145 conn_lock = SPIN_LOCK_UNLOCKED;
148 void ptlrpc_cleanup_connection(void)
150 struct list_head *tmp, *pos;
151 struct ptlrpc_connection *c;
153 spin_lock(&conn_lock);
154 list_for_each_safe(tmp, pos, &conn_unused_list) {
155 c = list_entry(tmp, struct ptlrpc_connection, c_link);
156 list_del(&c->c_link);
157 OBD_FREE(c, sizeof(*c));
159 list_for_each_safe(tmp, pos, &conn_list) {
160 c = list_entry(tmp, struct ptlrpc_connection, c_link);
161 CERROR("Connection %p/%s has refcount %d (nid=%s)\n",
162 c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
163 libcfs_nid2str(c->c_peer.nid));
164 list_del(&c->c_link);
165 OBD_FREE(c, sizeof(*c));
167 spin_unlock(&conn_lock);