4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdd/mdd_device.c
34 * Lustre Metadata Server (mdd) routines
36 * Author: Wang Di <wangdi@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <linux/module.h>
42 #include <linux/kthread.h>
43 #include <obd_class.h>
44 #include <uapi/linux/lustre/lustre_ioctl.h>
45 #include <lustre_mds.h>
46 #include <obd_support.h>
47 #include <lu_object.h>
48 #include <uapi/linux/lustre/lustre_param.h>
49 #include <lustre_fid.h>
50 #include <lustre_nodemap.h>
51 #include <lustre_barrier.h>
53 #include "mdd_internal.h"
55 static const struct md_device_operations mdd_ops;
56 static struct lu_device_type mdd_device_type;
58 static const char mdd_root_dir_name[] = "ROOT";
59 static const char mdd_obf_dir_name[] = "fid";
60 static const char mdd_lpf_dir_name[] = "lost+found";
62 /* Slab for MDD object allocation */
63 struct kmem_cache *mdd_object_kmem;
65 static struct lu_kmem_descr mdd_caches[] = {
67 .ckd_cache = &mdd_object_kmem,
68 .ckd_name = "mdd_obj",
69 .ckd_size = sizeof(struct mdd_object)
76 static int mdd_connect_to_next(const struct lu_env *env, struct mdd_device *m,
79 struct obd_connect_data *data = NULL;
80 struct lu_device *lud = mdd2lu_dev(m);
81 struct obd_device *obd;
85 LASSERT(m->mdd_child_exp == NULL);
87 OBD_ALLOC(data, sizeof(*data));
89 GOTO(out, rc = -ENOMEM);
91 obd = class_name2obd(nextdev);
93 CERROR("can't locate next device: %s\n", nextdev);
94 GOTO(out, rc = -ENOTCONN);
97 data->ocd_connect_flags = OBD_CONNECT_VERSION;
98 data->ocd_version = LUSTRE_VERSION_CODE;
100 rc = obd_connect(NULL, &m->mdd_child_exp, obd, &obd->obd_uuid, data, NULL);
102 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
106 lud->ld_site = m->mdd_child_exp->exp_obd->obd_lu_dev->ld_site;
107 LASSERT(lud->ld_site);
108 m->mdd_child = lu2dt_dev(m->mdd_child_exp->exp_obd->obd_lu_dev);
109 m->mdd_bottom = lu2dt_dev(lud->ld_site->ls_bottom_dev);
110 lu_dev_add_linkage(lud->ld_site, lud);
114 OBD_FREE(data, sizeof(*data));
118 static int mdd_init0(const struct lu_env *env, struct mdd_device *mdd,
119 struct lu_device_type *t, struct lustre_cfg *lcfg)
125 /* LU-8040 Set defaults here, before values configs */
126 mdd->mdd_cl.mc_flags = 0; /* off by default */
127 mdd->mdd_cl.mc_mask = CHANGELOG_DEFMASK;
129 dev = lustre_cfg_string(lcfg, 0);
133 mdd->mdd_md_dev.md_lu_dev.ld_obd = class_name2obd(dev);
134 if (mdd->mdd_md_dev.md_lu_dev.ld_obd == NULL)
136 mdd->mdd_md_dev.md_lu_dev.ld_ops = &mdd_lu_ops;
137 mdd->mdd_md_dev.md_ops = &mdd_ops;
139 rc = mdd_connect_to_next(env, mdd, lustre_cfg_string(lcfg, 3));
143 mdd->mdd_atime_diff = MAX_ATIME_DIFF;
144 /* sync permission changes */
145 mdd->mdd_sync_permission = 1;
147 dt_conf_get(env, mdd->mdd_child, &mdd->mdd_dt_conf);
149 /* we are using service name but not mdd obd name
150 * for compatibility reasons.
151 * It is passed from MDT in lustre_cfg[2] buffer */
152 rc = mdd_procfs_init(mdd, lustre_cfg_string(lcfg, 2));
154 obd_disconnect(mdd->mdd_child_exp);
159 static struct lu_device *mdd_device_fini(const struct lu_env *env,
162 struct mdd_device *mdd = lu2mdd_dev(d);
165 lu_dev_del_linkage(d->ld_site, d);
167 mdd_procfs_fini(mdd);
171 static int changelog_init_cb(const struct lu_env *env, struct llog_handle *llh,
172 struct llog_rec_hdr *hdr, void *data)
174 struct mdd_device *mdd = (struct mdd_device *)data;
175 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
177 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
178 LASSERT(rec->cr_hdr.lrh_type == CHANGELOG_REC);
181 "seeing record at index %d/%d/%llu t=%x %.*s in log"
182 DFID"\n", hdr->lrh_index, rec->cr_hdr.lrh_index,
183 rec->cr.cr_index, rec->cr.cr_type, rec->cr.cr_namelen,
184 changelog_rec_name(&rec->cr), PFID(&llh->lgh_id.lgl_oi.oi_fid));
186 mdd->mdd_cl.mc_index = rec->cr.cr_index;
187 return LLOG_PROC_BREAK;
190 static int changelog_user_init_cb(const struct lu_env *env,
191 struct llog_handle *llh,
192 struct llog_rec_hdr *hdr, void *data)
194 struct mdd_device *mdd = (struct mdd_device *)data;
195 struct llog_changelog_user_rec *rec =
196 (struct llog_changelog_user_rec *)hdr;
198 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
199 LASSERT(rec->cur_hdr.lrh_type == CHANGELOG_USER_REC);
201 CDEBUG(D_INFO, "seeing user at index %d/%d id=%d endrec=%llu"
202 " in log "DFID"\n", hdr->lrh_index, rec->cur_hdr.lrh_index,
203 rec->cur_id, rec->cur_endrec, PFID(&llh->lgh_id.lgl_oi.oi_fid));
205 spin_lock(&mdd->mdd_cl.mc_user_lock);
206 mdd->mdd_cl.mc_lastuser = rec->cur_id;
207 if (rec->cur_endrec > mdd->mdd_cl.mc_index)
208 mdd->mdd_cl.mc_index = rec->cur_endrec;
209 spin_unlock(&mdd->mdd_cl.mc_user_lock);
211 return LLOG_PROC_BREAK;
214 static int llog_changelog_cancel_cb(const struct lu_env *env,
215 struct llog_handle *llh,
216 struct llog_rec_hdr *hdr, void *data)
218 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
219 struct llog_cookie cookie;
220 long long endrec = *(long long *)data;
225 /* This is always a (sub)log, not the catalog */
226 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
228 if (rec->cr.cr_index > endrec)
229 /* records are in order, so we're done */
230 RETURN(LLOG_PROC_BREAK);
232 cookie.lgc_lgl = llh->lgh_id;
233 cookie.lgc_index = hdr->lrh_index;
235 /* cancel them one at a time. I suppose we could store up the cookies
236 * and cancel them all at once; probably more efficient, but this is
237 * done as a user call, so who cares... */
238 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1,
240 RETURN(rc < 0 ? rc : 0);
243 static int llog_changelog_cancel(const struct lu_env *env,
244 struct llog_ctxt *ctxt,
245 struct llog_cookie *cookies, int flags)
247 struct llog_handle *cathandle = ctxt->loc_handle;
252 /* This should only be called with the catalog handle */
253 LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
255 rc = llog_cat_process(env, cathandle, llog_changelog_cancel_cb,
256 (void *)cookies, 0, 0);
258 /* 0 or 1 means we're done */
261 CERROR("%s: cancel idx %u of catalog "DFID": rc = %d\n",
262 ctxt->loc_obd->obd_name, cathandle->lgh_last_idx,
263 PFID(&cathandle->lgh_id.lgl_oi.oi_fid), rc);
268 static struct llog_operations changelog_orig_logops;
271 mdd_changelog_write_header(const struct lu_env *env, struct mdd_device *mdd,
275 mdd_changelog_on(const struct lu_env *env, struct mdd_device *mdd)
279 if ((mdd->mdd_cl.mc_flags & CLM_ON) != 0)
282 LCONSOLE_INFO("%s: changelog on\n", mdd2obd_dev(mdd)->obd_name);
283 if (mdd->mdd_cl.mc_flags & CLM_ERR) {
284 CERROR("Changelogs cannot be enabled due to error "
285 "condition (see %s log).\n",
286 mdd2obd_dev(mdd)->obd_name);
289 spin_lock(&mdd->mdd_cl.mc_lock);
290 mdd->mdd_cl.mc_flags |= CLM_ON;
291 spin_unlock(&mdd->mdd_cl.mc_lock);
292 rc = mdd_changelog_write_header(env, mdd, CLM_START);
298 mdd_changelog_off(const struct lu_env *env, struct mdd_device *mdd)
302 if ((mdd->mdd_cl.mc_flags & CLM_ON) != CLM_ON)
305 LCONSOLE_INFO("%s: changelog off\n", mdd2obd_dev(mdd)->obd_name);
306 rc = mdd_changelog_write_header(env, mdd, CLM_FINI);
307 spin_lock(&mdd->mdd_cl.mc_lock);
308 mdd->mdd_cl.mc_flags &= ~CLM_ON;
309 spin_unlock(&mdd->mdd_cl.mc_lock);
314 static int mdd_changelog_llog_init(const struct lu_env *env,
315 struct mdd_device *mdd)
317 struct obd_device *obd = mdd2obd_dev(mdd);
318 struct llog_ctxt *ctxt = NULL, *uctxt = NULL;
323 /* LU-2844 mdd setup failure should not cause umount oops */
324 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_INIT))
327 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
328 obd->obd_lvfs_ctxt.dt = mdd->mdd_bottom;
329 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
330 obd, &changelog_orig_logops);
332 CERROR("%s: changelog llog setup failed: rc = %d\n",
337 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
340 rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
343 GOTO(out_cleanup, rc);
345 rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
349 rc = llog_cat_reverse_process(env, ctxt->loc_handle,
350 changelog_init_cb, mdd);
353 CERROR("%s: changelog init failed: rc = %d\n", obd->obd_name,
358 CDEBUG(D_IOCTL, "changelog starting index=%llu\n",
359 mdd->mdd_cl.mc_index);
361 /* setup user changelog */
362 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
363 obd, &changelog_orig_logops);
365 CERROR("%s: changelog users llog setup failed: rc = %d\n",
370 uctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
373 rc = llog_open_create(env, uctxt, &uctxt->loc_handle, NULL,
376 GOTO(out_ucleanup, rc);
378 uctxt->loc_handle->lgh_logops->lop_add = llog_cat_add_rec;
379 uctxt->loc_handle->lgh_logops->lop_declare_add = llog_cat_declare_add_rec;
381 rc = llog_init_handle(env, uctxt->loc_handle, LLOG_F_IS_CAT, NULL);
383 GOTO(out_uclose, rc);
385 rc = llog_cat_reverse_process(env, uctxt->loc_handle,
386 changelog_user_init_cb, mdd);
388 CERROR("%s: changelog user init failed: rc = %d\n",
390 GOTO(out_uclose, rc);
393 /* If we have registered users, assume we want changelogs on */
394 if (mdd->mdd_cl.mc_lastuser > 0) {
395 rc = mdd_changelog_on(env, mdd);
397 GOTO(out_uclose, rc);
400 llog_ctxt_put(uctxt);
403 llog_cat_close(env, uctxt->loc_handle);
405 llog_cleanup(env, uctxt);
407 llog_cat_close(env, ctxt->loc_handle);
409 llog_cleanup(env, ctxt);
413 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
415 struct obd_device *obd = mdd2obd_dev(mdd);
418 mdd->mdd_cl.mc_index = 0;
419 spin_lock_init(&mdd->mdd_cl.mc_lock);
420 mdd->mdd_cl.mc_starttime = ktime_get();
421 spin_lock_init(&mdd->mdd_cl.mc_user_lock);
422 mdd->mdd_cl.mc_lastuser = 0;
424 rc = mdd_changelog_llog_init(env, mdd);
426 CERROR("%s: changelog setup during init failed: rc = %d\n",
428 mdd->mdd_cl.mc_flags |= CLM_ERR;
434 static void mdd_changelog_fini(const struct lu_env *env,
435 struct mdd_device *mdd)
437 struct obd_device *obd = mdd2obd_dev(mdd);
438 struct llog_ctxt *ctxt;
440 mdd->mdd_cl.mc_flags = 0;
442 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
444 llog_cat_close(env, ctxt->loc_handle);
445 llog_cleanup(env, ctxt);
447 ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
449 llog_cat_close(env, ctxt->loc_handle);
450 llog_cleanup(env, ctxt);
454 /** Remove entries with indicies up to and including \a endrec from the
461 mdd_changelog_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
464 struct obd_device *obd = mdd2obd_dev(mdd);
465 struct llog_ctxt *ctxt;
466 long long unsigned cur;
469 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
473 spin_lock(&mdd->mdd_cl.mc_lock);
474 cur = (long long)mdd->mdd_cl.mc_index;
475 spin_unlock(&mdd->mdd_cl.mc_lock);
479 /* purge to "0" is shorthand for everything */
483 /* If purging all records, write a header entry so we don't have an
484 empty catalog and we're sure to have a valid starting index next
485 time. In case of crash, we just restart with old log so we're
488 /* XXX: transaction is started by llog itself */
489 rc = mdd_changelog_write_header(env, mdd, CLM_PURGE);
494 /* Some records were purged, so reset repeat-access time (so we
495 record new mtime update records, so users can see a file has been
496 changed since the last purge) */
497 mdd->mdd_cl.mc_starttime = ktime_get();
499 rc = llog_cancel(env, ctxt, (struct llog_cookie *)&endrec, 0);
505 /** Add a CL_MARK record to the changelog
507 * \param markerflags - CLM_*
510 int mdd_changelog_write_header(const struct lu_env *env,
511 struct mdd_device *mdd, int markerflags)
513 struct obd_device *obd = mdd2obd_dev(mdd);
514 struct llog_changelog_rec *rec;
516 struct llog_ctxt *ctxt;
518 int len = strlen(obd->obd_name);
523 if (mdd->mdd_cl.mc_mask & (1 << CL_MARK)) {
524 mdd->mdd_cl.mc_starttime = ktime_get();
528 reclen = llog_data_len(sizeof(*rec) + len);
529 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
530 if (buf->lb_buf == NULL)
534 rec->cr.cr_flags = CLF_VERSION;
535 rec->cr.cr_type = CL_MARK;
536 rec->cr.cr_namelen = len;
537 memcpy(changelog_rec_name(&rec->cr), obd->obd_name, rec->cr.cr_namelen);
538 /* Status and action flags */
539 rec->cr.cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
540 rec->cr_hdr.lrh_len = llog_data_len(changelog_rec_size(&rec->cr) +
542 rec->cr_hdr.lrh_type = CHANGELOG_REC;
543 rec->cr.cr_time = cl_time();
544 spin_lock(&mdd->mdd_cl.mc_lock);
545 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
546 spin_unlock(&mdd->mdd_cl.mc_lock);
548 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
551 rc = llog_cat_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL);
556 /* assume on or off event; reset repeat-access time */
557 mdd->mdd_cl.mc_starttime = ktime_get();
562 * Lookup method for "fid" object. Only filenames with correct SEQ:OID format
563 * are valid. We also check if object with passed fid exists or not.
565 static int obf_lookup(const struct lu_env *env, struct md_object *p,
566 const struct lu_name *lname, struct lu_fid *f,
567 struct md_op_spec *spec)
569 char *name = (char *)lname->ln_name;
570 struct mdd_device *mdd = mdo2mdd(p);
571 struct mdd_object *child;
577 sscanf(name, SFID, RFID(f));
578 if (!fid_is_sane(f)) {
579 CWARN("%s: Trying to lookup invalid FID [%s] in %s/%s, FID "
580 "format should be "DFID"\n", mdd2obd_dev(mdd)->obd_name,
581 lname->ln_name, dot_lustre_name, mdd_obf_dir_name,
582 (__u64)FID_SEQ_NORMAL, 1, 0);
583 GOTO(out, rc = -EINVAL);
586 if (!fid_is_norm(f) && !fid_is_igif(f) && !fid_is_root(f) &&
587 !fid_seq_is_dot(f->f_seq)) {
588 CWARN("%s: Trying to lookup invalid FID "DFID" in %s/%s, "
589 "sequence should be >= %#llx or within [%#llx,"
590 "%#llx].\n", mdd2obd_dev(mdd)->obd_name, PFID(f),
591 dot_lustre_name, mdd_obf_dir_name, (__u64)FID_SEQ_NORMAL,
592 (__u64)FID_SEQ_IGIF, (__u64)FID_SEQ_IGIF_MAX);
593 GOTO(out, rc = -EINVAL);
596 /* Check if object with this fid exists */
597 child = mdd_object_find(env, mdd, f);
599 GOTO(out, rc = PTR_ERR(child));
601 if (mdd_object_exists(child) == 0)
604 mdd_object_put(env, child);
610 static int mdd_dummy_create(const struct lu_env *env,
611 struct md_object *pobj,
612 const struct lu_name *lname,
613 struct md_object *child,
614 struct md_op_spec *spec,
620 static int mdd_dummy_rename(const struct lu_env *env,
621 struct md_object *src_pobj,
622 struct md_object *tgt_pobj,
623 const struct lu_fid *lf,
624 const struct lu_name *lsname,
625 struct md_object *tobj,
626 const struct lu_name *ltname,
632 static int mdd_dummy_link(const struct lu_env *env,
633 struct md_object *tgt_obj,
634 struct md_object *src_obj,
635 const struct lu_name *lname,
641 static int mdd_dummy_unlink(const struct lu_env *env,
642 struct md_object *pobj,
643 struct md_object *cobj,
644 const struct lu_name *lname,
651 static struct md_dir_operations mdd_obf_dir_ops = {
652 .mdo_lookup = obf_lookup,
653 .mdo_create = mdd_dummy_create,
654 .mdo_rename = mdd_dummy_rename,
655 .mdo_link = mdd_dummy_link,
656 .mdo_unlink = mdd_dummy_unlink
659 static struct md_dir_operations mdd_lpf_dir_ops = {
660 .mdo_lookup = mdd_lookup,
661 .mdo_create = mdd_dummy_create,
662 .mdo_rename = mdd_dummy_rename,
663 .mdo_link = mdd_dummy_link,
664 .mdo_unlink = mdd_dummy_unlink
667 static struct md_object *mdo_locate(const struct lu_env *env,
668 struct md_device *md,
669 const struct lu_fid *fid)
671 struct lu_object *obj;
672 struct md_object *mdo;
674 obj = lu_object_find(env, &md->md_lu_dev, fid, NULL);
676 obj = lu_object_locate(obj->lo_header, md->md_lu_dev.ld_type);
677 LASSERT(obj != NULL);
680 mdo = ERR_PTR(PTR_ERR(obj));
685 static int mdd_lpf_setup(const struct lu_env *env, struct mdd_device *m)
687 struct md_object *mdo;
688 struct mdd_object *mdd_lpf;
689 struct lu_fid fid = LU_LPF_FID;
693 rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
694 mdd_lpf_dir_name, S_IFDIR | S_IRUSR | S_IXUSR,
699 mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
701 RETURN(PTR_ERR(mdo));
703 LASSERT(lu_object_exists(&mdo->mo_lu));
705 mdd_lpf = md2mdd_obj(mdo);
706 mdd_lpf->mod_obj.mo_dir_ops = &mdd_lpf_dir_ops;
707 m->mdd_dot_lustre_objs.mdd_lpf = mdd_lpf;
713 * Create special in-memory "fid" object for open-by-fid.
715 static int mdd_obf_setup(const struct lu_env *env, struct mdd_device *m)
717 struct md_object *mdo;
718 struct mdd_object *mdd_obf;
719 struct lu_fid fid = LU_OBF_FID;
722 rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
723 mdd_obf_dir_name, S_IFDIR | S_IXUSR, &fid);
727 mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
729 RETURN(PTR_ERR(mdo));
731 LASSERT(lu_object_exists(&mdo->mo_lu));
733 mdd_obf = md2mdd_obj(mdo);
734 mdd_obf->mod_obj.mo_dir_ops = &mdd_obf_dir_ops;
735 m->mdd_dot_lustre_objs.mdd_obf = mdd_obf;
740 static void mdd_dot_lustre_cleanup(const struct lu_env *env,
741 struct mdd_device *m)
743 if (m->mdd_dot_lustre_objs.mdd_lpf != NULL) {
744 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_lpf);
745 m->mdd_dot_lustre_objs.mdd_lpf = NULL;
747 if (m->mdd_dot_lustre_objs.mdd_obf != NULL) {
748 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_obf);
749 m->mdd_dot_lustre_objs.mdd_obf = NULL;
751 if (m->mdd_dot_lustre != NULL) {
752 mdd_object_put(env, m->mdd_dot_lustre);
753 m->mdd_dot_lustre = NULL;
757 /** Setup ".lustre" directory object */
758 static int mdd_dot_lustre_setup(const struct lu_env *env, struct mdd_device *m)
760 struct md_object *mdo;
765 /* Create ".lustre" directory in ROOT. */
766 fid = LU_DOT_LUSTRE_FID;
767 rc = mdd_local_file_create(env, m, &m->mdd_root_fid,
769 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO,
773 mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
775 RETURN(PTR_ERR(mdo));
776 LASSERT(lu_object_exists(&mdo->mo_lu));
778 m->mdd_dot_lustre = md2mdd_obj(mdo);
780 rc = mdd_obf_setup(env, m);
782 CERROR("%s: error initializing \"fid\" object: rc = %d.\n",
783 mdd2obd_dev(m)->obd_name, rc);
787 rc = mdd_lpf_setup(env, m);
789 CERROR("%s: error initializing \"lost+found\": rc = %d.\n",
790 mdd2obd_dev(m)->obd_name, rc);
797 mdd_dot_lustre_cleanup(env, m);
803 static struct llog_operations hsm_actions_logops;
806 * set llog methods and create LLOG_AGENT_ORIG_CTXT llog
807 * object in obd_device
809 static int mdd_hsm_actions_llog_init(const struct lu_env *env,
810 struct mdd_device *m)
812 struct obd_device *obd = mdd2obd_dev(m);
813 struct llog_ctxt *ctxt = NULL;
817 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
818 obd->obd_lvfs_ctxt.dt = m->mdd_bottom;
820 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_AGENT_ORIG_CTXT,
821 obd, &hsm_actions_logops);
823 CERROR("%s: hsm actions llog setup failed: rc = %d\n",
828 ctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
831 rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
834 CERROR("%s: hsm actions llog open_create failed: rc = %d\n",
836 GOTO(out_cleanup, rc);
839 rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
847 llog_cat_close(env, ctxt->loc_handle);
848 ctxt->loc_handle = NULL;
850 llog_cleanup(env, ctxt);
856 * cleanup the context created by llog_setup_named()
858 static int mdd_hsm_actions_llog_fini(const struct lu_env *env,
859 struct mdd_device *m)
861 struct obd_device *obd = mdd2obd_dev(m);
862 struct llog_ctxt *lctxt;
865 lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
867 llog_cat_close(env, lctxt->loc_handle);
868 lctxt->loc_handle = NULL;
869 llog_cleanup(env, lctxt);
875 static void mdd_device_shutdown(const struct lu_env *env, struct mdd_device *m,
876 struct lustre_cfg *cfg)
878 barrier_deregister(m->mdd_bottom);
879 lfsck_degister(env, m->mdd_bottom);
880 mdd_hsm_actions_llog_fini(env, m);
881 mdd_changelog_fini(env, m);
882 orph_index_fini(env, m);
883 mdd_dot_lustre_cleanup(env, m);
884 if (mdd2obd_dev(m)->u.obt.obt_nodemap_config_file) {
885 nm_config_file_deregister_tgt(env,
886 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file);
887 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file = NULL;
889 if (m->mdd_los != NULL) {
890 local_oid_storage_fini(env, m->mdd_los);
893 lu_site_purge(env, mdd2lu_dev(m)->ld_site, ~0);
895 if (m->mdd_child_exp)
896 obd_disconnect(m->mdd_child_exp);
899 static int mdd_process_config(const struct lu_env *env,
900 struct lu_device *d, struct lustre_cfg *cfg)
902 struct mdd_device *m = lu2mdd_dev(d);
903 struct dt_device *dt = m->mdd_child;
904 struct lu_device *next = &dt->dd_lu_dev;
908 switch (cfg->lcfg_command) {
910 struct obd_device *obd = mdd2obd_dev(m);
912 rc = class_process_proc_param(PARAM_MDD, obd->obd_vars, cfg, m);
913 if (rc > 0 || rc == -ENOSYS)
914 /* we don't understand; pass it on */
915 rc = next->ld_ops->ldo_process_config(env, next, cfg);
919 rc = next->ld_ops->ldo_process_config(env, next, cfg);
922 dt_conf_get(env, dt, &m->mdd_dt_conf);
924 case LCFG_PRE_CLEANUP:
925 rc = next->ld_ops->ldo_process_config(env, next, cfg);
926 mdd_generic_thread_stop(&m->mdd_orph_cleanup_thread);
929 rc = next->ld_ops->ldo_process_config(env, next, cfg);
930 lu_dev_del_linkage(d->ld_site, d);
931 mdd_device_shutdown(env, m, cfg);
934 rc = next->ld_ops->ldo_process_config(env, next, cfg);
941 static int mdd_recovery_complete(const struct lu_env *env,
944 struct mdd_device *mdd = lu2mdd_dev(d);
945 struct lu_device *next;
949 LASSERT(mdd != NULL);
950 next = &mdd->mdd_child->dd_lu_dev;
952 /* XXX: orphans handling. */
953 if (!mdd->mdd_bottom->dd_rdonly)
954 mdd_orphan_cleanup(env, mdd);
955 rc = next->ld_ops->ldo_recovery_complete(env, next);
960 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
961 const struct lu_fid *pfid, const char *name,
962 __u32 mode, struct lu_fid *fid)
964 struct dt_object *parent, *dto;
969 LASSERT(!fid_is_zero(pfid));
970 parent = dt_locate(env, mdd->mdd_bottom, pfid);
971 if (unlikely(IS_ERR(parent)))
972 RETURN(PTR_ERR(parent));
974 /* create local file/dir, if @fid is passed then try to use it */
975 if (fid_is_zero(fid))
976 dto = local_file_find_or_create(env, mdd->mdd_los, parent,
979 dto = local_file_find_or_create_with_fid(env, mdd->mdd_bottom,
983 GOTO(out_put, rc = PTR_ERR(dto));
984 *fid = *lu_object_fid(&dto->do_lu);
985 /* since stack is not fully set up the local_storage uses own stack
986 * and we should drop its object from cache */
987 dt_object_put_nocache(env, dto);
990 dt_object_put(env, parent);
994 static int mdd_lfsck_out_notify(const struct lu_env *env, void *data,
995 enum lfsck_events event)
1000 static int mdd_prepare(const struct lu_env *env,
1001 struct lu_device *pdev,
1002 struct lu_device *cdev)
1004 struct mdd_device *mdd = lu2mdd_dev(cdev);
1005 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
1006 struct nm_config_file *nodemap_config;
1007 struct obd_device_target *obt = &mdd2obd_dev(mdd)->u.obt;
1013 rc = next->ld_ops->ldo_prepare(env, cdev, next);
1017 /* Setup local dirs */
1018 fid.f_seq = FID_SEQ_LOCAL_NAME;
1021 rc = local_oid_storage_init(env, mdd->mdd_bottom, &fid,
1026 rc = dt_root_get(env, mdd->mdd_child, &mdd->mdd_local_root_fid);
1031 if (mdd_seq_site(mdd)->ss_node_id == 0) {
1032 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
1033 mdd_root_dir_name, S_IFDIR |
1034 S_IRUGO | S_IWUSR | S_IXUGO, &fid);
1036 CERROR("%s: create root fid failed: rc = %d\n",
1037 mdd2obd_dev(mdd)->obd_name, rc);
1041 mdd->mdd_root_fid = fid;
1042 rc = mdd_dot_lustre_setup(env, mdd);
1044 CERROR("%s: initializing .lustre failed: rc = %d\n",
1045 mdd2obd_dev(mdd)->obd_name, rc);
1049 /* Normal client usually send root access to MDT0 directly,
1050 * the root FID on non-MDT0 will only be used by echo client. */
1051 mdd->mdd_root_fid = fid;
1054 rc = orph_index_init(env, mdd);
1058 rc = mdd_changelog_init(env, mdd);
1060 CERROR("%s: failed to initialize changelog: rc = %d\n",
1061 mdd2obd_dev(mdd)->obd_name, rc);
1065 rc = mdd_hsm_actions_llog_init(env, mdd);
1067 GOTO(out_changelog, rc);
1069 nodemap_config = nm_config_file_register_tgt(env, mdd->mdd_bottom,
1071 if (IS_ERR(nodemap_config)) {
1072 rc = PTR_ERR(nodemap_config);
1076 obt->obt_nodemap_config_file = nodemap_config;
1079 rc = lfsck_register(env, mdd->mdd_bottom, mdd->mdd_child,
1080 mdd2obd_dev(mdd), mdd_lfsck_out_notify,
1083 CERROR("%s: failed to initialize lfsck: rc = %d\n",
1084 mdd2obd_dev(mdd)->obd_name, rc);
1085 GOTO(out_nodemap, rc);
1088 rc = barrier_register(mdd->mdd_bottom, mdd->mdd_child);
1090 CERROR("%s: failed to register to barrier: rc = %d\n",
1091 mdd2obd_dev(mdd)->obd_name, rc);
1092 GOTO(out_lfsck, rc);
1098 lfsck_degister(env, mdd->mdd_bottom);
1100 nm_config_file_deregister_tgt(env, obt->obt_nodemap_config_file);
1101 obt->obt_nodemap_config_file = NULL;
1103 mdd_hsm_actions_llog_fini(env, mdd);
1105 mdd_changelog_fini(env, mdd);
1107 orph_index_fini(env, mdd);
1109 if (mdd_seq_site(mdd)->ss_node_id == 0)
1110 mdd_dot_lustre_cleanup(env, mdd);
1112 local_oid_storage_fini(env, mdd->mdd_los);
1113 mdd->mdd_los = NULL;
1118 const struct lu_device_operations mdd_lu_ops = {
1119 .ldo_object_alloc = mdd_object_alloc,
1120 .ldo_process_config = mdd_process_config,
1121 .ldo_recovery_complete = mdd_recovery_complete,
1122 .ldo_prepare = mdd_prepare,
1125 static int mdd_root_get(const struct lu_env *env,
1126 struct md_device *m, struct lu_fid *f)
1128 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1131 *f = mdd->mdd_root_fid;
1136 * No permission check is needed.
1138 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1139 struct obd_statfs *sfs)
1141 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1146 rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
1148 sfs->os_namelen = min_t(__u32, sfs->os_namelen, NAME_MAX);
1153 static int mdd_maxeasize_get(const struct lu_env *env, struct md_device *m,
1156 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1159 *easize = mdd->mdd_dt_conf.ddp_max_ea_size;
1164 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1167 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1169 *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1170 return (*h == NULL ? -ENOENT : 0);
1173 static struct lu_device *mdd_device_free(const struct lu_env *env,
1174 struct lu_device *lu)
1176 struct mdd_device *m = lu2mdd_dev(lu);
1179 LASSERT(atomic_read(&lu->ld_ref) == 0);
1180 md_device_fini(&m->mdd_md_dev);
1185 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1186 struct lu_device_type *t,
1187 struct lustre_cfg *lcfg)
1189 struct lu_device *l;
1190 struct mdd_device *m;
1194 l = ERR_PTR(-ENOMEM);
1199 md_device_init(&m->mdd_md_dev, t);
1200 rc = mdd_init0(env, m, t, lcfg);
1202 mdd_device_free(env, l);
1211 * we use exports to track all mdd users
1213 static int mdd_obd_connect(const struct lu_env *env, struct obd_export **exp,
1214 struct obd_device *obd, struct obd_uuid *cluuid,
1215 struct obd_connect_data *data, void *localdata)
1217 struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1218 struct lustre_handle conn;
1222 CDEBUG(D_CONFIG, "connect #%d\n", mdd->mdd_connects);
1224 rc = class_connect(&conn, obd, cluuid);
1228 *exp = class_conn2export(&conn);
1230 /* Why should there ever be more than 1 connect? */
1231 LASSERT(mdd->mdd_connects == 0);
1232 mdd->mdd_connects++;
1238 * once last export (we don't count self-export) disappeared
1239 * mdd can be released
1241 static int mdd_obd_disconnect(struct obd_export *exp)
1243 struct obd_device *obd = exp->exp_obd;
1244 struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1245 int rc, release = 0;
1248 mdd->mdd_connects--;
1249 if (mdd->mdd_connects == 0)
1252 rc = class_disconnect(exp);
1254 if (rc == 0 && release)
1255 class_manual_cleanup(obd);
1259 static int mdd_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1260 __u32 keylen, void *key, __u32 *vallen, void *val)
1264 if (KEY_IS(KEY_OSP_CONNECTED)) {
1265 struct obd_device *obd = exp->exp_obd;
1266 struct mdd_device *mdd;
1268 if (!obd->obd_set_up || obd->obd_stopping)
1271 mdd = lu2mdd_dev(obd->obd_lu_dev);
1273 rc = obd_get_info(env, mdd->mdd_child_exp, keylen, key, vallen,
1281 static int mdd_obd_set_info_async(const struct lu_env *env,
1282 struct obd_export *exp,
1283 __u32 keylen, void *key,
1284 __u32 vallen, void *val,
1285 struct ptlrpc_request_set *set)
1287 struct obd_device *obd = exp->exp_obd;
1288 struct mdd_device *mdd;
1291 if (!obd->obd_set_up || obd->obd_stopping)
1294 mdd = lu2mdd_dev(obd->obd_lu_dev);
1296 rc = obd_set_info_async(env, mdd->mdd_child_exp, keylen, key,
1301 static struct obd_ops mdd_obd_device_ops = {
1302 .o_owner = THIS_MODULE,
1303 .o_connect = mdd_obd_connect,
1304 .o_disconnect = mdd_obd_disconnect,
1305 .o_get_info = mdd_obd_get_info,
1306 .o_set_info_async = mdd_obd_set_info_async,
1309 static int mdd_changelog_user_register(const struct lu_env *env,
1310 struct mdd_device *mdd, int *id)
1312 struct llog_ctxt *ctxt;
1313 struct llog_changelog_user_rec *rec;
1317 ctxt = llog_get_context(mdd2obd_dev(mdd),
1318 LLOG_CHANGELOG_USER_ORIG_CTXT);
1324 llog_ctxt_put(ctxt);
1328 /* Assume we want it on since somebody registered */
1329 rc = mdd_changelog_on(env, mdd);
1333 rec->cur_hdr.lrh_len = sizeof(*rec);
1334 rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1335 spin_lock(&mdd->mdd_cl.mc_user_lock);
1336 if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1337 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1338 CERROR("Maximum number of changelog users exceeded!\n");
1339 GOTO(out, rc = -EOVERFLOW);
1341 *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1342 rec->cur_endrec = mdd->mdd_cl.mc_index;
1343 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1345 rc = llog_cat_add(env, ctxt->loc_handle, &rec->cur_hdr, NULL);
1347 CDEBUG(D_IOCTL, "Registered changelog user %d\n", *id);
1350 llog_ctxt_put(ctxt);
1354 struct mdd_changelog_user_purge {
1356 __u32 mcup_usercount;
1362 * changelog_user_purge callback
1364 * Is called once per user.
1366 * Check to see the user requested is available from rec.
1367 * Truncate the changelog.
1368 * Keep track of the total number of users (calls).
1370 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1371 struct llog_handle *llh,
1372 struct llog_rec_hdr *hdr, void *data)
1374 struct llog_changelog_user_rec *rec;
1375 struct mdd_changelog_user_purge *mcup = data;
1376 struct llog_cookie cookie;
1381 if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
1384 rec = container_of(hdr, struct llog_changelog_user_rec, cur_hdr);
1386 mcup->mcup_usercount++;
1388 if (rec->cur_id != mcup->mcup_id) {
1389 /* truncate to the lowest endrec that is not this user */
1390 mcup->mcup_minrec = min(mcup->mcup_minrec,
1395 /* Unregister this user */
1396 cookie.lgc_lgl = llh->lgh_id;
1397 cookie.lgc_index = hdr->lrh_index;
1399 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1402 mcup->mcup_found = true;
1403 mcup->mcup_usercount--;
1409 static int mdd_changelog_user_purge(const struct lu_env *env,
1410 struct mdd_device *mdd, __u32 id)
1412 struct mdd_changelog_user_purge mcup = {
1414 .mcup_found = false,
1415 .mcup_usercount = 0,
1416 .mcup_minrec = ULLONG_MAX,
1418 struct llog_ctxt *ctxt;
1423 CDEBUG(D_IOCTL, "%s: Purge request: id=%u\n",
1424 mdd2obd_dev(mdd)->obd_name, id);
1426 ctxt = llog_get_context(mdd2obd_dev(mdd),
1427 LLOG_CHANGELOG_USER_ORIG_CTXT);
1429 (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
1430 GOTO(out, rc = -ENXIO);
1432 rc = llog_cat_process(env, ctxt->loc_handle,
1433 mdd_changelog_user_purge_cb, &mcup,
1436 if ((rc == 0) && mcup.mcup_found) {
1437 CDEBUG(D_IOCTL, "%s: Purging changelog entries for user %d "
1439 mdd2obd_dev(mdd)->obd_name, id, mcup.mcup_minrec);
1440 /* Cancelling record 0 destroys the entire changelog, make sure
1441 we don't do that unless we mean it. */
1442 if (mcup.mcup_minrec != 0 || mcup.mcup_usercount == 0) {
1443 rc = mdd_changelog_llog_cancel(env, mdd,
1447 CWARN("%s: No changelog for user %u; rc=%d\n",
1448 mdd2obd_dev(mdd)->obd_name, id, rc);
1449 GOTO(out, rc = -ENOENT);
1452 if ((rc == 0) && (mcup.mcup_usercount == 0)) {
1453 /* No more users; turn changelogs off */
1454 CDEBUG(D_IOCTL, "turning off changelogs\n");
1455 rc = mdd_changelog_off(env, mdd);
1461 llog_ctxt_put(ctxt);
1466 struct mdd_changelog_user_clear {
1474 * changelog_clear callback
1476 * Is called once per user.
1478 * Check to see the user requested is available from rec.
1479 * Check the oldest (smallest) record for boundary conditions.
1480 * Truncate the changelog.
1482 static int mdd_changelog_clear_cb(const struct lu_env *env,
1483 struct llog_handle *llh,
1484 struct llog_rec_hdr *hdr,
1487 struct llog_changelog_user_rec *rec;
1488 struct mdd_changelog_user_clear *mcuc = data;
1493 if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
1496 rec = container_of(hdr, struct llog_changelog_user_rec, cur_hdr);
1498 /* Does the changelog id match the requested id? */
1499 if (rec->cur_id != mcuc->mcuc_id) {
1500 mcuc->mcuc_minrec = min(mcuc->mcuc_minrec,
1505 /* cur_endrec is the oldest purgeable record, make sure we're newer */
1506 if (rec->cur_endrec > mcuc->mcuc_endrec) {
1507 CDEBUG(D_IOCTL, "Request %llu out of range: %llu\n",
1508 mcuc->mcuc_endrec, rec->cur_endrec);
1512 /* Flag that we've met all the range and user checks.
1513 * We now know the record to flush.
1515 rec->cur_endrec = mcuc->mcuc_endrec;
1516 mcuc->mcuc_flush = true;
1518 CDEBUG(D_IOCTL, "Rewriting changelog user %u endrec to %llu\n",
1519 mcuc->mcuc_id, rec->cur_endrec);
1521 /* Update the endrec */
1522 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1528 * Clear a changelog up to entry specified by endrec for user id.
1530 static int mdd_changelog_clear(const struct lu_env *env,
1531 struct mdd_device *mdd, __u32 id,
1534 struct mdd_changelog_user_clear mcuc = {
1536 .mcuc_minrec = endrec,
1537 .mcuc_flush = false,
1539 struct llog_ctxt *ctxt;
1545 CDEBUG(D_IOCTL, "%s: Purge request: id=%u, endrec=%llu\n",
1546 mdd2obd_dev(mdd)->obd_name, id, endrec);
1547 /* start_rec is the newest (largest value) entry in the changelogs*/
1548 spin_lock(&mdd->mdd_cl.mc_lock);
1549 start_rec = mdd->mdd_cl.mc_index;
1550 spin_unlock(&mdd->mdd_cl.mc_lock);
1552 if (start_rec < endrec) {
1553 CDEBUG(D_IOCTL, "%s: Could not clear changelog, requested "\
1554 "address out of range\n", mdd2obd_dev(mdd)->obd_name);
1559 mcuc.mcuc_endrec = start_rec;
1560 mcuc.mcuc_minrec = ULLONG_MAX;
1562 mcuc.mcuc_endrec = endrec;
1565 ctxt = llog_get_context(mdd2obd_dev(mdd),
1566 LLOG_CHANGELOG_USER_ORIG_CTXT);
1568 (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
1569 GOTO(out, rc = -ENXIO);
1571 rc = llog_cat_process(env, ctxt->loc_handle,
1572 mdd_changelog_clear_cb, (void *)&mcuc,
1576 CWARN("%s: Failure to clear the changelog for user %d: %d\n",
1577 mdd2obd_dev(mdd)->obd_name, id, rc);
1578 } else if (mcuc.mcuc_flush) {
1579 /* Cancelling record 0 destroys the entire changelog, make sure
1580 we don't do that unless we mean it. */
1581 if (mcuc.mcuc_minrec != 0) {
1582 CDEBUG(D_IOCTL, "%s: Purging changelog entries up "\
1583 "to %llu\n", mdd2obd_dev(mdd)->obd_name,
1586 rc = mdd_changelog_llog_cancel(env, mdd,
1590 CWARN("%s: No entry for user %d\n",
1591 mdd2obd_dev(mdd)->obd_name, id);
1598 llog_ctxt_put(ctxt);
1604 * May be called remotely from mdt_iocontrol_handle or locally from
1605 * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
1606 * an obd_ioctl_data format (but local ioctl handler does).
1608 * \param len - data len
1609 * \param karg - ioctl data, in kernel space
1611 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
1612 unsigned int cmd, int len, void *karg)
1614 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1615 struct obd_ioctl_data *data = karg;
1619 /* Doesn't use obd_ioctl_data */
1621 case OBD_IOC_CHANGELOG_CLEAR: {
1622 struct changelog_setinfo *cs = karg;
1624 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1625 RETURN(-EINPROGRESS);
1627 rc = mdd_changelog_clear(env, mdd, cs->cs_id,
1629 barrier_exit(mdd->mdd_bottom);
1632 case OBD_IOC_GET_MNTOPT: {
1633 mntopt_t *mntopts = (mntopt_t *)karg;
1634 *mntopts = mdd->mdd_dt_conf.ddp_mntopts;
1637 case OBD_IOC_START_LFSCK: {
1638 rc = lfsck_start(env, mdd->mdd_bottom,
1639 (struct lfsck_start_param *)karg);
1642 case OBD_IOC_STOP_LFSCK: {
1643 rc = lfsck_stop(env, mdd->mdd_bottom,
1644 (struct lfsck_stop *)karg);
1647 case OBD_IOC_QUERY_LFSCK: {
1648 rc = lfsck_query(env, mdd->mdd_bottom, NULL, NULL,
1649 (struct lfsck_query *)karg);
1654 /* Below ioctls use obd_ioctl_data */
1655 if (len != sizeof(*data)) {
1656 CERROR("Bad ioctl size %d\n", len);
1659 if (data->ioc_version != OBD_IOCTL_VERSION) {
1660 CERROR("Bad magic %x != %x\n", data->ioc_version,
1666 case OBD_IOC_CHANGELOG_REG:
1667 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1668 RETURN(-EINPROGRESS);
1670 rc = mdd_changelog_user_register(env, mdd, &data->ioc_u32_1);
1671 barrier_exit(mdd->mdd_bottom);
1673 case OBD_IOC_CHANGELOG_DEREG:
1674 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1675 RETURN(-EINPROGRESS);
1677 rc = mdd_changelog_user_purge(env, mdd, data->ioc_u32_1);
1678 barrier_exit(mdd->mdd_bottom);
1687 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
1688 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key);
1690 static const struct md_device_operations mdd_ops = {
1691 .mdo_statfs = mdd_statfs,
1692 .mdo_root_get = mdd_root_get,
1693 .mdo_llog_ctxt_get = mdd_llog_ctxt_get,
1694 .mdo_iocontrol = mdd_iocontrol,
1695 .mdo_maxeasize_get = mdd_maxeasize_get,
1698 static struct lu_device_type_operations mdd_device_type_ops = {
1699 .ldto_init = mdd_type_init,
1700 .ldto_fini = mdd_type_fini,
1702 .ldto_start = mdd_type_start,
1703 .ldto_stop = mdd_type_stop,
1705 .ldto_device_alloc = mdd_device_alloc,
1706 .ldto_device_free = mdd_device_free,
1708 .ldto_device_fini = mdd_device_fini
1711 static struct lu_device_type mdd_device_type = {
1712 .ldt_tags = LU_DEVICE_MD,
1713 .ldt_name = LUSTRE_MDD_NAME,
1714 .ldt_ops = &mdd_device_type_ops,
1715 .ldt_ctx_tags = LCT_MD_THREAD
1718 /* context key constructor: mdd_key_init */
1719 LU_KEY_INIT(mdd, struct mdd_thread_info);
1721 static void mdd_key_fini(const struct lu_context *ctx,
1722 struct lu_context_key *key, void *data)
1724 struct mdd_thread_info *info = data;
1726 lu_buf_free(&info->mti_big_buf);
1727 lu_buf_free(&info->mti_link_buf);
1728 lu_buf_free(&info->mti_xattr_buf);
1733 /* context key: mdd_thread_key */
1734 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
1736 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
1737 int (*func)(void *), void *data, char *name)
1739 struct task_struct *task;
1741 LASSERT(thread->mgt_init == false);
1742 init_completion(&thread->mgt_started);
1743 init_completion(&thread->mgt_finished);
1744 thread->mgt_data = data;
1745 thread->mgt_abort = false;
1746 thread->mgt_init = true;
1748 task = kthread_run(func, thread, name);
1750 complete(&thread->mgt_finished);
1751 return PTR_ERR(task);
1753 wait_for_completion(&thread->mgt_started);
1757 void mdd_generic_thread_stop(struct mdd_generic_thread *thread)
1759 if (thread->mgt_init == true) {
1760 thread->mgt_abort = true;
1761 wait_for_completion(&thread->mgt_finished);
1765 static int __init mdd_init(void)
1769 rc = lu_kmem_init(mdd_caches);
1773 changelog_orig_logops = llog_osd_ops;
1774 changelog_orig_logops.lop_cancel = llog_changelog_cancel;
1775 changelog_orig_logops.lop_add = llog_cat_add_rec;
1776 changelog_orig_logops.lop_declare_add = llog_cat_declare_add_rec;
1778 hsm_actions_logops = llog_osd_ops;
1779 hsm_actions_logops.lop_add = llog_cat_add_rec;
1780 hsm_actions_logops.lop_declare_add = llog_cat_declare_add_rec;
1782 rc = class_register_type(&mdd_obd_device_ops, NULL, true, NULL,
1783 LUSTRE_MDD_NAME, &mdd_device_type);
1785 lu_kmem_fini(mdd_caches);
1789 static void __exit mdd_exit(void)
1791 class_unregister_type(LUSTRE_MDD_NAME);
1792 lu_kmem_fini(mdd_caches);
1795 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1796 MODULE_DESCRIPTION("Lustre Meta-data Device Driver ("LUSTRE_MDD_NAME")");
1797 MODULE_VERSION(LUSTRE_VERSION_STRING);
1798 MODULE_LICENSE("GPL");
1800 module_init(mdd_init);
1801 module_exit(mdd_exit);