Whamcloud - gitweb
LU-181 ptlrpc: reorganize ptlrpc_request
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_member.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) 2013, Trustees of Indiana University
24  * Author: Joshua Walgenbach <jjw@iu.edu>
25  */
26 #include <linux/module.h>
27 #include <lustre_net.h>
28 #include <obd_class.h>
29 #include "nodemap_internal.h"
30
31 #define HASH_NODEMAP_MEMBER_BKT_BITS 3
32 #define HASH_NODEMAP_MEMBER_CUR_BITS 3
33 #define HASH_NODEMAP_MEMBER_MAX_BITS 7
34
35 /**
36  * member hash functions
37  *
38  * The purpose of this hash is to maintain the list of
39  * exports that are connected and associated with a
40  * particular nodemap
41  */
42 static void nm_member_getref(struct obd_export *exp)
43 {
44 }
45
46 void nm_member_putref(struct obd_export *exp)
47 {
48 }
49
50 static __u32 nm_member_hashfn(cfs_hash_t *hash_body,
51                            const void *key, unsigned mask)
52 {
53         return cfs_hash_djb2_hash(key, strlen(key), mask);
54 }
55
56 static void *nm_member_hs_key(struct hlist_node *hnode)
57 {
58         struct obd_export       *exp;
59
60         exp = hlist_entry(hnode, struct obd_export,
61                           exp_target_data.ted_nodemap_member);
62
63         return exp;
64 }
65
66 static int nm_member_hs_keycmp(const void *key, struct hlist_node *hnode)
67 {
68         struct obd_export       *exp;
69
70         exp = hlist_entry(hnode, struct obd_export,
71                           exp_target_data.ted_nodemap_member);
72
73         return key == exp;
74 }
75
76 static void *nm_member_hs_hashobject(struct hlist_node *hnode)
77 {
78         return hlist_entry(hnode, struct obd_export,
79                            exp_target_data.ted_nodemap_member);
80 }
81
82 static void nm_member_hs_get(cfs_hash_t *hs, struct hlist_node *hnode)
83 {
84         struct obd_export       *exp;
85
86         exp = hlist_entry(hnode, struct obd_export,
87                           exp_target_data.ted_nodemap_member);
88         nm_member_getref(exp);
89 }
90
91 static void nm_member_hs_put_locked(cfs_hash_t *hs,
92                                  struct hlist_node *hnode)
93 {
94         struct obd_export       *exp;
95
96         exp = hlist_entry(hnode, struct obd_export,
97                           exp_target_data.ted_nodemap_member);
98         nm_member_putref(exp);
99 }
100
101 /**
102  * Delete a member from a member hash
103  *
104  * \param       nodemap         nodemap containing hash
105  * \paraa       nid             nid of member to delete
106  */
107 void nm_member_del(struct lu_nodemap *nodemap, struct obd_export *exp)
108 {
109
110         exp->exp_target_data.ted_nodemap = NULL;
111         exp = cfs_hash_del_key(nodemap->nm_member_hash, exp);
112         if (exp == NULL)
113                 return;
114
115         class_export_put(exp);
116 }
117
118 static cfs_hash_ops_t nm_member_hash_operations = {
119         .hs_hash        = nm_member_hashfn,
120         .hs_key         = nm_member_hs_key,
121         .hs_keycmp      = nm_member_hs_keycmp,
122         .hs_object      = nm_member_hs_hashobject,
123         .hs_get         = nm_member_hs_get,
124         .hs_put_locked  = nm_member_hs_put_locked,
125 };
126
127 /**
128  * Init a member hash of a nodemap
129  *
130  * \param       nodemap         nodemap containing the member hash
131  */
132 int nm_member_init_hash(struct lu_nodemap *nodemap)
133 {
134         char nodemap_hashname[LUSTRE_NODEMAP_NAME_LENGTH + 3];
135
136
137         snprintf(nodemap_hashname, sizeof(nodemap_hashname),
138                  "nm-%s", nodemap->nm_name);
139         nodemap->nm_member_hash = cfs_hash_create(nodemap_hashname,
140                                           HASH_NODEMAP_MEMBER_CUR_BITS,
141                                           HASH_NODEMAP_MEMBER_MAX_BITS,
142                                           HASH_NODEMAP_MEMBER_BKT_BITS, 0,
143                                           CFS_HASH_MIN_THETA,
144                                           CFS_HASH_MAX_THETA,
145                                           &nm_member_hash_operations,
146                                           CFS_HASH_DEFAULT);
147         if (nodemap->nm_member_hash == NULL)
148                 return -ENOMEM;
149
150         return 0;
151 }
152
153 /**
154  * Callback from deleting a hash member
155  */
156 static int nm_member_delete_hash_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
157                                  struct hlist_node *hnode, void *data)
158 {
159         struct obd_export       *exp;
160
161         exp = hlist_entry(hnode, struct obd_export,
162                           exp_target_data.ted_nodemap_member);
163
164         exp->exp_target_data.ted_nodemap = NULL;
165         cfs_hash_bd_del_locked(hs, bd, hnode);
166         class_export_put(exp);
167
168         return 0;
169 }
170
171 /**
172  * Delete a member hash from a nodemap
173  *
174  * \param       nodemap         nodemap to remove the hash from
175  */
176 void nm_member_delete_hash(struct lu_nodemap *nodemap)
177 {
178         cfs_hash_for_each_safe(nodemap->nm_member_hash,
179                                nm_member_delete_hash_cb,
180                                nodemap);
181         cfs_hash_putref(nodemap->nm_member_hash);
182 }
183
184 /**
185  * Add a member export to a nodemap
186  *
187  * \param       nodemap         nodemap to search
188  * \param       exp             obd_export to search
189  * \retval      -EEXIST         export is already hashed to a different nodemap
190  * \retval      -EINVAL         export is NULL
191  */
192 int nm_member_add(struct lu_nodemap *nodemap, struct obd_export *exp)
193 {
194         int     rc = 0;
195
196         if (exp == NULL) {
197                 CWARN("attempted to add null export to nodemap %s\n",
198                       nodemap->nm_name);
199                 return -EINVAL;
200         }
201
202         if (hlist_unhashed(&exp->exp_target_data.ted_nodemap_member) == 0) {
203                 /* export is already member of nodemap */
204                 if (exp->exp_target_data.ted_nodemap == nodemap)
205                         return 0;
206
207                 /* possibly reconnecting while about to be reclassified */
208                 CWARN("export %p %s already hashed, failed to add to "
209                       "nodemap %s already member of %s\n", exp,
210                       exp->exp_client_uuid.uuid,
211                       nodemap->nm_name,
212                       (exp->exp_target_data.ted_nodemap == NULL) ? "unknown" :
213                                 exp->exp_target_data.ted_nodemap->nm_name);
214                 return -EEXIST;
215         }
216
217         exp->exp_target_data.ted_nodemap = nodemap;
218
219         rc = cfs_hash_add_unique(nodemap->nm_member_hash, exp,
220                                  &exp->exp_target_data.ted_nodemap_member);
221
222         if (rc == 0 )
223                 class_export_get(exp);
224         /* else -EALREADY - exp already in nodemap hash */
225
226         return rc;
227 }
228
229 /**
230  * Revokes the locks on an export if it is attached to an MDT and not in
231  * recovery. As a performance enhancement, the lock revoking process could
232  * revoke only the locks that cover files affected by the nodemap change.
233  */
234 static void nm_member_exp_revoke(struct obd_export *exp)
235 {
236         struct obd_type *type = exp->exp_obd->obd_type;
237         if (strcmp(type->typ_name, LUSTRE_MDT_NAME) != 0)
238                 return;
239         if (exp->exp_obd->obd_recovering)
240                 return;
241
242         ldlm_revoke_export_locks(exp);
243 }
244
245 static int nm_member_reclassify_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
246                                    struct hlist_node *hnode, void *data)
247 {
248         struct obd_export       *exp;
249
250         exp = hlist_entry(hnode, struct obd_export,
251                           exp_target_data.ted_nodemap_member);
252         if (exp == NULL)
253                 goto out;
254
255         /* Call member add before del so exp->nodemap is never NULL. Must use
256          * bd_del_locked inside a cfs_hash callback. For those reasons, can't
257          * use member_del.
258          */
259         nodemap_add_member(exp->exp_connection->c_peer.nid, exp);
260         cfs_hash_bd_del_locked(hs, bd, hnode);
261         class_export_put(exp);
262
263         nm_member_exp_revoke(exp);
264 out:
265         return 0;
266 }
267
268 /**
269  * Reclassify the members of a nodemap after range changes or activation,
270  * based on the member export's NID and the nodemap's new NID ranges. Exports
271  * that are no longer classified as being part of this nodemap are moved to the
272  * nodemap whose NID ranges contain the export's NID, and their locks are
273  * revoked.
274  *
275  * \param       nodemap         nodemap with members to reclassify
276  */
277 void nm_member_reclassify_nodemap(struct lu_nodemap *nodemap)
278 {
279         cfs_hash_for_each_safe(nodemap->nm_member_hash,
280                                nm_member_reclassify_cb,
281                                NULL);
282 }
283
284 static int nm_member_revoke_locks_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
285                                      struct hlist_node *hnode, void *data)
286 {
287         struct obd_export       *exp;
288         exp = hlist_entry(hnode, struct obd_export,
289                           exp_target_data.ted_nodemap_member);
290         if (exp == NULL)
291                 return 0;
292
293         nm_member_exp_revoke(exp);
294         return 0;
295 }
296
297 /**
298  * Revoke the locks for member exports. Changing the idmap is
299  * akin to deleting the security context. If the locks are not
300  * canceled, the client could cache permissions that are no
301  * longer correct with the map.
302  *
303  * \param       nodemap         nodemap that has been altered
304  */
305 void nm_member_revoke_locks(struct lu_nodemap *nodemap)
306 {
307         cfs_hash_for_each(nodemap->nm_member_hash, nm_member_revoke_locks_cb,
308                           NULL);
309 }