Whamcloud - gitweb
- Fix two leaks
[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/lustre_net.h>
26
27 static spinlock_t conn_lock;
28 static struct list_head conn_list;
29 static struct list_head conn_unused_list;
30
31 struct ptlrpc_connection *ptlrpc_get_connection(struct lustre_peer *peer)
32 {
33         struct list_head *tmp, *pos;
34         struct ptlrpc_connection *c;
35         ENTRY;
36
37         spin_lock(&conn_lock);
38         list_for_each(tmp, &conn_list) {
39                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
40                 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0) {
41                         ptlrpc_connection_addref(c);
42                         GOTO(out, c);
43                 }
44         }
45
46         list_for_each_safe(tmp, pos, &conn_unused_list) {
47                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
48                 if (memcmp(peer, &c->c_peer, sizeof(*peer)) == 0) {
49                         ptlrpc_connection_addref(c);
50                         list_del(&c->c_link);
51                         list_add(&c->c_link, &conn_list);
52                         GOTO(out, c);
53                 }
54         }
55
56         /* FIXME: this should be a slab once we can validate slab addresses
57          * without OOPSing */
58         OBD_ALLOC(c, sizeof(*c));
59         if (c == NULL)
60                 GOTO(out, c);
61
62         c->c_level = LUSTRE_CONN_NEW;
63         c->c_xid_in = 1;
64         c->c_xid_out = 1;
65         c->c_generation = 1;
66         c->c_epoch = 1;
67         c->c_bootcount = 0;
68         atomic_set(&c->c_refcount, 0);
69         ptlrpc_connection_addref(c);
70         spin_lock_init(&c->c_lock);
71
72         memcpy(&c->c_peer, peer, sizeof(c->c_peer));
73         list_add(&c->c_link, &conn_list);
74
75         EXIT;
76  out:
77         spin_unlock(&conn_lock);
78         return c;
79 }
80
81 int ptlrpc_put_connection(struct ptlrpc_connection *c)
82 {
83         int rc = 0;
84         ENTRY;
85
86         CDEBUG(D_INFO, "connection=%p\n", c);
87         if (atomic_dec_and_test(&c->c_refcount)) {
88                 spin_lock(&conn_lock);
89                 list_del(&c->c_link);
90                 list_add(&c->c_link, &conn_unused_list);
91                 spin_unlock(&conn_lock);
92                 rc = 1;
93         }
94         if (atomic_read(&c->c_refcount) < 0)
95                 CDEBUG(D_INFO, "refcount < 0 for connection %p!\n", c);
96
97         RETURN(rc);
98 }
99
100 struct ptlrpc_connection *ptlrpc_connection_addref(struct ptlrpc_connection *c)
101 {
102         ENTRY;
103         CDEBUG(D_INFO, "connection=%p\n", c);
104         atomic_inc(&c->c_refcount);
105         RETURN(c);
106 }
107
108 void ptlrpc_init_connection(void)
109 {
110         INIT_LIST_HEAD(&conn_list);
111         INIT_LIST_HEAD(&conn_unused_list);
112         conn_lock = SPIN_LOCK_UNLOCKED;
113 }
114
115 void ptlrpc_cleanup_connection(void)
116 {
117         struct list_head *tmp, *pos;
118         struct ptlrpc_connection *c;
119
120         spin_lock(&conn_lock);
121         list_for_each_safe(tmp, pos, &conn_unused_list) {
122                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
123                 list_del(&c->c_link);
124                 OBD_FREE(c, sizeof(*c));
125         }
126         list_for_each_safe(tmp, pos, &conn_list) {
127                 c = list_entry(tmp, struct ptlrpc_connection, c_link);
128                 CERROR("Connection %p has refcount %d at cleanup (nid=%lu)!\n",
129                        c, atomic_read(&c->c_refcount),
130                        (unsigned long)c->c_peer.peer_nid);
131                 list_del(&c->c_link);
132                 OBD_FREE(c, sizeof(*c));
133         }
134         spin_unlock(&conn_lock);
135 }