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