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/mdd/mdd_device.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
49 #include <linux/jbd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
56 #include <lustre_disk.h>
57 #include <lustre_fid.h>
58 #include <linux/ldiskfs_fs.h>
59 #include <lustre_mds.h>
60 #include <lustre/lustre_idl.h>
61 #include <lustre_disk.h> /* for changelogs */
62 #include <lustre_param.h>
64 #include "mdd_internal.h"
66 const struct md_device_operations mdd_ops;
68 static const char mdd_root_dir_name[] = "ROOT";
70 static int mdd_device_init(const struct lu_env *env, struct lu_device *d,
71 const char *name, struct lu_device *next)
73 struct mdd_device *mdd = lu2mdd_dev(d);
77 mdd->mdd_child = lu2dt_dev(next);
79 /* Prepare transactions callbacks. */
80 mdd->mdd_txn_cb.dtc_txn_start = mdd_txn_start_cb;
81 mdd->mdd_txn_cb.dtc_txn_stop = mdd_txn_stop_cb;
82 mdd->mdd_txn_cb.dtc_txn_commit = mdd_txn_commit_cb;
83 mdd->mdd_txn_cb.dtc_cookie = mdd;
84 CFS_INIT_LIST_HEAD(&mdd->mdd_txn_cb.dtc_linkage);
85 mdd->mdd_atime_diff = MAX_ATIME_DIFF;
87 rc = mdd_procfs_init(mdd, name);
91 static struct lu_device *mdd_device_fini(const struct lu_env *env,
94 struct mdd_device *mdd = lu2mdd_dev(d);
95 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
98 rc = mdd_procfs_fini(mdd);
100 CERROR("proc fini error %d \n", rc);
106 static void mdd_changelog_fini(const struct lu_env *env,
107 struct mdd_device *mdd);
109 static void mdd_device_shutdown(const struct lu_env *env,
110 struct mdd_device *m, struct lustre_cfg *cfg)
113 mdd_changelog_fini(env, m);
114 dt_txn_callback_del(m->mdd_child, &m->mdd_txn_cb);
116 mdd_fini_obd(env, m, cfg);
117 orph_index_fini(env, m);
118 /* remove upcall device*/
119 md_upcall_fini(&m->mdd_md_dev);
123 static int changelog_init_cb(struct llog_handle *llh, struct llog_rec_hdr *hdr,
126 struct mdd_device *mdd = (struct mdd_device *)data;
127 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
130 if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
131 CERROR("log is not plain\n");
134 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
135 CERROR("Not a changelog rec? %d\n", rec->cr_hdr.lrh_type);
140 "seeing record at index %d/%d/"LPU64" t=%x %.*s in log "LPX64"\n",
141 hdr->lrh_index, rec->cr_hdr.lrh_index, rec->cr_index,
142 rec->cr_type, rec->cr_namelen, rec->cr_name,
143 llh->lgh_id.lgl_oid);
145 mdd->mdd_cl.mc_index = rec->cr_index;
146 RETURN(LLOG_PROC_BREAK);
149 static int mdd_changelog_llog_init(struct mdd_device *mdd)
151 struct obd_device *obd = mdd2obd_dev(mdd);
152 struct llog_ctxt *ctxt;
155 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
157 CERROR("no context\n");
160 if (!ctxt->loc_handle) {
161 CERROR("no handle\n");
164 rc = llog_cat_reverse_process(ctxt->loc_handle, changelog_init_cb, mdd);
168 CERROR("changelog init failed: %d\n", rc);
170 rc = 0; /* llog_proc_break is ok */
172 CDEBUG(D_INODE, "changelog_init index="LPU64"\n", mdd->mdd_cl.mc_index);
177 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
181 mdd->mdd_cl.mc_index = 0;
182 spin_lock_init(&mdd->mdd_cl.mc_lock);
183 cfs_waitq_init(&mdd->mdd_cl.mc_waitq);
185 mdd->mdd_cl.mc_starttime = cfs_time_current_64();
186 mdd->mdd_cl.mc_flags = 0; /* off by default */
187 mdd->mdd_cl.mc_mask = CL_DEFMASK;
188 rc = mdd_changelog_llog_init(mdd);
190 CERROR("Changelog setup during init failed %d\n", rc);
191 mdd->mdd_cl.mc_flags |= CLM_ERR;
196 static void mdd_changelog_fini(const struct lu_env *env, struct mdd_device *mdd)
198 mdd->mdd_cl.mc_flags = 0;
201 /** Add a changelog entry \a rec to the changelog llog
204 * \param handle - currently ignored since llogs start their own transaction;
205 * this will hopefully be fixed in llog rewrite
208 int mdd_changelog_llog_write(struct mdd_device *mdd,
209 struct llog_changelog_rec *rec,
210 struct thandle *handle)
212 struct obd_device *obd = mdd2obd_dev(mdd);
213 struct llog_ctxt *ctxt;
216 if ((mdd->mdd_cl.mc_mask & (1 << rec->cr_type)) == 0)
219 rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr_namelen);
220 /* llog_lvfs_write_rec sets the llog tail len */
221 rec->cr_hdr.lrh_type = CHANGELOG_REC;
222 rec->cr_time = cfs_time_current_64();
223 spin_lock(&mdd->mdd_cl.mc_lock);
224 /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
225 but as long as the MDD transactions are ordered correctly for e.g.
226 rename conflicts, I don't think this should matter. */
227 rec->cr_index = ++mdd->mdd_cl.mc_index;
228 spin_unlock(&mdd->mdd_cl.mc_lock);
229 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
233 /* nested journal transaction */
234 rc = llog_add(ctxt, &rec->cr_hdr, NULL, NULL, 0);
237 cfs_waitq_signal(&mdd->mdd_cl.mc_waitq);
242 /** Remove entries with indicies up to and including \a endrec from the
248 int mdd_changelog_llog_cancel(struct mdd_device *mdd, long long endrec)
250 struct obd_device *obd = mdd2obd_dev(mdd);
251 struct llog_ctxt *ctxt;
254 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
258 /* Some records purged; reset repeat-access time */
259 mdd->mdd_cl.mc_starttime = cfs_time_current_64();
261 rc = llog_cancel(ctxt, NULL, 1, (struct llog_cookie *)&endrec, 0);
268 /** Add a CL_MARK record to the changelog
270 * \param markerflags - CLM_*
273 int mdd_changelog_write_header(struct mdd_device *mdd, int markerflags)
275 struct obd_device *obd = mdd2obd_dev(mdd);
276 struct llog_changelog_rec *rec;
278 int len = strlen(obd->obd_name);
282 reclen = llog_data_len(sizeof(*rec) + len);
283 OBD_ALLOC(rec, reclen);
287 rec->cr_flags = CLF_VERSION;
288 rec->cr_type = CL_MARK;
289 rec->cr_namelen = len;
290 memcpy(rec->cr_name, obd->obd_name, rec->cr_namelen);
291 /* Status and action flags */
292 rec->cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
294 rc = mdd_changelog_llog_write(mdd, rec, NULL);
296 /* assume on or off event; reset repeat-access time */
297 mdd->mdd_cl.mc_starttime = rec->cr_time;
299 OBD_FREE(rec, reclen);
303 static int mdd_process_config(const struct lu_env *env,
304 struct lu_device *d, struct lustre_cfg *cfg)
306 struct mdd_device *m = lu2mdd_dev(d);
307 struct dt_device *dt = m->mdd_child;
308 struct lu_device *next = &dt->dd_lu_dev;
312 switch (cfg->lcfg_command) {
314 struct lprocfs_static_vars lvars;
316 lprocfs_mdd_init_vars(&lvars);
317 rc = class_process_proc_param(PARAM_MDD, lvars.obd_vars, cfg,m);
318 if (rc > 0 || rc == -ENOSYS)
319 /* we don't understand; pass it on */
320 rc = next->ld_ops->ldo_process_config(env, next, cfg);
324 rc = next->ld_ops->ldo_process_config(env, next, cfg);
327 dt->dd_ops->dt_conf_get(env, dt, &m->mdd_dt_conf);
329 rc = mdd_init_obd(env, m, cfg);
331 CERROR("lov init error %d \n", rc);
334 rc = mdd_txn_init_credits(env, m);
338 mdd_changelog_init(env, m);
341 mdd_device_shutdown(env, m, cfg);
343 rc = next->ld_ops->ldo_process_config(env, next, cfg);
351 static int mdd_lov_set_nextid(const struct lu_env *env,
352 struct mdd_device *mdd)
354 struct mds_obd *mds = &mdd->mdd_obd_dev->u.mds;
358 LASSERT(mds->mds_lov_objids != NULL);
359 rc = obd_set_info_async(mds->mds_osc_exp, strlen(KEY_NEXT_ID),
360 KEY_NEXT_ID, mds->mds_lov_desc.ld_tgt_count,
361 mds->mds_lov_objids, NULL);
366 static int mdd_cleanup_unlink_llog(const struct lu_env *env,
367 struct mdd_device *mdd)
369 /* XXX: to be implemented! */
374 static int mdd_recovery_complete(const struct lu_env *env,
377 struct mdd_device *mdd = lu2mdd_dev(d);
378 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
379 struct obd_device *obd = mdd2obd_dev(mdd);
383 LASSERT(mdd != NULL);
384 LASSERT(obd != NULL);
386 /* XXX: Do we need this in new stack? */
387 rc = mdd_lov_set_nextid(env, mdd);
389 CERROR("mdd_lov_set_nextid() failed %d\n",
394 /* XXX: cleanup unlink. */
395 rc = mdd_cleanup_unlink_llog(env, mdd);
397 CERROR("mdd_cleanup_unlink_llog() failed %d\n",
402 /* Call that with obd_recovering = 1 just to update objids */
403 obd_notify(obd->u.mds.mds_osc_obd, NULL, (obd->obd_async_recov ?
404 OBD_NOTIFY_SYNC_NONBLOCK : OBD_NOTIFY_SYNC), NULL);
406 /* Drop obd_recovering to 0 and call o_postrecov to recover mds_lov */
407 obd->obd_recovering = 0;
408 obd->obd_type->typ_dt_ops->o_postrecov(obd);
410 /* XXX: orphans handling. */
411 __mdd_orphan_cleanup(env, mdd);
412 rc = next->ld_ops->ldo_recovery_complete(env, next);
417 static int mdd_prepare(const struct lu_env *env,
418 struct lu_device *pdev,
419 struct lu_device *cdev)
421 struct mdd_device *mdd = lu2mdd_dev(cdev);
422 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
423 struct dt_object *root;
427 rc = next->ld_ops->ldo_prepare(env, cdev, next);
431 dt_txn_callback_add(mdd->mdd_child, &mdd->mdd_txn_cb);
432 root = dt_store_open(env, mdd->mdd_child, "", mdd_root_dir_name,
435 LASSERT(root != NULL);
436 lu_object_put(env, &root->do_lu);
437 rc = orph_index_init(env, mdd);
445 const struct lu_device_operations mdd_lu_ops = {
446 .ldo_object_alloc = mdd_object_alloc,
447 .ldo_process_config = mdd_process_config,
448 .ldo_recovery_complete = mdd_recovery_complete,
449 .ldo_prepare = mdd_prepare,
453 * No permission check is needed.
455 static int mdd_root_get(const struct lu_env *env,
456 struct md_device *m, struct lu_fid *f)
458 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
461 *f = mdd->mdd_root_fid;
466 * No permission check is needed.
468 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
471 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
476 rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
482 * No permission check is needed.
484 static int mdd_maxsize_get(const struct lu_env *env, struct md_device *m,
485 int *md_size, int *cookie_size)
487 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
490 *md_size = mdd_lov_mdsize(env, mdd);
491 *cookie_size = mdd_lov_cookiesize(env, mdd);
496 static int mdd_init_capa_ctxt(const struct lu_env *env, struct md_device *m,
497 int mode, unsigned long timeout, __u32 alg,
498 struct lustre_capa_key *keys)
500 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
501 struct mds_obd *mds = &mdd2obd_dev(mdd)->u.mds;
505 mds->mds_capa_keys = keys;
506 rc = mdd_child_ops(mdd)->dt_init_capa_ctxt(env, mdd->mdd_child, mode,
511 static int mdd_update_capa_key(const struct lu_env *env,
513 struct lustre_capa_key *key)
515 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
516 struct obd_export *lov_exp = mdd2obd_dev(mdd)->u.mds.mds_osc_exp;
520 rc = obd_set_info_async(lov_exp, sizeof(KEY_CAPA_KEY), KEY_CAPA_KEY,
521 sizeof(*key), key, NULL);
525 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
526 struct lu_device_type *t,
527 struct lustre_cfg *lcfg)
530 struct mdd_device *m;
534 l = ERR_PTR(-ENOMEM);
536 md_device_init(&m->mdd_md_dev, t);
538 l->ld_ops = &mdd_lu_ops;
539 m->mdd_md_dev.md_ops = &mdd_ops;
540 md_upcall_init(&m->mdd_md_dev, NULL);
546 static struct lu_device *mdd_device_free(const struct lu_env *env,
547 struct lu_device *lu)
549 struct mdd_device *m = lu2mdd_dev(lu);
550 struct lu_device *next = &m->mdd_child->dd_lu_dev;
553 LASSERT(atomic_read(&lu->ld_ref) == 0);
554 md_device_fini(&m->mdd_md_dev);
559 static struct obd_ops mdd_obd_device_ops = {
560 .o_owner = THIS_MODULE
563 /* context key constructor/destructor: mdd_ucred_key_init, mdd_ucred_key_fini */
564 LU_KEY_INIT_FINI(mdd_ucred, struct md_ucred);
566 static struct lu_context_key mdd_ucred_key = {
567 .lct_tags = LCT_SESSION,
568 .lct_init = mdd_ucred_key_init,
569 .lct_fini = mdd_ucred_key_fini
572 struct md_ucred *md_ucred(const struct lu_env *env)
574 LASSERT(env->le_ses != NULL);
575 return lu_context_key_get(env->le_ses, &mdd_ucred_key);
577 EXPORT_SYMBOL(md_ucred);
580 * context key constructor/destructor:
581 * mdd_capainfo_key_init, mdd_capainfo_key_fini
583 LU_KEY_INIT_FINI(mdd_capainfo, struct md_capainfo);
585 struct lu_context_key mdd_capainfo_key = {
586 .lct_tags = LCT_SESSION,
587 .lct_init = mdd_capainfo_key_init,
588 .lct_fini = mdd_capainfo_key_fini
591 struct md_capainfo *md_capainfo(const struct lu_env *env)
593 /* NB, in mdt_init0 */
594 if (env->le_ses == NULL)
596 return lu_context_key_get(env->le_ses, &mdd_capainfo_key);
598 EXPORT_SYMBOL(md_capainfo);
600 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
601 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key, &mdd_ucred_key, &mdd_capainfo_key);
603 const struct md_device_operations mdd_ops = {
604 .mdo_statfs = mdd_statfs,
605 .mdo_root_get = mdd_root_get,
606 .mdo_maxsize_get = mdd_maxsize_get,
607 .mdo_init_capa_ctxt = mdd_init_capa_ctxt,
608 .mdo_update_capa_key= mdd_update_capa_key,
609 #ifdef HAVE_QUOTA_SUPPORT
611 .mqo_notify = mdd_quota_notify,
612 .mqo_setup = mdd_quota_setup,
613 .mqo_cleanup = mdd_quota_cleanup,
614 .mqo_recovery = mdd_quota_recovery,
615 .mqo_check = mdd_quota_check,
616 .mqo_on = mdd_quota_on,
617 .mqo_off = mdd_quota_off,
618 .mqo_setinfo = mdd_quota_setinfo,
619 .mqo_getinfo = mdd_quota_getinfo,
620 .mqo_setquota = mdd_quota_setquota,
621 .mqo_getquota = mdd_quota_getquota,
622 .mqo_getoinfo = mdd_quota_getoinfo,
623 .mqo_getoquota = mdd_quota_getoquota,
624 .mqo_invalidate = mdd_quota_invalidate,
625 .mqo_finvalidate = mdd_quota_finvalidate
630 static struct lu_device_type_operations mdd_device_type_ops = {
631 .ldto_init = mdd_type_init,
632 .ldto_fini = mdd_type_fini,
634 .ldto_start = mdd_type_start,
635 .ldto_stop = mdd_type_stop,
637 .ldto_device_alloc = mdd_device_alloc,
638 .ldto_device_free = mdd_device_free,
640 .ldto_device_init = mdd_device_init,
641 .ldto_device_fini = mdd_device_fini
644 static struct lu_device_type mdd_device_type = {
645 .ldt_tags = LU_DEVICE_MD,
646 .ldt_name = LUSTRE_MDD_NAME,
647 .ldt_ops = &mdd_device_type_ops,
648 .ldt_ctx_tags = LCT_MD_THREAD
651 /* context key constructor: mdd_key_init */
652 LU_KEY_INIT(mdd, struct mdd_thread_info);
654 static void mdd_key_fini(const struct lu_context *ctx,
655 struct lu_context_key *key, void *data)
657 struct mdd_thread_info *info = data;
658 if (info->mti_max_lmm != NULL)
659 OBD_FREE(info->mti_max_lmm, info->mti_max_lmm_size);
660 if (info->mti_max_cookie != NULL)
661 OBD_FREE(info->mti_max_cookie, info->mti_max_cookie_size);
662 mdd_buf_put(&info->mti_big_buf);
667 /* context key: mdd_thread_key */
668 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
670 static struct lu_local_obj_desc llod_capa_key = {
671 .llod_name = CAPA_KEYS,
672 .llod_oid = MDD_CAPA_KEYS_OID,
676 static struct lu_local_obj_desc llod_mdd_orphan = {
677 .llod_name = orph_index_name,
678 .llod_oid = MDD_ORPHAN_OID,
680 .llod_feat = &dt_directory_features,
683 static struct lu_local_obj_desc llod_mdd_root = {
684 .llod_name = mdd_root_dir_name,
685 .llod_oid = MDD_ROOT_INDEX_OID,
687 .llod_feat = &dt_directory_features,
690 static int __init mdd_mod_init(void)
692 struct lprocfs_static_vars lvars;
693 lprocfs_mdd_init_vars(&lvars);
695 llo_local_obj_register(&llod_capa_key);
696 llo_local_obj_register(&llod_mdd_orphan);
697 llo_local_obj_register(&llod_mdd_root);
699 return class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
700 LUSTRE_MDD_NAME, &mdd_device_type);
703 static void __exit mdd_mod_exit(void)
705 llo_local_obj_unregister(&llod_capa_key);
706 llo_local_obj_unregister(&llod_mdd_orphan);
707 llo_local_obj_unregister(&llod_mdd_root);
709 class_unregister_type(LUSTRE_MDD_NAME);
712 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
713 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD_NAME")");
714 MODULE_LICENSE("GPL");
716 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);