Whamcloud - gitweb
LU-5458: libcfs: protect kkuc_groups from write access
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_RPC
38 #include <obd_support.h>
39 #include <obd_class.h>
40 #include <lustre_net.h>
41
42 #include "ptlrpc_internal.h"
43
44 static cfs_hash_t *conn_hash = NULL;
45 static cfs_hash_ops_t conn_hash_ops;
46
47 struct ptlrpc_connection *
48 ptlrpc_connection_get(lnet_process_id_t peer, lnet_nid_t self,
49                       struct obd_uuid *uuid)
50 {
51         struct ptlrpc_connection *conn, *conn2;
52         ENTRY;
53
54         conn = cfs_hash_lookup(conn_hash, &peer);
55         if (conn)
56                 GOTO(out, conn);
57
58         OBD_ALLOC_PTR(conn);
59         if (!conn)
60                 RETURN(NULL);
61
62         conn->c_peer = peer;
63         conn->c_self = self;
64         INIT_HLIST_NODE(&conn->c_hash);
65         atomic_set(&conn->c_refcount, 1);
66         if (uuid)
67                 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
68
69         /*
70          * Add the newly created conn to the hash, on key collision we
71          * lost a racing addition and must destroy our newly allocated
72          * connection.  The object which exists in the has will be
73          * returned and may be compared against out object.
74          */
75         /* In the function below, .hs_keycmp resolves to
76          * conn_keycmp() */
77         /* coverity[overrun-buffer-val] */
78         conn2 = cfs_hash_findadd_unique(conn_hash, &peer, &conn->c_hash);
79         if (conn != conn2) {
80                 OBD_FREE_PTR(conn);
81                 conn = conn2;
82         }
83         EXIT;
84 out:
85         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
86                conn, atomic_read(&conn->c_refcount),
87                libcfs_nid2str(conn->c_peer.nid));
88         return conn;
89 }
90 EXPORT_SYMBOL(ptlrpc_connection_get);
91
92 int ptlrpc_connection_put(struct ptlrpc_connection *conn)
93 {
94         int rc = 0;
95         ENTRY;
96
97         if (!conn)
98                 RETURN(rc);
99
100         LASSERT(atomic_read(&conn->c_refcount) > 1);
101
102         /*
103          * We do not remove connection from hashtable and
104          * do not free it even if last caller released ref,
105          * as we want to have it cached for the case it is
106          * needed again.
107          *
108          * Deallocating it and later creating new connection
109          * again would be wastful. This way we also avoid
110          * expensive locking to protect things from get/put
111          * race when found cached connection is freed by
112          * ptlrpc_connection_put().
113          *
114          * It will be freed later in module unload time,
115          * when ptlrpc_connection_fini()->lh_exit->conn_exit()
116          * path is called.
117          */
118         if (atomic_dec_return(&conn->c_refcount) == 1)
119                 rc = 1;
120
121         CDEBUG(D_INFO, "PUT conn=%p refcount %d to %s\n",
122                conn, atomic_read(&conn->c_refcount),
123                libcfs_nid2str(conn->c_peer.nid));
124
125         RETURN(rc);
126 }
127 EXPORT_SYMBOL(ptlrpc_connection_put);
128
129 struct ptlrpc_connection *
130 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
131 {
132         ENTRY;
133
134         atomic_inc(&conn->c_refcount);
135         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
136                conn, atomic_read(&conn->c_refcount),
137                libcfs_nid2str(conn->c_peer.nid));
138
139         RETURN(conn);
140 }
141 EXPORT_SYMBOL(ptlrpc_connection_addref);
142
143 int ptlrpc_connection_init(void)
144 {
145         ENTRY;
146
147         conn_hash = cfs_hash_create("CONN_HASH",
148                                     HASH_CONN_CUR_BITS,
149                                     HASH_CONN_MAX_BITS,
150                                     HASH_CONN_BKT_BITS, 0,
151                                     CFS_HASH_MIN_THETA,
152                                     CFS_HASH_MAX_THETA,
153                                     &conn_hash_ops, CFS_HASH_DEFAULT);
154         if (!conn_hash)
155                 RETURN(-ENOMEM);
156
157         RETURN(0);
158 }
159 EXPORT_SYMBOL(ptlrpc_connection_init);
160
161 void ptlrpc_connection_fini(void) {
162         ENTRY;
163         cfs_hash_putref(conn_hash);
164         EXIT;
165 }
166 EXPORT_SYMBOL(ptlrpc_connection_fini);
167
168 /*
169  * Hash operations for net_peer<->connection
170  */
171 static unsigned
172 conn_hashfn(cfs_hash_t *hs, const void *key, unsigned mask)
173 {
174         return cfs_hash_djb2_hash(key, sizeof(lnet_process_id_t), mask);
175 }
176
177 static int
178 conn_keycmp(const void *key, struct hlist_node *hnode)
179 {
180         struct ptlrpc_connection *conn;
181         const lnet_process_id_t *conn_key;
182
183         LASSERT(key != NULL);
184         conn_key = (lnet_process_id_t *)key;
185         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
186
187         return conn_key->nid == conn->c_peer.nid &&
188                conn_key->pid == conn->c_peer.pid;
189 }
190
191 static void *
192 conn_key(struct hlist_node *hnode)
193 {
194         struct ptlrpc_connection *conn;
195         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
196         return &conn->c_peer;
197 }
198
199 static void *
200 conn_object(struct hlist_node *hnode)
201 {
202         return hlist_entry(hnode, struct ptlrpc_connection, c_hash);
203 }
204
205 static void
206 conn_get(cfs_hash_t *hs, struct hlist_node *hnode)
207 {
208         struct ptlrpc_connection *conn;
209
210         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
211         atomic_inc(&conn->c_refcount);
212 }
213
214 static void
215 conn_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
216 {
217         struct ptlrpc_connection *conn;
218
219         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
220         atomic_dec(&conn->c_refcount);
221 }
222
223 static void
224 conn_exit(cfs_hash_t *hs, struct hlist_node *hnode)
225 {
226         struct ptlrpc_connection *conn;
227
228         conn = hlist_entry(hnode, struct ptlrpc_connection, c_hash);
229         /*
230          * Nothing should be left. Connection user put it and
231          * connection also was deleted from table by this time
232          * so we should have 0 refs.
233          */
234         LASSERTF(atomic_read(&conn->c_refcount) == 0,
235                  "Busy connection with %d refs\n",
236                  atomic_read(&conn->c_refcount));
237         OBD_FREE_PTR(conn);
238 }
239
240 static cfs_hash_ops_t conn_hash_ops = {
241         .hs_hash        = conn_hashfn,
242         .hs_keycmp      = conn_keycmp,
243         .hs_key         = conn_key,
244         .hs_object      = conn_object,
245         .hs_get         = conn_get,
246         .hs_put_locked  = conn_put_locked,
247         .hs_exit        = conn_exit,
248 };