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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/fld/fld_request.c
34 * FLD (Fids Location Database)
36 * Author: Yury Umanets <umka@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_FLD
41 #include <linux/libcfs/libcfs.h>
42 #include <linux/module.h>
43 #include <asm/div64.h>
46 #include <obd_class.h>
47 #include <uapi/linux/lustre/lustre_ver.h>
48 #include <obd_support.h>
49 #include <lprocfs_status.h>
51 #include <lustre_req_layout.h>
52 #include <lustre_fld.h>
53 #include <lustre_mdc.h>
54 #include "fld_internal.h"
56 static int fld_rrb_hash(struct lu_client_fld *fld, u64 seq)
58 LASSERT(fld->lcf_count > 0);
59 return do_div(seq, fld->lcf_count);
62 static struct lu_fld_target *
63 fld_rrb_scan(struct lu_client_fld *fld, u64 seq)
65 struct lu_fld_target *target;
68 /* Because almost all of special sequence located in MDT0,
69 * it should go to index 0 directly, instead of calculating
70 * hash again, and also if other MDTs is not being connected,
71 * the fld lookup requests(for seq on MDT0) should not be
72 * blocked because of other MDTs
74 if (fid_seq_is_norm(seq))
75 hash = fld_rrb_hash(fld, seq);
80 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
81 if (target->ft_idx == hash)
86 /* It is possible the remote target(MDT) are not connected to
87 * with client yet, so we will refer this to MDT0, which should
88 * be connected during mount
94 CERROR("%s: Can't find target by hash %d (seq %#llx). Targets (%d):\n",
95 fld->lcf_name, hash, seq, fld->lcf_count);
97 list_for_each_entry(target, &fld->lcf_targets, ft_chain) {
98 const char *srv_name = target->ft_srv ?
99 target->ft_srv->lsf_name : "<null>";
100 const char *exp_name = target->ft_exp ?
101 (char *)target->ft_exp->exp_obd->obd_uuid.uuid :
104 CERROR(" exp: 0x%p (%s), srv: 0x%p (%s), idx: %llu\n",
105 target->ft_exp, exp_name, target->ft_srv,
106 srv_name, target->ft_idx);
110 * If target is not found, there is logical error anyway, so here is
111 * LBUG() to catch this situation.
117 struct lu_fld_hash fld_hash[] = {
120 .fh_hash_func = fld_rrb_hash,
121 .fh_scan_func = fld_rrb_scan
128 static struct lu_fld_target *
129 fld_client_get_target(struct lu_client_fld *fld, u64 seq)
131 struct lu_fld_target *target;
133 LASSERT(fld->lcf_hash);
135 spin_lock(&fld->lcf_lock);
136 target = fld->lcf_hash->fh_scan_func(fld, seq);
137 spin_unlock(&fld->lcf_lock);
140 CDEBUG(D_INFO, "%s: Found target (idx %llu) by seq %#llx\n",
141 fld->lcf_name, target->ft_idx, seq);
148 * Add export to FLD. This is usually done by CMM and LMV as they are main users
151 int fld_client_add_target(struct lu_client_fld *fld,
152 struct lu_fld_target *tar)
155 struct lu_fld_target *target, *tmp;
158 name = fld_target_name(tar);
160 LASSERT(tar->ft_srv || tar->ft_exp);
162 CDEBUG(D_INFO, "%s: Adding target %s (idx %llu)\n",
163 fld->lcf_name, name, tar->ft_idx);
165 target = kzalloc(sizeof(*target), GFP_NOFS);
169 spin_lock(&fld->lcf_lock);
170 list_for_each_entry(tmp, &fld->lcf_targets, ft_chain) {
171 if (tmp->ft_idx == tar->ft_idx) {
172 spin_unlock(&fld->lcf_lock);
174 CERROR("Target %s exists in FLD and known as %s:#%llu\n",
175 name, fld_target_name(tmp), tmp->ft_idx);
180 target->ft_exp = tar->ft_exp;
182 class_export_get(target->ft_exp);
183 target->ft_srv = tar->ft_srv;
184 target->ft_idx = tar->ft_idx;
186 list_add_tail(&target->ft_chain, &fld->lcf_targets);
189 spin_unlock(&fld->lcf_lock);
193 EXPORT_SYMBOL(fld_client_add_target);
195 /* Remove export from FLD */
196 int fld_client_del_target(struct lu_client_fld *fld, __u64 idx)
198 struct lu_fld_target *target, *tmp;
200 spin_lock(&fld->lcf_lock);
201 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
202 if (target->ft_idx == idx) {
204 list_del(&target->ft_chain);
205 spin_unlock(&fld->lcf_lock);
208 class_export_put(target->ft_exp);
214 spin_unlock(&fld->lcf_lock);
218 static struct dentry *fld_debugfs_dir;
220 static int fld_client_debugfs_init(struct lu_client_fld *fld)
224 fld->lcf_debugfs_entry = ldebugfs_register(fld->lcf_name,
228 if (IS_ERR_OR_NULL(fld->lcf_debugfs_entry)) {
229 CERROR("%s: LdebugFS failed in fld-init\n", fld->lcf_name);
230 rc = fld->lcf_debugfs_entry ? PTR_ERR(fld->lcf_debugfs_entry)
232 fld->lcf_debugfs_entry = NULL;
236 rc = ldebugfs_add_vars(fld->lcf_debugfs_entry,
237 fld_client_debugfs_list, fld);
239 CERROR("%s: Can't init FLD debufs, rc %d\n", fld->lcf_name, rc);
246 fld_client_debugfs_fini(fld);
250 void fld_client_debugfs_fini(struct lu_client_fld *fld)
252 if (!IS_ERR_OR_NULL(fld->lcf_debugfs_entry))
253 ldebugfs_remove(&fld->lcf_debugfs_entry);
255 EXPORT_SYMBOL(fld_client_debugfs_fini);
257 static inline int hash_is_sane(int hash)
259 return (hash >= 0 && hash < ARRAY_SIZE(fld_hash));
262 int fld_client_init(struct lu_client_fld *fld,
263 const char *prefix, int hash)
265 int cache_size, cache_threshold;
268 snprintf(fld->lcf_name, sizeof(fld->lcf_name),
271 if (!hash_is_sane(hash)) {
272 CERROR("%s: Wrong hash function %#x\n",
273 fld->lcf_name, hash);
278 spin_lock_init(&fld->lcf_lock);
279 fld->lcf_hash = &fld_hash[hash];
280 INIT_LIST_HEAD(&fld->lcf_targets);
282 cache_size = FLD_CLIENT_CACHE_SIZE /
283 sizeof(struct fld_cache_entry);
285 cache_threshold = cache_size *
286 FLD_CLIENT_CACHE_THRESHOLD / 100;
288 fld->lcf_cache = fld_cache_init(fld->lcf_name,
289 cache_size, cache_threshold);
290 if (IS_ERR(fld->lcf_cache)) {
291 rc = PTR_ERR(fld->lcf_cache);
292 fld->lcf_cache = NULL;
296 rc = fld_client_debugfs_init(fld);
301 fld_client_fini(fld);
303 CDEBUG(D_INFO, "%s: Using \"%s\" hash\n",
304 fld->lcf_name, fld->lcf_hash->fh_name);
307 EXPORT_SYMBOL(fld_client_init);
309 void fld_client_fini(struct lu_client_fld *fld)
311 struct lu_fld_target *target, *tmp;
313 spin_lock(&fld->lcf_lock);
314 list_for_each_entry_safe(target, tmp, &fld->lcf_targets, ft_chain) {
316 list_del(&target->ft_chain);
318 class_export_put(target->ft_exp);
321 spin_unlock(&fld->lcf_lock);
323 if (fld->lcf_cache) {
324 if (!IS_ERR(fld->lcf_cache))
325 fld_cache_fini(fld->lcf_cache);
326 fld->lcf_cache = NULL;
329 EXPORT_SYMBOL(fld_client_fini);
331 int fld_client_rpc(struct obd_export *exp,
332 struct lu_seq_range *range, __u32 fld_op,
333 struct ptlrpc_request **reqp)
335 struct ptlrpc_request *req = NULL;
336 struct lu_seq_range *prange;
339 struct obd_import *imp;
343 imp = class_exp2cliimp(exp);
346 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_QUERY,
347 LUSTRE_MDS_VERSION, FLD_QUERY);
352 * XXX: only needed when talking to old server(< 2.6), it should
353 * be removed when < 2.6 server is not supported
355 op = req_capsule_client_get(&req->rq_pill, &RMF_FLD_OPC);
358 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS)
359 req->rq_allow_replay = 1;
362 req = ptlrpc_request_alloc_pack(imp, &RQF_FLD_READ,
363 LUSTRE_MDS_VERSION, FLD_READ);
367 req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA,
368 RCL_SERVER, PAGE_SIZE);
377 prange = req_capsule_client_get(&req->rq_pill, &RMF_FLD_MDFLD);
379 ptlrpc_request_set_replen(req);
380 req->rq_request_portal = FLD_REQUEST_PORTAL;
381 req->rq_reply_portal = MDC_REPLY_PORTAL;
382 ptlrpc_at_set_req_timeout(req);
384 obd_get_request_slot(&exp->exp_obd->u.cli);
385 rc = ptlrpc_queue_wait(req);
386 obd_put_request_slot(&exp->exp_obd->u.cli);
390 if (fld_op == FLD_QUERY) {
391 prange = req_capsule_server_get(&req->rq_pill, &RMF_FLD_MDFLD);
401 ptlrpc_req_finished(req);
411 int fld_client_lookup(struct lu_client_fld *fld, u64 seq, u32 *mds,
412 __u32 flags, const struct lu_env *env)
414 struct lu_seq_range res = { 0 };
415 struct lu_fld_target *target;
418 rc = fld_cache_lookup(fld->lcf_cache, seq, &res);
420 *mds = res.lsr_index;
424 /* Can not find it in the cache */
425 target = fld_client_get_target(fld, seq);
428 CDEBUG(D_INFO, "%s: Lookup fld entry (seq: %#llx) on target %s (idx %llu)\n",
429 fld->lcf_name, seq, fld_target_name(target), target->ft_idx);
432 fld_range_set_type(&res, flags);
433 rc = fld_client_rpc(target->ft_exp, &res, FLD_QUERY, NULL);
436 *mds = res.lsr_index;
438 fld_cache_insert(fld->lcf_cache, &res);
442 EXPORT_SYMBOL(fld_client_lookup);
444 void fld_client_flush(struct lu_client_fld *fld)
446 fld_cache_flush(fld->lcf_cache);
449 static int __init fld_init(void)
451 fld_debugfs_dir = ldebugfs_register(LUSTRE_FLD_NAME,
454 return PTR_ERR_OR_ZERO(fld_debugfs_dir);
457 static void __exit fld_exit(void)
459 if (!IS_ERR_OR_NULL(fld_debugfs_dir))
460 ldebugfs_remove(&fld_debugfs_dir);
463 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
464 MODULE_DESCRIPTION("Lustre FID Location Database");
465 MODULE_VERSION(LUSTRE_VERSION_STRING);
466 MODULE_LICENSE("GPL");
468 module_init(fld_init)
469 module_exit(fld_exit)