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, 2013, 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
46 # include <libcfs/libcfs.h>
47 # include <linux/module.h>
48 # include <linux/math64.h>
49 #else /* __KERNEL__ */
50 # include <liblustre.h>
51 # include <libcfs/list.h>
55 #include <obd_class.h>
56 #include <obd_support.h>
57 #include <lprocfs_status.h>
58 #include <lustre_req_layout.h>
59 #include <lustre_fld.h>
60 #include <lustre_mdc.h>
61 #include "fld_internal.h"
63 static int fld_rrb_hash(struct lu_client_fld *fld,
66 LASSERT(fld->lcf_count > 0);
67 return do_div(seq, fld->lcf_count);
70 static struct lu_fld_target *
71 fld_rrb_scan(struct lu_client_fld *fld, seqno_t seq)
73 struct lu_fld_target *target;
77 /* Because almost all of special sequence located in MDT0,
78 * it should go to index 0 directly, instead of calculating
79 * hash again, and also if other MDTs is not being connected,
80 * the fld lookup requests(for seq on MDT0) should not be
81 * blocked because of other MDTs */
82 if (fid_seq_is_norm(seq))
83 hash = fld_rrb_hash(fld, seq);
88 cfs_list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
89 if (target->ft_idx == hash)
94 /* It is possible the remote target(MDT) are not connected to
95 * with client yet, so we will refer this to MDT0, which should
96 * be connected during mount */
101 CERROR("%s: Can't find target by hash %d (seq "LPX64"). "
102 "Targets (%d):\n", fld->lcf_name, hash, seq,
105 cfs_list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
106 const char *srv_name = target->ft_srv != NULL ?
107 target->ft_srv->lsf_name : "<null>";
108 const char *exp_name = target->ft_exp != NULL ?
109 (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
112 CERROR(" exp: 0x%p (%s), srv: 0x%p (%s), idx: "LPU64"\n",
113 target->ft_exp, exp_name, target->ft_srv,
114 srv_name, target->ft_idx);
118 * If target is not found, there is logical error anyway, so here is
119 * LBUG() to catch this situation.
125 struct lu_fld_hash fld_hash[] = {
128 .fh_hash_func = fld_rrb_hash,
129 .fh_scan_func = fld_rrb_scan
136 static struct lu_fld_target *
137 fld_client_get_target(struct lu_client_fld *fld, seqno_t seq)
139 struct lu_fld_target *target;
142 LASSERT(fld->lcf_hash != NULL);
144 spin_lock(&fld->lcf_lock);
145 target = fld->lcf_hash->fh_scan_func(fld, seq);
146 spin_unlock(&fld->lcf_lock);
148 if (target != NULL) {
149 CDEBUG(D_INFO, "%s: Found target (idx "LPU64
150 ") by seq "LPX64"\n", fld->lcf_name,
151 target->ft_idx, seq);
158 * Add export to FLD. This is usually done by CMM and LMV as they are main users
161 int fld_client_add_target(struct lu_client_fld *fld,
162 struct lu_fld_target *tar)
165 struct lu_fld_target *target, *tmp;
168 LASSERT(tar != NULL);
169 name = fld_target_name(tar);
170 LASSERT(name != NULL);
171 LASSERT(tar->ft_srv != NULL || tar->ft_exp != NULL);
173 if (fld->lcf_flags != LUSTRE_FLD_INIT) {
174 CERROR("%s: Attempt to add target %s (idx "LPU64") "
175 "on fly - skip it\n", fld->lcf_name, name,
179 CDEBUG(D_INFO, "%s: Adding target %s (idx "
180 LPU64")\n", fld->lcf_name, name, tar->ft_idx);
183 OBD_ALLOC_PTR(target);
187 spin_lock(&fld->lcf_lock);
188 cfs_list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
189 if (tmp->ft_idx == tar->ft_idx) {
190 spin_unlock(&fld->lcf_lock);
191 OBD_FREE_PTR(target);
192 CERROR("Target %s exists in FLD and known as %s:#"LPU64"\n",
193 name, fld_target_name(tmp), tmp->ft_idx);
198 target->ft_exp = tar->ft_exp;
199 if (target->ft_exp != NULL)
200 class_export_get(target->ft_exp);
201 target->ft_srv = tar->ft_srv;
202 target->ft_idx = tar->ft_idx;
204 cfs_list_add_tail(&target->ft_chain,
208 spin_unlock(&fld->lcf_lock);
212 EXPORT_SYMBOL(fld_client_add_target);
214 /* Remove export from FLD */
215 int fld_client_del_target(struct lu_client_fld *fld, __u64 idx)
217 struct lu_fld_target *target, *tmp;
220 spin_lock(&fld->lcf_lock);
221 cfs_list_for_each_entry_safe(target, tmp,
222 &fld->lcf_targets, ft_chain) {
223 if (target->ft_idx == idx) {
225 cfs_list_del(&target->ft_chain);
226 spin_unlock(&fld->lcf_lock);
228 if (target->ft_exp != NULL)
229 class_export_put(target->ft_exp);
231 OBD_FREE_PTR(target);
235 spin_unlock(&fld->lcf_lock);
238 EXPORT_SYMBOL(fld_client_del_target);
241 static int fld_client_proc_init(struct lu_client_fld *fld)
246 fld->lcf_proc_dir = lprocfs_seq_register(fld->lcf_name,
249 if (IS_ERR(fld->lcf_proc_dir)) {
250 CERROR("%s: LProcFS failed in fld-init\n",
252 rc = PTR_ERR(fld->lcf_proc_dir);
256 rc = lprocfs_seq_add_vars(fld->lcf_proc_dir,
257 fld_client_proc_list, fld);
259 CERROR("%s: Can't init FLD proc, rc %d\n",
261 GOTO(out_cleanup, rc);
267 fld_client_proc_fini(fld);
271 void fld_client_proc_fini(struct lu_client_fld *fld)
274 if (fld->lcf_proc_dir) {
275 if (!IS_ERR(fld->lcf_proc_dir))
276 lprocfs_remove(&fld->lcf_proc_dir);
277 fld->lcf_proc_dir = NULL;
282 static int fld_client_proc_init(struct lu_client_fld *fld)
287 void fld_client_proc_fini(struct lu_client_fld *fld)
293 EXPORT_SYMBOL(fld_client_proc_fini);
295 static inline int hash_is_sane(int hash)
297 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
300 int fld_client_init(struct lu_client_fld *fld,
301 const char *prefix, int hash)
303 int cache_size, cache_threshold;
307 LASSERT(fld != NULL);
309 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
312 if (!hash_is_sane(hash)) {
313 CERROR("%s: Wrong hash function %#x\n",
314 fld->lcf_name, hash);
319 spin_lock_init(&fld->lcf_lock);
320 fld->lcf_hash = &fld_hash[hash];
321 fld->lcf_flags = LUSTRE_FLD_INIT;
322 CFS_INIT_LIST_HEAD(&fld->lcf_targets);
324 cache_size = FLD_CLIENT_CACHE_SIZE /
325 sizeof(struct fld_cache_entry);
327 cache_threshold = cache_size *
328 FLD_CLIENT_CACHE_THRESHOLD / 100;
330 fld->lcf_cache = fld_cache_init(fld->lcf_name,
331 cache_size, cache_threshold);
332 if (IS_ERR(fld->lcf_cache)) {
333 rc = PTR_ERR(fld->lcf_cache);
334 fld->lcf_cache = NULL;
338 rc = fld_client_proc_init(fld);
344 fld_client_fini(fld);
346 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
347 fld->lcf_name, fld->lcf_hash->fh_name);
350 EXPORT_SYMBOL(fld_client_init);
352 void fld_client_fini(struct lu_client_fld *fld)
354 struct lu_fld_target *target, *tmp;
357 spin_lock(&fld->lcf_lock);
358 cfs_list_for_each_entry_safe(target, tmp,
359 &fld->lcf_targets, ft_chain) {
361 cfs_list_del(&target->ft_chain);
362 if (target->ft_exp != NULL)
363 class_export_put(target->ft_exp);
364 OBD_FREE_PTR(target);
366 spin_unlock(&fld->lcf_lock);
368 if (fld->lcf_cache != NULL) {
369 if (!IS_ERR(fld->lcf_cache))
370 fld_cache_fini(fld->lcf_cache);
371 fld->lcf_cache = NULL;
376 EXPORT_SYMBOL(fld_client_fini);
378 int fld_client_rpc(struct obd_export *exp,
379 struct lu_seq_range *range, __u32 fld_op,
380 struct ptlrpc_request **reqp)
382 struct ptlrpc_request *req = NULL;
383 struct lu_seq_range *prange;
386 struct obd_import *imp;
389 LASSERT(exp != NULL);
392 imp = class_exp2cliimp(exp);
395 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
396 LUSTRE_MDS_VERSION, FLD_QUERY);
400 /* XXX: only needed when talking to old server(< 2.6), it should
401 * be removed when < 2.6 server is not supported */
402 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
405 /* For MDS_MDS seq lookup, it will always use LWP connection,
406 * but LWP will be evicted after restart, so cause the error.
407 * so we will set no_delay for seq lookup request, once the
408 * request fails because of the eviction. always retry here */
409 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) {
410 req->rq_allow_replay = 1;
411 req->rq_no_delay = 1;
415 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
416 LUSTRE_MDS_VERSION, FLD_READ);
420 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
421 RCL_SERVER, PAGE_CACHE_SIZE);
431 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
433 ptlrpc_request_set_replen(req);
434 req->rq_request_portal = FLD_REQUEST_PORTAL;
435 req->rq_reply_portal = MDC_REPLY_PORTAL;
436 ptlrpc_at_set_req_timeout(req);
438 obd_get_request_slot(&exp->exp_obd->u.cli);
439 rc = ptlrpc_queue_wait(req);
440 obd_put_request_slot(&exp->exp_obd->u.cli);
442 if (rc == -EWOULDBLOCK) {
443 /* For no_delay req(see above), EWOULDBLOCK means the
444 * connection is being evicted, but this seq lookup
445 * should not return error, since it would cause
446 * unecessary failure of the application, instead
447 * it should retry here */
448 ptlrpc_req_finished(req);
455 if (fld_op == FLD_QUERY) {
456 prange = req_capsule_server_get(&req->rq_pill,
459 GOTO(out_req, rc = -EFAULT);
465 if (rc != 0 || reqp == NULL) {
466 ptlrpc_req_finished(req);
476 int fld_client_lookup(struct lu_client_fld *fld, seqno_t seq, mdsno_t *mds,
477 __u32 flags, const struct lu_env *env)
479 struct lu_seq_range res = { 0 };
480 struct lu_fld_target *target;
484 fld->lcf_flags |= LUSTRE_FLD_RUN;
486 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
488 *mds = res.lsr_index;
492 /* Can not find it in the cache */
493 target = fld_client_get_target(fld, seq);
494 LASSERT(target != NULL);
496 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: "LPX64") on "
497 "target %s (idx "LPU64")\n", fld->lcf_name, seq,
498 fld_target_name(target), target->ft_idx);
501 fld_range_set_type(&res, flags);
503 #if defined(__KERNEL__) && defined(HAVE_SERVER_SUPPORT)
504 if (target->ft_srv != NULL) {
505 LASSERT(env != NULL);
506 rc = fld_server_lookup(env, target->ft_srv, seq, &res);
510 rc = fld_client_rpc(target->ft_exp, &res, FLD_QUERY, NULL);
514 *mds = res.lsr_index;
515 fld_cache_insert(fld->lcf_cache, &res);
520 EXPORT_SYMBOL(fld_client_lookup);
522 void fld_client_flush(struct lu_client_fld *fld)
524 fld_cache_flush(fld->lcf_cache);
526 EXPORT_SYMBOL(fld_client_flush);
530 struct proc_dir_entry *fld_type_proc_dir;
532 static int __init fld_mod_init(void)
534 fld_type_proc_dir = lprocfs_seq_register(LUSTRE_FLD_NAME,
537 if (IS_ERR(fld_type_proc_dir))
538 return PTR_ERR(fld_type_proc_dir);
540 #ifdef HAVE_SERVER_SUPPORT
541 fld_server_mod_init();
547 static void __exit fld_mod_exit(void)
549 #ifdef HAVE_SERVER_SUPPORT
550 fld_server_mod_exit();
553 if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
554 lprocfs_remove(&fld_type_proc_dir);
555 fld_type_proc_dir = NULL;
559 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
560 MODULE_DESCRIPTION("Lustre FLD");
561 MODULE_LICENSE("GPL");
563 cfs_module(mdd, LUSTRE_VERSION_STRING, fld_mod_init, fld_mod_exit);
564 #endif /* __KERNEL__ */