Whamcloud - gitweb
* newconfig liblustre/tcpnal runs sanity OK
[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=%s)\n",
47                        c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
48                        libcfs_nid2str(c->c_peer.nid));
49         }
50         EXIT;
51 }
52
53 struct ptlrpc_connection *ptlrpc_get_connection(ptl_process_id_t peer,
54                                                 struct obd_uuid *uuid)
55 {
56         struct list_head *tmp, *pos;
57         struct ptlrpc_connection *c;
58         ENTRY;
59
60         CDEBUG(D_INFO, "peer is %s\n", libcfs_id2str(peer));
61
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);
67                         GOTO(out, c);
68                 }
69         }
70
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);
75                         list_del(&c->c_link);
76                         list_add(&c->c_link, &conn_list);
77                         GOTO(out, c);
78                 }
79         }
80
81         /* FIXME: this should be a slab once we can validate slab addresses
82          * without OOPSing */
83         OBD_ALLOC_GFP(c, sizeof(*c), GFP_ATOMIC);
84         
85         if (c == NULL)
86                 GOTO(out, c);
87
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));
92
93         ptlrpc_connection_addref(c);
94
95         list_add(&c->c_link, &conn_list);
96
97         EXIT;
98  out:
99         spin_unlock(&conn_lock);
100         return c;
101 }
102
103 int ptlrpc_put_connection(struct ptlrpc_connection *c)
104 {
105         int rc = 0;
106         ENTRY;
107
108         if (c == NULL) {
109                 CERROR("NULL connection\n");
110                 RETURN(0);
111         }
112
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));
116
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);
122                 rc = 1;
123         }
124         if (atomic_read(&c->c_refcount) < 0)
125                 CERROR("connection %p refcount %d!\n",
126                        c, atomic_read(&c->c_refcount));
127
128         RETURN(rc);
129 }
130
131 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
132 {
133         ENTRY;
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));
138         RETURN(c);
139 }
140
141 void ptlrpc_init_connection(void)
142 {
143         INIT_LIST_HEAD(&conn_list);
144         INIT_LIST_HEAD(&conn_unused_list);
145         conn_lock = SPIN_LOCK_UNLOCKED;
146 }
147
148 void ptlrpc_cleanup_connection(void)
149 {
150         struct list_head *tmp, *pos;
151         struct ptlrpc_connection *c;
152
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));
158         }
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));
166         }
167         spin_unlock(&conn_lock);
168 }