1 /* -*- MODE: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (mdd) routines
7 * Copyright (C) 2006 Cluster File Systems, Inc.
8 * Author: Wang Di <wangdi@clusterfs.com>
10 * This file is part of the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
13 * You may have signed or agreed to another license before downloading
14 * this software. If so, you are bound by the terms and conditions
15 * of that agreement, and the following does not apply to you. See the
16 * LICENSE file included with this distribution for more information.
18 * If you did not agree to a different license, then this copy of Lustre
19 * is open source software; you can redistribute it and/or modify it
20 * under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * In either case, Lustre is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * license text for more details.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
33 #include <linux/module.h>
34 #include <linux/jbd.h>
36 #include <obd_class.h>
37 #include <lustre_ver.h>
38 #include <obd_support.h>
39 #include <lprocfs_status.h>
41 #include <linux/ldiskfs_fs.h>
42 #include <lustre_mds.h>
43 #include <lustre/lustre_idl.h>
45 #include "mdd_internal.h"
47 struct md_device_operations mdd_ops;
49 static const char *mdd_root_dir_name = "root";
50 static int mdd_device_init(const struct lu_env *env, struct lu_device *d,
51 const char *name, struct lu_device *next)
53 struct mdd_device *mdd = lu2mdd_dev(d);
58 mdd->mdd_child = lu2dt_dev(next);
61 /* Prepare transactions callbacks. */
62 mdd->mdd_txn_cb.dtc_txn_start = mdd_txn_start_cb;
63 mdd->mdd_txn_cb.dtc_txn_stop = mdd_txn_stop_cb;
64 mdd->mdd_txn_cb.dtc_txn_commit = mdd_txn_commit_cb;
65 mdd->mdd_txn_cb.dtc_cookie = mdd;
66 INIT_LIST_HEAD(&mdd->mdd_txn_cb.dtc_linkage);
67 rc = mdd_procfs_init(mdd, name);
71 static struct lu_device *mdd_device_fini(const struct lu_env *env,
74 struct mdd_device *mdd = lu2mdd_dev(d);
75 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
78 rc = mdd_procfs_fini(mdd);
80 CERROR("proc fini error %d \n", rc);
86 static int mdd_mount(const struct lu_env *env, struct mdd_device *mdd)
89 struct dt_object *root;
92 dt_txn_callback_add(mdd->mdd_child, &mdd->mdd_txn_cb);
93 root = dt_store_open(env, mdd->mdd_child, mdd_root_dir_name,
96 LASSERT(root != NULL);
97 lu_object_put(env, &root->do_lu);
98 rc = orph_index_init(env, mdd);
105 static void mdd_device_shutdown(const struct lu_env *env,
106 struct mdd_device *m)
108 dt_txn_callback_del(m->mdd_child, &m->mdd_txn_cb);
110 mdd_fini_obd(env, m);
111 orph_index_fini(env, m);
114 static int mdd_process_config(const struct lu_env *env,
115 struct lu_device *d, struct lustre_cfg *cfg)
117 struct mdd_device *m = lu2mdd_dev(d);
118 struct dt_device *dt = m->mdd_child;
119 struct lu_device *next = &dt->dd_lu_dev;
123 switch (cfg->lcfg_command) {
125 rc = next->ld_ops->ldo_process_config(env, next, cfg);
128 dt->dd_ops->dt_conf_get(env, dt, &m->mdd_dt_conf);
130 rc = mdd_init_obd(env, m, cfg);
132 CERROR("lov init error %d \n", rc);
135 rc = mdd_mount(env, m);
138 rc = mdd_txn_init_credits(env, m);
141 mdd_device_shutdown(env, m);
143 rc = next->ld_ops->ldo_process_config(env, next, cfg);
150 static int mdd_lov_set_nextid(const struct lu_env *env,
151 struct mdd_device *mdd)
153 struct mds_obd *mds = &mdd->mdd_obd_dev->u.mds;
157 LASSERT(mds->mds_lov_objids != NULL);
158 rc = obd_set_info_async(mds->mds_osc_exp, strlen(KEY_NEXT_ID),
159 KEY_NEXT_ID, mds->mds_lov_desc.ld_tgt_count,
160 mds->mds_lov_objids, NULL);
165 static int mdd_cleanup_unlink_llog(const struct lu_env *env,
166 struct mdd_device *mdd)
168 /* XXX: to be implemented! */
173 static int mdd_recovery_complete(const struct lu_env *env,
176 struct mdd_device *mdd = lu2mdd_dev(d);
177 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
178 struct obd_device *obd = mdd2obd_dev(mdd);
182 LASSERT(mdd != NULL);
183 LASSERT(obd != NULL);
185 /* XXX: Do we need this in new stack? */
186 rc = mdd_lov_set_nextid(env, mdd);
188 CERROR("mdd_lov_set_nextid() failed %d\n",
193 /* XXX: cleanup unlink. */
194 rc = mdd_cleanup_unlink_llog(env, mdd);
196 CERROR("mdd_cleanup_unlink_llog() failed %d\n",
201 obd_notify(obd->u.mds.mds_osc_obd, NULL,
202 (obd->obd_async_recov ?
203 OBD_NOTIFY_SYNC_NONBLOCK :
204 OBD_NOTIFY_SYNC), NULL);
206 obd->obd_recovering = 0;
207 obd->obd_type->typ_dt_ops->o_postrecov(obd);
209 /* XXX: orphans handling. */
210 __mdd_orphan_cleanup(env, mdd);
211 rc = next->ld_ops->ldo_recovery_complete(env, next);
216 struct lu_device_operations mdd_lu_ops = {
217 .ldo_object_alloc = mdd_object_alloc,
218 .ldo_process_config = mdd_process_config,
219 .ldo_recovery_complete = mdd_recovery_complete
223 * No permission check is needed.
225 static int mdd_root_get(const struct lu_env *env,
226 struct md_device *m, struct lu_fid *f)
228 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
231 *f = mdd->mdd_root_fid;
236 * No permission check is needed.
238 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
241 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
246 rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
252 * No permission check is needed.
254 static int mdd_maxsize_get(const struct lu_env *env, struct md_device *m,
255 int *md_size, int *cookie_size)
257 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
260 *md_size = mdd_lov_mdsize(env, mdd);
261 *cookie_size = mdd_lov_cookiesize(env, mdd);
266 static int mdd_init_capa_ctxt(const struct lu_env *env, struct md_device *m,
267 int mode, unsigned long timeout, __u32 alg,
268 struct lustre_capa_key *keys)
270 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
271 struct mds_obd *mds = &mdd2obd_dev(mdd)->u.mds;
275 mds->mds_capa_keys = keys;
276 rc = mdd_child_ops(mdd)->dt_init_capa_ctxt(env, mdd->mdd_child, mode,
281 static int mdd_update_capa_key(const struct lu_env *env,
283 struct lustre_capa_key *key)
285 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
286 struct obd_export *lov_exp = mdd2obd_dev(mdd)->u.mds.mds_osc_exp;
290 rc = obd_set_info_async(lov_exp, strlen(KEY_CAPA_KEY), KEY_CAPA_KEY,
291 sizeof(*key), key, NULL);
295 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
296 struct lu_device_type *t,
297 struct lustre_cfg *lcfg)
300 struct mdd_device *m;
304 l = ERR_PTR(-ENOMEM);
306 md_device_init(&m->mdd_md_dev, t);
308 l->ld_ops = &mdd_lu_ops;
309 m->mdd_md_dev.md_ops = &mdd_ops;
315 static void mdd_device_free(const struct lu_env *env,
316 struct lu_device *lu)
318 struct mdd_device *m = lu2mdd_dev(lu);
320 LASSERT(atomic_read(&lu->ld_ref) == 0);
321 md_device_fini(&m->mdd_md_dev);
325 static struct obd_ops mdd_obd_device_ops = {
326 .o_owner = THIS_MODULE
329 static void *mdd_ucred_key_init(const struct lu_context *ctx,
330 struct lu_context_key *key)
336 uc = ERR_PTR(-ENOMEM);
340 static void mdd_ucred_key_fini(const struct lu_context *ctx,
341 struct lu_context_key *key, void *data)
343 struct md_ucred *uc = data;
347 static struct lu_context_key mdd_ucred_key = {
348 .lct_tags = LCT_SESSION,
349 .lct_init = mdd_ucred_key_init,
350 .lct_fini = mdd_ucred_key_fini
353 struct md_ucred *md_ucred(const struct lu_env *env)
355 LASSERT(env->le_ses != NULL);
356 return lu_context_key_get(env->le_ses, &mdd_ucred_key);
358 EXPORT_SYMBOL(md_ucred);
360 static void *mdd_capainfo_key_init(const struct lu_context *ctx,
361 struct lu_context_key *key)
363 struct md_capainfo *ci;
367 ci = ERR_PTR(-ENOMEM);
371 static void mdd_capainfo_key_fini(const struct lu_context *ctx,
372 struct lu_context_key *key, void *data)
374 struct md_capainfo *ci = data;
378 struct lu_context_key mdd_capainfo_key = {
379 .lct_tags = LCT_SESSION,
380 .lct_init = mdd_capainfo_key_init,
381 .lct_fini = mdd_capainfo_key_fini
384 struct md_capainfo *md_capainfo(const struct lu_env *env)
386 /* NB, in mdt_init0 */
387 if (env->le_ses == NULL)
389 return lu_context_key_get(env->le_ses, &mdd_capainfo_key);
391 EXPORT_SYMBOL(md_capainfo);
393 static int mdd_type_init(struct lu_device_type *t)
397 result = lu_context_key_register(&mdd_thread_key);
399 result = lu_context_key_register(&mdd_ucred_key);
401 result = lu_context_key_register(&mdd_capainfo_key);
405 static void mdd_type_fini(struct lu_device_type *t)
407 lu_context_key_degister(&mdd_capainfo_key);
408 lu_context_key_degister(&mdd_ucred_key);
409 lu_context_key_degister(&mdd_thread_key);
412 struct md_device_operations mdd_ops = {
413 .mdo_statfs = mdd_statfs,
414 .mdo_root_get = mdd_root_get,
415 .mdo_maxsize_get = mdd_maxsize_get,
416 .mdo_init_capa_ctxt = mdd_init_capa_ctxt,
417 .mdo_update_capa_key= mdd_update_capa_key,
420 static struct lu_device_type_operations mdd_device_type_ops = {
421 .ldto_init = mdd_type_init,
422 .ldto_fini = mdd_type_fini,
424 .ldto_device_alloc = mdd_device_alloc,
425 .ldto_device_free = mdd_device_free,
427 .ldto_device_init = mdd_device_init,
428 .ldto_device_fini = mdd_device_fini
431 static struct lu_device_type mdd_device_type = {
432 .ldt_tags = LU_DEVICE_MD,
433 .ldt_name = LUSTRE_MDD_NAME,
434 .ldt_ops = &mdd_device_type_ops,
435 .ldt_ctx_tags = LCT_MD_THREAD
438 static void *mdd_key_init(const struct lu_context *ctx,
439 struct lu_context_key *key)
441 struct mdd_thread_info *info;
445 info = ERR_PTR(-ENOMEM);
449 static void mdd_key_fini(const struct lu_context *ctx,
450 struct lu_context_key *key, void *data)
452 struct mdd_thread_info *info = data;
456 struct lu_context_key mdd_thread_key = {
457 .lct_tags = LCT_MD_THREAD,
458 .lct_init = mdd_key_init,
459 .lct_fini = mdd_key_fini
462 struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
466 struct lprocfs_vars lprocfs_mdd_module_vars[] = {
470 LPROCFS_INIT_VARS(mdd, lprocfs_mdd_module_vars, lprocfs_mdd_obd_vars);
472 static int __init mdd_mod_init(void)
474 struct lprocfs_static_vars lvars;
475 lprocfs_init_vars(mdd, &lvars);
476 return class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
477 LUSTRE_MDD_NAME, &mdd_device_type);
480 static void __exit mdd_mod_exit(void)
482 class_unregister_type(LUSTRE_MDD_NAME);
485 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
486 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD_NAME")");
487 MODULE_LICENSE("GPL");
489 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);