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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/fld/fld_handler.c
38 * FLD (Fids Location Database)
40 * Author: Yury Umanets <umka@clusterfs.com>
41 * Author: WangDi <wangdi@clusterfs.com>
42 * Author: Pravin Shelar <pravin.shelar@sun.com>
45 #define DEBUG_SUBSYSTEM S_FLD
47 #include <libcfs/libcfs.h>
48 #include <linux/module.h>
51 #include <obd_support.h>
52 #include <lustre_fid.h>
53 #include <lustre_fld.h>
54 #include <md_object.h>
55 #include <lustre_req_layout.h>
56 #include <lprocfs_status.h>
57 #include "fld_internal.h"
59 /* context key constructor/destructor: fld_key_init, fld_key_fini */
60 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
62 /* context key: fld_thread_key */
63 /* MGS thread may create llog file causing FLD lookup */
64 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD);
66 int fld_server_mod_init(void)
68 LU_CONTEXT_KEY_INIT(&fld_thread_key);
69 return lu_context_key_register(&fld_thread_key);
72 void fld_server_mod_exit(void)
74 lu_context_key_degister(&fld_thread_key);
77 int fld_declare_server_create(const struct lu_env *env,
78 struct lu_server_fld *fld,
79 const struct lu_seq_range *range,
84 rc = fld_declare_index_create(env, fld, range, th);
87 EXPORT_SYMBOL(fld_declare_server_create);
90 * Insert FLD index entry and update FLD cache.
92 * This function is called from the sequence allocator when a super-sequence
93 * is granted to a server.
95 int fld_server_create(const struct lu_env *env, struct lu_server_fld *fld,
96 const struct lu_seq_range *range, struct thandle *th)
100 mutex_lock(&fld->lsf_lock);
101 rc = fld_index_create(env, fld, range, th);
102 mutex_unlock(&fld->lsf_lock);
106 EXPORT_SYMBOL(fld_server_create);
109 * Extract index information from fld name like srv-fsname-MDT0000
111 int fld_name_to_index(const char *name, __u32 *index)
117 CDEBUG(D_INFO, "get index from %s\n", name);
118 dash = strrchr(name, '-');
122 rc = target_name2index(dash, index, NULL);
127 * Retrieve fldb entry from MDT0 and add to local FLDB and cache.
129 int fld_update_from_controller(const struct lu_env *env,
130 struct lu_server_fld *fld)
132 struct fld_thread_info *info;
133 struct lu_seq_range *range;
134 struct lu_seq_range_array *lsra;
136 struct ptlrpc_request *req;
141 /* Update only happens during initalization, i.e. local FLDB
142 * does not exist yet */
146 rc = fld_name_to_index(fld->lsf_name, &index);
150 /* No need update fldb for MDT0 */
154 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
155 LASSERT(info != NULL);
156 range = &info->fti_lrange;
157 memset(range, 0, sizeof(*range));
158 range->lsr_index = index;
159 fld_range_set_mdt(range);
162 rc = fld_client_rpc(fld->lsf_control_exp, range, FLD_READ,
164 if (rc != 0 && rc != -EAGAIN)
167 LASSERT(req != NULL);
168 lsra = (struct lu_seq_range_array *)req_capsule_server_get(
169 &req->rq_pill, &RMF_GENERIC_DATA);
171 GOTO(out, rc = -EPROTO);
173 range_array_le_to_cpu(lsra, lsra);
174 for (i = 0; i < lsra->lsra_count; i++) {
177 if (lsra->lsra_lsr[i].lsr_flags != LU_SEQ_RANGE_MDT)
178 GOTO(out, rc = -EINVAL);
180 if (lsra->lsra_lsr[i].lsr_index != index)
181 GOTO(out, rc = -EINVAL);
183 rc1 = fld_insert_entry(env, fld, &lsra->lsra_lsr[i]);
188 *range = lsra->lsra_lsr[lsra->lsra_count - 1];
189 } while (rc == -EAGAIN);
194 ptlrpc_req_finished(req);
198 EXPORT_SYMBOL(fld_update_from_controller);
201 * Lookup sequece in local cache/fldb.
203 int fld_local_lookup(const struct lu_env *env, struct lu_server_fld *fld,
204 seqno_t seq, struct lu_seq_range *range)
206 struct lu_seq_range *erange;
207 struct fld_thread_info *info;
211 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
212 LASSERT(info != NULL);
213 erange = &info->fti_lrange;
215 /* Lookup it in the cache. */
216 rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
218 if (unlikely(fld_range_type(erange) != fld_range_type(range) &&
219 !fld_range_is_any(range))) {
220 CERROR("%s: FLD cache range "DRANGE" does not match"
221 "requested flag %x: rc = %d\n", fld->lsf_name,
222 PRANGE(erange), range->lsr_flags, -EIO);
230 EXPORT_SYMBOL(fld_local_lookup);
233 * Lookup MDT/OST by seq, returns a range for given seq.
235 * If that entry is not cached in fld cache, request is sent to super
236 * sequence controller node (MDT0). All other MDT[1...N] and client
237 * cache fld entries, but this cache is not persistent.
239 int fld_server_lookup(const struct lu_env *env, struct lu_server_fld *fld,
240 seqno_t seq, struct lu_seq_range *range)
246 rc = fld_local_lookup(env, fld, seq, range);
250 rc = fld_name_to_index(fld->lsf_name, &index);
254 if (index == 0 && rc == LDD_F_SV_TYPE_MDT) {
255 /* On server side, all entries should be in cache.
256 * If we can not find it in cache, just return error */
257 CERROR("%s: Cannot find sequence "LPX64": rc = %d\n",
258 fld->lsf_name, seq, -ENOENT);
261 if (fld->lsf_control_exp == NULL) {
262 CERROR("%s: lookup "LPX64", but not connects to MDT0"
263 "yet: rc = %d.\n", fld->lsf_name, seq, -EIO);
266 /* send request to mdt0 i.e. super seq. controller.
267 * This is temporary solution, long term solution is fld
268 * replication on all mdt servers.
270 range->lsr_start = seq;
271 rc = fld_client_rpc(fld->lsf_control_exp,
272 range, FLD_QUERY, NULL);
274 fld_cache_insert(fld->lsf_cache, range);
278 EXPORT_SYMBOL(fld_server_lookup);
281 * All MDT server handle fld lookup operation. But only MDT0 has fld index.
282 * if entry is not found in cache we need to forward lookup request to MDT0
284 static int fld_handle_lookup(struct tgt_session_info *tsi)
286 struct obd_export *exp = tsi->tsi_exp;
287 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
288 struct lu_server_fld *fld;
289 struct lu_seq_range *in;
290 struct lu_seq_range *out;
295 in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
297 RETURN(err_serious(-EPROTO));
299 rc = req_capsule_server_pack(tsi->tsi_pill);
300 if (unlikely(rc != 0))
301 RETURN(err_serious(rc));
303 out = req_capsule_server_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
305 RETURN(err_serious(-EPROTO));
308 fld = lu_site2seq(site)->ss_server_fld;
310 rc = fld_server_lookup(tsi->tsi_env, fld, in->lsr_start, out);
312 CDEBUG(D_INFO, "%s: FLD req handle: error %d (range: "DRANGE")\n",
313 fld->lsf_name, rc, PRANGE(out));
318 static int fld_handle_read(struct tgt_session_info *tsi)
320 struct obd_export *exp = tsi->tsi_exp;
321 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
322 struct lu_seq_range *in;
328 req_capsule_set(tsi->tsi_pill, &RQF_FLD_READ);
330 in = req_capsule_client_get(tsi->tsi_pill, &RMF_FLD_MDFLD);
332 RETURN(err_serious(-EPROTO));
334 req_capsule_set_size(tsi->tsi_pill, &RMF_GENERIC_DATA, RCL_SERVER,
337 rc = req_capsule_server_pack(tsi->tsi_pill);
338 if (unlikely(rc != 0))
339 RETURN(err_serious(rc));
341 data = req_capsule_server_get(tsi->tsi_pill, &RMF_GENERIC_DATA);
343 rc = fld_server_read(tsi->tsi_env, lu_site2seq(site)->ss_server_fld,
344 in, data, PAGE_CACHE_SIZE);
348 static int fld_handle_query(struct tgt_session_info *tsi)
354 req_capsule_set(tsi->tsi_pill, &RQF_FLD_QUERY);
356 rc = fld_handle_lookup(tsi);
362 * Returns true, if fid is local to this server node.
364 * WARNING: this function is *not* guaranteed to return false if fid is
365 * remote: it makes an educated conservative guess only.
367 * fid_is_local() is supposed to be used in assertion checks only.
369 int fid_is_local(const struct lu_env *env,
370 struct lu_site *site, const struct lu_fid *fid)
373 struct seq_server_site *ss_site;
374 struct lu_seq_range *range;
375 struct fld_thread_info *info;
378 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
379 range = &info->fti_lrange;
381 result = 1; /* conservatively assume fid is local */
382 ss_site = lu_site2seq(site);
383 if (ss_site->ss_client_fld != NULL) {
386 rc = fld_cache_lookup(ss_site->ss_client_fld->lcf_cache,
387 fid_seq(fid), range);
389 result = (range->lsr_index == ss_site->ss_node_id);
393 EXPORT_SYMBOL(fid_is_local);
395 static void fld_server_proc_fini(struct lu_server_fld *fld);
398 static int fld_server_proc_init(struct lu_server_fld *fld)
403 fld->lsf_proc_dir = lprocfs_seq_register(fld->lsf_name,
405 fld_server_proc_list, fld);
406 if (IS_ERR(fld->lsf_proc_dir)) {
407 rc = PTR_ERR(fld->lsf_proc_dir);
411 rc = lprocfs_seq_create(fld->lsf_proc_dir, "fldb", 0444,
412 &fld_proc_seq_fops, fld);
414 lprocfs_remove(&fld->lsf_proc_dir);
415 fld->lsf_proc_dir = NULL;
421 static void fld_server_proc_fini(struct lu_server_fld *fld)
424 if (fld->lsf_proc_dir != NULL) {
425 if (!IS_ERR(fld->lsf_proc_dir))
426 lprocfs_remove(&fld->lsf_proc_dir);
427 fld->lsf_proc_dir = NULL;
432 static int fld_server_proc_init(struct lu_server_fld *fld)
437 static void fld_server_proc_fini(struct lu_server_fld *fld)
443 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
444 struct dt_device *dt, const char *prefix, int type)
446 int cache_size, cache_threshold;
451 snprintf(fld->lsf_name, sizeof(fld->lsf_name), "srv-%s", prefix);
453 cache_size = FLD_SERVER_CACHE_SIZE / sizeof(struct fld_cache_entry);
455 cache_threshold = cache_size * FLD_SERVER_CACHE_THRESHOLD / 100;
457 mutex_init(&fld->lsf_lock);
458 fld->lsf_cache = fld_cache_init(fld->lsf_name, cache_size,
460 if (IS_ERR(fld->lsf_cache)) {
461 rc = PTR_ERR(fld->lsf_cache);
462 fld->lsf_cache = NULL;
466 rc = fld_index_init(env, fld, dt, type);
470 rc = fld_server_proc_init(fld);
474 fld->lsf_control_exp = NULL;
478 fld_index_fini(env, fld);
480 fld_cache_fini(fld->lsf_cache);
483 EXPORT_SYMBOL(fld_server_init);
485 void fld_server_fini(const struct lu_env *env, struct lu_server_fld *fld)
489 fld_server_proc_fini(fld);
490 fld_index_fini(env, fld);
492 if (fld->lsf_cache != NULL) {
493 if (!IS_ERR(fld->lsf_cache))
494 fld_cache_fini(fld->lsf_cache);
495 fld->lsf_cache = NULL;
500 EXPORT_SYMBOL(fld_server_fini);
502 struct tgt_handler fld_handlers[] = {
503 TGT_FLD_HDL_VAR(0, FLD_QUERY, fld_handle_query),
504 TGT_FLD_HDL_VAR(0, FLD_READ, fld_handle_read),
506 EXPORT_SYMBOL(fld_handlers);