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_handler.c
34 * FLD (Fids Location Database)
36 * Author: Yury Umanets <umka@clusterfs.com>
37 * Author: WangDi <wangdi@clusterfs.com>
38 * Author: Pravin Shelar <pravin.shelar@sun.com>
41 #define DEBUG_SUBSYSTEM S_FLD
43 #include <libcfs/libcfs.h>
44 #include <linux/module.h>
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <lustre_fld.h>
50 #include <lustre_req_layout.h>
51 #include <lprocfs_status.h>
52 #include "fld_internal.h"
54 /* context key constructor/destructor: fld_key_init, fld_key_fini */
55 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
57 /* context key: fld_thread_key */
58 /* MGS thread may create llog file causing FLD lookup */
59 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD);
61 int fld_server_mod_init(void)
63 LU_CONTEXT_KEY_INIT(&fld_thread_key);
64 return lu_context_key_register(&fld_thread_key);
67 void fld_server_mod_exit(void)
69 lu_context_key_degister(&fld_thread_key);
72 int fld_declare_server_create(const struct lu_env *env,
73 struct lu_server_fld *fld,
74 const struct lu_seq_range *range,
79 rc = fld_declare_index_create(env, fld, range, th);
82 EXPORT_SYMBOL(fld_declare_server_create);
85 * Insert FLD index entry and update FLD cache.
87 * This function is called from the sequence allocator when a super-sequence
88 * is granted to a server.
90 int fld_server_create(const struct lu_env *env, struct lu_server_fld *fld,
91 const struct lu_seq_range *range, struct thandle *th)
95 mutex_lock(&fld->lsf_lock);
96 rc = fld_index_create(env, fld, range, th);
97 mutex_unlock(&fld->lsf_lock);
101 EXPORT_SYMBOL(fld_server_create);
104 * Extract index information from fld name like srv-fsname-MDT0000
106 int fld_name_to_index(const char *name, u32 *index)
113 CDEBUG(D_INFO, "get index from %s\n", name);
114 dash = strrchr(name, '-');
118 rc = target_name2index(dash, index, NULL);
123 * Retrieve fldb entry from MDT0 and add to local FLDB and cache.
125 int fld_update_from_controller(const struct lu_env *env,
126 struct lu_server_fld *fld)
128 struct fld_thread_info *info;
129 struct lu_seq_range *range;
130 struct lu_seq_range_array *lsra;
132 struct ptlrpc_request *req;
139 * Update only happens during initalization, i.e. local FLDB
145 rc = fld_name_to_index(fld->lsf_name, &index);
149 /* No need update fldb for MDT0 */
153 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
154 LASSERT(info != NULL);
155 range = &info->fti_lrange;
156 memset(range, 0, sizeof(*range));
157 range->lsr_index = index;
158 fld_range_set_mdt(range);
161 rc = fld_client_rpc(fld->lsf_control_exp, range, FLD_READ,
163 if (rc != 0 && rc != -EAGAIN)
166 LASSERT(req != NULL);
167 lsra = (struct lu_seq_range_array *)req_capsule_server_get(
168 &req->rq_pill, &RMF_GENERIC_DATA);
170 GOTO(out, rc = -EPROTO);
172 range_array_le_to_cpu(lsra, lsra);
173 for (i = 0; i < lsra->lsra_count; i++) {
176 if (lsra->lsra_lsr[i].lsr_flags != LU_SEQ_RANGE_MDT)
177 GOTO(out, rc = -EINVAL);
179 if (lsra->lsra_lsr[i].lsr_index != index)
180 GOTO(out, rc = -EINVAL);
182 mutex_lock(&fld->lsf_lock);
183 rc1 = fld_insert_entry(env, fld, &lsra->lsra_lsr[i]);
184 mutex_unlock(&fld->lsf_lock);
190 *range = lsra->lsra_lsr[lsra->lsra_count - 1];
191 } while (rc == -EAGAIN);
196 ptlrpc_req_finished(req);
200 EXPORT_SYMBOL(fld_update_from_controller);
203 * Lookup sequece in local cache/fldb.
205 int fld_local_lookup(const struct lu_env *env, struct lu_server_fld *fld,
206 u64 seq, struct lu_seq_range *range)
208 struct lu_seq_range *erange;
209 struct fld_thread_info *info;
214 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
215 LASSERT(info != NULL);
216 erange = &info->fti_lrange;
218 /* Lookup it in the cache. */
219 rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
221 if (unlikely(fld_range_type(erange) != fld_range_type(range) &&
222 !fld_range_is_any(range))) {
223 CERROR("%s: FLD cache range "DRANGE" does not match requested flag %x: rc = %d\n",
224 fld->lsf_name, PRANGE(erange), range->lsr_flags,
233 EXPORT_SYMBOL(fld_local_lookup);
236 * Lookup MDT/OST by seq, returns a range for given seq.
238 * If that entry is not cached in fld cache, request is sent to super
239 * sequence controller node (MDT0). All other MDT[1...N] and client
240 * cache fld entries, but this cache is not persistent.
242 int fld_server_lookup(const struct lu_env *env, struct lu_server_fld *fld,
243 u64 seq, struct lu_seq_range *range)
250 rc = fld_local_lookup(env, fld, seq, range);
254 rc = fld_name_to_index(fld->lsf_name, &index);
258 if (index == 0 && rc == LDD_F_SV_TYPE_MDT) {
260 * On server side, all entries should be in cache.
261 * If we can not find it in cache, just return error
263 CERROR("%s: Cannot find sequence %#llx: rc = %d\n",
264 fld->lsf_name, seq, -ENOENT);
267 if (!fld->lsf_control_exp) {
268 CERROR("%s: lookup %#llx, but not connects to MDT0 yet: rc = %d.\n",
269 fld->lsf_name, seq, -EIO);
273 * send request to mdt0 i.e. super seq. controller.
274 * This is temporary solution, long term solution is fld
275 * replication on all mdt servers.
277 range->lsr_start = seq;
278 rc = fld_client_rpc(fld->lsf_control_exp,
279 range, FLD_QUERY, NULL);
281 fld_cache_insert(fld->lsf_cache, range);
285 EXPORT_SYMBOL(fld_server_lookup);
288 * All MDT server handle fld lookup operation. But only MDT0 has fld index.
289 * if entry is not found in cache we need to forward lookup request to MDT0
291 static int fld_handle_lookup(struct tgt_session_info *tsi)
293 struct obd_export *exp = tsi->tsi_exp;
294 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
295 struct lu_server_fld *fld;
296 struct lu_seq_range *in;
297 struct lu_seq_range *out;
302 in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
304 RETURN(err_serious(-EPROTO));
306 rc = req_capsule_server_pack(tsi->tsi_pill);
307 if (unlikely(rc != 0))
308 RETURN(err_serious(rc));
310 out = req_capsule_server_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
312 RETURN(err_serious(-EPROTO));
315 fld = lu_site2seq(site)->ss_server_fld;
317 rc = fld_server_lookup(tsi->tsi_env, fld, in->lsr_start, out);
319 CDEBUG(D_INFO, "%s: FLD req handle: error %d (range: "DRANGE")\n",
320 fld->lsf_name, rc, PRANGE(out));
325 static int fld_handle_read(struct tgt_session_info *tsi)
327 struct obd_export *exp = tsi->tsi_exp;
328 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
329 struct lu_seq_range *in;
335 req_capsule_set(tsi->tsi_pill, &RQF_FLD_READ);
337 in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
339 RETURN(err_serious(-EPROTO));
341 req_capsule_set_size(tsi->tsi_pill, &RMF_GENERIC_DATA, RCL_SERVER,
344 rc = req_capsule_server_pack(tsi->tsi_pill);
345 if (unlikely(rc != 0))
346 RETURN(err_serious(rc));
348 data = req_capsule_server_get(tsi->tsi_pill, &RMF_GENERIC_DATA);
350 rc = fld_server_read(tsi->tsi_env, lu_site2seq(site)->ss_server_fld,
351 in, data, PAGE_SIZE);
355 static int fld_handle_query(struct tgt_session_info *tsi)
361 req_capsule_set(tsi->tsi_pill, &RQF_FLD_QUERY);
363 rc = fld_handle_lookup(tsi);
369 * Returns true, if fid is local to this server node.
371 * WARNING: this function is *not* guaranteed to return false if fid is
372 * remote: it makes an educated conservative guess only.
374 * fid_is_local() is supposed to be used in assertion checks only.
376 int fid_is_local(const struct lu_env *env,
377 struct lu_site *site, const struct lu_fid *fid)
380 struct seq_server_site *ss_site;
381 struct lu_seq_range *range;
382 struct fld_thread_info *info;
386 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
387 range = &info->fti_lrange;
389 result = 1; /* conservatively assume fid is local */
390 ss_site = lu_site2seq(site);
391 if (ss_site->ss_client_fld) {
394 rc = fld_cache_lookup(ss_site->ss_client_fld->lcf_cache,
395 fid_seq(fid), range);
397 result = (range->lsr_index == ss_site->ss_node_id);
401 EXPORT_SYMBOL(fid_is_local);
403 static void fld_server_debugfs_fini(struct lu_server_fld *fld)
405 debugfs_remove_recursive(fld->lsf_debugfs_entry);
408 static void fld_server_debugfs_init(struct lu_server_fld *fld)
411 fld->lsf_debugfs_entry = debugfs_create_dir(fld->lsf_name,
414 debugfs_create_file("fldb", 0444, fld->lsf_debugfs_entry, fld,
415 &fld_debugfs_seq_fops);
418 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
419 struct dt_device *dt, const char *prefix, int type)
421 int cache_size, cache_threshold;
426 snprintf(fld->lsf_name, sizeof(fld->lsf_name), "srv-%s", prefix);
428 cache_size = FLD_SERVER_CACHE_SIZE / sizeof(struct fld_cache_entry);
430 cache_threshold = cache_size * FLD_SERVER_CACHE_THRESHOLD / 100;
432 mutex_init(&fld->lsf_lock);
433 fld->lsf_cache = fld_cache_init(fld->lsf_name, cache_size,
435 if (IS_ERR(fld->lsf_cache)) {
436 rc = PTR_ERR(fld->lsf_cache);
437 fld->lsf_cache = NULL;
441 rc = fld_index_init(env, fld, dt, type);
445 fld_server_debugfs_init(fld);
447 fld->lsf_control_exp = NULL;
448 fld->lsf_seq_lookup = fld_server_lookup;
450 fld->lsf_seq_lookup = fld_server_lookup;
453 fld_cache_fini(fld->lsf_cache);
456 EXPORT_SYMBOL(fld_server_init);
458 void fld_server_fini(const struct lu_env *env, struct lu_server_fld *fld)
462 fld_server_debugfs_fini(fld);
463 fld_index_fini(env, fld);
465 if (fld->lsf_cache) {
466 if (!IS_ERR(fld->lsf_cache))
467 fld_cache_fini(fld->lsf_cache);
468 fld->lsf_cache = NULL;
473 EXPORT_SYMBOL(fld_server_fini);
475 struct tgt_handler fld_handlers[] = {
476 TGT_FLD_HDL_VAR(0, FLD_QUERY, fld_handle_query),
477 TGT_FLD_HDL_VAR(0, FLD_READ, fld_handle_read),
479 EXPORT_SYMBOL(fld_handlers);