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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/fld/fld_request.c
34 * FLD (Fids Location Database)
36 * Author: Yury Umanets <umka@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_FLD
41 #include <libcfs/libcfs.h>
42 #include <linux/module.h>
43 #include <linux/math64.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lprocfs_status.h>
49 #include <lustre_req_layout.h>
50 #include <lustre_fld.h>
51 #include <lustre_mdc.h>
52 #include "fld_internal.h"
54 static int fld_rrb_hash(struct lu_client_fld *fld, u64 seq)
56 LASSERT(fld->lcf_count > 0);
57 return do_div(seq, fld->lcf_count);
60 static struct lu_fld_target *
61 fld_rrb_scan(struct lu_client_fld *fld, u64 seq)
63 struct lu_fld_target *target;
69 * Because almost all of special sequence located in MDT0,
70 * it should go to index 0 directly, instead of calculating
71 * hash again, and also if other MDTs is not being connected,
72 * the fld lookup requests(for seq on MDT0) should not be
73 * blocked because of other MDTs
75 if (fid_seq_is_norm(seq))
76 hash = fld_rrb_hash(fld, seq);
81 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
82 if (target->ft_idx == hash)
88 * It is possible the remote target(MDT) are not connected to
89 * with client yet, so we will refer this to MDT0, which should
90 * be connected during mount
96 CERROR("%s: Can't find target by hash %d (seq %#llx). Targets (%d):\n",
97 fld->lcf_name, hash, seq, fld->lcf_count);
99 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
100 const char *srv_name = target->ft_srv != NULL ?
101 target->ft_srv->lsf_name : "<null>";
102 const char *exp_name = target->ft_exp != NULL ?
103 (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
106 CERROR(" exp: 0x%p (%s), srv: 0x%p (%s), idx: %llu\n",
107 target->ft_exp, exp_name, target->ft_srv,
108 srv_name, target->ft_idx);
112 * If target is not found, there is logical error anyway, so here is
113 * LBUG() to catch this situation.
119 struct lu_fld_hash fld_hash[] = {
122 .fh_hash_func = fld_rrb_hash,
123 .fh_scan_func = fld_rrb_scan
130 static struct lu_fld_target *
131 fld_client_get_target(struct lu_client_fld *fld, u64 seq)
133 struct lu_fld_target *target;
137 LASSERT(fld->lcf_hash != NULL);
139 spin_lock(&fld->lcf_lock);
140 target = fld->lcf_hash->fh_scan_func(fld, seq);
141 spin_unlock(&fld->lcf_lock);
144 CDEBUG(D_INFO, "%s: Found target (idx %llu) by seq %#llx\n",
145 fld->lcf_name, target->ft_idx, seq);
152 * Add export to FLD. This is usually done by CMM and LMV as they are main users
155 int fld_client_add_target(struct lu_client_fld *fld,
156 struct lu_fld_target *tar)
159 struct lu_fld_target *target, *tmp;
163 LASSERT(tar != NULL);
164 name = fld_target_name(tar);
165 LASSERT(name != NULL);
166 LASSERT(tar->ft_srv != NULL || tar->ft_exp != NULL);
168 CDEBUG(D_INFO, "%s: Adding target %s (idx %llu)\n", fld->lcf_name,
171 OBD_ALLOC_PTR(target);
175 spin_lock(&fld->lcf_lock);
176 list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
177 if (tmp->ft_idx == tar->ft_idx) {
178 spin_unlock(&fld->lcf_lock);
179 OBD_FREE_PTR(target);
180 CERROR("Target %s exists in FLD and known as %s:#%llu\n",
181 name, fld_target_name(tmp), tmp->ft_idx);
186 target->ft_exp = tar->ft_exp;
188 class_export_get(target->ft_exp);
189 target->ft_srv = tar->ft_srv;
190 target->ft_idx = tar->ft_idx;
192 list_add_tail(&target->ft_chain, &fld->lcf_targets);
195 spin_unlock(&fld->lcf_lock);
199 EXPORT_SYMBOL(fld_client_add_target);
201 /* Remove export from FLD */
202 int fld_client_del_target(struct lu_client_fld *fld, u64 idx)
204 struct lu_fld_target *target, *tmp;
208 spin_lock(&fld->lcf_lock);
209 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
210 if (target->ft_idx == idx) {
212 list_del(&target->ft_chain);
213 spin_unlock(&fld->lcf_lock);
216 class_export_put(target->ft_exp);
218 OBD_FREE_PTR(target);
222 spin_unlock(&fld->lcf_lock);
226 struct dentry *fld_debugfs_dir;
228 static int fld_client_debugfs_init(struct lu_client_fld *fld)
233 fld->lcf_debugfs_entry = ldebugfs_register(fld->lcf_name,
235 fld_client_debugfs_list,
237 if (IS_ERR_OR_NULL(fld->lcf_debugfs_entry)) {
238 CERROR("%s: LdebugFS failed in fld-init\n", fld->lcf_name);
239 rc = fld->lcf_debugfs_entry ? PTR_ERR(fld->lcf_debugfs_entry)
241 fld->lcf_debugfs_entry = NULL;
248 void fld_client_debugfs_fini(struct lu_client_fld *fld)
250 if (!IS_ERR_OR_NULL(fld->lcf_debugfs_entry))
251 ldebugfs_remove(&fld->lcf_debugfs_entry);
253 EXPORT_SYMBOL(fld_client_debugfs_fini);
255 static inline int hash_is_sane(int hash)
257 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
260 int fld_client_init(struct lu_client_fld *fld,
261 const char *prefix, int hash)
263 int cache_size, cache_threshold;
267 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
270 if (!hash_is_sane(hash)) {
271 CERROR("%s: Wrong hash function %#x\n",
272 fld->lcf_name, hash);
277 spin_lock_init(&fld->lcf_lock);
278 fld->lcf_hash = &fld_hash[hash];
279 INIT_LIST_HEAD(&fld->lcf_targets);
281 cache_size = FLD_CLIENT_CACHE_SIZE /
282 sizeof(struct fld_cache_entry);
284 cache_threshold = cache_size *
285 FLD_CLIENT_CACHE_THRESHOLD / 100;
287 fld->lcf_cache = fld_cache_init(fld->lcf_name,
288 cache_size, cache_threshold);
289 if (IS_ERR(fld->lcf_cache)) {
290 rc = PTR_ERR(fld->lcf_cache);
291 fld->lcf_cache = NULL;
295 rc = fld_client_debugfs_init(fld);
301 fld_client_fini(fld);
303 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
304 fld->lcf_name, fld->lcf_hash->fh_name);
307 EXPORT_SYMBOL(fld_client_init);
309 void fld_client_fini(struct lu_client_fld *fld)
311 struct lu_fld_target *target, *tmp;
315 spin_lock(&fld->lcf_lock);
316 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
318 list_del(&target->ft_chain);
320 class_export_put(target->ft_exp);
321 OBD_FREE_PTR(target);
323 spin_unlock(&fld->lcf_lock);
325 if (fld->lcf_cache) {
326 if (!IS_ERR(fld->lcf_cache))
327 fld_cache_fini(fld->lcf_cache);
328 fld->lcf_cache = NULL;
333 EXPORT_SYMBOL(fld_client_fini);
335 int fld_client_rpc(struct obd_export *exp,
336 struct lu_seq_range *range, u32 fld_op,
337 struct ptlrpc_request **reqp)
339 struct ptlrpc_request *req = NULL;
340 struct lu_seq_range *prange;
343 struct obd_import *imp;
347 LASSERT(exp != NULL);
349 imp = class_exp2cliimp(exp);
352 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
353 LUSTRE_MDS_VERSION, FLD_QUERY);
358 * XXX: only needed when talking to old server(< 2.6), it should
359 * be removed when < 2.6 server is not supported
361 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
365 * For MDS_MDS seq lookup, it will always use LWP connection,
366 * but LWP will be evicted after restart, so cause the error.
367 * so we will set no_delay for seq lookup request, once the
368 * request fails because of the eviction. always retry here
370 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) {
371 req->rq_allow_replay = 1;
372 req->rq_no_delay = 1;
376 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
377 LUSTRE_MDS_VERSION, FLD_READ);
381 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
382 RCL_SERVER, PAGE_SIZE);
392 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
394 ptlrpc_request_set_replen(req);
395 req->rq_request_portal = FLD_REQUEST_PORTAL;
396 req->rq_reply_portal = MDC_REPLY_PORTAL;
397 ptlrpc_at_set_req_timeout(req);
399 if (OBD_FAIL_CHECK(OBD_FAIL_FLD_QUERY_REQ && req->rq_no_delay)) {
400 /* the same error returned by ptlrpc_import_delay_req */
404 obd_get_request_slot(&exp->exp_obd->u.cli);
405 rc = ptlrpc_queue_wait(req);
406 obd_put_request_slot(&exp->exp_obd->u.cli);
410 /* Don't loop forever on non-existing FID sequences. */
415 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
416 !imp->imp_deactive &&
417 imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS &&
418 OCD_HAS_FLAG(&imp->imp_connect_data, LIGHTWEIGHT) &&
421 * Since LWP is not replayable, so notify the caller
422 * to retry if needed after a while.
429 if (fld_op == FLD_QUERY) {
430 prange = req_capsule_server_get(&req->rq_pill,
433 GOTO(out_req, rc = -EFAULT);
439 if (rc != 0 || !reqp) {
440 ptlrpc_req_finished(req);
450 int fld_client_lookup(struct lu_client_fld *fld, u64 seq, u32 *mds,
451 u32 flags, const struct lu_env *env)
453 struct lu_seq_range res = { 0 };
454 struct lu_fld_target *target;
455 struct lu_fld_target *origin;
460 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
462 *mds = res.lsr_index;
466 /* Can not find it in the cache */
467 target = fld_client_get_target(fld, seq);
468 LASSERT(target != NULL);
471 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: %#llx) on target %s (idx %llu)\n",
472 fld->lcf_name, seq, fld_target_name(target), target->ft_idx);
475 fld_range_set_type(&res, flags);
477 #ifdef HAVE_SERVER_SUPPORT
478 if (target->ft_srv) {
479 LASSERT(env != NULL);
480 rc = fld_server_lookup(env, target->ft_srv, seq, &res);
482 #endif /* HAVE_SERVER_SUPPORT */
484 rc = fld_client_rpc(target->ft_exp, &res, FLD_QUERY, NULL);
487 if (rc == -ESHUTDOWN) {
489 * If fld lookup failed because the target has been shutdown,
490 * then try next target in the list, until trying all targets
491 * or fld lookup succeeds
493 spin_lock(&fld->lcf_lock);
495 * If the next entry in the list is the head of the list,
496 * move to the next entry after the head and retrieve
497 * the target. Else retreive the next target entry.
499 if (target->ft_chain.next == &fld->lcf_targets)
500 target = list_entry(target->ft_chain.next->next,
501 struct lu_fld_target, ft_chain);
503 target = list_entry(target->ft_chain.next,
504 struct lu_fld_target,
506 spin_unlock(&fld->lcf_lock);
507 if (target != origin)
511 *mds = res.lsr_index;
512 fld_cache_insert(fld->lcf_cache, &res);
517 EXPORT_SYMBOL(fld_client_lookup);
519 void fld_client_flush(struct lu_client_fld *fld)
521 fld_cache_flush(fld->lcf_cache);
524 static int __init fld_init(void)
526 #ifdef HAVE_SERVER_SUPPORT
529 rc = fld_server_mod_init();
532 #endif /* HAVE_SERVER_SUPPORT */
534 fld_debugfs_dir = ldebugfs_register(LUSTRE_FLD_NAME,
537 return PTR_ERR_OR_ZERO(fld_debugfs_dir);
540 static void __exit fld_exit(void)
542 #ifdef HAVE_SERVER_SUPPORT
543 fld_server_mod_exit();
544 #endif /* HAVE_SERVER_SUPPORT */
546 if (!IS_ERR_OR_NULL(fld_debugfs_dir))
547 ldebugfs_remove(&fld_debugfs_dir);
550 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
551 MODULE_DESCRIPTION("Lustre FID Location Database");
552 MODULE_VERSION(LUSTRE_VERSION_STRING);
553 MODULE_LICENSE("GPL");
555 module_init(fld_init);
556 module_exit(fld_exit);