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 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 /* Insert index entry and update cache. */
112 int fld_server_create(struct lu_server_fld *fld,
113 const struct lu_env *env,
114 seqno_t seq, mdsno_t mds)
119 rc = fld_index_create(fld, env, seq, mds);
123 * Do not return result of calling fld_cache_insert()
124 * here. First of all because it may return -EEXISTS. Another
125 * reason is that, we do not want to stop proceeding even after
128 fld_cache_insert(fld->lsf_cache, seq, mds);
133 EXPORT_SYMBOL(fld_server_create);
135 /* Delete index entry. */
136 int fld_server_delete(struct lu_server_fld *fld,
137 const struct lu_env *env,
143 fld_cache_delete(fld->lsf_cache, seq);
144 rc = fld_index_delete(fld, env, seq);
148 EXPORT_SYMBOL(fld_server_delete);
150 /* Lookup mds by seq. */
151 int fld_server_lookup(struct lu_server_fld *fld,
152 const struct lu_env *env,
153 seqno_t seq, mdsno_t *mds)
158 /* Lookup it in the cache. */
159 rc = fld_cache_lookup(fld->lsf_cache, seq, mds);
163 rc = fld_index_lookup(fld, env, seq, mds);
166 * Do not return error here as well. See previous comment in
167 * same situation in function fld_server_create().
169 fld_cache_insert(fld->lsf_cache, seq, *mds);
173 EXPORT_SYMBOL(fld_server_lookup);
175 static int fld_server_handle(struct lu_server_fld *fld,
176 const struct lu_env *env,
177 __u32 opc, struct md_fld *mf,
178 struct fld_thread_info *info)
185 rc = fld_server_create(fld, env,
186 mf->mf_seq, mf->mf_mds);
188 /* Do not return -EEXIST error for resent case */
189 if ((info->fti_flags & MSG_RESENT) && rc == -EEXIST)
193 rc = fld_server_delete(fld, env, mf->mf_seq);
195 /* Do not return -ENOENT error for resent case */
196 if ((info->fti_flags & MSG_RESENT) && rc == -ENOENT)
200 rc = fld_server_lookup(fld, env,
201 mf->mf_seq, &mf->mf_mds);
208 CDEBUG(D_INFO, "%s: FLD req handle: error %d (opc: %d, seq: "
209 LPX64", mds: "LPU64")\n", fld->lsf_name, rc, opc,
210 mf->mf_seq, mf->mf_mds);
216 static int fld_req_handle(struct ptlrpc_request *req,
217 struct fld_thread_info *info)
219 struct lu_site *site;
226 site = req->rq_export->exp_obd->obd_lu_dev->ld_site;
228 rc = req_capsule_server_pack(info->fti_pill);
230 RETURN(err_serious(rc));
232 opc = req_capsule_client_get(info->fti_pill, &RMF_FLD_OPC);
234 in = req_capsule_client_get(info->fti_pill, &RMF_FLD_MDFLD);
236 RETURN(err_serious(-EPROTO));
237 out = req_capsule_server_get(info->fti_pill, &RMF_FLD_MDFLD);
239 RETURN(err_serious(-EPROTO));
242 rc = fld_server_handle(lu_site2md(site)->ms_server_fld,
243 req->rq_svc_thread->t_env,
246 rc = err_serious(-EPROTO);
251 static void fld_thread_info_init(struct ptlrpc_request *req,
252 struct fld_thread_info *info)
254 info->fti_flags = lustre_msg_get_flags(req->rq_reqmsg);
256 info->fti_pill = &req->rq_pill;
257 /* Init request capsule. */
258 req_capsule_init(info->fti_pill, req, RCL_SERVER);
259 req_capsule_set(info->fti_pill, &RQF_FLD_QUERY);
262 static void fld_thread_info_fini(struct fld_thread_info *info)
264 req_capsule_fini(info->fti_pill);
267 static int fld_handle(struct ptlrpc_request *req)
269 struct fld_thread_info *info;
270 const struct lu_env *env;
273 env = req->rq_svc_thread->t_env;
274 LASSERT(env != NULL);
276 info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
277 LASSERT(info != NULL);
279 fld_thread_info_init(req, info);
280 rc = fld_req_handle(req, info);
281 fld_thread_info_fini(info);
287 * Entry point for handling FLD RPCs called from MDT.
289 int fld_query(struct com_thread_info *info)
291 return fld_handle(info->cti_pill->rc_req);
293 EXPORT_SYMBOL(fld_query);
296 * Returns true, if fid is local to this server node.
298 * WARNING: this function is *not* guaranteed to return false if fid is
299 * remote: it makes an educated conservative guess only.
301 * fid_is_local() is supposed to be used in assertion checks only.
303 int fid_is_local(struct lu_site *site, const struct lu_fid *fid)
306 struct md_site *msite;
308 result = 1; /* conservatively assume fid is local */
309 msite = lu_site2md(site);
310 if (msite->ms_client_fld != NULL) {
314 rc = fld_cache_lookup(msite->ms_client_fld->lcf_cache,
317 result = (mds == msite->ms_node_id);
321 EXPORT_SYMBOL(fid_is_local);
323 static void fld_server_proc_fini(struct lu_server_fld *fld);
326 static int fld_server_proc_init(struct lu_server_fld *fld)
331 fld->lsf_proc_dir = lprocfs_register(fld->lsf_name,
333 fld_server_proc_list, fld);
334 if (IS_ERR(fld->lsf_proc_dir)) {
335 rc = PTR_ERR(fld->lsf_proc_dir);
342 static void fld_server_proc_fini(struct lu_server_fld *fld)
345 if (fld->lsf_proc_dir != NULL) {
346 if (!IS_ERR(fld->lsf_proc_dir))
347 lprocfs_remove(&fld->lsf_proc_dir);
348 fld->lsf_proc_dir = NULL;
353 static int fld_server_proc_init(struct lu_server_fld *fld)
358 static void fld_server_proc_fini(struct lu_server_fld *fld)
364 int fld_server_init(struct lu_server_fld *fld, struct dt_device *dt,
365 const char *prefix, const struct lu_env *env)
367 int cache_size, cache_threshold;
371 snprintf(fld->lsf_name, sizeof(fld->lsf_name),
374 cache_size = FLD_SERVER_CACHE_SIZE /
375 sizeof(struct fld_cache_entry);
377 cache_threshold = cache_size *
378 FLD_SERVER_CACHE_THRESHOLD / 100;
380 fld->lsf_cache = fld_cache_init(fld->lsf_name,
381 FLD_SERVER_HTABLE_SIZE,
382 cache_size, cache_threshold);
383 if (IS_ERR(fld->lsf_cache)) {
384 rc = PTR_ERR(fld->lsf_cache);
385 fld->lsf_cache = NULL;
389 rc = fld_index_init(fld, env, dt);
393 rc = fld_server_proc_init(fld);
400 fld_server_fini(fld, env);
403 EXPORT_SYMBOL(fld_server_init);
405 void fld_server_fini(struct lu_server_fld *fld,
406 const struct lu_env *env)
410 fld_server_proc_fini(fld);
411 fld_index_fini(fld, env);
413 if (fld->lsf_cache != NULL) {
414 if (!IS_ERR(fld->lsf_cache))
415 fld_cache_fini(fld->lsf_cache);
416 fld->lsf_cache = NULL;
421 EXPORT_SYMBOL(fld_server_fini);
423 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
424 MODULE_DESCRIPTION("Lustre FLD");
425 MODULE_LICENSE("GPL");
427 cfs_module(mdd, "0.1.0", fld_mod_init, fld_mod_exit);