Whamcloud - gitweb
7d4663dca02f27a4ab0fb477ed8a8e07c36fef85
[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  */
31
32 #define DEBUG_SUBSYSTEM S_RPC
33
34 #include <linux/delay.h>
35 #include <libcfs/linux/linux-hash.h>
36 #include <obd_support.h>
37 #include <obd_class.h>
38 #include <lustre_net.h>
39
40 #include "ptlrpc_internal.h"
41
42 static struct rhashtable conn_hash;
43
44 /*
45  * struct lnet_process_id may contain unassigned bytes which might not
46  * be zero, so we cannot just hash and compare bytes.
47  */
48
49 static u32 lnet_process_id_hash(const void *data, u32 len, u32 seed)
50 {
51         const struct lnet_processid *lpi = data;
52
53         seed = cfs_hash_32(seed ^ lpi->pid, 32);
54         seed = cfs_hash_32(nidhash(&lpi->nid) ^ seed, 32);
55         return seed;
56 }
57
58 static int lnet_process_id_cmp(struct rhashtable_compare_arg *arg,
59                                const void *obj)
60 {
61         const struct lnet_processid *lpi = arg->key;
62         const struct ptlrpc_connection *con = obj;
63
64         if (nid_same(&lpi->nid, &con->c_peer.nid) &&
65             lpi->pid == con->c_peer.pid)
66                 return 0;
67         return -ESRCH;
68 }
69
70 static const struct rhashtable_params conn_hash_params = {
71         .key_len        = 1,    /* actually variable-length */
72         .key_offset     = offsetof(struct ptlrpc_connection, c_peer),
73         .head_offset    = offsetof(struct ptlrpc_connection, c_hash),
74         .hashfn         = lnet_process_id_hash,
75         .obj_cmpfn      = lnet_process_id_cmp,
76 };
77
78 struct ptlrpc_connection *
79 ptlrpc_connection_get(struct lnet_process_id peer4, lnet_nid_t self,
80                       struct obd_uuid *uuid)
81 {
82         struct ptlrpc_connection *conn, *conn2;
83         struct lnet_processid peer;
84         ENTRY;
85
86         lnet_pid4_to_pid(peer4, &peer);
87         LNetPrimaryNID(&peer.nid);
88         conn = rhashtable_lookup_fast(&conn_hash, &peer, conn_hash_params);
89         if (conn) {
90                 ptlrpc_connection_addref(conn);
91                 GOTO(out, conn);
92         }
93
94         OBD_ALLOC_PTR(conn);
95         if (!conn)
96                 RETURN(NULL);
97
98         conn->c_peer = peer;
99         lnet_nid4_to_nid(self, &conn->c_self);
100         atomic_set(&conn->c_refcount, 1);
101         if (uuid)
102                 obd_str2uuid(&conn->c_remote_uuid, uuid->uuid);
103
104         /*
105          * Add the newly created conn to the hash, on key collision we
106          * lost a racing addition and must destroy our newly allocated
107          * connection.  The object which exists in the hash will be
108          * returned,otherwise NULL is returned on success.
109          */
110 try_again:
111         conn2 = rhashtable_lookup_get_insert_fast(&conn_hash, &conn->c_hash,
112                                                   conn_hash_params);
113         if (conn2) {
114                 /* insertion failed */
115                 if (IS_ERR(conn2)) {
116                         /* hash table could be resizing. */
117                         if (PTR_ERR(conn2) == -ENOMEM ||
118                             PTR_ERR(conn2) == -EBUSY) {
119                                 msleep(5);
120                                 goto try_again;
121                         }
122                         conn2 = NULL;
123                 }
124                 OBD_FREE_PTR(conn);
125                 conn = conn2;
126                 if (conn)
127                         ptlrpc_connection_addref(conn);
128         }
129         EXIT;
130 out:
131         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
132                conn, atomic_read(&conn->c_refcount),
133                libcfs_nidstr(&conn->c_peer.nid));
134         return conn;
135 }
136
137 struct ptlrpc_connection *
138 ptlrpc_connection_addref(struct ptlrpc_connection *conn)
139 {
140         ENTRY;
141
142         atomic_inc(&conn->c_refcount);
143         CDEBUG(D_INFO, "conn=%p refcount %d to %s\n",
144                conn, atomic_read(&conn->c_refcount),
145                libcfs_nidstr(&conn->c_peer.nid));
146
147         RETURN(conn);
148 }
149
150 static void
151 conn_exit(void *vconn, void *data)
152 {
153         struct ptlrpc_connection *conn = vconn;
154
155         /*
156          * Nothing should be left. Connection user put it and
157          * connection also was deleted from table by this time
158          * so we should have 0 refs.
159          */
160         LASSERTF(atomic_read(&conn->c_refcount) == 0,
161                  "Busy connection with %d refs\n",
162                  atomic_read(&conn->c_refcount));
163         OBD_FREE_PTR(conn);
164 }
165
166 int ptlrpc_connection_init(void)
167 {
168         return rhashtable_init(&conn_hash, &conn_hash_params);
169 }
170
171 void ptlrpc_connection_fini(void)
172 {
173         rhashtable_free_and_destroy(&conn_hash, conn_exit, NULL);
174 }