Whamcloud - gitweb
Merge b_md into HEAD
[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, struct obd_uuid *uuid)
36 {
37         struct obd_uuid zero_uuid;
38         memset(&zero_uuid, 0, sizeof(zero_uuid));
39
40         if (uuid)
41                 return memcmp(c->c_remote_uuid.uuid, uuid->uuid, 
42                               sizeof(uuid->uuid));
43
44         return memcmp(c->c_remote_uuid.uuid, &zero_uuid, sizeof(zero_uuid));
45 }
46
47 struct ptlrpc_connection *ptlrpc_get_connection(struct lustre_peer *peer,
48                                                 struct obd_uuid *uuid)
49 {
50         struct list_head *tmp, *pos;
51         struct ptlrpc_connection *c;
52         ENTRY;
53
54         CDEBUG(D_INFO, "peer is %08x %08lx %08lx\n",
55                peer->peer_nid, peer->peer_ni.nal_idx, peer->peer_ni.handle_idx);
56
57         spin_lock(&conn_lock);
58         list_for_each(tmp, &conn_list) {
59                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
60                 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0 &&
61                     !match_connection_uuid(c, uuid)) {
62                         ptlrpc_connection_addref(c);
63                         GOTO(out, c);
64                 }
65         }
66
67         list_for_each_safe(tmp, pos, &conn_unused_list) {
68                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
69                 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0 &&
70                     !match_connection_uuid(c, uuid)) {
71                         ptlrpc_connection_addref(c);
72                         list_del(&c->c_link);
73                         list_add(&c->c_link, &conn_list);
74                         GOTO(out, c);
75                 }
76         }
77
78         /* FIXME: this should be a slab once we can validate slab addresses
79          * without OOPSing */
80         OBD_ALLOC(c, sizeof(*c));
81         if (c == NULL)
82                 GOTO(out, c);
83
84         c->c_generation = 1;
85         c->c_epoch = 1;
86         c->c_bootcount = 0;
87         c->c_flags = 0;
88         if (uuid->uuid)
89                 obd_str2uuid(&c->c_remote_uuid, uuid->uuid);
90         INIT_LIST_HEAD(&c->c_imports);
91         INIT_LIST_HEAD(&c->c_exports);
92         INIT_LIST_HEAD(&c->c_sb_chain);
93         INIT_LIST_HEAD(&c->c_recovd_data.rd_managed_chain);
94         INIT_LIST_HEAD(&c->c_delayed_head);
95         atomic_set(&c->c_refcount, 0);
96         ptlrpc_connection_addref(c);
97         spin_lock_init(&c->c_lock);
98
99         memcpy(&c->c_peer, peer, sizeof(c->c_peer));
100         list_add(&c->c_link, &conn_list);
101
102         EXIT;
103  out:
104         spin_unlock(&conn_lock);
105         return c;
106 }
107
108 int ptlrpc_put_connection(struct ptlrpc_connection *c)
109 {
110         int rc = 0;
111         ENTRY;
112
113         if (c == NULL) {
114                 CERROR("NULL connection\n");
115                 RETURN(0);
116         }
117
118         CDEBUG(D_INFO, "connection=%p refcount %d\n",
119                c, atomic_read(&c->c_refcount) - 1);
120         if (atomic_dec_and_test(&c->c_refcount)) {
121                 recovd_conn_unmanage(c);
122                 spin_lock(&conn_lock);
123                 list_del(&c->c_link);
124                 list_add(&c->c_link, &conn_unused_list);
125                 spin_unlock(&conn_lock);
126                 rc = 1;
127         }
128         if (atomic_read(&c->c_refcount) < 0)
129                 CERROR("connection %p refcount %d!\n",
130                        c, atomic_read(&c->c_refcount));
131
132         RETURN(rc);
133 }
134
135 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
136 {
137         ENTRY;
138         CDEBUG(D_INFO, "connection=%p refcount %d\n",
139                c, atomic_read(&c->c_refcount) + 1);
140         atomic_inc(&c->c_refcount);
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=%lu)\n",
165                        c, c->c_remote_uuid.uuid, atomic_read(&c->c_refcount),
166                        (unsigned long)c->c_peer.peer_nid);
167                 list_del(&c->c_link);
168                 OBD_FREE(c, sizeof(*c));
169         }
170         spin_unlock(&conn_lock);
171 }