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, Whamcloud, Inc.
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/jbd.h>
49 # include <asm/div64.h>
50 #else /* __KERNEL__ */
51 # include <liblustre.h>
52 # include <libcfs/list.h>
56 #include <obd_class.h>
57 #include <lustre_ver.h>
58 #include <obd_support.h>
59 #include <lprocfs_status.h>
61 #include <dt_object.h>
62 #include <md_object.h>
63 #include <lustre_req_layout.h>
64 #include <lustre_fld.h>
65 #include <lustre_mdc.h>
66 #include "fld_internal.h"
68 /* TODO: these 3 functions are copies of flow-control code from mdc_lib.c
69 * It should be common thing. The same about mdc RPC lock */
70 static int fld_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
74 client_obd_list_lock(&cli->cl_loi_list_lock);
75 rc = cfs_list_empty(&mcw->mcw_entry);
76 client_obd_list_unlock(&cli->cl_loi_list_lock);
80 static void fld_enter_request(struct client_obd *cli)
82 struct mdc_cache_waiter mcw;
83 struct l_wait_info lwi = { 0 };
85 client_obd_list_lock(&cli->cl_loi_list_lock);
86 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
87 cfs_list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
88 cfs_waitq_init(&mcw.mcw_waitq);
89 client_obd_list_unlock(&cli->cl_loi_list_lock);
90 l_wait_event(mcw.mcw_waitq, fld_req_avail(cli, &mcw), &lwi);
92 cli->cl_r_in_flight++;
93 client_obd_list_unlock(&cli->cl_loi_list_lock);
97 static void fld_exit_request(struct client_obd *cli)
100 struct mdc_cache_waiter *mcw;
102 client_obd_list_lock(&cli->cl_loi_list_lock);
103 cli->cl_r_in_flight--;
104 cfs_list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
106 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
107 /* No free request slots anymore */
111 mcw = cfs_list_entry(l, struct mdc_cache_waiter, mcw_entry);
112 cfs_list_del_init(&mcw->mcw_entry);
113 cli->cl_r_in_flight++;
114 cfs_waitq_signal(&mcw->mcw_waitq);
116 client_obd_list_unlock(&cli->cl_loi_list_lock);
119 static int fld_rrb_hash(struct lu_client_fld *fld,
122 LASSERT(fld->lcf_count > 0);
123 return do_div(seq, fld->lcf_count);
126 static struct lu_fld_target *
127 fld_rrb_scan(struct lu_client_fld *fld, seqno_t seq)
129 struct lu_fld_target *target;
133 hash = fld_rrb_hash(fld, seq);
135 cfs_list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
136 if (target->ft_idx == hash)
140 CERROR("%s: Can't find target by hash %d (seq "LPX64"). "
141 "Targets (%d):\n", fld->lcf_name, hash, seq,
144 cfs_list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
145 const char *srv_name = target->ft_srv != NULL ?
146 target->ft_srv->lsf_name : "<null>";
147 const char *exp_name = target->ft_exp != NULL ?
148 (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
151 CERROR(" exp: 0x%p (%s), srv: 0x%p (%s), idx: "LPU64"\n",
152 target->ft_exp, exp_name, target->ft_srv,
153 srv_name, target->ft_idx);
157 * If target is not found, there is logical error anyway, so here is
158 * LBUG() to catch this situation.
164 struct lu_fld_hash fld_hash[] = {
167 .fh_hash_func = fld_rrb_hash,
168 .fh_scan_func = fld_rrb_scan
175 static struct lu_fld_target *
176 fld_client_get_target(struct lu_client_fld *fld,
179 struct lu_fld_target *target;
182 LASSERT(fld->lcf_hash != NULL);
184 cfs_spin_lock(&fld->lcf_lock);
185 target = fld->lcf_hash->fh_scan_func(fld, seq);
186 cfs_spin_unlock(&fld->lcf_lock);
188 if (target != NULL) {
189 CDEBUG(D_INFO, "%s: Found target (idx "LPU64
190 ") by seq "LPX64"\n", fld->lcf_name,
191 target->ft_idx, seq);
198 * Add export to FLD. This is usually done by CMM and LMV as they are main users
201 int fld_client_add_target(struct lu_client_fld *fld,
202 struct lu_fld_target *tar)
204 const char *name = fld_target_name(tar);
205 struct lu_fld_target *target, *tmp;
208 LASSERT(tar != NULL);
209 LASSERT(name != NULL);
210 LASSERT(tar->ft_srv != NULL || tar->ft_exp != NULL);
212 if (fld->lcf_flags != LUSTRE_FLD_INIT) {
213 CERROR("%s: Attempt to add target %s (idx "LPU64") "
214 "on fly - skip it\n", fld->lcf_name, name,
218 CDEBUG(D_INFO, "%s: Adding target %s (idx "
219 LPU64")\n", fld->lcf_name, name, tar->ft_idx);
222 OBD_ALLOC_PTR(target);
226 cfs_spin_lock(&fld->lcf_lock);
227 cfs_list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
228 if (tmp->ft_idx == tar->ft_idx) {
229 cfs_spin_unlock(&fld->lcf_lock);
230 OBD_FREE_PTR(target);
231 CERROR("Target %s exists in FLD and known as %s:#"LPU64"\n",
232 name, fld_target_name(tmp), tmp->ft_idx);
237 target->ft_exp = tar->ft_exp;
238 if (target->ft_exp != NULL)
239 class_export_get(target->ft_exp);
240 target->ft_srv = tar->ft_srv;
241 target->ft_idx = tar->ft_idx;
243 cfs_list_add_tail(&target->ft_chain,
247 cfs_spin_unlock(&fld->lcf_lock);
251 EXPORT_SYMBOL(fld_client_add_target);
253 /* Remove export from FLD */
254 int fld_client_del_target(struct lu_client_fld *fld,
257 struct lu_fld_target *target, *tmp;
260 cfs_spin_lock(&fld->lcf_lock);
261 cfs_list_for_each_entry_safe(target, tmp,
262 &fld->lcf_targets, ft_chain) {
263 if (target->ft_idx == idx) {
265 cfs_list_del(&target->ft_chain);
266 cfs_spin_unlock(&fld->lcf_lock);
268 if (target->ft_exp != NULL)
269 class_export_put(target->ft_exp);
271 OBD_FREE_PTR(target);
275 cfs_spin_unlock(&fld->lcf_lock);
278 EXPORT_SYMBOL(fld_client_del_target);
281 static int fld_client_proc_init(struct lu_client_fld *fld)
286 fld->lcf_proc_dir = lprocfs_register(fld->lcf_name,
290 if (IS_ERR(fld->lcf_proc_dir)) {
291 CERROR("%s: LProcFS failed in fld-init\n",
293 rc = PTR_ERR(fld->lcf_proc_dir);
297 rc = lprocfs_add_vars(fld->lcf_proc_dir,
298 fld_client_proc_list, fld);
300 CERROR("%s: Can't init FLD proc, rc %d\n",
302 GOTO(out_cleanup, rc);
308 fld_client_proc_fini(fld);
312 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;
323 static int fld_client_proc_init(struct lu_client_fld *fld)
328 void fld_client_proc_fini(struct lu_client_fld *fld)
334 EXPORT_SYMBOL(fld_client_proc_fini);
336 static inline int hash_is_sane(int hash)
338 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
341 int fld_client_init(struct lu_client_fld *fld,
342 const char *prefix, int hash)
344 int cache_size, cache_threshold;
348 LASSERT(fld != NULL);
350 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
353 if (!hash_is_sane(hash)) {
354 CERROR("%s: Wrong hash function %#x\n",
355 fld->lcf_name, hash);
360 cfs_spin_lock_init(&fld->lcf_lock);
361 fld->lcf_hash = &fld_hash[hash];
362 fld->lcf_flags = LUSTRE_FLD_INIT;
363 CFS_INIT_LIST_HEAD(&fld->lcf_targets);
365 cache_size = FLD_CLIENT_CACHE_SIZE /
366 sizeof(struct fld_cache_entry);
368 cache_threshold = cache_size *
369 FLD_CLIENT_CACHE_THRESHOLD / 100;
371 fld->lcf_cache = fld_cache_init(fld->lcf_name,
372 cache_size, cache_threshold);
373 if (IS_ERR(fld->lcf_cache)) {
374 rc = PTR_ERR(fld->lcf_cache);
375 fld->lcf_cache = NULL;
379 rc = fld_client_proc_init(fld);
385 fld_client_fini(fld);
387 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
388 fld->lcf_name, fld->lcf_hash->fh_name);
391 EXPORT_SYMBOL(fld_client_init);
393 void fld_client_fini(struct lu_client_fld *fld)
395 struct lu_fld_target *target, *tmp;
398 cfs_spin_lock(&fld->lcf_lock);
399 cfs_list_for_each_entry_safe(target, tmp,
400 &fld->lcf_targets, ft_chain) {
402 cfs_list_del(&target->ft_chain);
403 if (target->ft_exp != NULL)
404 class_export_put(target->ft_exp);
405 OBD_FREE_PTR(target);
407 cfs_spin_unlock(&fld->lcf_lock);
409 if (fld->lcf_cache != NULL) {
410 if (!IS_ERR(fld->lcf_cache))
411 fld_cache_fini(fld->lcf_cache);
412 fld->lcf_cache = NULL;
417 EXPORT_SYMBOL(fld_client_fini);
419 int fld_client_rpc(struct obd_export *exp,
420 struct lu_seq_range *range, __u32 fld_op)
422 struct ptlrpc_request *req;
423 struct lu_seq_range *prange;
428 LASSERT(exp != NULL);
430 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_FLD_QUERY,
431 LUSTRE_MDS_VERSION, FLD_QUERY);
435 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
438 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
441 ptlrpc_request_set_replen(req);
442 req->rq_request_portal = FLD_REQUEST_PORTAL;
443 ptlrpc_at_set_req_timeout(req);
445 if (fld_op != FLD_LOOKUP)
446 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
447 fld_enter_request(&exp->exp_obd->u.cli);
448 rc = ptlrpc_queue_wait(req);
449 fld_exit_request(&exp->exp_obd->u.cli);
450 if (fld_op != FLD_LOOKUP)
451 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
455 prange = req_capsule_server_get(&req->rq_pill, &RMF_FLD_MDFLD);
457 GOTO(out_req, rc = -EFAULT);
461 ptlrpc_req_finished(req);
465 int fld_client_lookup(struct lu_client_fld *fld, seqno_t seq, mdsno_t *mds,
466 __u32 flags, const struct lu_env *env)
468 struct lu_seq_range res;
469 struct lu_fld_target *target;
473 fld->lcf_flags |= LUSTRE_FLD_RUN;
475 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
477 *mds = res.lsr_index;
481 /* Can not find it in the cache */
482 target = fld_client_get_target(fld, seq);
483 LASSERT(target != NULL);
485 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: "LPX64") on "
486 "target %s (idx "LPU64")\n", fld->lcf_name, seq,
487 fld_target_name(target), target->ft_idx);
490 res.lsr_flags = flags;
492 if (target->ft_srv != NULL) {
493 LASSERT(env != NULL);
494 rc = fld_server_lookup(target->ft_srv,
498 rc = fld_client_rpc(target->ft_exp,
505 *mds = res.lsr_index;
507 fld_cache_insert(fld->lcf_cache, &res);
511 EXPORT_SYMBOL(fld_client_lookup);
513 void fld_client_flush(struct lu_client_fld *fld)
515 fld_cache_flush(fld->lcf_cache);
517 EXPORT_SYMBOL(fld_client_flush);