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>
46 # define EXPORT_SYMTAB
48 #define DEBUG_SUBSYSTEM S_FLD
51 # include <libcfs/libcfs.h>
52 # include <linux/module.h>
53 # include <linux/jbd.h>
54 # include <asm/div64.h>
55 #else /* __KERNEL__ */
56 # include <liblustre.h>
57 # include <libcfs/list.h>
61 #include <obd_class.h>
62 #include <lustre_ver.h>
63 #include <obd_support.h>
64 #include <lprocfs_status.h>
66 #include <md_object.h>
67 #include <lustre_fid.h>
68 #include <lustre_req_layout.h>
69 #include "fld_internal.h"
70 #include <lustre_fid.h>
74 /* context key constructor/destructor: fld_key_init, fld_key_fini */
75 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
77 /* context key: fld_thread_key */
78 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD|LCT_DT_THREAD);
80 cfs_proc_dir_entry_t *fld_type_proc_dir = NULL;
82 static struct lu_local_obj_desc llod_fld_index = {
83 .llod_name = fld_index_name,
84 .llod_oid = FLD_INDEX_OID,
86 .llod_feat = &fld_index_features,
89 static int __init fld_mod_init(void)
91 fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
94 if (IS_ERR(fld_type_proc_dir))
95 return PTR_ERR(fld_type_proc_dir);
97 llo_local_obj_register(&llod_fld_index);
99 LU_CONTEXT_KEY_INIT(&fld_thread_key);
100 lu_context_key_register(&fld_thread_key);
104 static void __exit fld_mod_exit(void)
106 llo_local_obj_unregister(&llod_fld_index);
107 lu_context_key_degister(&fld_thread_key);
108 if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
109 lprocfs_remove(&fld_type_proc_dir);
110 fld_type_proc_dir = NULL;
114 int fld_declare_server_create(struct lu_server_fld *fld,
115 const struct lu_env *env,
118 struct dt_object *dt_obj = fld->lsf_obj;
123 /* for ldiskfs OSD it's enough to declare operation with any ops
124 * with DMU we'll probably need to specify exact key/value */
125 rc = dt_obj->do_index_ops->dio_declare_delete(env, dt_obj, NULL, th);
128 rc = dt_obj->do_index_ops->dio_declare_delete(env, dt_obj, NULL, th);
131 rc = dt_obj->do_index_ops->dio_declare_insert(env, dt_obj,
136 EXPORT_SYMBOL(fld_declare_server_create);
139 * Insert FLD index entry and update FLD cache.
141 * First it try to merge given range with existing range then update
142 * FLD index and FLD cache accordingly. FLD index consistency is maintained
144 * This function is called from the sequence allocator when a super-sequence
145 * is granted to a server.
148 int fld_server_create(struct lu_server_fld *fld,
149 const struct lu_env *env,
150 struct lu_seq_range *add_range,
153 struct lu_seq_range *erange;
154 struct lu_seq_range *new;
155 struct fld_thread_info *info;
161 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
162 cfs_mutex_lock(&fld->lsf_lock);
164 erange = &info->fti_lrange;
165 new = &info->fti_irange;
168 /* STEP 1: try to merge with previous range */
169 rc = fld_index_lookup(fld, env, new->lsr_start, erange);
171 /* in case of range overlap, the location must be same */
172 if (range_compare_loc(new, erange) != 0) {
173 CERROR("the start of given range "DRANGE" conflict to"
174 "an existing range "DRANGE"\n",
175 PRANGE(new), PRANGE(erange));
176 GOTO(out, rc = -EIO);
179 if (new->lsr_end < erange->lsr_end)
182 } else if (rc == -ENOENT) {
183 /* check for merge case: optimizes for single mds lustre.
184 * As entry does not exist, returned entry must be left side
185 * entry compared to start of new range (ref dio_lookup()).
186 * So try to merge from left.
188 if (new->lsr_start == erange->lsr_end &&
189 range_compare_loc(new, erange) == 0)
192 /* no overlap allowed in fld, so failure in lookup is error */
197 /* new range will be merged with the existing one.
198 * delete this range at first. */
199 rc = fld_index_delete(fld, env, erange, th);
203 new->lsr_start = min(erange->lsr_start, new->lsr_start);
204 new->lsr_end = max(erange->lsr_end, new->lsr_end);
208 /* STEP 2: try to merge with next range */
209 rc = fld_index_lookup(fld, env, new->lsr_end, erange);
211 /* found a matched range, meaning we're either
212 * overlapping or ajacent, must merge with it. */
214 } else if (rc == -ENOENT) {
215 /* this range is left of new range end point */
216 LASSERT(erange->lsr_end <= new->lsr_end);
218 * the found left range must be either:
219 * 1. withing new range.
220 * 2. left of new range (no overlapping).
221 * because if they're partly overlapping, the STEP 1 must have
222 * been removed this range.
224 LASSERTF(erange->lsr_start > new->lsr_start ||
225 erange->lsr_end < new->lsr_start ||
226 (erange->lsr_end == new->lsr_start &&
227 range_compare_loc(new, erange) != 0),
228 "left "DRANGE", new "DRANGE"\n",
229 PRANGE(erange), PRANGE(new));
231 /* if it's within the new range, merge it */
232 if (erange->lsr_start > new->lsr_start)
239 if (range_compare_loc(new, erange) != 0) {
240 CERROR("the end of given range "DRANGE" overlaps "
241 "with an existing range "DRANGE"\n",
242 PRANGE(new), PRANGE(erange));
243 GOTO(out, rc = -EIO);
246 /* merge with next range */
247 rc = fld_index_delete(fld, env, erange, th);
251 new->lsr_start = min(erange->lsr_start, new->lsr_start);
252 new->lsr_end = max(erange->lsr_end, new->lsr_end);
255 /* now update fld entry. */
256 rc = fld_index_create(fld, env, new, th);
258 LASSERT(rc != -EEXIST);
261 fld_cache_insert(fld->lsf_cache, new);
263 cfs_mutex_unlock(&fld->lsf_lock);
265 CDEBUG((rc != 0 ? D_ERROR : D_INFO),
266 "%s: FLD create: given range : "DRANGE
267 "after merge "DRANGE" rc = %d \n", fld->lsf_name,
268 PRANGE(add_range), PRANGE(new), rc);
272 EXPORT_SYMBOL(fld_server_create);
275 * Lookup mds by seq, returns a range for given seq.
277 * If that entry is not cached in fld cache, request is sent to super
278 * sequence controller node (MDT0). All other MDT[1...N] and client
279 * cache fld entries, but this cache is not persistent.
282 int fld_server_lookup(struct lu_server_fld *fld,
283 const struct lu_env *env,
284 seqno_t seq, struct lu_seq_range *range)
286 struct lu_seq_range *erange;
287 struct fld_thread_info *info;
291 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
292 erange = &info->fti_lrange;
294 /* Lookup it in the cache. */
295 rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
297 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
298 CERROR("FLD cache found a range "DRANGE" doesn't "
299 "match the requested flag %x\n",
300 PRANGE(erange), range->lsr_flags);
308 rc = fld_index_lookup(fld, env, seq, erange);
310 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
311 CERROR("FLD found a range "DRANGE" doesn't "
312 "match the requested flag %x\n",
313 PRANGE(erange), range->lsr_flags);
319 LASSERT(fld->lsf_control_exp);
320 /* send request to mdt0 i.e. super seq. controller.
321 * This is temporary solution, long term solution is fld
322 * replication on all mdt servers.
324 rc = fld_client_rpc(fld->lsf_control_exp,
329 fld_cache_insert(fld->lsf_cache, range);
333 EXPORT_SYMBOL(fld_server_lookup);
336 * All MDT server handle fld lookup operation. But only MDT0 has fld index.
337 * if entry is not found in cache we need to forward lookup request to MDT0
340 static int fld_server_handle(struct lu_server_fld *fld,
341 const struct lu_env *env,
342 __u32 opc, struct lu_seq_range *range,
343 struct fld_thread_info *info)
350 rc = fld_server_lookup(fld, env,
351 range->lsr_start, range);
358 CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, range: "
359 DRANGE"\n", fld->lsf_name, rc, opc, PRANGE(range));
365 static int fld_req_handle(struct ptlrpc_request *req,
366 struct fld_thread_info *info)
368 struct obd_export *exp = req->rq_export;
369 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
370 struct lu_seq_range *in;
371 struct lu_seq_range *out;
376 rc = req_capsule_server_pack(info->fti_pill);
378 RETURN(err_serious(rc));
380 opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
382 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
384 RETURN(err_serious(-EPROTO));
385 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
387 RETURN(err_serious(-EPROTO));
390 /* For old 2.0 client, the 'lsr_flags' is uninitialized.
391 * Set it as 'LU_SEQ_RANGE_MDT' by default.
392 * Old 2.0 liblustre client cannot talk with new 2.1 server. */
393 if (!(exp->exp_connect_flags & OBD_CONNECT_64BITHASH) &&
395 out->lsr_flags = LU_SEQ_RANGE_MDT;
397 rc = fld_server_handle(lu_site2md(site)->ms_server_fld,
398 req->rq_svc_thread->t_env,
401 rc = err_serious(-EPROTO);
406 static void fld_thread_info_init(struct ptlrpc_request *req,
407 struct fld_thread_info *info)
409 info->fti_pill = &req->rq_pill;
410 /* Init request capsule. */
411 req_capsule_init(info->fti_pill, req, RCL_SERVER);
412 req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
415 static void fld_thread_info_fini(struct fld_thread_info *info)
417 req_capsule_fini(info->fti_pill);
420 static int fld_handle(struct ptlrpc_request *req)
422 struct fld_thread_info *info;
423 const struct lu_env *env;
426 env = req->rq_svc_thread->t_env;
427 LASSERT(env != NULL);
429 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
430 LASSERT(info != NULL);
432 fld_thread_info_init(req, info);
433 rc = fld_req_handle(req, info);
434 fld_thread_info_fini(info);
440 * Entry point for handling FLD RPCs called from MDT.
442 int fld_query(struct com_thread_info *info)
444 return fld_handle(info->cti_pill->rc_req);
446 EXPORT_SYMBOL(fld_query);
449 * Returns true, if fid is local to this server node.
451 * WARNING: this function is *not* guaranteed to return false if fid is
452 * remote: it makes an educated conservative guess only.
454 * fid_is_local() is supposed to be used in assertion checks only.
456 int fid_is_local(const struct lu_env *env,
457 struct lu_site *site, const struct lu_fid *fid)
460 struct md_site *msite;
461 struct lu_seq_range *range;
462 struct fld_thread_info *info;
465 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
466 range = &info->fti_lrange;
468 result = 1; /* conservatively assume fid is local */
469 msite = lu_site2md(site);
470 if (msite->ms_client_fld != NULL) {
473 rc = fld_cache_lookup(msite->ms_client_fld->lcf_cache,
474 fid_seq(fid), range);
476 result = (range->lsr_index == msite->ms_node_id);
480 EXPORT_SYMBOL(fid_is_local);
482 static void fld_server_proc_fini(struct lu_server_fld *fld);
485 static int fld_server_proc_init(struct lu_server_fld *fld)
490 fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
492 fld_server_proc_list, fld);
493 if (IS_ERR(fld->lsf_proc_dir)) {
494 rc = PTR_ERR(fld->lsf_proc_dir);
501 static void fld_server_proc_fini(struct lu_server_fld *fld)
504 if (fld->lsf_proc_dir != NULL) {
505 if (!IS_ERR(fld->lsf_proc_dir))
506 lprocfs_remove(&fld->lsf_proc_dir);
507 fld->lsf_proc_dir = NULL;
512 static int fld_server_proc_init(struct lu_server_fld *fld)
517 static void fld_server_proc_fini(struct lu_server_fld *fld)
523 int fld_server_init(struct lu_server_fld *fld, struct dt_device *dt,
524 const char *prefix, const struct lu_env *env,
527 int cache_size, cache_threshold;
528 struct lu_seq_range range;
532 snprintf(fld->lsf_name, sizeof(fld->lsf_name),
535 cache_size = FLD_SERVER_CACHE_SIZE /
536 sizeof(struct fld_cache_entry);
538 cache_threshold = cache_size *
539 FLD_SERVER_CACHE_THRESHOLD / 100;
541 cfs_mutex_init(&fld->lsf_lock);
542 fld->lsf_cache = fld_cache_init(fld->lsf_name,
543 cache_size, cache_threshold);
544 if (IS_ERR(fld->lsf_cache)) {
545 rc = PTR_ERR(fld->lsf_cache);
546 fld->lsf_cache = NULL;
551 rc = fld_index_init(fld, env, dt);
557 rc = fld_server_proc_init(fld);
561 fld->lsf_control_exp = NULL;
563 /* Insert reserved sequence number of ".lustre" into fld cache. */
564 range.lsr_start = FID_SEQ_DOT_LUSTRE;
565 range.lsr_end = FID_SEQ_DOT_LUSTRE + 1;
567 range.lsr_flags = LU_SEQ_RANGE_MDT;
568 fld_cache_insert(fld->lsf_cache, &range);
573 fld_server_fini(fld, env);
576 EXPORT_SYMBOL(fld_server_init);
578 void fld_server_fini(struct lu_server_fld *fld,
579 const struct lu_env *env)
583 fld_server_proc_fini(fld);
584 fld_index_fini(fld, env);
586 if (fld->lsf_cache != NULL) {
587 if (!IS_ERR(fld->lsf_cache))
588 fld_cache_fini(fld->lsf_cache);
589 fld->lsf_cache = NULL;
594 EXPORT_SYMBOL(fld_server_fini);
596 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
597 MODULE_DESCRIPTION("Lustre FLD");
598 MODULE_LICENSE("GPL");
600 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);