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(struct lu_server_fld *fld,
102 const struct lu_env *env,
105 struct dt_object *dt_obj = fld->lsf_obj;
110 if (fld->lsf_no_range_lookup) {
111 /* Stub for underlying FS which can't lookup ranges */
115 /* for ldiskfs OSD it's enough to declare operation with any ops
116 * with DMU we'll probably need to specify exact key/value */
117 rc = dt_obj->do_index_ops->dio_declare_delete(env, dt_obj, NULL, th);
120 rc = dt_obj->do_index_ops->dio_declare_delete(env, dt_obj, NULL, th);
123 rc = dt_obj->do_index_ops->dio_declare_insert(env, dt_obj,
128 EXPORT_SYMBOL(fld_declare_server_create);
131 * Insert FLD index entry and update FLD cache.
133 * First it try to merge given range with existing range then update
134 * FLD index and FLD cache accordingly. FLD index consistency is maintained
136 * This function is called from the sequence allocator when a super-sequence
137 * is granted to a server.
140 int fld_server_create(struct lu_server_fld *fld,
141 const struct lu_env *env,
142 struct lu_seq_range *add_range,
145 struct lu_seq_range *erange;
146 struct lu_seq_range *new;
147 struct fld_thread_info *info;
153 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
154 cfs_mutex_lock(&fld->lsf_lock);
156 erange = &info->fti_lrange;
157 new = &info->fti_irange;
160 /* STEP 1: try to merge with previous range */
161 rc = fld_index_lookup(fld, env, new->lsr_start, erange);
163 /* in case of range overlap, the location must be same */
164 if (range_compare_loc(new, erange) != 0) {
165 CERROR("the start of given range "DRANGE" conflict to"
166 "an existing range "DRANGE"\n",
167 PRANGE(new), PRANGE(erange));
168 GOTO(out, rc = -EIO);
171 if (new->lsr_end < erange->lsr_end)
174 } else if (rc == -ENOENT) {
175 /* check for merge case: optimizes for single mds lustre.
176 * As entry does not exist, returned entry must be left side
177 * entry compared to start of new range (ref dio_lookup()).
178 * So try to merge from left.
180 if (new->lsr_start == erange->lsr_end &&
181 range_compare_loc(new, erange) == 0)
184 /* no overlap allowed in fld, so failure in lookup is error */
189 /* new range will be merged with the existing one.
190 * delete this range at first. */
191 rc = fld_index_delete(fld, env, erange, th);
195 new->lsr_start = min(erange->lsr_start, new->lsr_start);
196 new->lsr_end = max(erange->lsr_end, new->lsr_end);
200 /* STEP 2: try to merge with next range */
201 rc = fld_index_lookup(fld, env, new->lsr_end, erange);
203 /* found a matched range, meaning we're either
204 * overlapping or ajacent, must merge with it. */
206 } else if (rc == -ENOENT) {
207 /* this range is left of new range end point */
208 LASSERT(erange->lsr_end <= new->lsr_end);
210 * the found left range must be either:
211 * 1. withing new range.
212 * 2. left of new range (no overlapping).
213 * because if they're partly overlapping, the STEP 1 must have
214 * been removed this range.
216 LASSERTF(erange->lsr_start > new->lsr_start ||
217 erange->lsr_end < new->lsr_start ||
218 (erange->lsr_end == new->lsr_start &&
219 range_compare_loc(new, erange) != 0),
220 "left "DRANGE", new "DRANGE"\n",
221 PRANGE(erange), PRANGE(new));
223 /* if it's within the new range, merge it */
224 if (erange->lsr_start > new->lsr_start)
231 if (range_compare_loc(new, erange) != 0) {
232 CERROR("the end of given range "DRANGE" overlaps "
233 "with an existing range "DRANGE"\n",
234 PRANGE(new), PRANGE(erange));
235 GOTO(out, rc = -EIO);
238 /* merge with next range */
239 rc = fld_index_delete(fld, env, erange, th);
243 new->lsr_start = min(erange->lsr_start, new->lsr_start);
244 new->lsr_end = max(erange->lsr_end, new->lsr_end);
247 /* now update fld entry. */
248 rc = fld_index_create(fld, env, new, th);
250 LASSERT(rc != -EEXIST);
253 fld_cache_insert(fld->lsf_cache, new);
255 cfs_mutex_unlock(&fld->lsf_lock);
257 CDEBUG((rc != 0 ? D_ERROR : D_INFO),
258 "%s: FLD create: given range : "DRANGE
259 "after merge "DRANGE" rc = %d \n", fld->lsf_name,
260 PRANGE(add_range), PRANGE(new), rc);
264 EXPORT_SYMBOL(fld_server_create);
267 * Lookup mds by seq, returns a range for given seq.
269 * If that entry is not cached in fld cache, request is sent to super
270 * sequence controller node (MDT0). All other MDT[1...N] and client
271 * cache fld entries, but this cache is not persistent.
274 int fld_server_lookup(struct lu_server_fld *fld,
275 const struct lu_env *env,
276 seqno_t seq, struct lu_seq_range *range)
278 struct lu_seq_range *erange;
279 struct fld_thread_info *info;
283 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
284 erange = &info->fti_lrange;
286 /* Lookup it in the cache. */
287 rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
289 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
290 CERROR("FLD cache found a range "DRANGE" doesn't "
291 "match the requested flag %x\n",
292 PRANGE(erange), range->lsr_flags);
300 rc = fld_index_lookup(fld, env, seq, erange);
302 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
303 CERROR("FLD found a range "DRANGE" doesn't "
304 "match the requested flag %x\n",
305 PRANGE(erange), range->lsr_flags);
311 LASSERT(fld->lsf_control_exp);
312 /* send request to mdt0 i.e. super seq. controller.
313 * This is temporary solution, long term solution is fld
314 * replication on all mdt servers.
316 rc = fld_client_rpc(fld->lsf_control_exp,
321 fld_cache_insert(fld->lsf_cache, range);
325 EXPORT_SYMBOL(fld_server_lookup);
328 * All MDT server handle fld lookup operation. But only MDT0 has fld index.
329 * if entry is not found in cache we need to forward lookup request to MDT0
332 static int fld_server_handle(struct lu_server_fld *fld,
333 const struct lu_env *env,
334 __u32 opc, struct lu_seq_range *range,
335 struct fld_thread_info *info)
342 rc = fld_server_lookup(fld, env,
343 range->lsr_start, range);
350 CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, range: "
351 DRANGE"\n", fld->lsf_name, rc, opc, PRANGE(range));
357 static int fld_req_handle(struct ptlrpc_request *req,
358 struct fld_thread_info *info)
360 struct obd_export *exp = req->rq_export;
361 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
362 struct lu_seq_range *in;
363 struct lu_seq_range *out;
368 rc = req_capsule_server_pack(info->fti_pill);
370 RETURN(err_serious(rc));
372 opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
374 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
376 RETURN(err_serious(-EPROTO));
377 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
379 RETURN(err_serious(-EPROTO));
382 /* For old 2.0 client, the 'lsr_flags' is uninitialized.
383 * Set it as 'LU_SEQ_RANGE_MDT' by default.
384 * Old 2.0 liblustre client cannot talk with new 2.1 server. */
385 if (!(exp->exp_connect_flags & OBD_CONNECT_64BITHASH) &&
387 out->lsr_flags = LU_SEQ_RANGE_MDT;
389 rc = fld_server_handle(lu_site2md(site)->ms_server_fld,
390 req->rq_svc_thread->t_env,
393 rc = err_serious(-EPROTO);
398 static void fld_thread_info_init(struct ptlrpc_request *req,
399 struct fld_thread_info *info)
401 info->fti_pill = &req->rq_pill;
402 /* Init request capsule. */
403 req_capsule_init(info->fti_pill, req, RCL_SERVER);
404 req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
407 static void fld_thread_info_fini(struct fld_thread_info *info)
409 req_capsule_fini(info->fti_pill);
412 static int fld_handle(struct ptlrpc_request *req)
414 struct fld_thread_info *info;
415 const struct lu_env *env;
418 env = req->rq_svc_thread->t_env;
419 LASSERT(env != NULL);
421 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
422 LASSERT(info != NULL);
424 fld_thread_info_init(req, info);
425 rc = fld_req_handle(req, info);
426 fld_thread_info_fini(info);
432 * Entry point for handling FLD RPCs called from MDT.
434 int fld_query(struct com_thread_info *info)
436 return fld_handle(info->cti_pill->rc_req);
438 EXPORT_SYMBOL(fld_query);
441 * Returns true, if fid is local to this server node.
443 * WARNING: this function is *not* guaranteed to return false if fid is
444 * remote: it makes an educated conservative guess only.
446 * fid_is_local() is supposed to be used in assertion checks only.
448 int fid_is_local(const struct lu_env *env,
449 struct lu_site *site, const struct lu_fid *fid)
452 struct md_site *msite;
453 struct lu_seq_range *range;
454 struct fld_thread_info *info;
457 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
458 range = &info->fti_lrange;
460 result = 1; /* conservatively assume fid is local */
461 msite = lu_site2md(site);
462 if (msite->ms_client_fld != NULL) {
465 rc = fld_cache_lookup(msite->ms_client_fld->lcf_cache,
466 fid_seq(fid), range);
468 result = (range->lsr_index == msite->ms_node_id);
472 EXPORT_SYMBOL(fid_is_local);
474 static void fld_server_proc_fini(struct lu_server_fld *fld);
477 static int fld_server_proc_init(struct lu_server_fld *fld)
482 fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
484 fld_server_proc_list, fld);
485 if (IS_ERR(fld->lsf_proc_dir)) {
486 rc = PTR_ERR(fld->lsf_proc_dir);
490 rc = lprocfs_seq_create(fld->lsf_proc_dir, "fldb", 0444,
491 &fld_proc_seq_fops, fld);
493 lprocfs_remove(&fld->lsf_proc_dir);
494 fld->lsf_proc_dir = NULL;
500 static void fld_server_proc_fini(struct lu_server_fld *fld)
503 if (fld->lsf_proc_dir != NULL) {
504 if (!IS_ERR(fld->lsf_proc_dir))
505 lprocfs_remove(&fld->lsf_proc_dir);
506 fld->lsf_proc_dir = NULL;
511 static int fld_server_proc_init(struct lu_server_fld *fld)
516 static void fld_server_proc_fini(struct lu_server_fld *fld)
522 int fld_server_init(struct lu_server_fld *fld, struct dt_device *dt,
523 const char *prefix, const struct lu_env *env,
526 int cache_size, cache_threshold;
527 struct lu_seq_range range;
531 snprintf(fld->lsf_name, sizeof(fld->lsf_name),
534 cache_size = FLD_SERVER_CACHE_SIZE /
535 sizeof(struct fld_cache_entry);
537 cache_threshold = cache_size *
538 FLD_SERVER_CACHE_THRESHOLD / 100;
540 cfs_mutex_init(&fld->lsf_lock);
541 fld->lsf_cache = fld_cache_init(fld->lsf_name,
542 cache_size, cache_threshold);
543 if (IS_ERR(fld->lsf_cache)) {
544 rc = PTR_ERR(fld->lsf_cache);
545 fld->lsf_cache = NULL;
550 rc = fld_index_init(fld, env, dt);
556 rc = fld_server_proc_init(fld);
560 fld->lsf_control_exp = NULL;
562 /* Insert reserved sequence number of ".lustre" into fld cache. */
563 range.lsr_start = FID_SEQ_DOT_LUSTRE;
564 range.lsr_end = FID_SEQ_DOT_LUSTRE + 1;
566 range.lsr_flags = LU_SEQ_RANGE_MDT;
567 fld_cache_insert(fld->lsf_cache, &range);
572 fld_server_fini(fld, env);
575 EXPORT_SYMBOL(fld_server_init);
577 void fld_server_fini(struct lu_server_fld *fld,
578 const struct lu_env *env)
582 fld_server_proc_fini(fld);
583 fld_index_fini(fld, env);
585 if (fld->lsf_cache != NULL) {
586 if (!IS_ERR(fld->lsf_cache))
587 fld_cache_fini(fld->lsf_cache);
588 fld->lsf_cache = NULL;
593 EXPORT_SYMBOL(fld_server_fini);
595 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
596 MODULE_DESCRIPTION("Lustre FLD");
597 MODULE_LICENSE("GPL");
599 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);