4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (C) 2013, Trustees of Indiana University
24 * Author: Joshua Walgenbach <jjw@iu.edu>
26 #include <linux/module.h>
27 #include <lustre_net.h>
28 #include <obd_class.h>
29 #include "nodemap_internal.h"
31 #define HASH_NODEMAP_MEMBER_BKT_BITS 3
32 #define HASH_NODEMAP_MEMBER_CUR_BITS 3
33 #define HASH_NODEMAP_MEMBER_MAX_BITS 7
36 * member hash functions
38 * The purpose of this hash is to maintain the list of
39 * exports that are connected and associated with a
42 static void nm_member_getref(struct obd_export *exp)
46 void nm_member_putref(struct obd_export *exp)
50 static __u32 nm_member_hashfn(cfs_hash_t *hash_body,
51 const void *key, unsigned mask)
53 return cfs_hash_djb2_hash(key, strlen(key), mask);
56 static void *nm_member_hs_key(struct hlist_node *hnode)
58 struct obd_export *exp;
60 exp = hlist_entry(hnode, struct obd_export,
61 exp_target_data.ted_nodemap_member);
66 static int nm_member_hs_keycmp(const void *key, struct hlist_node *hnode)
68 struct obd_export *exp;
70 exp = hlist_entry(hnode, struct obd_export,
71 exp_target_data.ted_nodemap_member);
76 static void *nm_member_hs_hashobject(struct hlist_node *hnode)
78 return hlist_entry(hnode, struct obd_export,
79 exp_target_data.ted_nodemap_member);
82 static void nm_member_hs_get(cfs_hash_t *hs, struct hlist_node *hnode)
84 struct obd_export *exp;
86 exp = hlist_entry(hnode, struct obd_export,
87 exp_target_data.ted_nodemap_member);
88 nm_member_getref(exp);
91 static void nm_member_hs_put_locked(cfs_hash_t *hs,
92 struct hlist_node *hnode)
94 struct obd_export *exp;
96 exp = hlist_entry(hnode, struct obd_export,
97 exp_target_data.ted_nodemap_member);
98 nm_member_putref(exp);
102 * Delete a member from a member hash
104 * \param nodemap nodemap containing hash
105 * \paraa nid nid of member to delete
107 void nm_member_del(struct lu_nodemap *nodemap, struct obd_export *exp)
110 exp->exp_target_data.ted_nodemap = NULL;
111 exp = cfs_hash_del_key(nodemap->nm_member_hash, exp);
115 class_export_put(exp);
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,
128 * Init a member hash of a nodemap
130 * \param nodemap nodemap containing the member hash
132 int nm_member_init_hash(struct lu_nodemap *nodemap)
134 char nodemap_hashname[LUSTRE_NODEMAP_NAME_LENGTH + 3];
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,
145 &nm_member_hash_operations,
147 if (nodemap->nm_member_hash == NULL)
154 * Callback from deleting a hash member
156 static int nm_member_delete_hash_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
157 struct hlist_node *hnode, void *data)
159 struct obd_export *exp;
161 exp = hlist_entry(hnode, struct obd_export,
162 exp_target_data.ted_nodemap_member);
164 exp->exp_target_data.ted_nodemap = NULL;
165 cfs_hash_bd_del_locked(hs, bd, hnode);
166 class_export_put(exp);
172 * Delete a member hash from a nodemap
174 * \param nodemap nodemap to remove the hash from
176 void nm_member_delete_hash(struct lu_nodemap *nodemap)
178 cfs_hash_for_each_safe(nodemap->nm_member_hash,
179 nm_member_delete_hash_cb,
181 cfs_hash_putref(nodemap->nm_member_hash);
185 * Add a member export to a nodemap
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
192 int nm_member_add(struct lu_nodemap *nodemap, struct obd_export *exp)
197 CWARN("attempted to add null export to nodemap %s\n",
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)
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,
212 (exp->exp_target_data.ted_nodemap == NULL) ? "unknown" :
213 exp->exp_target_data.ted_nodemap->nm_name);
217 exp->exp_target_data.ted_nodemap = nodemap;
219 rc = cfs_hash_add_unique(nodemap->nm_member_hash, exp,
220 &exp->exp_target_data.ted_nodemap_member);
223 class_export_get(exp);
224 /* else -EALREADY - exp already in nodemap hash */
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.
234 static void nm_member_exp_revoke(struct obd_export *exp)
236 struct obd_type *type = exp->exp_obd->obd_type;
237 if (strcmp(type->typ_name, LUSTRE_MDT_NAME) != 0)
239 if (exp->exp_obd->obd_recovering)
242 ldlm_revoke_export_locks(exp);
245 static int nm_member_reclassify_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
246 struct hlist_node *hnode, void *data)
248 struct obd_export *exp;
250 exp = hlist_entry(hnode, struct obd_export,
251 exp_target_data.ted_nodemap_member);
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
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);
263 nm_member_exp_revoke(exp);
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
275 * \param nodemap nodemap with members to reclassify
277 void nm_member_reclassify_nodemap(struct lu_nodemap *nodemap)
279 cfs_hash_for_each_safe(nodemap->nm_member_hash,
280 nm_member_reclassify_cb,
284 static int nm_member_revoke_locks_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
285 struct hlist_node *hnode, void *data)
287 struct obd_export *exp;
288 exp = hlist_entry(hnode, struct obd_export,
289 exp_target_data.ted_nodemap_member);
293 nm_member_exp_revoke(exp);
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.
303 * \param nodemap nodemap that has been altered
305 void nm_member_revoke_locks(struct lu_nodemap *nodemap)
307 cfs_hash_for_each(nodemap->nm_member_hash, nm_member_revoke_locks_cb,