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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/fld/fld_request.c
38 * FLD (Fids Location Database)
40 * Author: Yury Umanets <umka@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_FLD
45 #include <libcfs/libcfs.h>
46 #include <linux/module.h>
47 #include <linux/math64.h>
50 #include <obd_class.h>
51 #include <obd_support.h>
52 #include <lprocfs_status.h>
53 #include <lustre_req_layout.h>
54 #include <lustre_fld.h>
55 #include <lustre_mdc.h>
56 #include "fld_internal.h"
58 static int fld_rrb_hash(struct lu_client_fld *fld, u64 seq)
60 LASSERT(fld->lcf_count > 0);
61 return do_div(seq, fld->lcf_count);
64 static struct lu_fld_target *
65 fld_rrb_scan(struct lu_client_fld *fld, u64 seq)
67 struct lu_fld_target *target;
71 /* Because almost all of special sequence located in MDT0,
72 * it should go to index 0 directly, instead of calculating
73 * hash again, and also if other MDTs is not being connected,
74 * the fld lookup requests(for seq on MDT0) should not be
75 * blocked because of other MDTs */
76 if (fid_seq_is_norm(seq))
77 hash = fld_rrb_hash(fld, seq);
82 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
83 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 */
95 CERROR("%s: Can't find target by hash %d (seq "LPX64"). "
96 "Targets (%d):\n", fld->lcf_name, hash, seq,
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: "LPU64"\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;
136 LASSERT(fld->lcf_hash != NULL);
138 spin_lock(&fld->lcf_lock);
139 target = fld->lcf_hash->fh_scan_func(fld, seq);
140 spin_unlock(&fld->lcf_lock);
142 if (target != NULL) {
143 CDEBUG(D_INFO, "%s: Found target (idx "LPU64
144 ") by seq "LPX64"\n", fld->lcf_name,
145 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;
162 LASSERT(tar != NULL);
163 name = fld_target_name(tar);
164 LASSERT(name != NULL);
165 LASSERT(tar->ft_srv != NULL || tar->ft_exp != NULL);
167 if (fld->lcf_flags != LUSTRE_FLD_INIT) {
168 CERROR("%s: Attempt to add target %s (idx "LPU64") "
169 "on fly - skip it\n", fld->lcf_name, name,
173 CDEBUG(D_INFO, "%s: Adding target %s (idx "
174 LPU64")\n", fld->lcf_name, name, tar->ft_idx);
177 OBD_ALLOC_PTR(target);
181 spin_lock(&fld->lcf_lock);
182 list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
183 if (tmp->ft_idx == tar->ft_idx) {
184 spin_unlock(&fld->lcf_lock);
185 OBD_FREE_PTR(target);
186 CERROR("Target %s exists in FLD and known as %s:#"LPU64"\n",
187 name, fld_target_name(tmp), tmp->ft_idx);
192 target->ft_exp = tar->ft_exp;
193 if (target->ft_exp != NULL)
194 class_export_get(target->ft_exp);
195 target->ft_srv = tar->ft_srv;
196 target->ft_idx = tar->ft_idx;
198 list_add_tail(&target->ft_chain, &fld->lcf_targets);
201 spin_unlock(&fld->lcf_lock);
205 EXPORT_SYMBOL(fld_client_add_target);
207 /* Remove export from FLD */
208 int fld_client_del_target(struct lu_client_fld *fld, __u64 idx)
210 struct lu_fld_target *target, *tmp;
213 spin_lock(&fld->lcf_lock);
214 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
215 if (target->ft_idx == idx) {
217 list_del(&target->ft_chain);
218 spin_unlock(&fld->lcf_lock);
220 if (target->ft_exp != NULL)
221 class_export_put(target->ft_exp);
223 OBD_FREE_PTR(target);
227 spin_unlock(&fld->lcf_lock);
231 #ifdef CONFIG_PROC_FS
232 static int fld_client_proc_init(struct lu_client_fld *fld)
237 fld->lcf_proc_dir = lprocfs_register(fld->lcf_name, fld_type_proc_dir,
239 if (IS_ERR(fld->lcf_proc_dir)) {
240 CERROR("%s: LProcFS failed in fld-init\n",
242 rc = PTR_ERR(fld->lcf_proc_dir);
246 rc = lprocfs_add_vars(fld->lcf_proc_dir, fld_client_proc_list, fld);
248 CERROR("%s: Can't init FLD proc, rc %d\n",
250 GOTO(out_cleanup, rc);
256 fld_client_proc_fini(fld);
260 void fld_client_proc_fini(struct lu_client_fld *fld)
263 if (fld->lcf_proc_dir) {
264 if (!IS_ERR(fld->lcf_proc_dir))
265 lprocfs_remove(&fld->lcf_proc_dir);
266 fld->lcf_proc_dir = NULL;
270 #else /* !CONFIG_PROC_FS */
271 static int fld_client_proc_init(struct lu_client_fld *fld)
276 void fld_client_proc_fini(struct lu_client_fld *fld)
280 #endif /* CONFIG_PROC_FS */
282 EXPORT_SYMBOL(fld_client_proc_fini);
284 static inline int hash_is_sane(int hash)
286 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
289 int fld_client_init(struct lu_client_fld *fld,
290 const char *prefix, int hash)
292 int cache_size, cache_threshold;
296 LASSERT(fld != NULL);
298 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
301 if (!hash_is_sane(hash)) {
302 CERROR("%s: Wrong hash function %#x\n",
303 fld->lcf_name, hash);
308 spin_lock_init(&fld->lcf_lock);
309 fld->lcf_hash = &fld_hash[hash];
310 fld->lcf_flags = LUSTRE_FLD_INIT;
311 INIT_LIST_HEAD(&fld->lcf_targets);
313 cache_size = FLD_CLIENT_CACHE_SIZE /
314 sizeof(struct fld_cache_entry);
316 cache_threshold = cache_size *
317 FLD_CLIENT_CACHE_THRESHOLD / 100;
319 fld->lcf_cache = fld_cache_init(fld->lcf_name,
320 cache_size, cache_threshold);
321 if (IS_ERR(fld->lcf_cache)) {
322 rc = PTR_ERR(fld->lcf_cache);
323 fld->lcf_cache = NULL;
327 rc = fld_client_proc_init(fld);
333 fld_client_fini(fld);
335 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
336 fld->lcf_name, fld->lcf_hash->fh_name);
339 EXPORT_SYMBOL(fld_client_init);
341 void fld_client_fini(struct lu_client_fld *fld)
343 struct lu_fld_target *target, *tmp;
346 spin_lock(&fld->lcf_lock);
347 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
349 list_del(&target->ft_chain);
350 if (target->ft_exp != NULL)
351 class_export_put(target->ft_exp);
352 OBD_FREE_PTR(target);
354 spin_unlock(&fld->lcf_lock);
356 if (fld->lcf_cache != NULL) {
357 if (!IS_ERR(fld->lcf_cache))
358 fld_cache_fini(fld->lcf_cache);
359 fld->lcf_cache = NULL;
364 EXPORT_SYMBOL(fld_client_fini);
366 int fld_client_rpc(struct obd_export *exp,
367 struct lu_seq_range *range, __u32 fld_op,
368 struct ptlrpc_request **reqp)
370 struct ptlrpc_request *req = NULL;
371 struct lu_seq_range *prange;
374 struct obd_import *imp;
377 LASSERT(exp != NULL);
380 imp = class_exp2cliimp(exp);
383 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
384 LUSTRE_MDS_VERSION, FLD_QUERY);
388 /* XXX: only needed when talking to old server(< 2.6), it should
389 * be removed when < 2.6 server is not supported */
390 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
393 /* For MDS_MDS seq lookup, it will always use LWP connection,
394 * but LWP will be evicted after restart, so cause the error.
395 * so we will set no_delay for seq lookup request, once the
396 * request fails because of the eviction. always retry here */
397 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) {
398 req->rq_allow_replay = 1;
399 req->rq_no_delay = 1;
403 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
404 LUSTRE_MDS_VERSION, FLD_READ);
408 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
409 RCL_SERVER, PAGE_CACHE_SIZE);
419 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
421 ptlrpc_request_set_replen(req);
422 req->rq_request_portal = FLD_REQUEST_PORTAL;
423 req->rq_reply_portal = MDC_REPLY_PORTAL;
424 ptlrpc_at_set_req_timeout(req);
426 obd_get_request_slot(&exp->exp_obd->u.cli);
427 rc = ptlrpc_queue_wait(req);
428 obd_put_request_slot(&exp->exp_obd->u.cli);
431 /* Don't loop forever on non-existing FID sequences. */
436 if (imp->imp_state != LUSTRE_IMP_CLOSED && !imp->imp_deactive) {
437 /* Since LWP is not replayable, so it will keep
438 * trying unless umount happens, otherwise it would
439 * cause unecessary failure of the application. */
440 ptlrpc_req_finished(req);
447 if (fld_op == FLD_QUERY) {
448 prange = req_capsule_server_get(&req->rq_pill,
451 GOTO(out_req, rc = -EFAULT);
457 if (rc != 0 || reqp == NULL) {
458 ptlrpc_req_finished(req);
468 int fld_client_lookup(struct lu_client_fld *fld, u64 seq, u32 *mds,
469 __u32 flags, const struct lu_env *env)
471 struct lu_seq_range res = { 0 };
472 struct lu_fld_target *target;
473 struct lu_fld_target *origin;
477 fld->lcf_flags |= LUSTRE_FLD_RUN;
479 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
481 *mds = res.lsr_index;
485 /* Can not find it in the cache */
486 target = fld_client_get_target(fld, seq);
487 LASSERT(target != NULL);
490 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: "LPX64") on "
491 "target %s (idx "LPU64")\n", fld->lcf_name, seq,
492 fld_target_name(target), target->ft_idx);
495 fld_range_set_type(&res, flags);
497 #ifdef HAVE_SERVER_SUPPORT
498 if (target->ft_srv != NULL) {
499 LASSERT(env != NULL);
500 rc = fld_server_lookup(env, target->ft_srv, seq, &res);
502 #endif /* HAVE_SERVER_SUPPORT */
504 rc = fld_client_rpc(target->ft_exp, &res, FLD_QUERY, NULL);
507 if (rc == -ESHUTDOWN) {
508 /* If fld lookup failed because the target has been shutdown,
509 * then try next target in the list, until trying all targets
510 * or fld lookup succeeds */
511 spin_lock(&fld->lcf_lock);
512 if (target->ft_chain.next == fld->lcf_targets.prev)
513 target = list_entry(fld->lcf_targets.next,
514 struct lu_fld_target, ft_chain);
516 target = list_entry(target->ft_chain.next,
517 struct lu_fld_target,
519 spin_unlock(&fld->lcf_lock);
520 if (target != origin)
524 *mds = res.lsr_index;
525 fld_cache_insert(fld->lcf_cache, &res);
530 EXPORT_SYMBOL(fld_client_lookup);
532 void fld_client_flush(struct lu_client_fld *fld)
534 fld_cache_flush(fld->lcf_cache);
538 struct proc_dir_entry *fld_type_proc_dir;
540 static int __init fld_mod_init(void)
542 fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
545 if (IS_ERR(fld_type_proc_dir))
546 return PTR_ERR(fld_type_proc_dir);
548 #ifdef HAVE_SERVER_SUPPORT
549 fld_server_mod_init();
550 #endif /* HAVE_SERVER_SUPPORT */
555 static void __exit fld_mod_exit(void)
557 #ifdef HAVE_SERVER_SUPPORT
558 fld_server_mod_exit();
559 #endif /* HAVE_SERVER_SUPPORT */
561 if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
562 lprocfs_remove(&fld_type_proc_dir);
563 fld_type_proc_dir = NULL;
567 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
568 MODULE_DESCRIPTION("Lustre FLD");
569 MODULE_VERSION(LUSTRE_VERSION_STRING);
570 MODULE_LICENSE("GPL");
572 module_init(fld_mod_init);
573 module_exit(fld_mod_exit);