1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_FLD
50 # include <libcfs/libcfs.h>
51 # include <linux/module.h>
52 # include <linux/jbd.h>
53 # include <asm/div64.h>
54 #else /* __KERNEL__ */
55 # include <liblustre.h>
56 # include <libcfs/list.h>
60 #include <obd_class.h>
61 #include <lustre_ver.h>
62 #include <obd_support.h>
63 #include <lprocfs_status.h>
65 #include <md_object.h>
66 #include <lustre_fid.h>
67 #include <lustre_req_layout.h>
68 #include "fld_internal.h"
72 /* context key constructor/destructor: fld_key_init, fld_key_fini */
73 LU_KEY_INIT_FINI(fld, struct fld_thread_info);
75 /* context key: fld_thread_key */
76 LU_CONTEXT_KEY_DEFINE(fld, LCT_MD_THREAD|LCT_DT_THREAD);
78 cfs_proc_dir_entry_t *fld_type_proc_dir = NULL;
80 static struct lu_local_obj_desc llod_fld_index = {
81 .llod_name = fld_index_name,
82 .llod_oid = FLD_INDEX_OID,
84 .llod_feat = &fld_index_features,
87 static int __init fld_mod_init(void)
89 fld_type_proc_dir = lprocfs_register(LUSTRE_FLD_NAME,
92 if (IS_ERR(fld_type_proc_dir))
93 return PTR_ERR(fld_type_proc_dir);
95 llo_local_obj_register(&llod_fld_index);
97 LU_CONTEXT_KEY_INIT(&fld_thread_key);
98 lu_context_key_register(&fld_thread_key);
102 static void __exit fld_mod_exit(void)
104 llo_local_obj_unregister(&llod_fld_index);
105 lu_context_key_degister(&fld_thread_key);
106 if (fld_type_proc_dir != NULL && !IS_ERR(fld_type_proc_dir)) {
107 lprocfs_remove(&fld_type_proc_dir);
108 fld_type_proc_dir = NULL;
112 /* Insert index entry and update cache. */
113 int fld_server_create(struct lu_server_fld *fld,
114 const struct lu_env *env,
115 seqno_t seq, mdsno_t mds)
120 rc = fld_index_create(fld, env, seq, mds);
124 * Do not return result of calling fld_cache_insert()
125 * here. First of all because it may return -EEXISTS. Another
126 * reason is that, we do not want to stop proceeding even after
129 fld_cache_insert(fld->lsf_cache, seq, mds);
134 EXPORT_SYMBOL(fld_server_create);
136 /* Delete index entry. */
137 int fld_server_delete(struct lu_server_fld *fld,
138 const struct lu_env *env,
144 fld_cache_delete(fld->lsf_cache, seq);
145 rc = fld_index_delete(fld, env, seq);
149 EXPORT_SYMBOL(fld_server_delete);
151 /* Lookup mds by seq. */
152 int fld_server_lookup(struct lu_server_fld *fld,
153 const struct lu_env *env,
154 seqno_t seq, mdsno_t *mds)
159 /* Lookup it in the cache. */
160 rc = fld_cache_lookup(fld->lsf_cache, seq, mds);
164 rc = fld_index_lookup(fld, env, seq, mds);
167 * Do not return error here as well. See previous comment in
168 * same situation in function fld_server_create().
170 fld_cache_insert(fld->lsf_cache, seq, *mds);
174 EXPORT_SYMBOL(fld_server_lookup);
176 static int fld_server_handle(struct lu_server_fld *fld,
177 const struct lu_env *env,
178 __u32 opc, struct md_fld *mf,
179 struct fld_thread_info *info)
186 rc = fld_server_create(fld, env,
187 mf->mf_seq, mf->mf_mds);
189 /* Do not return -EEXIST error for resent case */
190 if ((info->fti_flags & MSG_RESENT) && rc == -EEXIST)
194 rc = fld_server_delete(fld, env, mf->mf_seq);
196 /* Do not return -ENOENT error for resent case */
197 if ((info->fti_flags & MSG_RESENT) && rc == -ENOENT)
201 rc = fld_server_lookup(fld, env,
202 mf->mf_seq, &mf->mf_mds);
209 CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, seq: "
210 LPX64", mds: "LPU64")\n", fld->lsf_name, rc, opc,
211 mf->mf_seq, mf->mf_mds);
217 static int fld_req_handle(struct ptlrpc_request *req,
218 struct fld_thread_info *info)
220 struct lu_site *site;
227 site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
229 rc = req_capsule_server_pack(info->fti_pill);
231 RETURN(err_serious(rc));
233 opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
235 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
237 RETURN(err_serious(-EPROTO));
238 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
240 RETURN(err_serious(-EPROTO));
243 rc = fld_server_handle(lu_site2md(site)->ms_server_fld,
244 req->rq_svc_thread->t_env,
247 rc = err_serious(-EPROTO);
252 static void fld_thread_info_init(struct ptlrpc_request *req,
253 struct fld_thread_info *info)
255 info->fti_flags = lustre_msg_get_flags(req->rq_reqmsg);
257 info->fti_pill = &req->rq_pill;
258 /* Init request capsule. */
259 req_capsule_init(info->fti_pill, req, RCL_SERVER);
260 req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
263 static void fld_thread_info_fini(struct fld_thread_info *info)
265 req_capsule_fini(info->fti_pill);
268 static int fld_handle(struct ptlrpc_request *req)
270 struct fld_thread_info *info;
271 const struct lu_env *env;
274 env = req->rq_svc_thread->t_env;
275 LASSERT(env != NULL);
277 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
278 LASSERT(info != NULL);
280 fld_thread_info_init(req, info);
281 rc = fld_req_handle(req, info);
282 fld_thread_info_fini(info);
288 * Entry point for handling FLD RPCs called from MDT.
290 int fld_query(struct com_thread_info *info)
292 return fld_handle(info->cti_pill->rc_req);
294 EXPORT_SYMBOL(fld_query);
297 * Returns true, if fid is local to this server node.
299 * WARNING: this function is *not* guaranteed to return false if fid is
300 * remote: it makes an educated conservative guess only.
302 * fid_is_local() is supposed to be used in assertion checks only.
304 int fid_is_local(struct lu_site *site, const struct lu_fid *fid)
307 struct md_site *msite;
309 result = 1; /* conservatively assume fid is local */
310 msite = lu_site2md(site);
311 if (msite->ms_client_fld != NULL) {
315 rc = fld_cache_lookup(msite->ms_client_fld->lcf_cache,
318 result = (mds == msite->ms_node_id);
322 EXPORT_SYMBOL(fid_is_local);
324 static void fld_server_proc_fini(struct lu_server_fld *fld);
327 static int fld_server_proc_init(struct lu_server_fld *fld)
332 fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
334 fld_server_proc_list, fld);
335 if (IS_ERR(fld->lsf_proc_dir)) {
336 rc = PTR_ERR(fld->lsf_proc_dir);
343 static void fld_server_proc_fini(struct lu_server_fld *fld)
346 if (fld->lsf_proc_dir != NULL) {
347 if (!IS_ERR(fld->lsf_proc_dir))
348 lprocfs_remove(&fld->lsf_proc_dir);
349 fld->lsf_proc_dir = NULL;
354 static int fld_server_proc_init(struct lu_server_fld *fld)
359 static void fld_server_proc_fini(struct lu_server_fld *fld)
365 int fld_server_init(struct lu_server_fld *fld, struct dt_device *dt,
366 const char *prefix, const struct lu_env *env)
368 int cache_size, cache_threshold;
372 snprintf(fld->lsf_name, sizeof(fld->lsf_name),
375 cache_size = FLD_SERVER_CACHE_SIZE /
376 sizeof(struct fld_cache_entry);
378 cache_threshold = cache_size *
379 FLD_SERVER_CACHE_THRESHOLD / 100;
381 fld->lsf_cache = fld_cache_init(fld->lsf_name,
382 FLD_SERVER_HTABLE_SIZE,
383 cache_size, cache_threshold);
384 if (IS_ERR(fld->lsf_cache)) {
385 rc = PTR_ERR(fld->lsf_cache);
386 fld->lsf_cache = NULL;
390 rc = fld_index_init(fld, env, dt);
394 rc = fld_server_proc_init(fld);
401 fld_server_fini(fld, env);
404 EXPORT_SYMBOL(fld_server_init);
406 void fld_server_fini(struct lu_server_fld *fld,
407 const struct lu_env *env)
411 fld_server_proc_fini(fld);
412 fld_index_fini(fld, env);
414 if (fld->lsf_cache != NULL) {
415 if (!IS_ERR(fld->lsf_cache))
416 fld_cache_fini(fld->lsf_cache);
417 fld->lsf_cache = NULL;
422 EXPORT_SYMBOL(fld_server_fini);
424 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
425 MODULE_DESCRIPTION("Lustre FLD");
426 MODULE_LICENSE("GPL");
428 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);