Whamcloud - gitweb
c6a4163e9c4f4b89abf3a5f4ce54ab16126404a6
[fs/lustre-release.git] / lustre / ptlrpc / connection.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24 #ifdef __KERNEL__
25 #include <linux/obd_support.h>
26 #include <linux/obd_class.h>
27 #include <linux/lustre_net.h>
28 #else
29 #include <liblustre.h>
30 #endif
31
32 #include "ptlrpc_internal.h"
33
34 static spinlock_t conn_lock;
35 static struct list_head conn_list;
36 static struct list_head conn_unused_list;
37
38 void ptlrpc_dump_connections(void)
39 {
40         struct list_head *tmp;
41         struct ptlrpc_connection *c;
42         ENTRY;
43
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="LPX64" on %s)\n",
47                        c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
48                        c->c_peer.peer_nid, c->c_peer.peer_ni->pni_name);
49         }
50         EXIT;
51 }
52
53 struct ptlrpc_connection *ptlrpc_get_connection(struct ptlrpc_peer *peer,
54                                                 struct obd_uuid *uuid)
55 {
56         struct list_head *tmp, *pos;
57         struct ptlrpc_connection *c;
58         ENTRY;
59
60
61         CDEBUG(D_INFO, "peer is "LPX64" on %s\n",
62                peer->peer_nid, peer->peer_ni->pni_name);
63
64         spin_lock(&conn_lock);
65         list_for_each(tmp, &conn_list) {
66                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
67                 if (peer->peer_nid == c->c_peer.peer_nid &&
68                     peer->peer_ni == c->c_peer.peer_ni) {
69                         ptlrpc_connection_addref(c);
70                         GOTO(out, c);
71                 }
72         }
73
74         list_for_each_safe(tmp, pos, &conn_unused_list) {
75                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
76                 if (peer->peer_nid == c->c_peer.peer_nid &&
77                     peer->peer_ni == c->c_peer.peer_ni) {
78                         ptlrpc_connection_addref(c);
79                         list_del(&c->c_link);
80                         list_add(&c->c_link, &conn_list);
81                         GOTO(out, c);
82                 }
83         }
84
85         /* FIXME: this should be a slab once we can validate slab addresses
86          * without OOPSing */
87         OBD_ALLOC_GFP(c, sizeof(*c), GFP_ATOMIC);
88         
89         if (c == NULL)
90                 GOTO(out, c);
91
92         if (uuid && uuid->uuid)                         /* XXX ???? */
93                 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
94         atomic_set(&c->c_refcount, 0);
95         memcpy(&c->c_peer, peer, sizeof(c->c_peer));
96
97         ptlrpc_connection_addref(c);
98
99         list_add(&c->c_link, &conn_list);
100
101         EXIT;
102  out:
103         spin_unlock(&conn_lock);
104         return c;
105 }
106
107 int ptlrpc_put_connection(struct ptlrpc_connection *c)
108 {
109         int rc = 0;
110         ENTRY;
111
112         if (c == NULL) {
113                 CERROR("NULL connection\n");
114                 RETURN(0);
115         }
116
117         CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
118                 c, atomic_read(&c->c_refcount) - 1, c->c_peer.peer_nid,
119                 c->c_peer.peer_ni->pni_name);
120
121         if (atomic_dec_and_test(&c->c_refcount)) {
122                 spin_lock(&conn_lock);
123                 list_del(&c->c_link);
124                 list_add(&c->c_link, &conn_unused_list);
125                 spin_unlock(&conn_lock);
126                 rc = 1;
127         }
128         if (atomic_read(&c->c_refcount) < 0)
129                 CERROR("connection %p refcount %d!\n",
130                        c, atomic_read(&c->c_refcount));
131
132         RETURN(rc);
133 }
134
135 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
136 {
137         ENTRY;
138         atomic_inc(&c->c_refcount);
139         CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
140                 c, atomic_read(&c->c_refcount), c->c_peer.peer_nid,
141                 c->c_peer.peer_ni->pni_name);
142         RETURN(c);
143 }
144
145 void ptlrpc_init_connection(void)
146 {
147         INIT_LIST_HEAD(&conn_list);
148         INIT_LIST_HEAD(&conn_unused_list);
149         conn_lock = SPIN_LOCK_UNLOCKED;
150 }
151
152 void ptlrpc_cleanup_connection(void)
153 {
154         struct list_head *tmp, *pos;
155         struct ptlrpc_connection *c;
156
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));
162         }
163         list_for_each_safe(tmp, pos, &conn_list) {
164                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
165                 CERROR("Connection %p/%s has refcount %d (nid="LPX64" on %s)\n",
166                        c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
167                        c->c_peer.peer_nid, c->c_peer.peer_ni->pni_name);
168                 list_del(&c->c_link);
169                 OBD_FREE(c, sizeof(*c));
170         }
171         spin_unlock(&conn_lock);
172 }