1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_FLD
49 # include <libcfs/libcfs.h>
50 # include <linux/module.h>
51 # include <linux/jbd.h>
52 # include <asm/div64.h>
53 #else /* __KERNEL__ */
54 # include <liblustre.h>
55 # include <libcfs/list.h>
59 #include <obd_class.h>
60 #include <lustre_ver.h>
61 #include <obd_support.h>
62 #include <lprocfs_status.h>
64 #include <dt_object.h>
65 #include <md_object.h>
66 #include <lustre_req_layout.h>
67 #include <lustre_fld.h>
68 #include <lustre_mdc.h>
69 #include "fld_internal.h"
71 /* TODO: these 3 functions are copies of flow-control code from mdc_lib.c
72 * It should be common thing. The same about mdc RPC lock */
73 static int fld_req_avail(struct client_obd *cli, struct mdc_cache_waiter *mcw)
77 spin_lock(&cli->cl_loi_list_lock);
78 rc = list_empty(&mcw->mcw_entry);
79 spin_unlock(&cli->cl_loi_list_lock);
83 static void fld_enter_request(struct client_obd *cli)
85 struct mdc_cache_waiter mcw;
86 struct l_wait_info lwi = { 0 };
88 spin_lock(&cli->cl_loi_list_lock);
89 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
90 list_add_tail(&mcw.mcw_entry, &cli->cl_cache_waiters);
91 cfs_waitq_init(&mcw.mcw_waitq);
92 spin_unlock(&cli->cl_loi_list_lock);
93 l_wait_event(mcw.mcw_waitq, fld_req_avail(cli, &mcw), &lwi);
95 cli->cl_r_in_flight++;
96 spin_unlock(&cli->cl_loi_list_lock);
100 static void fld_exit_request(struct client_obd *cli)
102 struct list_head *l, *tmp;
103 struct mdc_cache_waiter *mcw;
105 spin_lock(&cli->cl_loi_list_lock);
106 cli->cl_r_in_flight--;
107 list_for_each_safe(l, tmp, &cli->cl_cache_waiters) {
109 if (cli->cl_r_in_flight >= cli->cl_max_rpcs_in_flight) {
110 /* No free request slots anymore */
114 mcw = list_entry(l, struct mdc_cache_waiter, mcw_entry);
115 list_del_init(&mcw->mcw_entry);
116 cli->cl_r_in_flight++;
117 cfs_waitq_signal(&mcw->mcw_waitq);
119 spin_unlock(&cli->cl_loi_list_lock);
122 static int fld_rrb_hash(struct lu_client_fld *fld,
125 LASSERT(fld->lcf_count > 0);
126 return do_div(seq, fld->lcf_count);
129 static struct lu_fld_target *
130 fld_rrb_scan(struct lu_client_fld *fld, seqno_t seq)
132 struct lu_fld_target *target;
136 hash = fld_rrb_hash(fld, seq);
138 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
139 if (target->ft_idx == hash)
143 CERROR("%s: Can't find target by hash %d (seq "LPX64"). "
144 "Targets (%d):\n", fld->lcf_name, hash, seq,
147 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
148 const char *srv_name = target->ft_srv != NULL ?
149 target->ft_srv->lsf_name : "<null>";
150 const char *exp_name = target->ft_exp != NULL ?
151 (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
154 CERROR(" exp: 0x%p (%s), srv: 0x%p (%s), idx: "LPU64"\n",
155 target->ft_exp, exp_name, target->ft_srv,
156 srv_name, target->ft_idx);
160 * If target is not found, there is logical error anyway, so here is
161 * LBUG() to catch this situation.
167 struct lu_fld_hash fld_hash[] = {
170 .fh_hash_func = fld_rrb_hash,
171 .fh_scan_func = fld_rrb_scan
178 static struct lu_fld_target *
179 fld_client_get_target(struct lu_client_fld *fld,
182 struct lu_fld_target *target;
185 LASSERT(fld->lcf_hash != NULL);
187 spin_lock(&fld->lcf_lock);
188 target = fld->lcf_hash->fh_scan_func(fld, seq);
189 spin_unlock(&fld->lcf_lock);
191 if (target != NULL) {
192 CDEBUG(D_INFO, "%s: Found target (idx "LPU64
193 ") by seq "LPX64"\n", fld->lcf_name,
194 target->ft_idx, seq);
201 * Add export to FLD. This is usually done by CMM and LMV as they are main users
204 int fld_client_add_target(struct lu_client_fld *fld,
205 struct lu_fld_target *tar)
207 const char *name = fld_target_name(tar);
208 struct lu_fld_target *target, *tmp;
211 LASSERT(tar != NULL);
212 LASSERT(name != NULL);
213 LASSERT(tar->ft_srv != NULL || tar->ft_exp != NULL);
215 if (fld->lcf_flags != LUSTRE_FLD_INIT) {
216 CERROR("%s: Attempt to add target %s (idx "LPU64") "
217 "on fly - skip it\n", fld->lcf_name, name,
221 CDEBUG(D_INFO, "%s: Adding target %s (idx "
222 LPU64")\n", fld->lcf_name, name, tar->ft_idx);
225 OBD_ALLOC_PTR(target);
229 spin_lock(&fld->lcf_lock);
230 list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
231 if (tmp->ft_idx == tar->ft_idx) {
232 spin_unlock(&fld->lcf_lock);
233 OBD_FREE_PTR(target);
234 CERROR("Target %s exists in FLD and known as %s:#"LPU64"\n",
235 name, fld_target_name(tmp), tmp->ft_idx);
240 target->ft_exp = tar->ft_exp;
241 if (target->ft_exp != NULL)
242 class_export_get(target->ft_exp);
243 target->ft_srv = tar->ft_srv;
244 target->ft_idx = tar->ft_idx;
246 list_add_tail(&target->ft_chain,
250 spin_unlock(&fld->lcf_lock);
254 EXPORT_SYMBOL(fld_client_add_target);
256 /* Remove export from FLD */
257 int fld_client_del_target(struct lu_client_fld *fld,
260 struct lu_fld_target *target, *tmp;
263 spin_lock(&fld->lcf_lock);
264 list_for_each_entry_safe(target, tmp,
265 &fld->lcf_targets, ft_chain) {
266 if (target->ft_idx == idx) {
268 list_del(&target->ft_chain);
269 spin_unlock(&fld->lcf_lock);
271 if (target->ft_exp != NULL)
272 class_export_put(target->ft_exp);
274 OBD_FREE_PTR(target);
278 spin_unlock(&fld->lcf_lock);
281 EXPORT_SYMBOL(fld_client_del_target);
283 static void fld_client_proc_fini(struct lu_client_fld *fld);
286 static int fld_client_proc_init(struct lu_client_fld *fld)
291 fld->lcf_proc_dir = lprocfs_register(fld->lcf_name,
295 if (IS_ERR(fld->lcf_proc_dir)) {
296 CERROR("%s: LProcFS failed in fld-init\n",
298 rc = PTR_ERR(fld->lcf_proc_dir);
302 rc = lprocfs_add_vars(fld->lcf_proc_dir,
303 fld_client_proc_list, fld);
305 CERROR("%s: Can't init FLD proc, rc %d\n",
307 GOTO(out_cleanup, rc);
313 fld_client_proc_fini(fld);
317 static void fld_client_proc_fini(struct lu_client_fld *fld)
320 if (fld->lcf_proc_dir) {
321 if (!IS_ERR(fld->lcf_proc_dir))
322 lprocfs_remove(&fld->lcf_proc_dir);
323 fld->lcf_proc_dir = NULL;
328 static int fld_client_proc_init(struct lu_client_fld *fld)
333 static void fld_client_proc_fini(struct lu_client_fld *fld)
339 static inline int hash_is_sane(int hash)
341 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
344 int fld_client_init(struct lu_client_fld *fld,
345 const char *prefix, int hash)
348 int cache_size, cache_threshold;
353 LASSERT(fld != NULL);
355 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
358 if (!hash_is_sane(hash)) {
359 CERROR("%s: Wrong hash function %#x\n",
360 fld->lcf_name, hash);
365 spin_lock_init(&fld->lcf_lock);
366 fld->lcf_hash = &fld_hash[hash];
367 fld->lcf_flags = LUSTRE_FLD_INIT;
368 CFS_INIT_LIST_HEAD(&fld->lcf_targets);
371 cache_size = FLD_CLIENT_CACHE_SIZE /
372 sizeof(struct fld_cache_entry);
374 cache_threshold = cache_size *
375 FLD_CLIENT_CACHE_THRESHOLD / 100;
377 fld->lcf_cache = fld_cache_init(fld->lcf_name,
378 cache_size, cache_threshold);
379 if (IS_ERR(fld->lcf_cache)) {
380 rc = PTR_ERR(fld->lcf_cache);
381 fld->lcf_cache = NULL;
386 rc = fld_client_proc_init(fld);
392 fld_client_fini(fld);
394 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
395 fld->lcf_name, fld->lcf_hash->fh_name);
398 EXPORT_SYMBOL(fld_client_init);
400 void fld_client_fini(struct lu_client_fld *fld)
402 struct lu_fld_target *target, *tmp;
405 fld_client_proc_fini(fld);
407 spin_lock(&fld->lcf_lock);
408 list_for_each_entry_safe(target, tmp,
409 &fld->lcf_targets, ft_chain) {
411 list_del(&target->ft_chain);
412 if (target->ft_exp != NULL)
413 class_export_put(target->ft_exp);
414 OBD_FREE_PTR(target);
416 spin_unlock(&fld->lcf_lock);
419 if (fld->lcf_cache != NULL) {
420 if (!IS_ERR(fld->lcf_cache))
421 fld_cache_fini(fld->lcf_cache);
422 fld->lcf_cache = NULL;
428 EXPORT_SYMBOL(fld_client_fini);
430 int fld_client_rpc(struct obd_export *exp,
431 struct lu_seq_range *range, __u32 fld_op)
433 struct ptlrpc_request *req;
434 struct lu_seq_range *prange;
439 LASSERT(exp != NULL);
441 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_FLD_QUERY,
442 LUSTRE_MDS_VERSION, FLD_QUERY);
446 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
449 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
452 ptlrpc_request_set_replen(req);
453 req->rq_request_portal = FLD_REQUEST_PORTAL;
454 ptlrpc_at_set_req_timeout(req);
456 if (fld_op != FLD_LOOKUP)
457 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
458 fld_enter_request(&exp->exp_obd->u.cli);
459 rc = ptlrpc_queue_wait(req);
460 fld_exit_request(&exp->exp_obd->u.cli);
461 if (fld_op != FLD_LOOKUP)
462 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
466 prange = req_capsule_server_get(&req->rq_pill, &RMF_FLD_MDFLD);
468 GOTO(out_req, rc = -EFAULT);
472 ptlrpc_req_finished(req);
476 int fld_client_lookup(struct lu_client_fld *fld,
477 seqno_t seq, mdsno_t *mds,
478 const struct lu_env *env)
480 struct lu_seq_range res;
481 struct lu_fld_target *target;
485 fld->lcf_flags |= LUSTRE_FLD_RUN;
487 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
493 /* Can not find it in the cache */
494 target = fld_client_get_target(fld, seq);
495 LASSERT(target != NULL);
497 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: "LPX64") on "
498 "target %s (idx "LPU64")\n", fld->lcf_name, seq,
499 fld_target_name(target), target->ft_idx);
503 if (target->ft_srv != NULL) {
504 LASSERT(env != NULL);
505 rc = fld_server_lookup(target->ft_srv,
509 rc = fld_client_rpc(target->ft_exp,
518 fld_cache_insert(fld->lcf_cache, &res);
522 EXPORT_SYMBOL(fld_client_lookup);
524 void fld_client_flush(struct lu_client_fld *fld)
527 fld_cache_flush(fld->lcf_cache);
530 EXPORT_SYMBOL(fld_client_flush);