Whamcloud - gitweb
* Landed b_cray_portals_merge (3148, 3158)
[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         char str[PTL_NALFMT_SIZE];
41         struct list_head *tmp;
42         struct ptlrpc_connection *c;
43         ENTRY;
44
45         list_for_each(tmp, &conn_list) {
46                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
47                 CERROR("Connection %p/%s has refcount %d (nid=%s on %s)\n",
48                        c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
49                        ptlrpc_peernid2str(&c->c_peer, str),
50                        c->c_peer.peer_ni->pni_name);
51         }
52         EXIT;
53 }
54
55 struct ptlrpc_connection *ptlrpc_get_connection(struct ptlrpc_peer *peer,
56                                                 struct obd_uuid *uuid)
57 {
58         char str[PTL_NALFMT_SIZE];
59         struct list_head *tmp, *pos;
60         struct ptlrpc_connection *c;
61         ENTRY;
62
63
64         CDEBUG(D_INFO, "peer is %s on %s\n",
65                ptlrpc_peernid2str(peer, str), peer->peer_ni->pni_name);
66
67         spin_lock(&conn_lock);
68         list_for_each(tmp, &conn_list) {
69                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
70                 if (peer->peer_nid == c->c_peer.peer_nid &&
71                     peer->peer_ni == c->c_peer.peer_ni) {
72                         ptlrpc_connection_addref(c);
73                         GOTO(out, c);
74                 }
75         }
76
77         list_for_each_safe(tmp, pos, &conn_unused_list) {
78                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
79                 if (peer->peer_nid == c->c_peer.peer_nid &&
80                     peer->peer_ni == c->c_peer.peer_ni) {
81                         ptlrpc_connection_addref(c);
82                         list_del(&c->c_link);
83                         list_add(&c->c_link, &conn_list);
84                         GOTO(out, c);
85                 }
86         }
87
88         /* FIXME: this should be a slab once we can validate slab addresses
89          * without OOPSing */
90         OBD_ALLOC_GFP(c, sizeof(*c), GFP_ATOMIC);
91         
92         if (c == NULL)
93                 GOTO(out, c);
94
95         if (uuid && uuid->uuid)                         /* XXX ???? */
96                 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
97         atomic_set(&c->c_refcount, 0);
98         memcpy(&c->c_peer, peer, sizeof(c->c_peer));
99
100         ptlrpc_connection_addref(c);
101
102         list_add(&c->c_link, &conn_list);
103
104         EXIT;
105  out:
106         spin_unlock(&conn_lock);
107         return c;
108 }
109
110 int ptlrpc_put_connection(struct ptlrpc_connection *c)
111 {
112         char str[PTL_NALFMT_SIZE];
113         int rc = 0;
114         ENTRY;
115
116         if (c == NULL) {
117                 CERROR("NULL connection\n");
118                 RETURN(0);
119         }
120
121         CDEBUG (D_INFO, "connection=%p refcount %d to %s on %s\n",
122                 c, atomic_read(&c->c_refcount) - 1, 
123                 ptlrpc_peernid2str(&c->c_peer, str),
124                 c->c_peer.peer_ni->pni_name);
125
126         if (atomic_dec_and_test(&c->c_refcount)) {
127                 spin_lock(&conn_lock);
128                 list_del(&c->c_link);
129                 list_add(&c->c_link, &conn_unused_list);
130                 spin_unlock(&conn_lock);
131                 rc = 1;
132         }
133         if (atomic_read(&c->c_refcount) < 0)
134                 CERROR("connection %p refcount %d!\n",
135                        c, atomic_read(&c->c_refcount));
136
137         RETURN(rc);
138 }
139
140 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
141 {
142         char str[PTL_NALFMT_SIZE];
143         ENTRY;
144         atomic_inc(&c->c_refcount);
145         CDEBUG (D_INFO, "connection=%p refcount %d to %s on %s\n",
146                 c, atomic_read(&c->c_refcount),
147                 ptlrpc_peernid2str(&c->c_peer, str),
148                 c->c_peer.peer_ni->pni_name);
149         RETURN(c);
150 }
151
152 void ptlrpc_init_connection(void)
153 {
154         INIT_LIST_HEAD(&conn_list);
155         INIT_LIST_HEAD(&conn_unused_list);
156         conn_lock = SPIN_LOCK_UNLOCKED;
157 }
158
159 void ptlrpc_cleanup_connection(void)
160 {
161         char str[PTL_NALFMT_SIZE];
162         struct list_head *tmp, *pos;
163         struct ptlrpc_connection *c;
164
165         spin_lock(&conn_lock);
166         list_for_each_safe(tmp, pos, &conn_unused_list) {
167                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
168                 list_del(&c->c_link);
169                 OBD_FREE(c, sizeof(*c));
170         }
171         list_for_each_safe(tmp, pos, &conn_list) {
172                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
173                 CERROR("Connection %p/%s has refcount %d (nid=%s on %s)\n",
174                        c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
175                        ptlrpc_peernid2str(&c->c_peer, str),
176                        c->c_peer.peer_ni->pni_name);
177                 list_del(&c->c_link);
178                 OBD_FREE(c, sizeof(*c));
179         }
180         spin_unlock(&conn_lock);
181 }