Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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(c, sizeof(*c));
88         if (c == NULL)
89                 GOTO(out, c);
90
91         if (uuid && uuid->uuid)                         /* XXX ???? */
92                 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
93         atomic_set(&c->c_refcount, 0);
94         memcpy(&c->c_peer, peer, sizeof(c->c_peer));
95
96         ptlrpc_connection_addref(c);
97
98         list_add(&c->c_link, &conn_list);
99
100         EXIT;
101  out:
102         spin_unlock(&conn_lock);
103         return c;
104 }
105
106 int ptlrpc_put_connection(struct ptlrpc_connection *c)
107 {
108         int rc = 0;
109         ENTRY;
110
111         if (c == NULL) {
112                 CERROR("NULL connection\n");
113                 RETURN(0);
114         }
115
116         CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
117                 c, atomic_read(&c->c_refcount) - 1, c->c_peer.peer_nid,
118                 c->c_peer.peer_ni->pni_name);
119
120         if (atomic_dec_and_test(&c->c_refcount)) {
121                 spin_lock(&conn_lock);
122                 list_del(&c->c_link);
123                 list_add(&c->c_link, &conn_unused_list);
124                 spin_unlock(&conn_lock);
125                 rc = 1;
126         }
127         if (atomic_read(&c->c_refcount) < 0)
128                 CERROR("connection %p refcount %d!\n",
129                        c, atomic_read(&c->c_refcount));
130
131         RETURN(rc);
132 }
133
134 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
135 {
136         ENTRY;
137         atomic_inc(&c->c_refcount);
138         CDEBUG (D_INFO, "connection=%p refcount %d to "LPX64" on %s\n",
139                 c, atomic_read(&c->c_refcount), c->c_peer.peer_nid,
140                 c->c_peer.peer_ni->pni_name);
141         RETURN(c);
142 }
143
144 void ptlrpc_init_connection(void)
145 {
146         INIT_LIST_HEAD(&conn_list);
147         INIT_LIST_HEAD(&conn_unused_list);
148         conn_lock = SPIN_LOCK_UNLOCKED;
149 }
150
151 void ptlrpc_cleanup_connection(void)
152 {
153         struct list_head *tmp, *pos;
154         struct ptlrpc_connection *c;
155
156         spin_lock(&conn_lock);
157         list_for_each_safe(tmp, pos, &conn_unused_list) {
158                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
159                 list_del(&c->c_link);
160                 OBD_FREE(c, sizeof(*c));
161         }
162         list_for_each_safe(tmp, pos, &conn_list) {
163                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
164                 CERROR("Connection %p/%s has refcount %d (nid="LPX64" on %s)\n",
165                        c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
166                        c->c_peer.peer_nid, c->c_peer.peer_ni->pni_name);
167                 list_del(&c->c_link);
168                 OBD_FREE(c, sizeof(*c));
169         }
170         spin_unlock(&conn_lock);
171 }