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