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/
31 * lustre/fld/fld_handler.c
33 * FLD (Fids Location Database)
35 * Author: Yury Umanets <umka@clusterfs.com>
36 * Author: WangDi <wangdi@clusterfs.com>
37 * Author: Pravin Shelar <pravin.shelar@sun.com>
40 #define DEBUG_SUBSYSTEM S_FLD
42 #include <libcfs/libcfs.h>
43 #include <linux/module.h>
46 #include <obd_support.h>
47 #include <lustre_fid.h>
48 #include <lustre_fld.h>
49 #include <lustre_req_layout.h>
50 #include <lprocfs_status.h>
51 #include "fld_internal.h"
53 /* context key constructor/destructor: fld_key_init, fld_key_fini */
54 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
56 /* context key: fld_thread_key */
57 /* MGS thread may create llog file causing FLD lookup */
58 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD);
60 int fld_server_mod_init(void)
62 LU_CONTEXT_KEY_INIT(&fld_thread_key);
63 return lu_context_key_register(&fld_thread_key);
66 void fld_server_mod_exit(void)
68 lu_context_key_degister(&fld_thread_key);
71 int fld_declare_server_create(const struct lu_env *env,
72 struct lu_server_fld *fld,
73 const struct lu_seq_range *range,
78 rc = fld_declare_index_create(env, fld, range, th);
81 EXPORT_SYMBOL(fld_declare_server_create);
84 * Insert FLD index entry and update FLD cache.
86 * This function is called from the sequence allocator when a super-sequence
87 * is granted to a server.
89 int fld_server_create(const struct lu_env *env, struct lu_server_fld *fld,
90 const struct lu_seq_range *range, struct thandle *th)
94 mutex_lock(&fld->lsf_lock);
95 rc = fld_index_create(env, fld, range, th);
96 mutex_unlock(&fld->lsf_lock);
100 EXPORT_SYMBOL(fld_server_create);
103 * Extract index information from fld name like srv-fsname-MDT0000
105 int fld_name_to_index(const char *name, u32 *index)
112 CDEBUG(D_INFO, "get index from %s\n", name);
113 dash = strrchr(name, '-');
117 rc = target_name2index(dash, index, NULL);
122 * Retrieve fldb entry from MDT0 and add to local FLDB and cache.
124 int fld_update_from_controller(const struct lu_env *env,
125 struct lu_server_fld *fld)
127 struct fld_thread_info *info;
128 struct lu_seq_range *range;
129 struct lu_seq_range_array *lsra;
131 struct ptlrpc_request *req;
138 * Update only happens during initalization, i.e. local FLDB
144 rc = fld_name_to_index(fld->lsf_name, &index);
148 /* No need update fldb for MDT0 */
152 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
153 LASSERT(info != NULL);
154 range = &info->fti_lrange;
155 memset(range, 0, sizeof(*range));
156 range->lsr_index = index;
157 fld_range_set_mdt(range);
160 rc = fld_client_rpc(fld->lsf_control_exp, range, FLD_READ,
165 LASSERT(req != NULL);
166 lsra = (struct lu_seq_range_array *)req_capsule_server_get(
167 &req->rq_pill, &RMF_GENERIC_DATA);
169 GOTO(out, rc = -EPROTO);
171 range_array_le_to_cpu(lsra, lsra);
172 for (i = 0; i < lsra->lsra_count; i++) {
175 if (lsra->lsra_lsr[i].lsr_flags != LU_SEQ_RANGE_MDT)
176 GOTO(out, rc = -EINVAL);
178 if (lsra->lsra_lsr[i].lsr_index != index)
179 GOTO(out, rc = -EINVAL);
181 mutex_lock(&fld->lsf_lock);
182 rc1 = fld_insert_entry(env, fld, &lsra->lsra_lsr[i]);
183 mutex_unlock(&fld->lsf_lock);
189 *range = lsra->lsra_lsr[lsra->lsra_count - 1];
190 } while (rc == -EAGAIN);
195 ptlrpc_req_finished(req);
199 EXPORT_SYMBOL(fld_update_from_controller);
202 * Lookup sequece in local cache/fldb.
204 int fld_local_lookup(const struct lu_env *env, struct lu_server_fld *fld,
205 u64 seq, struct lu_seq_range *range)
207 struct lu_seq_range *erange;
208 struct fld_thread_info *info;
213 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
214 LASSERT(info != NULL);
215 erange = &info->fti_lrange;
217 /* Lookup it in the cache. */
218 rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
220 if (unlikely(fld_range_type(erange) != fld_range_type(range) &&
221 !fld_range_is_any(range))) {
222 CERROR("%s: FLD cache range "DRANGE" does not match requested flag %x: rc = %d\n",
223 fld->lsf_name, PRANGE(erange), range->lsr_flags,
232 EXPORT_SYMBOL(fld_local_lookup);
235 * Lookup MDT/OST by seq, returns a range for given seq.
237 * If that entry is not cached in fld cache, request is sent to super
238 * sequence controller node (MDT0). All other MDT[1...N] and client
239 * cache fld entries, but this cache is not persistent.
241 int fld_server_lookup(const struct lu_env *env, struct lu_server_fld *fld,
242 u64 seq, struct lu_seq_range *range)
249 rc = fld_local_lookup(env, fld, seq, range);
253 rc = fld_name_to_index(fld->lsf_name, &index);
257 if (index == 0 && rc == LDD_F_SV_TYPE_MDT) {
259 * On server side, all entries should be in cache.
260 * If we can not find it in cache, just return error
262 CERROR("%s: Cannot find sequence %#llx: rc = %d\n",
263 fld->lsf_name, seq, -ENOENT);
268 if (!fld->lsf_control_exp) {
269 CERROR("%s: lookup %#llx, but not connects to MDT0 yet: rc = %d.\n",
270 fld->lsf_name, seq, -EIO);
274 * send request to mdt0 i.e. super seq. controller.
275 * This is temporary solution, long term solution is fld
276 * replication on all mdt servers.
278 range->lsr_start = seq;
279 for (i = 0; i < 5; i++) {
280 rc = fld_client_rpc(fld->lsf_control_exp,
281 range, FLD_QUERY, NULL);
284 schedule_timeout_interruptible(cfs_time_seconds(1));
287 fld_cache_insert(fld->lsf_cache, range);
291 EXPORT_SYMBOL(fld_server_lookup);
294 * All MDT server handle fld lookup operation. But only MDT0 has fld index.
295 * if entry is not found in cache we need to forward lookup request to MDT0
297 static int fld_handle_lookup(struct tgt_session_info *tsi)
299 struct obd_export *exp = tsi->tsi_exp;
300 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
301 struct lu_server_fld *fld;
302 struct lu_seq_range *in;
303 struct lu_seq_range *out;
308 in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
310 RETURN(err_serious(-EPROTO));
312 rc = req_capsule_server_pack(tsi->tsi_pill);
313 if (unlikely(rc != 0))
314 RETURN(err_serious(rc));
316 out = req_capsule_server_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
318 RETURN(err_serious(-EPROTO));
321 fld = lu_site2seq(site)->ss_server_fld;
323 rc = fld_server_lookup(tsi->tsi_env, fld, in->lsr_start, out);
325 CDEBUG(D_INFO, "%s: FLD req handle: error %d (range: "DRANGE")\n",
326 fld->lsf_name, rc, PRANGE(out));
331 static int fld_handle_read(struct tgt_session_info *tsi)
333 struct obd_export *exp = tsi->tsi_exp;
334 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
335 struct lu_seq_range *in;
341 req_capsule_set(tsi->tsi_pill, &RQF_FLD_READ);
343 in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
345 RETURN(err_serious(-EPROTO));
347 req_capsule_set_size(tsi->tsi_pill, &RMF_GENERIC_DATA, RCL_SERVER,
350 rc = req_capsule_server_pack(tsi->tsi_pill);
351 if (unlikely(rc != 0))
352 RETURN(err_serious(rc));
354 data = req_capsule_server_get(tsi->tsi_pill, &RMF_GENERIC_DATA);
356 rc = fld_server_read(tsi->tsi_env, lu_site2seq(site)->ss_server_fld,
357 in, data, PAGE_SIZE);
361 static int fld_handle_query(struct tgt_session_info *tsi)
367 req_capsule_set(tsi->tsi_pill, &RQF_FLD_QUERY);
369 rc = fld_handle_lookup(tsi);
375 * Returns true, if fid is local to this server node.
377 * WARNING: this function is *not* guaranteed to return false if fid is
378 * remote: it makes an educated conservative guess only.
380 * fid_is_local() is supposed to be used in assertion checks only.
382 int fid_is_local(const struct lu_env *env,
383 struct lu_site *site, const struct lu_fid *fid)
386 struct seq_server_site *ss_site;
387 struct lu_seq_range *range;
388 struct fld_thread_info *info;
392 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
393 range = &info->fti_lrange;
395 result = 1; /* conservatively assume fid is local */
396 ss_site = lu_site2seq(site);
397 if (ss_site->ss_client_fld) {
400 rc = fld_cache_lookup(ss_site->ss_client_fld->lcf_cache,
401 fid_seq(fid), range);
403 result = (range->lsr_index == ss_site->ss_node_id);
407 EXPORT_SYMBOL(fid_is_local);
409 static void fld_server_debugfs_fini(struct lu_server_fld *fld)
411 debugfs_remove_recursive(fld->lsf_debugfs_entry);
414 static void fld_server_debugfs_init(struct lu_server_fld *fld)
417 fld->lsf_debugfs_entry = debugfs_create_dir(fld->lsf_name,
420 debugfs_create_file("fldb", 0444, fld->lsf_debugfs_entry, fld,
421 &fld_debugfs_seq_fops);
424 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
425 struct dt_device *dt, const char *prefix, int type)
427 int cache_size, cache_threshold;
432 snprintf(fld->lsf_name, sizeof(fld->lsf_name), "srv-%s", prefix);
434 cache_size = FLD_SERVER_CACHE_SIZE / sizeof(struct fld_cache_entry);
436 cache_threshold = cache_size * FLD_SERVER_CACHE_THRESHOLD / 100;
438 mutex_init(&fld->lsf_lock);
439 fld->lsf_cache = fld_cache_init(fld->lsf_name, cache_size,
441 if (IS_ERR(fld->lsf_cache)) {
442 rc = PTR_ERR(fld->lsf_cache);
443 fld->lsf_cache = NULL;
447 rc = fld_index_init(env, fld, dt, type);
451 fld_server_debugfs_init(fld);
453 fld->lsf_control_exp = NULL;
454 fld->lsf_seq_lookup = fld_server_lookup;
456 fld->lsf_seq_lookup = fld_server_lookup;
459 fld_cache_fini(fld->lsf_cache);
462 EXPORT_SYMBOL(fld_server_init);
464 void fld_server_fini(const struct lu_env *env, struct lu_server_fld *fld)
468 fld_server_debugfs_fini(fld);
469 fld_index_fini(env, fld);
471 if (fld->lsf_cache) {
472 if (!IS_ERR(fld->lsf_cache))
473 fld_cache_fini(fld->lsf_cache);
474 fld->lsf_cache = NULL;
479 EXPORT_SYMBOL(fld_server_fini);
481 struct tgt_handler fld_handlers[] = {
482 TGT_FLD_HDL_VAR(0, FLD_QUERY, fld_handle_query),
483 TGT_FLD_HDL_VAR(0, FLD_READ, fld_handle_read),
485 EXPORT_SYMBOL(fld_handlers);