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