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,
162 if (rc != 0 && rc != -EAGAIN)
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);
266 if (!fld->lsf_control_exp) {
267 CERROR("%s: lookup %#llx, but not connects to MDT0 yet: rc = %d.\n",
268 fld->lsf_name, seq, -EIO);
272 * send request to mdt0 i.e. super seq. controller.
273 * This is temporary solution, long term solution is fld
274 * replication on all mdt servers.
276 range->lsr_start = seq;
277 rc = fld_client_rpc(fld->lsf_control_exp,
278 range, FLD_QUERY, NULL);
280 fld_cache_insert(fld->lsf_cache, range);
284 EXPORT_SYMBOL(fld_server_lookup);
287 * All MDT server handle fld lookup operation. But only MDT0 has fld index.
288 * if entry is not found in cache we need to forward lookup request to MDT0
290 static int fld_handle_lookup(struct tgt_session_info *tsi)
292 struct obd_export *exp = tsi->tsi_exp;
293 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
294 struct lu_server_fld *fld;
295 struct lu_seq_range *in;
296 struct lu_seq_range *out;
301 in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
303 RETURN(err_serious(-EPROTO));
305 rc = req_capsule_server_pack(tsi->tsi_pill);
306 if (unlikely(rc != 0))
307 RETURN(err_serious(rc));
309 out = req_capsule_server_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
311 RETURN(err_serious(-EPROTO));
314 fld = lu_site2seq(site)->ss_server_fld;
316 rc = fld_server_lookup(tsi->tsi_env, fld, in->lsr_start, out);
318 CDEBUG(D_INFO, "%s: FLD req handle: error %d (range: "DRANGE")\n",
319 fld->lsf_name, rc, PRANGE(out));
324 static int fld_handle_read(struct tgt_session_info *tsi)
326 struct obd_export *exp = tsi->tsi_exp;
327 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
328 struct lu_seq_range *in;
334 req_capsule_set(tsi->tsi_pill, &RQF_FLD_READ);
336 in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
338 RETURN(err_serious(-EPROTO));
340 req_capsule_set_size(tsi->tsi_pill, &RMF_GENERIC_DATA, RCL_SERVER,
343 rc = req_capsule_server_pack(tsi->tsi_pill);
344 if (unlikely(rc != 0))
345 RETURN(err_serious(rc));
347 data = req_capsule_server_get(tsi->tsi_pill, &RMF_GENERIC_DATA);
349 rc = fld_server_read(tsi->tsi_env, lu_site2seq(site)->ss_server_fld,
350 in, data, PAGE_SIZE);
354 static int fld_handle_query(struct tgt_session_info *tsi)
360 req_capsule_set(tsi->tsi_pill, &RQF_FLD_QUERY);
362 rc = fld_handle_lookup(tsi);
368 * Returns true, if fid is local to this server node.
370 * WARNING: this function is *not* guaranteed to return false if fid is
371 * remote: it makes an educated conservative guess only.
373 * fid_is_local() is supposed to be used in assertion checks only.
375 int fid_is_local(const struct lu_env *env,
376 struct lu_site *site, const struct lu_fid *fid)
379 struct seq_server_site *ss_site;
380 struct lu_seq_range *range;
381 struct fld_thread_info *info;
385 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
386 range = &info->fti_lrange;
388 result = 1; /* conservatively assume fid is local */
389 ss_site = lu_site2seq(site);
390 if (ss_site->ss_client_fld) {
393 rc = fld_cache_lookup(ss_site->ss_client_fld->lcf_cache,
394 fid_seq(fid), range);
396 result = (range->lsr_index == ss_site->ss_node_id);
400 EXPORT_SYMBOL(fid_is_local);
402 static void fld_server_debugfs_fini(struct lu_server_fld *fld)
404 debugfs_remove_recursive(fld->lsf_debugfs_entry);
407 static void fld_server_debugfs_init(struct lu_server_fld *fld)
410 fld->lsf_debugfs_entry = debugfs_create_dir(fld->lsf_name,
413 debugfs_create_file("fldb", 0444, fld->lsf_debugfs_entry, fld,
414 &fld_debugfs_seq_fops);
417 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
418 struct dt_device *dt, const char *prefix, int type)
420 int cache_size, cache_threshold;
425 snprintf(fld->lsf_name, sizeof(fld->lsf_name), "srv-%s", prefix);
427 cache_size = FLD_SERVER_CACHE_SIZE / sizeof(struct fld_cache_entry);
429 cache_threshold = cache_size * FLD_SERVER_CACHE_THRESHOLD / 100;
431 mutex_init(&fld->lsf_lock);
432 fld->lsf_cache = fld_cache_init(fld->lsf_name, cache_size,
434 if (IS_ERR(fld->lsf_cache)) {
435 rc = PTR_ERR(fld->lsf_cache);
436 fld->lsf_cache = NULL;
440 rc = fld_index_init(env, fld, dt, type);
444 fld_server_debugfs_init(fld);
446 fld->lsf_control_exp = NULL;
447 fld->lsf_seq_lookup = fld_server_lookup;
449 fld->lsf_seq_lookup = fld_server_lookup;
452 fld_cache_fini(fld->lsf_cache);
455 EXPORT_SYMBOL(fld_server_init);
457 void fld_server_fini(const struct lu_env *env, struct lu_server_fld *fld)
461 fld_server_debugfs_fini(fld);
462 fld_index_fini(env, fld);
464 if (fld->lsf_cache) {
465 if (!IS_ERR(fld->lsf_cache))
466 fld_cache_fini(fld->lsf_cache);
467 fld->lsf_cache = NULL;
472 EXPORT_SYMBOL(fld_server_fini);
474 struct tgt_handler fld_handlers[] = {
475 TGT_FLD_HDL_VAR(0, FLD_QUERY, fld_handle_query),
476 TGT_FLD_HDL_VAR(0, FLD_READ, fld_handle_read),
478 EXPORT_SYMBOL(fld_handlers);