Whamcloud - gitweb
21a24a2753e71a7e4530e3d0b4ed4d76cdffced3
[fs/lustre-release.git] / lustre / ptlrpc / connection.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_RPC
34 #include <obd_support.h>
35 #include <obd_class.h>
36 #include <lustre_net.h>
37
38 #include "ptlrpc_internal.h"
39
40 static struct rhashtable conn_hash;
41
42 /*
43  * struct lnet_process_id may contain unassigned bytes which might not
44  * be zero, so we cannot just hash and compare bytes.
45  */
46
47 static u32 lnet_process_id_hash(const void *data, u32 len, u32 seed)
48 {
49         const struct lnet_process_id *lpi = data;
50
51         seed = hash_32(seed ^ lpi->pid, 32);
52         seed ^= hash_64(lpi->nid, 32);
53         return seed;
54 }
55
56 static int lnet_process_id_cmp(struct rhashtable_compare_arg *arg,
57                                const void *obj)
58 {
59         const struct lnet_process_id *lpi = arg->key;
60         const struct ptlrpc_connection *con = obj;
61
62         if (lpi->nid == con->c_peer.nid &&
63             lpi->pid == con->c_peer.pid)
64                 return 0;
65         return -ESRCH;
66 }
67
68 static const struct rhashtable_params conn_hash_params = {
69         .key_len        = 1,    /* actually variable-length */
70         .key_offset     = offsetof(struct ptlrpc_connection, c_peer),
71         .head_offset    = offsetof(struct ptlrpc_connection, c_hash),
72         .hashfn         = lnet_process_id_hash,
73         .obj_cmpfn      = lnet_process_id_cmp,
74 };
75
76 struct ptlrpc_connection *
77 ptlrpc_connection_get(struct lnet_process_id peer, lnet_nid_t self,
78                       struct obd_uuid *uuid)
79 {
80         struct ptlrpc_connection *conn, *conn2;
81         ENTRY;
82
83         peer.nid = LNetPrimaryNID(peer.nid);
84         conn = rhashtable_lookup_fast(&conn_hash, &peer, conn_hash_params);
85         if (conn) {
86                 ptlrpc_connection_addref(conn);
87                 GOTO(out, conn);
88         }
89
90         OBD_ALLOC_PTR(conn);
91         if (!conn)
92                 RETURN(NULL);
93
94         conn->c_peer = peer;
95         conn->c_self = self;
96         atomic_set(&conn->c_refcount, 1);
97         if (uuid)
98                 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
99
100         /*
101          * Add the newly created conn to the hash, on key collision we
102          * lost a racing addition and must destroy our newly allocated
103          * connection.  The object which exists in the hash will be
104          * returned,otherwise NULL is returned on success.
105          */
106         conn2 = rhashtable_lookup_get_insert_fast(&conn_hash, &conn->c_hash,
107                                                   conn_hash_params);
108         if (conn2) {
109                 /* insertion failed */
110                 OBD_FREE_PTR(conn);
111                 if (IS_ERR(conn2))
112                         return NULL;
113                 conn = conn2;
114                 ptlrpc_connection_addref(conn);
115         }
116         EXIT;
117 out:
118         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
119                conn, atomic_read(&conn->c_refcount),
120                libcfs_nid2str(conn->c_peer.nid));
121         return conn;
122 }
123
124 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
125 {
126         int rc = 0;
127         ENTRY;
128
129         if (!conn)
130                 RETURN(rc);
131
132         LASSERT(atomic_read(&conn->c_refcount) > 0);
133
134         /*
135          * We do not remove connection from hashtable and
136          * do not free it even if last caller released ref,
137          * as we want to have it cached for the case it is
138          * needed again.
139          *
140          * Deallocating it and later creating new connection
141          * again would be wastful. This way we also avoid
142          * expensive locking to protect things from get/put
143          * race when found cached connection is freed by
144          * ptlrpc_connection_put().
145          *
146          * It will be freed later in module unload time,
147          * when ptlrpc_connection_fini()->lh_exit->conn_exit()
148          * path is called.
149          */
150         if (atomic_dec_return(&conn->c_refcount) == 0)
151                 rc = 1;
152
153         CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
154                conn, atomic_read(&conn->c_refcount),
155                libcfs_nid2str(conn->c_peer.nid));
156
157         RETURN(rc);
158 }
159
160 struct ptlrpc_connection *
161 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
162 {
163         ENTRY;
164
165         atomic_inc(&conn->c_refcount);
166         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
167                conn, atomic_read(&conn->c_refcount),
168                libcfs_nid2str(conn->c_peer.nid));
169
170         RETURN(conn);
171 }
172
173 static void
174 conn_exit(void *vconn, void *data)
175 {
176         struct ptlrpc_connection *conn = vconn;
177
178         /*
179          * Nothing should be left. Connection user put it and
180          * connection also was deleted from table by this time
181          * so we should have 0 refs.
182          */
183         LASSERTF(atomic_read(&conn->c_refcount) == 0,
184                  "Busy connection with %d refs\n",
185                  atomic_read(&conn->c_refcount));
186         OBD_FREE_PTR(conn);
187 }
188
189 int ptlrpc_connection_init(void)
190 {
191         return rhashtable_init(&conn_hash, &conn_hash_params);
192 }
193
194 void ptlrpc_connection_fini(void)
195 {
196         rhashtable_free_and_destroy(&conn_hash, conn_exit, NULL);
197 }