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
45 #include "../../include/linux/libcfs/libcfs.h"
46 #include <linux/module.h>
47 #include <asm/div64.h>
49 #include "../include/obd.h"
50 #include "../include/obd_class.h"
51 #include "../include/lustre_ver.h"
52 #include "../include/obd_support.h"
53 #include "../include/lprocfs_status.h"
55 #include "../include/dt_object.h"
56 #include "../include/lustre_req_layout.h"
57 #include "../include/lustre_fld.h"
58 #include "../include/lustre_mdc.h"
59 #include "fld_internal.h"
61 /* TODO: these 3 functions are copies of flow-control code from mdc_lib.c
62 * It should be common thing. The same about mdc RPC lock */
63 static int fld_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
67 client_obd_list_lock(&cli->cl_loi_list_lock);
68 rc = list_empty(&mcw->mcw_entry);
69 client_obd_list_unlock(&cli->cl_loi_list_lock);
73 static void fld_enter_request(struct client_obd *cli)
75 struct mdc_cache_waiter mcw;
76 struct l_wait_info lwi = { 0 };
78 client_obd_list_lock(&cli->cl_loi_list_lock);
79 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
80 list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
81 init_waitqueue_head(&mcw.mcw_waitq);
82 client_obd_list_unlock(&cli->cl_loi_list_lock);
83 l_wait_event(mcw.mcw_waitq, fld_req_avail(cli, &mcw), &lwi);
85 cli->cl_r_in_flight++;
86 client_obd_list_unlock(&cli->cl_loi_list_lock);
90 static void fld_exit_request(struct client_obd *cli)
92 struct list_head *l, *tmp;
93 struct mdc_cache_waiter *mcw;
95 client_obd_list_lock(&cli->cl_loi_list_lock);
96 cli->cl_r_in_flight--;
97 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
99 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
100 /* No free request slots anymore */
104 mcw = list_entry(l, struct mdc_cache_waiter, mcw_entry);
105 list_del_init(&mcw->mcw_entry);
106 cli->cl_r_in_flight++;
107 wake_up(&mcw->mcw_waitq);
109 client_obd_list_unlock(&cli->cl_loi_list_lock);
112 static int fld_rrb_hash(struct lu_client_fld *fld, u64 seq)
114 LASSERT(fld->lcf_count > 0);
115 return do_div(seq, fld->lcf_count);
118 static struct lu_fld_target *
119 fld_rrb_scan(struct lu_client_fld *fld, u64 seq)
121 struct lu_fld_target *target;
124 /* Because almost all of special sequence located in MDT0,
125 * it should go to index 0 directly, instead of calculating
126 * hash again, and also if other MDTs is not being connected,
127 * the fld lookup requests(for seq on MDT0) should not be
128 * blocked because of other MDTs */
129 if (fid_seq_is_norm(seq))
130 hash = fld_rrb_hash(fld, seq);
135 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
136 if (target->ft_idx == hash)
141 /* It is possible the remote target(MDT) are not connected to
142 * with client yet, so we will refer this to MDT0, which should
143 * be connected during mount */
148 CERROR("%s: Can't find target by hash %d (seq %#llx). Targets (%d):\n",
149 fld->lcf_name, hash, seq, fld->lcf_count);
151 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
152 const char *srv_name = target->ft_srv != NULL ?
153 target->ft_srv->lsf_name : "<null>";
154 const char *exp_name = target->ft_exp != NULL ?
155 (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
158 CERROR(" exp: 0x%p (%s), srv: 0x%p (%s), idx: %llu\n",
159 target->ft_exp, exp_name, target->ft_srv,
160 srv_name, target->ft_idx);
164 * If target is not found, there is logical error anyway, so here is
165 * LBUG() to catch this situation.
171 struct lu_fld_hash fld_hash[] = {
174 .fh_hash_func = fld_rrb_hash,
175 .fh_scan_func = fld_rrb_scan
182 static struct lu_fld_target *
183 fld_client_get_target(struct lu_client_fld *fld, u64 seq)
185 struct lu_fld_target *target;
187 LASSERT(fld->lcf_hash != NULL);
189 spin_lock(&fld->lcf_lock);
190 target = fld->lcf_hash->fh_scan_func(fld, seq);
191 spin_unlock(&fld->lcf_lock);
193 if (target != NULL) {
194 CDEBUG(D_INFO, "%s: Found target (idx %llu) by seq %#llx\n",
195 fld->lcf_name, target->ft_idx, seq);
202 * Add export to FLD. This is usually done by CMM and LMV as they are main users
205 int fld_client_add_target(struct lu_client_fld *fld,
206 struct lu_fld_target *tar)
209 struct lu_fld_target *target, *tmp;
211 LASSERT(tar != NULL);
212 name = fld_target_name(tar);
213 LASSERT(name != NULL);
214 LASSERT(tar->ft_srv != NULL || tar->ft_exp != NULL);
216 if (fld->lcf_flags != LUSTRE_FLD_INIT) {
217 CERROR("%s: Attempt to add target %s (idx %llu) on fly - skip it\n",
218 fld->lcf_name, name, tar->ft_idx);
221 CDEBUG(D_INFO, "%s: Adding target %s (idx %llu)\n",
222 fld->lcf_name, name, tar->ft_idx);
224 OBD_ALLOC_PTR(target);
228 spin_lock(&fld->lcf_lock);
229 list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
230 if (tmp->ft_idx == tar->ft_idx) {
231 spin_unlock(&fld->lcf_lock);
232 OBD_FREE_PTR(target);
233 CERROR("Target %s exists in FLD and known as %s:#%llu\n",
234 name, fld_target_name(tmp), tmp->ft_idx);
239 target->ft_exp = tar->ft_exp;
240 if (target->ft_exp != NULL)
241 class_export_get(target->ft_exp);
242 target->ft_srv = tar->ft_srv;
243 target->ft_idx = tar->ft_idx;
245 list_add_tail(&target->ft_chain,
249 spin_unlock(&fld->lcf_lock);
253 EXPORT_SYMBOL(fld_client_add_target);
255 /* Remove export from FLD */
256 int fld_client_del_target(struct lu_client_fld *fld, __u64 idx)
258 struct lu_fld_target *target, *tmp;
260 spin_lock(&fld->lcf_lock);
261 list_for_each_entry_safe(target, tmp,
262 &fld->lcf_targets, ft_chain) {
263 if (target->ft_idx == idx) {
265 list_del(&target->ft_chain);
266 spin_unlock(&fld->lcf_lock);
268 if (target->ft_exp != NULL)
269 class_export_put(target->ft_exp);
271 OBD_FREE_PTR(target);
275 spin_unlock(&fld->lcf_lock);
278 EXPORT_SYMBOL(fld_client_del_target);
280 static struct proc_dir_entry *fld_type_proc_dir;
282 #if defined(CONFIG_PROC_FS)
283 static int fld_client_proc_init(struct lu_client_fld *fld)
287 fld->lcf_proc_dir = lprocfs_register(fld->lcf_name,
291 if (IS_ERR(fld->lcf_proc_dir)) {
292 CERROR("%s: LProcFS failed in fld-init\n",
294 rc = PTR_ERR(fld->lcf_proc_dir);
298 rc = lprocfs_add_vars(fld->lcf_proc_dir,
299 fld_client_proc_list, fld);
301 CERROR("%s: Can't init FLD proc, rc %d\n",
309 fld_client_proc_fini(fld);
313 void fld_client_proc_fini(struct lu_client_fld *fld)
315 if (fld->lcf_proc_dir) {
316 if (!IS_ERR(fld->lcf_proc_dir))
317 lprocfs_remove(&fld->lcf_proc_dir);
318 fld->lcf_proc_dir = NULL;
322 static int fld_client_proc_init(struct lu_client_fld *fld)
327 void fld_client_proc_fini(struct lu_client_fld *fld)
331 EXPORT_SYMBOL(fld_client_proc_fini);
333 static inline int hash_is_sane(int hash)
335 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
338 int fld_client_init(struct lu_client_fld *fld,
339 const char *prefix, int hash)
341 int cache_size, cache_threshold;
344 LASSERT(fld != NULL);
346 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
349 if (!hash_is_sane(hash)) {
350 CERROR("%s: Wrong hash function %#x\n",
351 fld->lcf_name, hash);
356 spin_lock_init(&fld->lcf_lock);
357 fld->lcf_hash = &fld_hash[hash];
358 fld->lcf_flags = LUSTRE_FLD_INIT;
359 INIT_LIST_HEAD(&fld->lcf_targets);
361 cache_size = FLD_CLIENT_CACHE_SIZE /
362 sizeof(struct fld_cache_entry);
364 cache_threshold = cache_size *
365 FLD_CLIENT_CACHE_THRESHOLD / 100;
367 fld->lcf_cache = fld_cache_init(fld->lcf_name,
368 cache_size, cache_threshold);
369 if (IS_ERR(fld->lcf_cache)) {
370 rc = PTR_ERR(fld->lcf_cache);
371 fld->lcf_cache = NULL;
375 rc = fld_client_proc_init(fld);
380 fld_client_fini(fld);
382 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
383 fld->lcf_name, fld->lcf_hash->fh_name);
386 EXPORT_SYMBOL(fld_client_init);
388 void fld_client_fini(struct lu_client_fld *fld)
390 struct lu_fld_target *target, *tmp;
392 spin_lock(&fld->lcf_lock);
393 list_for_each_entry_safe(target, tmp,
394 &fld->lcf_targets, ft_chain) {
396 list_del(&target->ft_chain);
397 if (target->ft_exp != NULL)
398 class_export_put(target->ft_exp);
399 OBD_FREE_PTR(target);
401 spin_unlock(&fld->lcf_lock);
403 if (fld->lcf_cache != NULL) {
404 if (!IS_ERR(fld->lcf_cache))
405 fld_cache_fini(fld->lcf_cache);
406 fld->lcf_cache = NULL;
409 EXPORT_SYMBOL(fld_client_fini);
411 int fld_client_rpc(struct obd_export *exp,
412 struct lu_seq_range *range, __u32 fld_op)
414 struct ptlrpc_request *req;
415 struct lu_seq_range *prange;
418 struct obd_import *imp;
420 LASSERT(exp != NULL);
422 imp = class_exp2cliimp(exp);
423 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY, LUSTRE_MDS_VERSION,
428 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
431 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
434 ptlrpc_request_set_replen(req);
435 req->rq_request_portal = FLD_REQUEST_PORTAL;
436 req->rq_reply_portal = MDC_REPLY_PORTAL;
437 ptlrpc_at_set_req_timeout(req);
439 if (fld_op == FLD_LOOKUP &&
440 imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS)
441 req->rq_allow_replay = 1;
443 if (fld_op != FLD_LOOKUP)
444 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
445 fld_enter_request(&exp->exp_obd->u.cli);
446 rc = ptlrpc_queue_wait(req);
447 fld_exit_request(&exp->exp_obd->u.cli);
448 if (fld_op != FLD_LOOKUP)
449 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
453 prange = req_capsule_server_get(&req->rq_pill, &RMF_FLD_MDFLD);
454 if (prange == NULL) {
460 ptlrpc_req_finished(req);
464 int fld_client_lookup(struct lu_client_fld *fld, u64 seq, u32 *mds,
465 __u32 flags, const struct lu_env *env)
467 struct lu_seq_range res = { 0 };
468 struct lu_fld_target *target;
471 fld->lcf_flags |= LUSTRE_FLD_RUN;
473 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
475 *mds = res.lsr_index;
479 /* Can not find it in the cache */
480 target = fld_client_get_target(fld, seq);
481 LASSERT(target != NULL);
483 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: %#llx) on target %s (idx %llu)\n",
484 fld->lcf_name, seq, fld_target_name(target), target->ft_idx);
487 fld_range_set_type(&res, flags);
488 rc = fld_client_rpc(target->ft_exp, &res, FLD_LOOKUP);
491 *mds = res.lsr_index;
493 fld_cache_insert(fld->lcf_cache, &res);
497 EXPORT_SYMBOL(fld_client_lookup);
499 void fld_client_flush(struct lu_client_fld *fld)
501 fld_cache_flush(fld->lcf_cache);
503 EXPORT_SYMBOL(fld_client_flush);
505 static int __init fld_mod_init(void)
507 fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
510 return PTR_ERR_OR_ZERO(fld_type_proc_dir);
513 static void __exit fld_mod_exit(void)
515 if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
516 lprocfs_remove(&fld_type_proc_dir);
517 fld_type_proc_dir = NULL;
521 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
522 MODULE_DESCRIPTION("Lustre FLD");
523 MODULE_LICENSE("GPL");
525 module_init(fld_mod_init)
526 module_exit(fld_mod_exit)