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/cmm/cmm_device.c
38 * Lustre Cluster Metadata Manager (cmm)
40 * Author: Mike Pershin <tappro@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
51 #include <obd_class.h>
52 #include <lprocfs_status.h>
53 #include <lustre_ver.h>
54 #include "cmm_internal.h"
55 #include "mdc_internal.h"
56 #ifdef HAVE_QUOTA_SUPPORT
57 # include <lustre_quota.h>
60 static struct obd_ops cmm_obd_device_ops = {
61 .o_owner = THIS_MODULE
64 static const struct lu_device_operations cmm_lu_ops;
66 static inline int lu_device_is_cmm(struct lu_device *d)
68 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &cmm_lu_ops);
71 int cmm_root_get(const struct lu_env *env, struct md_device *md,
74 struct cmm_device *cmm_dev = md2cmm_dev(md);
75 /* valid only on master MDS */
76 if (cmm_dev->cmm_local_num == 0)
77 return cmm_child_ops(cmm_dev)->mdo_root_get(env,
78 cmm_dev->cmm_child, fid);
83 static int cmm_statfs(const struct lu_env *env, struct md_device *md,
86 struct cmm_device *cmm_dev = md2cmm_dev(md);
90 rc = cmm_child_ops(cmm_dev)->mdo_statfs(env,
91 cmm_dev->cmm_child, sfs);
95 static int cmm_maxsize_get(const struct lu_env *env, struct md_device *md,
96 int *md_size, int *cookie_size)
98 struct cmm_device *cmm_dev = md2cmm_dev(md);
101 rc = cmm_child_ops(cmm_dev)->mdo_maxsize_get(env, cmm_dev->cmm_child,
102 md_size, cookie_size);
106 static int cmm_init_capa_ctxt(const struct lu_env *env, struct md_device *md,
107 int mode , unsigned long timeout, __u32 alg,
108 struct lustre_capa_key *keys)
110 struct cmm_device *cmm_dev = md2cmm_dev(md);
113 LASSERT(cmm_child_ops(cmm_dev)->mdo_init_capa_ctxt);
114 rc = cmm_child_ops(cmm_dev)->mdo_init_capa_ctxt(env, cmm_dev->cmm_child,
120 static int cmm_update_capa_key(const struct lu_env *env,
121 struct md_device *md,
122 struct lustre_capa_key *key)
124 struct cmm_device *cmm_dev = md2cmm_dev(md);
127 rc = cmm_child_ops(cmm_dev)->mdo_update_capa_key(env,
133 #ifdef HAVE_QUOTA_SUPPORT
134 static int cmm_quota_notify(const struct lu_env *env, struct md_device *m)
136 struct cmm_device *cmm_dev = md2cmm_dev(m);
140 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_notify(env,
145 static int cmm_quota_setup(const struct lu_env *env, struct md_device *m,
148 struct cmm_device *cmm_dev = md2cmm_dev(m);
152 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_setup(env,
158 static int cmm_quota_cleanup(const struct lu_env *env, struct md_device *m)
160 struct cmm_device *cmm_dev = md2cmm_dev(m);
164 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_cleanup(env,
169 static int cmm_quota_recovery(const struct lu_env *env, struct md_device *m)
171 struct cmm_device *cmm_dev = md2cmm_dev(m);
175 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_recovery(env,
180 static int cmm_quota_check(const struct lu_env *env, struct md_device *m,
181 struct obd_export *exp, __u32 type)
183 struct cmm_device *cmm_dev = md2cmm_dev(m);
187 /* disable quota for CMD case temporary. */
188 if (cmm_dev->cmm_tgt_count)
191 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_check(env,
197 static int cmm_quota_on(const struct lu_env *env, struct md_device *m,
200 struct cmm_device *cmm_dev = md2cmm_dev(m);
204 /* disable quota for CMD case temporary. */
205 if (cmm_dev->cmm_tgt_count)
208 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_on(env,
214 static int cmm_quota_off(const struct lu_env *env, struct md_device *m,
217 struct cmm_device *cmm_dev = md2cmm_dev(m);
221 /* disable quota for CMD case temporary. */
222 if (cmm_dev->cmm_tgt_count)
225 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_off(env,
231 static int cmm_quota_setinfo(const struct lu_env *env, struct md_device *m,
232 __u32 type, __u32 id, struct obd_dqinfo *dqinfo)
234 struct cmm_device *cmm_dev = md2cmm_dev(m);
238 /* disable quota for CMD case temporary. */
239 if (cmm_dev->cmm_tgt_count)
242 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_setinfo(env,
248 static int cmm_quota_getinfo(const struct lu_env *env,
249 const struct md_device *m,
250 __u32 type, __u32 id, struct obd_dqinfo *dqinfo)
252 struct cmm_device *cmm_dev = md2cmm_dev((struct md_device *)m);
256 /* disable quota for CMD case temporary. */
257 if (cmm_dev->cmm_tgt_count)
260 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_getinfo(env,
266 static int cmm_quota_setquota(const struct lu_env *env, struct md_device *m,
267 __u32 type, __u32 id, struct obd_dqblk *dqblk)
269 struct cmm_device *cmm_dev = md2cmm_dev(m);
273 /* disable quota for CMD case temporary. */
274 if (cmm_dev->cmm_tgt_count)
277 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_setquota(env,
283 static int cmm_quota_getquota(const struct lu_env *env,
284 const struct md_device *m,
285 __u32 type, __u32 id, struct obd_dqblk *dqblk)
287 struct cmm_device *cmm_dev = md2cmm_dev((struct md_device *)m);
291 /* disable quota for CMD case temporary. */
292 if (cmm_dev->cmm_tgt_count)
295 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_getquota(env,
301 static int cmm_quota_getoinfo(const struct lu_env *env,
302 const struct md_device *m,
303 __u32 type, __u32 id, struct obd_dqinfo *dqinfo)
305 struct cmm_device *cmm_dev = md2cmm_dev((struct md_device *)m);
309 /* disable quota for CMD case temporary. */
310 if (cmm_dev->cmm_tgt_count)
313 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_getoinfo(env,
319 static int cmm_quota_getoquota(const struct lu_env *env,
320 const struct md_device *m,
321 __u32 type, __u32 id, struct obd_dqblk *dqblk)
323 struct cmm_device *cmm_dev = md2cmm_dev((struct md_device *)m);
327 /* disable quota for CMD case temporary. */
328 if (cmm_dev->cmm_tgt_count)
331 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_getoquota(env,
337 static int cmm_quota_invalidate(const struct lu_env *env, struct md_device *m,
340 struct cmm_device *cmm_dev = md2cmm_dev(m);
344 /* disable quota for CMD case temporary. */
345 if (cmm_dev->cmm_tgt_count)
348 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_invalidate(env,
354 static int cmm_quota_finvalidate(const struct lu_env *env, struct md_device *m,
357 struct cmm_device *cmm_dev = md2cmm_dev(m);
361 /* disable quota for CMD case temporary. */
362 if (cmm_dev->cmm_tgt_count)
365 rc = cmm_child_ops(cmm_dev)->mdo_quota.mqo_finvalidate(env,
372 static const struct md_device_operations cmm_md_ops = {
373 .mdo_statfs = cmm_statfs,
374 .mdo_root_get = cmm_root_get,
375 .mdo_maxsize_get = cmm_maxsize_get,
376 .mdo_init_capa_ctxt = cmm_init_capa_ctxt,
377 .mdo_update_capa_key = cmm_update_capa_key,
378 #ifdef HAVE_QUOTA_SUPPORT
380 .mqo_notify = cmm_quota_notify,
381 .mqo_setup = cmm_quota_setup,
382 .mqo_cleanup = cmm_quota_cleanup,
383 .mqo_recovery = cmm_quota_recovery,
384 .mqo_check = cmm_quota_check,
385 .mqo_on = cmm_quota_on,
386 .mqo_off = cmm_quota_off,
387 .mqo_setinfo = cmm_quota_setinfo,
388 .mqo_getinfo = cmm_quota_getinfo,
389 .mqo_setquota = cmm_quota_setquota,
390 .mqo_getquota = cmm_quota_getquota,
391 .mqo_getoinfo = cmm_quota_getoinfo,
392 .mqo_getoquota = cmm_quota_getoquota,
393 .mqo_invalidate = cmm_quota_invalidate,
394 .mqo_finvalidate = cmm_quota_finvalidate
399 extern struct lu_device_type mdc_device_type;
401 static int cmm_post_init_mdc(const struct lu_env *env,
402 struct cmm_device *cmm)
404 int max_mdsize, max_cookiesize, rc;
405 struct mdc_device *mc, *tmp;
407 /* get the max mdsize and cookiesize from lower layer */
408 rc = cmm_maxsize_get(env, &cmm->cmm_md_dev, &max_mdsize,
413 spin_lock(&cmm->cmm_tgt_guard);
414 list_for_each_entry_safe(mc, tmp, &cmm->cmm_targets,
416 cmm_mdc_init_ea_size(env, mc, max_mdsize, max_cookiesize);
418 spin_unlock(&cmm->cmm_tgt_guard);
422 /* --- cmm_lu_operations --- */
423 /* add new MDC to the CMM, create MDC lu_device and connect it to mdc_obd */
424 static int cmm_add_mdc(const struct lu_env *env,
425 struct cmm_device *cm, struct lustre_cfg *cfg)
427 struct lu_device_type *ldt = &mdc_device_type;
428 char *p, *num = lustre_cfg_string(cfg, 2);
429 struct mdc_device *mc, *tmp;
430 struct lu_fld_target target;
431 struct lu_device *ld;
432 struct lu_device *cmm_lu = cmm2lu_dev(cm);
434 struct lu_site *site = cmm2lu_dev(cm)->ld_site;
436 #ifdef HAVE_QUOTA_SUPPORT
441 /* find out that there is no such mdc */
443 mdc_num = simple_strtol(num, &p, 10);
445 CERROR("Invalid index in lustre_cgf, offset 2\n");
449 spin_lock(&cm->cmm_tgt_guard);
450 list_for_each_entry_safe(mc, tmp, &cm->cmm_targets,
452 if (mc->mc_num == mdc_num) {
453 spin_unlock(&cm->cmm_tgt_guard);
457 spin_unlock(&cm->cmm_tgt_guard);
458 ld = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
464 rc = ldt->ldt_ops->ldto_device_init(env, ld, NULL, NULL);
466 ldt->ldt_ops->ldto_device_free(env, ld);
469 /* pass config to the just created MDC */
470 rc = ld->ld_ops->ldo_process_config(env, ld, cfg);
472 ldt->ldt_ops->ldto_device_fini(env, ld);
473 ldt->ldt_ops->ldto_device_free(env, ld);
477 spin_lock(&cm->cmm_tgt_guard);
478 list_for_each_entry_safe(mc, tmp, &cm->cmm_targets,
480 if (mc->mc_num == mdc_num) {
481 spin_unlock(&cm->cmm_tgt_guard);
482 ldt->ldt_ops->ldto_device_fini(env, ld);
483 ldt->ldt_ops->ldto_device_free(env, ld);
488 list_add_tail(&mc->mc_linkage, &cm->cmm_targets);
490 #ifdef HAVE_QUOTA_SUPPORT
491 first = cm->cmm_tgt_count;
493 spin_unlock(&cm->cmm_tgt_guard);
495 lu_device_get(cmm_lu);
496 lu_ref_add(&cmm_lu->ld_reference, "mdc-child", ld);
498 target.ft_srv = NULL;
499 target.ft_idx = mc->mc_num;
500 target.ft_exp = mc->mc_desc.cl_exp;
501 fld_client_add_target(cm->cmm_fld, &target);
503 if (mc->mc_num == 0) {
504 /* this is mdt0 -> mc export, fld lookup need this export
505 to forward fld lookup request. */
506 LASSERT(!lu_site2md(site)->ms_server_fld->lsf_control_exp);
507 lu_site2md(site)->ms_server_fld->lsf_control_exp =
510 #ifdef HAVE_QUOTA_SUPPORT
511 /* XXX: Disable quota for CMD case temporary. */
513 CWARN("Disable quota for CMD case temporary!\n");
514 cmm_child_ops(cm)->mdo_quota.mqo_off(env, cm->cmm_child, UGQUOTA);
517 /* Set max md size for the mdc. */
518 rc = cmm_post_init_mdc(env, cm);
522 static void cmm_device_shutdown(const struct lu_env *env,
523 struct cmm_device *cm,
524 struct lustre_cfg *cfg)
526 struct mdc_device *mc, *tmp;
529 /* Remove local target from FLD. */
530 fld_client_del_target(cm->cmm_fld, cm->cmm_local_num);
532 /* Finish all mdc devices. */
533 spin_lock(&cm->cmm_tgt_guard);
534 list_for_each_entry_safe(mc, tmp, &cm->cmm_targets, mc_linkage) {
535 struct lu_device *ld_m = mdc2lu_dev(mc);
536 fld_client_del_target(cm->cmm_fld, mc->mc_num);
537 ld_m->ld_ops->ldo_process_config(env, ld_m, cfg);
539 spin_unlock(&cm->cmm_tgt_guard);
541 /* remove upcall device*/
542 md_upcall_fini(&cm->cmm_md_dev);
547 static int cmm_device_mount(const struct lu_env *env,
548 struct cmm_device *m, struct lustre_cfg *cfg)
550 const char *index = lustre_cfg_string(cfg, 2);
553 LASSERT(index != NULL);
555 m->cmm_local_num = simple_strtol(index, &p, 10);
557 CERROR("Invalid index in lustre_cgf\n");
564 static int cmm_process_config(const struct lu_env *env,
565 struct lu_device *d, struct lustre_cfg *cfg)
567 struct cmm_device *m = lu2cmm_dev(d);
568 struct lu_device *next = md2lu_dev(m->cmm_child);
572 switch(cfg->lcfg_command) {
574 /* On first ADD_MDC add also local target. */
575 if (!(m->cmm_flags & CMM_INITIALIZED)) {
576 struct lu_site *ls = cmm2lu_dev(m)->ld_site;
577 struct lu_fld_target target;
579 target.ft_srv = lu_site2md(ls)->ms_server_fld;
580 target.ft_idx = m->cmm_local_num;
581 target.ft_exp = NULL;
583 fld_client_add_target(m->cmm_fld, &target);
585 err = cmm_add_mdc(env, m, cfg);
587 /* The first ADD_MDC can be counted as setup is finished. */
588 if (!(m->cmm_flags & CMM_INITIALIZED))
589 m->cmm_flags |= CMM_INITIALIZED;
594 /* lower layers should be set up at first */
595 err = next->ld_ops->ldo_process_config(env, next, cfg);
597 err = cmm_device_mount(env, m, cfg);
602 cmm_device_shutdown(env, m, cfg);
605 err = next->ld_ops->ldo_process_config(env, next, cfg);
610 static int cmm_recovery_complete(const struct lu_env *env,
613 struct cmm_device *m = lu2cmm_dev(d);
614 struct lu_device *next = md2lu_dev(m->cmm_child);
617 rc = next->ld_ops->ldo_recovery_complete(env, next);
621 static int cmm_prepare(const struct lu_env *env,
622 struct lu_device *pdev,
623 struct lu_device *dev)
625 struct cmm_device *cmm = lu2cmm_dev(dev);
626 struct lu_device *next = md2lu_dev(cmm->cmm_child);
630 rc = next->ld_ops->ldo_prepare(env, dev, next);
634 static const struct lu_device_operations cmm_lu_ops = {
635 .ldo_object_alloc = cmm_object_alloc,
636 .ldo_process_config = cmm_process_config,
637 .ldo_recovery_complete = cmm_recovery_complete,
638 .ldo_prepare = cmm_prepare,
641 /* --- lu_device_type operations --- */
642 int cmm_upcall(const struct lu_env *env, struct md_device *md,
643 enum md_upcall_event ev)
650 rc = cmm_post_init_mdc(env, md2cmm_dev(md));
652 CERROR("can not init md size %d\n", rc);
655 rc = md_do_upcall(env, md, ev);
660 static struct lu_device *cmm_device_free(const struct lu_env *env,
663 struct cmm_device *m = lu2cmm_dev(d);
664 struct lu_device *next = md2lu_dev(m->cmm_child);
667 LASSERT(m->cmm_tgt_count == 0);
668 LASSERT(list_empty(&m->cmm_targets));
669 if (m->cmm_fld != NULL) {
670 OBD_FREE_PTR(m->cmm_fld);
673 md_device_fini(&m->cmm_md_dev);
678 static struct lu_device *cmm_device_alloc(const struct lu_env *env,
679 struct lu_device_type *t,
680 struct lustre_cfg *cfg)
683 struct cmm_device *m;
688 l = ERR_PTR(-ENOMEM);
690 md_device_init(&m->cmm_md_dev, t);
691 m->cmm_md_dev.md_ops = &cmm_md_ops;
692 md_upcall_init(&m->cmm_md_dev, cmm_upcall);
694 l->ld_ops = &cmm_lu_ops;
696 OBD_ALLOC_PTR(m->cmm_fld);
698 cmm_device_free(env, l);
699 l = ERR_PTR(-ENOMEM);
705 /* context key constructor/destructor: cmm_key_init, cmm_key_fini */
706 LU_KEY_INIT_FINI(cmm, struct cmm_thread_info);
708 /* context key: cmm_thread_key */
709 LU_CONTEXT_KEY_DEFINE(cmm, LCT_MD_THREAD);
711 struct cmm_thread_info *cmm_env_info(const struct lu_env *env)
713 struct cmm_thread_info *info;
715 info = lu_context_key_get(&env->le_ctx, &cmm_thread_key);
716 LASSERT(info != NULL);
720 /* type constructor/destructor: cmm_type_init/cmm_type_fini */
721 LU_TYPE_INIT_FINI(cmm, &cmm_thread_key);
723 static int cmm_device_init(const struct lu_env *env, struct lu_device *d,
724 const char *name, struct lu_device *next)
726 struct cmm_device *m = lu2cmm_dev(d);
731 spin_lock_init(&m->cmm_tgt_guard);
732 CFS_INIT_LIST_HEAD(&m->cmm_targets);
733 m->cmm_tgt_count = 0;
734 m->cmm_child = lu2md_dev(next);
736 err = fld_client_init(m->cmm_fld, name,
737 LUSTRE_CLI_FLD_HASH_DHT);
739 CERROR("Can't init FLD, err %d\n", err);
743 /* Assign site's fld client ref, needed for asserts in osd. */
744 ls = cmm2lu_dev(m)->ld_site;
745 lu_site2md(ls)->ms_client_fld = m->cmm_fld;
746 err = cmm_procfs_init(m, name);
751 static struct lu_device *cmm_device_fini(const struct lu_env *env,
752 struct lu_device *ld)
754 struct cmm_device *cm = lu2cmm_dev(ld);
755 struct mdc_device *mc, *tmp;
759 /* Finish all mdc devices */
760 spin_lock(&cm->cmm_tgt_guard);
761 list_for_each_entry_safe(mc, tmp, &cm->cmm_targets, mc_linkage) {
762 struct lu_device *ld_m = mdc2lu_dev(mc);
763 struct lu_device *ld_c = cmm2lu_dev(cm);
765 list_del_init(&mc->mc_linkage);
766 lu_ref_del(&ld_c->ld_reference, "mdc-child", ld_m);
768 ld_m->ld_type->ldt_ops->ldto_device_fini(env, ld_m);
769 ld_m->ld_type->ldt_ops->ldto_device_free(env, ld_m);
772 spin_unlock(&cm->cmm_tgt_guard);
774 fld_client_fini(cm->cmm_fld);
775 ls = cmm2lu_dev(cm)->ld_site;
776 lu_site2md(ls)->ms_client_fld = NULL;
779 RETURN (md2lu_dev(cm->cmm_child));
782 static struct lu_device_type_operations cmm_device_type_ops = {
783 .ldto_init = cmm_type_init,
784 .ldto_fini = cmm_type_fini,
786 .ldto_start = cmm_type_start,
787 .ldto_stop = cmm_type_stop,
789 .ldto_device_alloc = cmm_device_alloc,
790 .ldto_device_free = cmm_device_free,
792 .ldto_device_init = cmm_device_init,
793 .ldto_device_fini = cmm_device_fini
796 static struct lu_device_type cmm_device_type = {
797 .ldt_tags = LU_DEVICE_MD,
798 .ldt_name = LUSTRE_CMM_NAME,
799 .ldt_ops = &cmm_device_type_ops,
800 .ldt_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
803 struct lprocfs_vars lprocfs_cmm_obd_vars[] = {
807 struct lprocfs_vars lprocfs_cmm_module_vars[] = {
811 static void lprocfs_cmm_init_vars(struct lprocfs_static_vars *lvars)
813 lvars->module_vars = lprocfs_cmm_module_vars;
814 lvars->obd_vars = lprocfs_cmm_obd_vars;
817 static int __init cmm_mod_init(void)
819 struct lprocfs_static_vars lvars;
821 lprocfs_cmm_init_vars(&lvars);
822 return class_register_type(&cmm_obd_device_ops, NULL, lvars.module_vars,
823 LUSTRE_CMM_NAME, &cmm_device_type);
826 static void __exit cmm_mod_exit(void)
828 class_unregister_type(LUSTRE_CMM_NAME);
831 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
832 MODULE_DESCRIPTION("Lustre Clustered Metadata Manager ("LUSTRE_CMM_NAME")");
833 MODULE_LICENSE("GPL");
835 cfs_module(cmm, "0.1.0", cmm_mod_init, cmm_mod_exit);