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,
109 if (fld->lsf_no_range_lookup) {
110 /* Stub for underlying FS which can't lookup ranges */
114 /* for ldiskfs OSD it's enough to declare operation with any ops
115 * with DMU we'll probably need to specify exact key/value */
116 rc = dt_declare_delete(env, fld->lsf_obj, NULL, th);
119 rc = dt_declare_delete(env, fld->lsf_obj, NULL, th);
122 rc = dt_declare_insert(env, fld->lsf_obj, NULL, NULL, th);
126 EXPORT_SYMBOL(fld_declare_server_create);
129 * Insert FLD index entry and update FLD cache.
131 * First it try to merge given range with existing range then update
132 * FLD index and FLD cache accordingly. FLD index consistency is maintained
134 * This function is called from the sequence allocator when a super-sequence
135 * is granted to a server.
138 int fld_server_create(struct lu_server_fld *fld,
139 const struct lu_env *env,
140 struct lu_seq_range *add_range,
143 struct lu_seq_range *erange;
144 struct lu_seq_range *new;
145 struct fld_thread_info *info;
151 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
152 mutex_lock(&fld->lsf_lock);
154 erange = &info->fti_lrange;
155 new = &info->fti_irange;
158 /* STEP 1: try to merge with previous range */
159 rc = fld_index_lookup(fld, env, new->lsr_start, erange);
161 /* in case of range overlap, the location must be same */
162 if (range_compare_loc(new, erange) != 0) {
163 CERROR("the start of given range "DRANGE" conflict to"
164 "an existing range "DRANGE"\n",
165 PRANGE(new), PRANGE(erange));
166 GOTO(out, rc = -EIO);
169 if (new->lsr_end < erange->lsr_end)
172 } else if (rc == -ENOENT) {
173 /* check for merge case: optimizes for single mds lustre.
174 * As entry does not exist, returned entry must be left side
175 * entry compared to start of new range (ref dio_lookup()).
176 * So try to merge from left.
178 if (new->lsr_start == erange->lsr_end &&
179 range_compare_loc(new, erange) == 0)
182 /* no overlap allowed in fld, so failure in lookup is error */
187 /* new range will be merged with the existing one.
188 * delete this range at first. */
189 rc = fld_index_delete(fld, env, erange, th);
193 new->lsr_start = min(erange->lsr_start, new->lsr_start);
194 new->lsr_end = max(erange->lsr_end, new->lsr_end);
198 /* STEP 2: try to merge with next range */
199 rc = fld_index_lookup(fld, env, new->lsr_end, erange);
201 /* found a matched range, meaning we're either
202 * overlapping or ajacent, must merge with it. */
204 } else if (rc == -ENOENT) {
205 /* this range is left of new range end point */
206 LASSERT(erange->lsr_end <= new->lsr_end);
208 * the found left range must be either:
209 * 1. withing new range.
210 * 2. left of new range (no overlapping).
211 * because if they're partly overlapping, the STEP 1 must have
212 * been removed this range.
214 LASSERTF(erange->lsr_start > new->lsr_start ||
215 erange->lsr_end < new->lsr_start ||
216 (erange->lsr_end == new->lsr_start &&
217 range_compare_loc(new, erange) != 0),
218 "left "DRANGE", new "DRANGE"\n",
219 PRANGE(erange), PRANGE(new));
221 /* if it's within the new range, merge it */
222 if (erange->lsr_start > new->lsr_start)
229 if (range_compare_loc(new, erange) != 0) {
230 CERROR("the end of given range "DRANGE" overlaps "
231 "with an existing range "DRANGE"\n",
232 PRANGE(new), PRANGE(erange));
233 GOTO(out, rc = -EIO);
236 /* merge with next range */
237 rc = fld_index_delete(fld, env, erange, th);
241 new->lsr_start = min(erange->lsr_start, new->lsr_start);
242 new->lsr_end = max(erange->lsr_end, new->lsr_end);
245 /* now update fld entry. */
246 rc = fld_index_create(fld, env, new, th);
248 LASSERT(rc != -EEXIST);
251 fld_cache_insert(fld->lsf_cache, new);
253 mutex_unlock(&fld->lsf_lock);
255 CDEBUG((rc != 0 ? D_ERROR : D_INFO),
256 "%s: FLD create: given range : "DRANGE
257 "after merge "DRANGE" rc = %d \n", fld->lsf_name,
258 PRANGE(add_range), PRANGE(new), rc);
262 EXPORT_SYMBOL(fld_server_create);
265 * Lookup mds by seq, returns a range for given seq.
267 * If that entry is not cached in fld cache, request is sent to super
268 * sequence controller node (MDT0). All other MDT[1...N] and client
269 * cache fld entries, but this cache is not persistent.
272 int fld_server_lookup(struct lu_server_fld *fld,
273 const struct lu_env *env,
274 seqno_t seq, struct lu_seq_range *range)
276 struct lu_seq_range *erange;
277 struct fld_thread_info *info;
281 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
282 erange = &info->fti_lrange;
284 /* Lookup it in the cache. */
285 rc = fld_cache_lookup(fld->lsf_cache, seq, erange);
287 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
288 CERROR("FLD cache found a range "DRANGE" doesn't "
289 "match the requested flag %x\n",
290 PRANGE(erange), range->lsr_flags);
298 rc = fld_index_lookup(fld, env, seq, erange);
300 if (unlikely(erange->lsr_flags != range->lsr_flags)) {
301 CERROR("FLD found a range "DRANGE" doesn't "
302 "match the requested flag %x\n",
303 PRANGE(erange), range->lsr_flags);
309 LASSERT(fld->lsf_control_exp);
310 /* send request to mdt0 i.e. super seq. controller.
311 * This is temporary solution, long term solution is fld
312 * replication on all mdt servers.
314 rc = fld_client_rpc(fld->lsf_control_exp,
319 fld_cache_insert(fld->lsf_cache, range);
323 EXPORT_SYMBOL(fld_server_lookup);
326 * All MDT server handle fld lookup operation. But only MDT0 has fld index.
327 * if entry is not found in cache we need to forward lookup request to MDT0
330 static int fld_server_handle(struct lu_server_fld *fld,
331 const struct lu_env *env,
332 __u32 opc, struct lu_seq_range *range,
333 struct fld_thread_info *info)
340 rc = fld_server_lookup(fld, env,
341 range->lsr_start, range);
348 CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, range: "
349 DRANGE"\n", fld->lsf_name, rc, opc, PRANGE(range));
355 static int fld_req_handle(struct ptlrpc_request *req,
356 struct fld_thread_info *info)
358 struct obd_export *exp = req->rq_export;
359 struct lu_site *site = exp->exp_obd->obd_lu_dev->ld_site;
360 struct lu_seq_range *in;
361 struct lu_seq_range *out;
366 rc = req_capsule_server_pack(info->fti_pill);
368 RETURN(err_serious(rc));
370 opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
372 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
374 RETURN(err_serious(-EPROTO));
375 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
377 RETURN(err_serious(-EPROTO));
380 /* For old 2.0 client, the 'lsr_flags' is uninitialized.
381 * Set it as 'LU_SEQ_RANGE_MDT' by default.
382 * Old 2.0 liblustre client cannot talk with new 2.1 server. */
383 if (!(exp->exp_connect_flags & OBD_CONNECT_64BITHASH) &&
385 out->lsr_flags = LU_SEQ_RANGE_MDT;
387 rc = fld_server_handle(lu_site2md(site)->ms_server_fld,
388 req->rq_svc_thread->t_env,
391 rc = err_serious(-EPROTO);
396 static void fld_thread_info_init(struct ptlrpc_request *req,
397 struct fld_thread_info *info)
399 info->fti_pill = &req->rq_pill;
400 /* Init request capsule. */
401 req_capsule_init(info->fti_pill, req, RCL_SERVER);
402 req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
405 static void fld_thread_info_fini(struct fld_thread_info *info)
407 req_capsule_fini(info->fti_pill);
410 static int fld_handle(struct ptlrpc_request *req)
412 struct fld_thread_info *info;
413 const struct lu_env *env;
416 env = req->rq_svc_thread->t_env;
417 LASSERT(env != NULL);
419 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
420 LASSERT(info != NULL);
422 fld_thread_info_init(req, info);
423 rc = fld_req_handle(req, info);
424 fld_thread_info_fini(info);
430 * Entry point for handling FLD RPCs called from MDT.
432 int fld_query(struct com_thread_info *info)
434 return fld_handle(info->cti_pill->rc_req);
436 EXPORT_SYMBOL(fld_query);
439 * Returns true, if fid is local to this server node.
441 * WARNING: this function is *not* guaranteed to return false if fid is
442 * remote: it makes an educated conservative guess only.
444 * fid_is_local() is supposed to be used in assertion checks only.
446 int fid_is_local(const struct lu_env *env,
447 struct lu_site *site, const struct lu_fid *fid)
450 struct md_site *msite;
451 struct lu_seq_range *range;
452 struct fld_thread_info *info;
455 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
456 range = &info->fti_lrange;
458 result = 1; /* conservatively assume fid is local */
459 msite = lu_site2md(site);
460 if (msite->ms_client_fld != NULL) {
463 rc = fld_cache_lookup(msite->ms_client_fld->lcf_cache,
464 fid_seq(fid), range);
466 result = (range->lsr_index == msite->ms_node_id);
470 EXPORT_SYMBOL(fid_is_local);
472 static void fld_server_proc_fini(struct lu_server_fld *fld);
475 static int fld_server_proc_init(struct lu_server_fld *fld)
480 fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
482 fld_server_proc_list, fld);
483 if (IS_ERR(fld->lsf_proc_dir)) {
484 rc = PTR_ERR(fld->lsf_proc_dir);
488 rc = lprocfs_seq_create(fld->lsf_proc_dir, "fldb", 0444,
489 &fld_proc_seq_fops, fld);
491 lprocfs_remove(&fld->lsf_proc_dir);
492 fld->lsf_proc_dir = NULL;
498 static void fld_server_proc_fini(struct lu_server_fld *fld)
501 if (fld->lsf_proc_dir != NULL) {
502 if (!IS_ERR(fld->lsf_proc_dir))
503 lprocfs_remove(&fld->lsf_proc_dir);
504 fld->lsf_proc_dir = NULL;
509 static int fld_server_proc_init(struct lu_server_fld *fld)
514 static void fld_server_proc_fini(struct lu_server_fld *fld)
520 int fld_server_init(struct lu_server_fld *fld, struct dt_device *dt,
521 const char *prefix, const struct lu_env *env,
524 int cache_size, cache_threshold;
525 struct lu_seq_range range;
529 snprintf(fld->lsf_name, sizeof(fld->lsf_name),
532 cache_size = FLD_SERVER_CACHE_SIZE /
533 sizeof(struct fld_cache_entry);
535 cache_threshold = cache_size *
536 FLD_SERVER_CACHE_THRESHOLD / 100;
538 mutex_init(&fld->lsf_lock);
539 fld->lsf_cache = fld_cache_init(fld->lsf_name,
540 cache_size, cache_threshold);
541 if (IS_ERR(fld->lsf_cache)) {
542 rc = PTR_ERR(fld->lsf_cache);
543 fld->lsf_cache = NULL;
548 rc = fld_index_init(fld, env, dt);
554 rc = fld_server_proc_init(fld);
558 fld->lsf_control_exp = NULL;
560 /* Insert reserved sequence number of ".lustre" into fld cache. */
561 range.lsr_start = FID_SEQ_DOT_LUSTRE;
562 range.lsr_end = FID_SEQ_DOT_LUSTRE + 1;
564 range.lsr_flags = LU_SEQ_RANGE_MDT;
565 fld_cache_insert(fld->lsf_cache, &range);
570 fld_server_fini(fld, env);
573 EXPORT_SYMBOL(fld_server_init);
575 void fld_server_fini(struct lu_server_fld *fld,
576 const struct lu_env *env)
580 fld_server_proc_fini(fld);
581 fld_index_fini(fld, env);
583 if (fld->lsf_cache != NULL) {
584 if (!IS_ERR(fld->lsf_cache))
585 fld_cache_fini(fld->lsf_cache);
586 fld->lsf_cache = NULL;
591 EXPORT_SYMBOL(fld_server_fini);
593 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
594 MODULE_DESCRIPTION("Lustre FLD");
595 MODULE_LICENSE("GPL");
597 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);