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