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;
67 /* Because almost all of special sequence located in MDT0,
68 * it should go to index 0 directly, instead of calculating
69 * hash again, and also if other MDTs is not being connected,
70 * the fld lookup requests(for seq on MDT0) should not be
71 * blocked because of other MDTs */
72 if (fid_seq_is_norm(seq))
73 hash = fld_rrb_hash(fld, seq);
78 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
79 if (target->ft_idx == hash)
84 /* It is possible the remote target(MDT) are not connected to
85 * with client yet, so we will refer this to MDT0, which should
86 * be connected during mount */
91 CERROR("%s: Can't find target by hash %d (seq %#llx). "
92 "Targets (%d):\n", fld->lcf_name, hash, seq,
95 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
96 const char *srv_name = target->ft_srv != NULL ?
97 target->ft_srv->lsf_name : "<null>";
98 const char *exp_name = target->ft_exp != NULL ?
99 (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
102 CERROR(" exp: 0x%p (%s), srv: 0x%p (%s), idx: %llu\n",
103 target->ft_exp, exp_name, target->ft_srv,
104 srv_name, target->ft_idx);
108 * If target is not found, there is logical error anyway, so here is
109 * LBUG() to catch this situation.
115 struct lu_fld_hash fld_hash[] = {
118 .fh_hash_func = fld_rrb_hash,
119 .fh_scan_func = fld_rrb_scan
126 static struct lu_fld_target *
127 fld_client_get_target(struct lu_client_fld *fld, u64 seq)
129 struct lu_fld_target *target;
132 LASSERT(fld->lcf_hash != NULL);
134 spin_lock(&fld->lcf_lock);
135 target = fld->lcf_hash->fh_scan_func(fld, seq);
136 spin_unlock(&fld->lcf_lock);
138 if (target != NULL) {
139 CDEBUG(D_INFO, "%s: Found target (idx %llu"
140 ") by seq %#llx\n", fld->lcf_name,
141 target->ft_idx, seq);
148 * Add export to FLD. This is usually done by CMM and LMV as they are main users
151 int fld_client_add_target(struct lu_client_fld *fld,
152 struct lu_fld_target *tar)
155 struct lu_fld_target *target, *tmp;
158 LASSERT(tar != NULL);
159 name = fld_target_name(tar);
160 LASSERT(name != NULL);
161 LASSERT(tar->ft_srv != NULL || tar->ft_exp != NULL);
163 CDEBUG(D_INFO, "%s: Adding target %s (idx %llu)\n", fld->lcf_name,
166 OBD_ALLOC_PTR(target);
170 spin_lock(&fld->lcf_lock);
171 list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
172 if (tmp->ft_idx == tar->ft_idx) {
173 spin_unlock(&fld->lcf_lock);
174 OBD_FREE_PTR(target);
175 CERROR("Target %s exists in FLD and known as %s:#%llu\n",
176 name, fld_target_name(tmp), tmp->ft_idx);
181 target->ft_exp = tar->ft_exp;
182 if (target->ft_exp != NULL)
183 class_export_get(target->ft_exp);
184 target->ft_srv = tar->ft_srv;
185 target->ft_idx = tar->ft_idx;
187 list_add_tail(&target->ft_chain, &fld->lcf_targets);
190 spin_unlock(&fld->lcf_lock);
194 EXPORT_SYMBOL(fld_client_add_target);
196 /* Remove export from FLD */
197 int fld_client_del_target(struct lu_client_fld *fld, __u64 idx)
199 struct lu_fld_target *target, *tmp;
202 spin_lock(&fld->lcf_lock);
203 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
204 if (target->ft_idx == idx) {
206 list_del(&target->ft_chain);
207 spin_unlock(&fld->lcf_lock);
209 if (target->ft_exp != NULL)
210 class_export_put(target->ft_exp);
212 OBD_FREE_PTR(target);
216 spin_unlock(&fld->lcf_lock);
220 struct dentry *fld_debugfs_dir;
222 static int fld_client_debugfs_init(struct lu_client_fld *fld)
227 fld->lcf_debugfs_entry = ldebugfs_register(fld->lcf_name,
229 fld_client_debugfs_list,
231 if (IS_ERR_OR_NULL(fld->lcf_debugfs_entry)) {
232 CERROR("%s: LdebugFS failed in fld-init\n", fld->lcf_name);
233 rc = fld->lcf_debugfs_entry ? PTR_ERR(fld->lcf_debugfs_entry)
235 fld->lcf_debugfs_entry = NULL;
242 void fld_client_debugfs_fini(struct lu_client_fld *fld)
244 if (!IS_ERR_OR_NULL(fld->lcf_debugfs_entry))
245 ldebugfs_remove(&fld->lcf_debugfs_entry);
247 EXPORT_SYMBOL(fld_client_debugfs_fini);
249 static inline int hash_is_sane(int hash)
251 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
254 int fld_client_init(struct lu_client_fld *fld,
255 const char *prefix, int hash)
257 int cache_size, cache_threshold;
261 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
264 if (!hash_is_sane(hash)) {
265 CERROR("%s: Wrong hash function %#x\n",
266 fld->lcf_name, hash);
271 spin_lock_init(&fld->lcf_lock);
272 fld->lcf_hash = &fld_hash[hash];
273 INIT_LIST_HEAD(&fld->lcf_targets);
275 cache_size = FLD_CLIENT_CACHE_SIZE /
276 sizeof(struct fld_cache_entry);
278 cache_threshold = cache_size *
279 FLD_CLIENT_CACHE_THRESHOLD / 100;
281 fld->lcf_cache = fld_cache_init(fld->lcf_name,
282 cache_size, cache_threshold);
283 if (IS_ERR(fld->lcf_cache)) {
284 rc = PTR_ERR(fld->lcf_cache);
285 fld->lcf_cache = NULL;
289 rc = fld_client_debugfs_init(fld);
295 fld_client_fini(fld);
297 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
298 fld->lcf_name, fld->lcf_hash->fh_name);
301 EXPORT_SYMBOL(fld_client_init);
303 void fld_client_fini(struct lu_client_fld *fld)
305 struct lu_fld_target *target, *tmp;
308 spin_lock(&fld->lcf_lock);
309 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
311 list_del(&target->ft_chain);
312 if (target->ft_exp != NULL)
313 class_export_put(target->ft_exp);
314 OBD_FREE_PTR(target);
316 spin_unlock(&fld->lcf_lock);
318 if (fld->lcf_cache != NULL) {
319 if (!IS_ERR(fld->lcf_cache))
320 fld_cache_fini(fld->lcf_cache);
321 fld->lcf_cache = NULL;
326 EXPORT_SYMBOL(fld_client_fini);
328 int fld_client_rpc(struct obd_export *exp,
329 struct lu_seq_range *range, __u32 fld_op,
330 struct ptlrpc_request **reqp)
332 struct ptlrpc_request *req = NULL;
333 struct lu_seq_range *prange;
336 struct obd_import *imp;
339 LASSERT(exp != NULL);
342 imp = class_exp2cliimp(exp);
345 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
346 LUSTRE_MDS_VERSION, FLD_QUERY);
350 /* XXX: only needed when talking to old server(< 2.6), it should
351 * be removed when < 2.6 server is not supported */
352 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
355 /* For MDS_MDS seq lookup, it will always use LWP connection,
356 * but LWP will be evicted after restart, so cause the error.
357 * so we will set no_delay for seq lookup request, once the
358 * request fails because of the eviction. always retry here */
359 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) {
360 req->rq_allow_replay = 1;
361 req->rq_no_delay = 1;
365 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
366 LUSTRE_MDS_VERSION, FLD_READ);
370 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
371 RCL_SERVER, PAGE_SIZE);
381 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
383 ptlrpc_request_set_replen(req);
384 req->rq_request_portal = FLD_REQUEST_PORTAL;
385 req->rq_reply_portal = MDC_REPLY_PORTAL;
386 ptlrpc_at_set_req_timeout(req);
388 obd_get_request_slot(&exp->exp_obd->u.cli);
389 rc = ptlrpc_queue_wait(req);
390 obd_put_request_slot(&exp->exp_obd->u.cli);
393 /* Don't loop forever on non-existing FID sequences. */
398 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
399 !imp->imp_deactive &&
400 imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS &&
401 OCD_HAS_FLAG(&imp->imp_connect_data, LIGHTWEIGHT) &&
403 /* Since LWP is not replayable, so it will keep
404 * trying unless umount happens or the remote
405 * target does not support the operation, otherwise
406 * it would cause unecessary failure of the
408 ptlrpc_req_finished(req);
415 if (fld_op == FLD_QUERY) {
416 prange = req_capsule_server_get(&req->rq_pill,
419 GOTO(out_req, rc = -EFAULT);
425 if (rc != 0 || reqp == NULL) {
426 ptlrpc_req_finished(req);
436 int fld_client_lookup(struct lu_client_fld *fld, u64 seq, u32 *mds,
437 __u32 flags, const struct lu_env *env)
439 struct lu_seq_range res = { 0 };
440 struct lu_fld_target *target;
441 struct lu_fld_target *origin;
445 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
447 *mds = res.lsr_index;
451 /* Can not find it in the cache */
452 target = fld_client_get_target(fld, seq);
453 LASSERT(target != NULL);
456 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: %#llx) on "
457 "target %s (idx %llu)\n", fld->lcf_name, seq,
458 fld_target_name(target), target->ft_idx);
461 fld_range_set_type(&res, flags);
463 #ifdef HAVE_SERVER_SUPPORT
464 if (target->ft_srv != NULL) {
465 LASSERT(env != NULL);
466 rc = fld_server_lookup(env, target->ft_srv, seq, &res);
468 #endif /* HAVE_SERVER_SUPPORT */
470 rc = fld_client_rpc(target->ft_exp, &res, FLD_QUERY, NULL);
473 if (rc == -ESHUTDOWN) {
474 /* If fld lookup failed because the target has been shutdown,
475 * then try next target in the list, until trying all targets
476 * or fld lookup succeeds */
477 spin_lock(&fld->lcf_lock);
479 /* If the next entry in the list is the head of the list,
480 * move to the next entry after the head and retrieve
481 * the target. Else retreive the next target entry. */
483 if (target->ft_chain.next == &fld->lcf_targets)
484 target = list_entry(target->ft_chain.next->next,
485 struct lu_fld_target, ft_chain);
487 target = list_entry(target->ft_chain.next,
488 struct lu_fld_target,
490 spin_unlock(&fld->lcf_lock);
491 if (target != origin)
495 *mds = res.lsr_index;
496 fld_cache_insert(fld->lcf_cache, &res);
501 EXPORT_SYMBOL(fld_client_lookup);
503 void fld_client_flush(struct lu_client_fld *fld)
505 fld_cache_flush(fld->lcf_cache);
508 static int __init fld_init(void)
510 #ifdef HAVE_SERVER_SUPPORT
513 rc = fld_server_mod_init();
516 #endif /* HAVE_SERVER_SUPPORT */
518 fld_debugfs_dir = ldebugfs_register(LUSTRE_FLD_NAME,
521 return PTR_ERR_OR_ZERO(fld_debugfs_dir);
524 static void __exit fld_exit(void)
526 #ifdef HAVE_SERVER_SUPPORT
527 fld_server_mod_exit();
528 #endif /* HAVE_SERVER_SUPPORT */
530 if (!IS_ERR_OR_NULL(fld_debugfs_dir))
531 ldebugfs_remove(&fld_debugfs_dir);
534 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
535 MODULE_DESCRIPTION("Lustre FID Location Database");
536 MODULE_VERSION(LUSTRE_VERSION_STRING);
537 MODULE_LICENSE("GPL");
539 module_init(fld_init);
540 module_exit(fld_exit);