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, 2012, Whamcloud, Inc.
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
48 # include <libcfs/libcfs.h>
49 # include <linux/module.h>
50 # include <linux/jbd.h>
51 # include <asm/div64.h>
52 #else /* __KERNEL__ */
53 # include <liblustre.h>
54 # include <libcfs/list.h>
58 #include <obd_class.h>
59 #include <lustre_ver.h>
60 #include <obd_support.h>
61 #include <lprocfs_status.h>
63 #include <md_object.h>
64 #include <lustre_fid.h>
65 #include <lustre_req_layout.h>
66 #include "fld_internal.h"
67 #include <lustre_fid.h>
71 /* context key constructor/destructor: fld_key_init, fld_key_fini */
72 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
74 /* context key: fld_thread_key */
75 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD|LCT_DT_THREAD);
77 cfs_proc_dir_entry_t *fld_type_proc_dir = NULL;
79 static int __init fld_mod_init(void)
81 fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
84 if (IS_ERR(fld_type_proc_dir))
85 return PTR_ERR(fld_type_proc_dir);
87 LU_CONTEXT_KEY_INIT(&fld_thread_key);
88 lu_context_key_register(&fld_thread_key);
92 static void __exit fld_mod_exit(void)
94 lu_context_key_degister(&fld_thread_key);
95 if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
96 lprocfs_remove(&fld_type_proc_dir);
97 fld_type_proc_dir = NULL;
101 int fld_declare_server_create(const struct lu_env *env,
102 struct lu_server_fld *fld,
103 struct lu_seq_range *range,
108 rc = fld_declare_index_create(env, fld, range, th);
111 EXPORT_SYMBOL(fld_declare_server_create);
114 * Insert FLD index entry and update FLD cache.
116 * This function is called from the sequence allocator when a super-sequence
117 * is granted to a server.
119 int fld_server_create(const struct lu_env *env, struct lu_server_fld *fld,
120 struct lu_seq_range *range, struct thandle *th)
124 mutex_lock(&fld->lsf_lock);
125 rc = fld_index_create(env, fld, range, th);
126 mutex_unlock(&fld->lsf_lock);
130 EXPORT_SYMBOL(fld_server_create);
133 * Lookup mds by seq, returns a range for given seq.
135 * If that entry is not cached in fld cache, request is sent to super
136 * sequence controller node (MDT0). All other MDT[1...N] and client
137 * cache fld entries, but this cache is not persistent.
140 int fld_server_lookup(const struct lu_env *env, struct lu_server_fld *fld,
141 seqno_t seq, struct lu_seq_range *range)
143 struct lu_seq_range *erange;
144 struct fld_thread_info *info;
148 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
149 erange = &info->fti_lrange;
151 /* Lookup it in the cache. */
152 rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
154 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
155 CERROR("FLD cache found a range "DRANGE" doesn't "
156 "match the requested flag %x\n",
157 PRANGE(erange), range->lsr_flags);
165 /* On server side, all entries should be in cache.
166 * If we can not find it in cache, just return error */
167 CERROR("%s: Can not found the seq "LPX64"\n",
171 LASSERT(fld->lsf_control_exp);
172 /* send request to mdt0 i.e. super seq. controller.
173 * This is temporary solution, long term solution is fld
174 * replication on all mdt servers.
176 rc = fld_client_rpc(fld->lsf_control_exp,
179 fld_cache_insert(fld->lsf_cache, range);
183 EXPORT_SYMBOL(fld_server_lookup);
186 * All MDT server handle fld lookup operation. But only MDT0 has fld index.
187 * if entry is not found in cache we need to forward lookup request to MDT0
190 static int fld_server_handle(struct lu_server_fld *fld,
191 const struct lu_env *env,
192 __u32 opc, struct lu_seq_range *range,
193 struct fld_thread_info *info)
200 rc = fld_server_lookup(env, fld, range->lsr_start, range);
207 CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, range: "
208 DRANGE"\n", fld->lsf_name, rc, opc, PRANGE(range));
214 static int fld_req_handle(struct ptlrpc_request *req,
215 struct fld_thread_info *info)
217 struct obd_export *exp = req->rq_export;
218 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
219 struct lu_seq_range *in;
220 struct lu_seq_range *out;
225 rc = req_capsule_server_pack(info->fti_pill);
227 RETURN(err_serious(rc));
229 opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
231 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
233 RETURN(err_serious(-EPROTO));
234 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
236 RETURN(err_serious(-EPROTO));
239 /* For old 2.0 client, the 'lsr_flags' is uninitialized.
240 * Set it as 'LU_SEQ_RANGE_MDT' by default.
241 * Old 2.0 liblustre client cannot talk with new 2.1 server. */
242 if (!(exp->exp_connect_flags & OBD_CONNECT_64BITHASH) &&
244 out->lsr_flags = LU_SEQ_RANGE_MDT;
246 rc = fld_server_handle(lu_site2md(site)->ms_server_fld,
247 req->rq_svc_thread->t_env,
250 rc = err_serious(-EPROTO);
255 static void fld_thread_info_init(struct ptlrpc_request *req,
256 struct fld_thread_info *info)
258 info->fti_pill = &req->rq_pill;
259 /* Init request capsule. */
260 req_capsule_init(info->fti_pill, req, RCL_SERVER);
261 req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
264 static void fld_thread_info_fini(struct fld_thread_info *info)
266 req_capsule_fini(info->fti_pill);
269 static int fld_handle(struct ptlrpc_request *req)
271 struct fld_thread_info *info;
272 const struct lu_env *env;
275 env = req->rq_svc_thread->t_env;
276 LASSERT(env != NULL);
278 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
279 LASSERT(info != NULL);
281 fld_thread_info_init(req, info);
282 rc = fld_req_handle(req, info);
283 fld_thread_info_fini(info);
289 * Entry point for handling FLD RPCs called from MDT.
291 int fld_query(struct com_thread_info *info)
293 return fld_handle(info->cti_pill->rc_req);
295 EXPORT_SYMBOL(fld_query);
298 * Returns true, if fid is local to this server node.
300 * WARNING: this function is *not* guaranteed to return false if fid is
301 * remote: it makes an educated conservative guess only.
303 * fid_is_local() is supposed to be used in assertion checks only.
305 int fid_is_local(const struct lu_env *env,
306 struct lu_site *site, const struct lu_fid *fid)
309 struct md_site *msite;
310 struct lu_seq_range *range;
311 struct fld_thread_info *info;
314 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
315 range = &info->fti_lrange;
317 result = 1; /* conservatively assume fid is local */
318 msite = lu_site2md(site);
319 if (msite->ms_client_fld != NULL) {
322 rc = fld_cache_lookup(msite->ms_client_fld->lcf_cache,
323 fid_seq(fid), range);
325 result = (range->lsr_index == msite->ms_node_id);
329 EXPORT_SYMBOL(fid_is_local);
331 static void fld_server_proc_fini(struct lu_server_fld *fld);
334 static int fld_server_proc_init(struct lu_server_fld *fld)
339 fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
341 fld_server_proc_list, fld);
342 if (IS_ERR(fld->lsf_proc_dir)) {
343 rc = PTR_ERR(fld->lsf_proc_dir);
347 rc = lprocfs_seq_create(fld->lsf_proc_dir, "fldb", 0444,
348 &fld_proc_seq_fops, fld);
350 lprocfs_remove(&fld->lsf_proc_dir);
351 fld->lsf_proc_dir = NULL;
357 static void fld_server_proc_fini(struct lu_server_fld *fld)
360 if (fld->lsf_proc_dir != NULL) {
361 if (!IS_ERR(fld->lsf_proc_dir))
362 lprocfs_remove(&fld->lsf_proc_dir);
363 fld->lsf_proc_dir = NULL;
368 static int fld_server_proc_init(struct lu_server_fld *fld)
373 static void fld_server_proc_fini(struct lu_server_fld *fld)
379 int fld_server_init(const struct lu_env *env, struct lu_server_fld *fld,
380 struct dt_device *dt, const char *prefix, int mds_node_id)
382 int cache_size, cache_threshold;
383 struct lu_seq_range range;
387 snprintf(fld->lsf_name, sizeof(fld->lsf_name),
390 cache_size = FLD_SERVER_CACHE_SIZE /
391 sizeof(struct fld_cache_entry);
393 cache_threshold = cache_size *
394 FLD_SERVER_CACHE_THRESHOLD / 100;
396 mutex_init(&fld->lsf_lock);
397 fld->lsf_cache = fld_cache_init(fld->lsf_name,
398 cache_size, cache_threshold);
399 if (IS_ERR(fld->lsf_cache)) {
400 rc = PTR_ERR(fld->lsf_cache);
401 fld->lsf_cache = NULL;
406 rc = fld_index_init(env, fld, dt);
412 rc = fld_server_proc_init(fld);
416 fld->lsf_control_exp = NULL;
418 /* Insert reserved sequence number of ".lustre" into fld cache. */
419 range.lsr_start = FID_SEQ_DOT_LUSTRE;
420 range.lsr_end = FID_SEQ_DOT_LUSTRE + 1;
422 range.lsr_flags = LU_SEQ_RANGE_MDT;
423 fld_cache_insert(fld->lsf_cache, &range);
428 fld_server_fini(env, fld);
431 EXPORT_SYMBOL(fld_server_init);
433 void fld_server_fini(const struct lu_env *env, struct lu_server_fld *fld)
437 fld_server_proc_fini(fld);
438 fld_index_fini(env, fld);
440 if (fld->lsf_cache != NULL) {
441 if (!IS_ERR(fld->lsf_cache))
442 fld_cache_fini(fld->lsf_cache);
443 fld->lsf_cache = NULL;
448 EXPORT_SYMBOL(fld_server_fini);
450 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
451 MODULE_DESCRIPTION("Lustre FLD");
452 MODULE_LICENSE("GPL");
454 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);