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 struct lu_local_obj_desc llod_fld_index = {
80 .llod_name = fld_index_name,
81 .llod_oid = FLD_INDEX_OID,
83 .llod_feat = &fld_index_features,
86 static int __init fld_mod_init(void)
88 fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
91 if (IS_ERR(fld_type_proc_dir))
92 return PTR_ERR(fld_type_proc_dir);
94 llo_local_obj_register(&llod_fld_index);
96 LU_CONTEXT_KEY_INIT(&fld_thread_key);
97 lu_context_key_register(&fld_thread_key);
101 static void __exit fld_mod_exit(void)
103 llo_local_obj_unregister(&llod_fld_index);
104 lu_context_key_degister(&fld_thread_key);
105 if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
106 lprocfs_remove(&fld_type_proc_dir);
107 fld_type_proc_dir = NULL;
111 int fld_declare_server_create(struct lu_server_fld *fld,
112 const struct lu_env *env,
115 struct dt_object *dt_obj = fld->lsf_obj;
120 /* for ldiskfs OSD it's enough to declare operation with any ops
121 * with DMU we'll probably need to specify exact key/value */
122 rc = dt_obj->do_index_ops->dio_declare_delete(env, dt_obj, NULL, th);
125 rc = dt_obj->do_index_ops->dio_declare_delete(env, dt_obj, NULL, th);
128 rc = dt_obj->do_index_ops->dio_declare_insert(env, dt_obj,
133 EXPORT_SYMBOL(fld_declare_server_create);
136 * Insert FLD index entry and update FLD cache.
138 * First it try to merge given range with existing range then update
139 * FLD index and FLD cache accordingly. FLD index consistency is maintained
141 * This function is called from the sequence allocator when a super-sequence
142 * is granted to a server.
145 int fld_server_create(struct lu_server_fld *fld,
146 const struct lu_env *env,
147 struct lu_seq_range *add_range,
150 struct lu_seq_range *erange;
151 struct lu_seq_range *new;
152 struct fld_thread_info *info;
158 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
159 cfs_mutex_lock(&fld->lsf_lock);
161 erange = &info->fti_lrange;
162 new = &info->fti_irange;
165 /* STEP 1: try to merge with previous range */
166 rc = fld_index_lookup(fld, env, new->lsr_start, erange);
168 /* in case of range overlap, the location must be same */
169 if (range_compare_loc(new, erange) != 0) {
170 CERROR("the start of given range "DRANGE" conflict to"
171 "an existing range "DRANGE"\n",
172 PRANGE(new), PRANGE(erange));
173 GOTO(out, rc = -EIO);
176 if (new->lsr_end < erange->lsr_end)
179 } else if (rc == -ENOENT) {
180 /* check for merge case: optimizes for single mds lustre.
181 * As entry does not exist, returned entry must be left side
182 * entry compared to start of new range (ref dio_lookup()).
183 * So try to merge from left.
185 if (new->lsr_start == erange->lsr_end &&
186 range_compare_loc(new, erange) == 0)
189 /* no overlap allowed in fld, so failure in lookup is error */
194 /* new range will be merged with the existing one.
195 * delete this range at first. */
196 rc = fld_index_delete(fld, env, erange, th);
200 new->lsr_start = min(erange->lsr_start, new->lsr_start);
201 new->lsr_end = max(erange->lsr_end, new->lsr_end);
205 /* STEP 2: try to merge with next range */
206 rc = fld_index_lookup(fld, env, new->lsr_end, erange);
208 /* found a matched range, meaning we're either
209 * overlapping or ajacent, must merge with it. */
211 } else if (rc == -ENOENT) {
212 /* this range is left of new range end point */
213 LASSERT(erange->lsr_end <= new->lsr_end);
215 * the found left range must be either:
216 * 1. withing new range.
217 * 2. left of new range (no overlapping).
218 * because if they're partly overlapping, the STEP 1 must have
219 * been removed this range.
221 LASSERTF(erange->lsr_start > new->lsr_start ||
222 erange->lsr_end < new->lsr_start ||
223 (erange->lsr_end == new->lsr_start &&
224 range_compare_loc(new, erange) != 0),
225 "left "DRANGE", new "DRANGE"\n",
226 PRANGE(erange), PRANGE(new));
228 /* if it's within the new range, merge it */
229 if (erange->lsr_start > new->lsr_start)
236 if (range_compare_loc(new, erange) != 0) {
237 CERROR("the end of given range "DRANGE" overlaps "
238 "with an existing range "DRANGE"\n",
239 PRANGE(new), PRANGE(erange));
240 GOTO(out, rc = -EIO);
243 /* merge with next range */
244 rc = fld_index_delete(fld, env, erange, th);
248 new->lsr_start = min(erange->lsr_start, new->lsr_start);
249 new->lsr_end = max(erange->lsr_end, new->lsr_end);
252 /* now update fld entry. */
253 rc = fld_index_create(fld, env, new, th);
255 LASSERT(rc != -EEXIST);
258 fld_cache_insert(fld->lsf_cache, new);
260 cfs_mutex_unlock(&fld->lsf_lock);
262 CDEBUG((rc != 0 ? D_ERROR : D_INFO),
263 "%s: FLD create: given range : "DRANGE
264 "after merge "DRANGE" rc = %d \n", fld->lsf_name,
265 PRANGE(add_range), PRANGE(new), rc);
269 EXPORT_SYMBOL(fld_server_create);
272 * Lookup mds by seq, returns a range for given seq.
274 * If that entry is not cached in fld cache, request is sent to super
275 * sequence controller node (MDT0). All other MDT[1...N] and client
276 * cache fld entries, but this cache is not persistent.
279 int fld_server_lookup(struct lu_server_fld *fld,
280 const struct lu_env *env,
281 seqno_t seq, struct lu_seq_range *range)
283 struct lu_seq_range *erange;
284 struct fld_thread_info *info;
288 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
289 erange = &info->fti_lrange;
291 /* Lookup it in the cache. */
292 rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
294 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
295 CERROR("FLD cache found a range "DRANGE" doesn't "
296 "match the requested flag %x\n",
297 PRANGE(erange), range->lsr_flags);
305 rc = fld_index_lookup(fld, env, seq, erange);
307 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
308 CERROR("FLD found a range "DRANGE" doesn't "
309 "match the requested flag %x\n",
310 PRANGE(erange), range->lsr_flags);
316 LASSERT(fld->lsf_control_exp);
317 /* send request to mdt0 i.e. super seq. controller.
318 * This is temporary solution, long term solution is fld
319 * replication on all mdt servers.
321 rc = fld_client_rpc(fld->lsf_control_exp,
326 fld_cache_insert(fld->lsf_cache, range);
330 EXPORT_SYMBOL(fld_server_lookup);
333 * All MDT server handle fld lookup operation. But only MDT0 has fld index.
334 * if entry is not found in cache we need to forward lookup request to MDT0
337 static int fld_server_handle(struct lu_server_fld *fld,
338 const struct lu_env *env,
339 __u32 opc, struct lu_seq_range *range,
340 struct fld_thread_info *info)
347 rc = fld_server_lookup(fld, env,
348 range->lsr_start, range);
355 CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, range: "
356 DRANGE"\n", fld->lsf_name, rc, opc, PRANGE(range));
362 static int fld_req_handle(struct ptlrpc_request *req,
363 struct fld_thread_info *info)
365 struct obd_export *exp = req->rq_export;
366 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
367 struct lu_seq_range *in;
368 struct lu_seq_range *out;
373 rc = req_capsule_server_pack(info->fti_pill);
375 RETURN(err_serious(rc));
377 opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
379 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
381 RETURN(err_serious(-EPROTO));
382 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
384 RETURN(err_serious(-EPROTO));
387 /* For old 2.0 client, the 'lsr_flags' is uninitialized.
388 * Set it as 'LU_SEQ_RANGE_MDT' by default.
389 * Old 2.0 liblustre client cannot talk with new 2.1 server. */
390 if (!(exp->exp_connect_flags & OBD_CONNECT_64BITHASH) &&
392 out->lsr_flags = LU_SEQ_RANGE_MDT;
394 rc = fld_server_handle(lu_site2md(site)->ms_server_fld,
395 req->rq_svc_thread->t_env,
398 rc = err_serious(-EPROTO);
403 static void fld_thread_info_init(struct ptlrpc_request *req,
404 struct fld_thread_info *info)
406 info->fti_pill = &req->rq_pill;
407 /* Init request capsule. */
408 req_capsule_init(info->fti_pill, req, RCL_SERVER);
409 req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
412 static void fld_thread_info_fini(struct fld_thread_info *info)
414 req_capsule_fini(info->fti_pill);
417 static int fld_handle(struct ptlrpc_request *req)
419 struct fld_thread_info *info;
420 const struct lu_env *env;
423 env = req->rq_svc_thread->t_env;
424 LASSERT(env != NULL);
426 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
427 LASSERT(info != NULL);
429 fld_thread_info_init(req, info);
430 rc = fld_req_handle(req, info);
431 fld_thread_info_fini(info);
437 * Entry point for handling FLD RPCs called from MDT.
439 int fld_query(struct com_thread_info *info)
441 return fld_handle(info->cti_pill->rc_req);
443 EXPORT_SYMBOL(fld_query);
446 * Returns true, if fid is local to this server node.
448 * WARNING: this function is *not* guaranteed to return false if fid is
449 * remote: it makes an educated conservative guess only.
451 * fid_is_local() is supposed to be used in assertion checks only.
453 int fid_is_local(const struct lu_env *env,
454 struct lu_site *site, const struct lu_fid *fid)
457 struct md_site *msite;
458 struct lu_seq_range *range;
459 struct fld_thread_info *info;
462 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
463 range = &info->fti_lrange;
465 result = 1; /* conservatively assume fid is local */
466 msite = lu_site2md(site);
467 if (msite->ms_client_fld != NULL) {
470 rc = fld_cache_lookup(msite->ms_client_fld->lcf_cache,
471 fid_seq(fid), range);
473 result = (range->lsr_index == msite->ms_node_id);
477 EXPORT_SYMBOL(fid_is_local);
479 static void fld_server_proc_fini(struct lu_server_fld *fld);
482 static int fld_server_proc_init(struct lu_server_fld *fld)
487 fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
489 fld_server_proc_list, fld);
490 if (IS_ERR(fld->lsf_proc_dir)) {
491 rc = PTR_ERR(fld->lsf_proc_dir);
495 rc = lprocfs_seq_create(fld->lsf_proc_dir, "fldb", 0444,
496 &fld_proc_seq_fops, fld);
498 lprocfs_remove(&fld->lsf_proc_dir);
499 fld->lsf_proc_dir = NULL;
505 static void fld_server_proc_fini(struct lu_server_fld *fld)
508 if (fld->lsf_proc_dir != NULL) {
509 if (!IS_ERR(fld->lsf_proc_dir))
510 lprocfs_remove(&fld->lsf_proc_dir);
511 fld->lsf_proc_dir = NULL;
516 static int fld_server_proc_init(struct lu_server_fld *fld)
521 static void fld_server_proc_fini(struct lu_server_fld *fld)
527 int fld_server_init(struct lu_server_fld *fld, struct dt_device *dt,
528 const char *prefix, const struct lu_env *env,
531 int cache_size, cache_threshold;
532 struct lu_seq_range range;
536 snprintf(fld->lsf_name, sizeof(fld->lsf_name),
539 cache_size = FLD_SERVER_CACHE_SIZE /
540 sizeof(struct fld_cache_entry);
542 cache_threshold = cache_size *
543 FLD_SERVER_CACHE_THRESHOLD / 100;
545 cfs_mutex_init(&fld->lsf_lock);
546 fld->lsf_cache = fld_cache_init(fld->lsf_name,
547 cache_size, cache_threshold);
548 if (IS_ERR(fld->lsf_cache)) {
549 rc = PTR_ERR(fld->lsf_cache);
550 fld->lsf_cache = NULL;
555 rc = fld_index_init(fld, env, dt);
561 rc = fld_server_proc_init(fld);
565 fld->lsf_control_exp = NULL;
567 /* Insert reserved sequence number of ".lustre" into fld cache. */
568 range.lsr_start = FID_SEQ_DOT_LUSTRE;
569 range.lsr_end = FID_SEQ_DOT_LUSTRE + 1;
571 range.lsr_flags = LU_SEQ_RANGE_MDT;
572 fld_cache_insert(fld->lsf_cache, &range);
577 fld_server_fini(fld, env);
580 EXPORT_SYMBOL(fld_server_init);
582 void fld_server_fini(struct lu_server_fld *fld,
583 const struct lu_env *env)
587 fld_server_proc_fini(fld);
588 fld_index_fini(fld, env);
590 if (fld->lsf_cache != NULL) {
591 if (!IS_ERR(fld->lsf_cache))
592 fld_cache_fini(fld->lsf_cache);
593 fld->lsf_cache = NULL;
598 EXPORT_SYMBOL(fld_server_fini);
600 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
601 MODULE_DESCRIPTION("Lustre FLD");
602 MODULE_LICENSE("GPL");
604 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);