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, 2017, 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;
128 mdd->mdd_cl.mc_deniednext = 60; /* 60 secs by default */
130 dev = lustre_cfg_string(lcfg, 0);
134 mdd->mdd_md_dev.md_lu_dev.ld_obd = class_name2obd(dev);
135 if (mdd->mdd_md_dev.md_lu_dev.ld_obd == NULL)
137 mdd->mdd_md_dev.md_lu_dev.ld_ops = &mdd_lu_ops;
138 mdd->mdd_md_dev.md_ops = &mdd_ops;
140 rc = mdd_connect_to_next(env, mdd, lustre_cfg_string(lcfg, 3));
144 mdd->mdd_atime_diff = MAX_ATIME_DIFF;
145 /* sync permission changes */
146 mdd->mdd_sync_permission = 1;
147 /* enable changelog garbage collection */
148 mdd->mdd_changelog_gc = 1;
149 /* with a significant amount of idle time */
150 mdd->mdd_changelog_max_idle_time = CHLOG_MAX_IDLE_TIME;
151 /* or a significant amount of late indexes */
152 mdd->mdd_changelog_max_idle_indexes = CHLOG_MAX_IDLE_INDEXES;
153 /* with a reasonable interval between each check */
154 mdd->mdd_changelog_min_gc_interval = CHLOG_MIN_GC_INTERVAL;
155 /* with a very few number of free entries */
156 mdd->mdd_changelog_min_free_cat_entries = CHLOG_MIN_FREE_CAT_ENTRIES;
158 dt_conf_get(env, mdd->mdd_child, &mdd->mdd_dt_conf);
160 /* we are using service name but not mdd obd name
161 * for compatibility reasons.
162 * It is passed from MDT in lustre_cfg[2] buffer */
163 rc = mdd_procfs_init(mdd, lustre_cfg_string(lcfg, 2));
165 obd_disconnect(mdd->mdd_child_exp);
170 static struct lu_device *mdd_device_fini(const struct lu_env *env,
173 struct mdd_device *mdd = lu2mdd_dev(d);
176 lu_dev_del_linkage(d->ld_site, d);
178 mdd_procfs_fini(mdd);
182 static int changelog_init_cb(const struct lu_env *env, struct llog_handle *llh,
183 struct llog_rec_hdr *hdr, void *data)
185 struct mdd_device *mdd = (struct mdd_device *)data;
186 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
188 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
189 LASSERT(rec->cr_hdr.lrh_type == CHANGELOG_REC);
192 "seeing record at index %d/%d/%llu t=%x %.*s in log"
193 DFID"\n", hdr->lrh_index, rec->cr_hdr.lrh_index,
194 rec->cr.cr_index, rec->cr.cr_type, rec->cr.cr_namelen,
195 changelog_rec_name(&rec->cr), PFID(&llh->lgh_id.lgl_oi.oi_fid));
197 mdd->mdd_cl.mc_index = rec->cr.cr_index;
198 return LLOG_PROC_BREAK;
201 static int changelog_user_init_cb(const struct lu_env *env,
202 struct llog_handle *llh,
203 struct llog_rec_hdr *hdr, void *data)
205 struct mdd_device *mdd = (struct mdd_device *)data;
206 struct llog_changelog_user_rec *rec =
207 (struct llog_changelog_user_rec *)hdr;
209 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
210 LASSERT(rec->cur_hdr.lrh_type == CHANGELOG_USER_REC);
212 CDEBUG(D_INFO, "seeing user at index %d/%d id=%d endrec=%llu"
213 " in log "DFID"\n", hdr->lrh_index, rec->cur_hdr.lrh_index,
214 rec->cur_id, rec->cur_endrec, PFID(&llh->lgh_id.lgl_oi.oi_fid));
216 spin_lock(&mdd->mdd_cl.mc_user_lock);
217 mdd->mdd_cl.mc_lastuser = rec->cur_id;
218 if (rec->cur_endrec > mdd->mdd_cl.mc_index)
219 mdd->mdd_cl.mc_index = rec->cur_endrec;
220 spin_unlock(&mdd->mdd_cl.mc_user_lock);
222 return LLOG_PROC_BREAK;
225 static int llog_changelog_cancel_cb(const struct lu_env *env,
226 struct llog_handle *llh,
227 struct llog_rec_hdr *hdr, void *data)
229 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
230 struct llog_cookie cookie;
231 long long endrec = *(long long *)data;
236 /* This is always a (sub)log, not the catalog */
237 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
239 if (rec->cr.cr_index > endrec)
240 /* records are in order, so we're done */
241 RETURN(LLOG_PROC_BREAK);
243 cookie.lgc_lgl = llh->lgh_id;
244 cookie.lgc_index = hdr->lrh_index;
246 /* cancel them one at a time. I suppose we could store up the cookies
247 * and cancel them all at once; probably more efficient, but this is
248 * done as a user call, so who cares... */
249 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1,
251 RETURN(rc < 0 ? rc : 0);
254 static int llog_changelog_cancel(const struct lu_env *env,
255 struct llog_ctxt *ctxt,
256 struct llog_cookie *cookies, int flags)
258 struct llog_handle *cathandle = ctxt->loc_handle;
263 /* This should only be called with the catalog handle */
264 LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
266 rc = llog_cat_process(env, cathandle, llog_changelog_cancel_cb,
267 (void *)cookies, 0, 0);
269 /* 0 or 1 means we're done */
272 CERROR("%s: cancel idx %u of catalog "DFID": rc = %d\n",
273 ctxt->loc_obd->obd_name, cathandle->lgh_last_idx,
274 PFID(&cathandle->lgh_id.lgl_oi.oi_fid), rc);
279 static struct llog_operations changelog_orig_logops;
282 mdd_changelog_write_header(const struct lu_env *env, struct mdd_device *mdd,
286 mdd_changelog_on(const struct lu_env *env, struct mdd_device *mdd)
290 if ((mdd->mdd_cl.mc_flags & CLM_ON) != 0)
293 LCONSOLE_INFO("%s: changelog on\n", mdd2obd_dev(mdd)->obd_name);
294 if (mdd->mdd_cl.mc_flags & CLM_ERR) {
295 CERROR("Changelogs cannot be enabled due to error "
296 "condition (see %s log).\n",
297 mdd2obd_dev(mdd)->obd_name);
300 spin_lock(&mdd->mdd_cl.mc_lock);
301 mdd->mdd_cl.mc_flags |= CLM_ON;
302 spin_unlock(&mdd->mdd_cl.mc_lock);
303 rc = mdd_changelog_write_header(env, mdd, CLM_START);
309 mdd_changelog_off(const struct lu_env *env, struct mdd_device *mdd)
313 if ((mdd->mdd_cl.mc_flags & CLM_ON) != CLM_ON)
316 LCONSOLE_INFO("%s: changelog off\n", mdd2obd_dev(mdd)->obd_name);
317 rc = mdd_changelog_write_header(env, mdd, CLM_FINI);
318 spin_lock(&mdd->mdd_cl.mc_lock);
319 mdd->mdd_cl.mc_flags &= ~CLM_ON;
320 spin_unlock(&mdd->mdd_cl.mc_lock);
325 static int mdd_changelog_llog_init(const struct lu_env *env,
326 struct mdd_device *mdd)
328 struct obd_device *obd = mdd2obd_dev(mdd);
329 struct llog_ctxt *ctxt = NULL, *uctxt = NULL;
334 /* LU-2844 mdd setup failure should not cause umount oops */
335 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_INIT))
338 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
339 obd->obd_lvfs_ctxt.dt = mdd->mdd_bottom;
340 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
341 obd, &changelog_orig_logops);
343 CERROR("%s: changelog llog setup failed: rc = %d\n",
348 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
351 rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
354 GOTO(out_cleanup, rc);
356 rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
360 rc = llog_cat_reverse_process(env, ctxt->loc_handle,
361 changelog_init_cb, mdd);
364 CERROR("%s: changelog init failed: rc = %d\n", obd->obd_name,
369 CDEBUG(D_IOCTL, "changelog starting index=%llu\n",
370 mdd->mdd_cl.mc_index);
372 /* setup user changelog */
373 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
374 obd, &changelog_orig_logops);
376 CERROR("%s: changelog users llog setup failed: rc = %d\n",
381 uctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
384 rc = llog_open_create(env, uctxt, &uctxt->loc_handle, NULL,
387 GOTO(out_ucleanup, rc);
389 uctxt->loc_handle->lgh_logops->lop_add = llog_cat_add_rec;
390 uctxt->loc_handle->lgh_logops->lop_declare_add = llog_cat_declare_add_rec;
392 rc = llog_init_handle(env, uctxt->loc_handle, LLOG_F_IS_CAT, NULL);
394 GOTO(out_uclose, rc);
396 rc = llog_cat_reverse_process(env, uctxt->loc_handle,
397 changelog_user_init_cb, mdd);
399 CERROR("%s: changelog user init failed: rc = %d\n",
401 GOTO(out_uclose, rc);
404 /* If we have registered users, assume we want changelogs on */
405 if (mdd->mdd_cl.mc_lastuser > 0) {
406 rc = mdd_changelog_on(env, mdd);
408 GOTO(out_uclose, rc);
411 llog_ctxt_put(uctxt);
414 llog_cat_close(env, uctxt->loc_handle);
416 llog_cleanup(env, uctxt);
418 llog_cat_close(env, ctxt->loc_handle);
420 llog_cleanup(env, ctxt);
424 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
426 struct obd_device *obd = mdd2obd_dev(mdd);
429 mdd->mdd_cl.mc_index = 0;
430 spin_lock_init(&mdd->mdd_cl.mc_lock);
431 mdd->mdd_cl.mc_starttime = ktime_get();
432 spin_lock_init(&mdd->mdd_cl.mc_user_lock);
433 mdd->mdd_cl.mc_lastuser = 0;
435 rc = mdd_changelog_llog_init(env, mdd);
437 CERROR("%s: changelog setup during init failed: rc = %d\n",
439 mdd->mdd_cl.mc_flags |= CLM_ERR;
445 static void mdd_changelog_fini(const struct lu_env *env,
446 struct mdd_device *mdd)
448 struct obd_device *obd = mdd2obd_dev(mdd);
449 struct llog_ctxt *ctxt;
451 mdd->mdd_cl.mc_flags = 0;
453 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
455 llog_cat_close(env, ctxt->loc_handle);
456 llog_cleanup(env, ctxt);
458 ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
460 llog_cat_close(env, ctxt->loc_handle);
461 llog_cleanup(env, ctxt);
465 /** Remove entries with indicies up to and including \a endrec from the
472 mdd_changelog_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
475 struct obd_device *obd = mdd2obd_dev(mdd);
476 struct llog_ctxt *ctxt;
477 long long unsigned cur;
480 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
484 spin_lock(&mdd->mdd_cl.mc_lock);
485 cur = (long long)mdd->mdd_cl.mc_index;
486 spin_unlock(&mdd->mdd_cl.mc_lock);
490 /* purge to "0" is shorthand for everything */
494 /* If purging all records, write a header entry so we don't have an
495 empty catalog and we're sure to have a valid starting index next
496 time. In case of crash, we just restart with old log so we're
499 /* XXX: transaction is started by llog itself */
500 rc = mdd_changelog_write_header(env, mdd, CLM_PURGE);
505 /* Some records were purged, so reset repeat-access time (so we
506 record new mtime update records, so users can see a file has been
507 changed since the last purge) */
508 mdd->mdd_cl.mc_starttime = ktime_get();
510 rc = llog_cancel(env, ctxt, (struct llog_cookie *)&endrec, 0);
516 /** Add a CL_MARK record to the changelog
518 * \param markerflags - CLM_*
521 int mdd_changelog_write_header(const struct lu_env *env,
522 struct mdd_device *mdd, int markerflags)
524 struct obd_device *obd = mdd2obd_dev(mdd);
525 struct llog_changelog_rec *rec;
527 struct llog_ctxt *ctxt;
529 int len = strlen(obd->obd_name);
534 if (mdd->mdd_cl.mc_mask & (1 << CL_MARK)) {
535 mdd->mdd_cl.mc_starttime = ktime_get();
539 reclen = llog_data_len(sizeof(*rec) + len);
540 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
541 if (buf->lb_buf == NULL)
545 rec->cr.cr_flags = CLF_VERSION;
546 rec->cr.cr_type = CL_MARK;
547 rec->cr.cr_namelen = len;
548 memcpy(changelog_rec_name(&rec->cr), obd->obd_name, rec->cr.cr_namelen);
549 /* Status and action flags */
550 rec->cr.cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
551 rec->cr_hdr.lrh_len = llog_data_len(changelog_rec_size(&rec->cr) +
553 rec->cr_hdr.lrh_type = CHANGELOG_REC;
554 rec->cr.cr_time = cl_time();
555 spin_lock(&mdd->mdd_cl.mc_lock);
556 rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
557 spin_unlock(&mdd->mdd_cl.mc_lock);
559 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
562 rc = llog_cat_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL);
567 /* assume on or off event; reset repeat-access time */
568 mdd->mdd_cl.mc_starttime = ktime_get();
573 * Lookup method for "fid" object. Only filenames with correct SEQ:OID format
574 * are valid. We also check if object with passed fid exists or not.
576 static int obf_lookup(const struct lu_env *env, struct md_object *p,
577 const struct lu_name *lname, struct lu_fid *f,
578 struct md_op_spec *spec)
580 char *name = (char *)lname->ln_name;
581 struct mdd_device *mdd = mdo2mdd(p);
582 struct mdd_object *child;
588 sscanf(name, SFID, RFID(f));
589 if (!fid_is_sane(f)) {
590 CWARN("%s: Trying to lookup invalid FID [%s] in %s/%s, FID "
591 "format should be "DFID"\n", mdd2obd_dev(mdd)->obd_name,
592 lname->ln_name, dot_lustre_name, mdd_obf_dir_name,
593 (__u64)FID_SEQ_NORMAL, 1, 0);
594 GOTO(out, rc = -EINVAL);
597 if (!fid_is_norm(f) && !fid_is_igif(f) && !fid_is_root(f) &&
598 !fid_seq_is_dot(f->f_seq)) {
599 CWARN("%s: Trying to lookup invalid FID "DFID" in %s/%s, "
600 "sequence should be >= %#llx or within [%#llx,"
601 "%#llx].\n", mdd2obd_dev(mdd)->obd_name, PFID(f),
602 dot_lustre_name, mdd_obf_dir_name, (__u64)FID_SEQ_NORMAL,
603 (__u64)FID_SEQ_IGIF, (__u64)FID_SEQ_IGIF_MAX);
604 GOTO(out, rc = -EINVAL);
607 /* Check if object with this fid exists */
608 child = mdd_object_find(env, mdd, f);
610 GOTO(out, rc = PTR_ERR(child));
612 if (mdd_object_exists(child) == 0)
615 mdd_object_put(env, child);
621 static int mdd_dummy_create(const struct lu_env *env,
622 struct md_object *pobj,
623 const struct lu_name *lname,
624 struct md_object *child,
625 struct md_op_spec *spec,
631 static int mdd_dummy_rename(const struct lu_env *env,
632 struct md_object *src_pobj,
633 struct md_object *tgt_pobj,
634 const struct lu_fid *lf,
635 const struct lu_name *lsname,
636 struct md_object *tobj,
637 const struct lu_name *ltname,
643 static int mdd_dummy_link(const struct lu_env *env,
644 struct md_object *tgt_obj,
645 struct md_object *src_obj,
646 const struct lu_name *lname,
652 static int mdd_dummy_unlink(const struct lu_env *env,
653 struct md_object *pobj,
654 struct md_object *cobj,
655 const struct lu_name *lname,
662 static struct md_dir_operations mdd_obf_dir_ops = {
663 .mdo_lookup = obf_lookup,
664 .mdo_create = mdd_dummy_create,
665 .mdo_rename = mdd_dummy_rename,
666 .mdo_link = mdd_dummy_link,
667 .mdo_unlink = mdd_dummy_unlink
670 static struct md_dir_operations mdd_lpf_dir_ops = {
671 .mdo_lookup = mdd_lookup,
672 .mdo_create = mdd_dummy_create,
673 .mdo_rename = mdd_dummy_rename,
674 .mdo_link = mdd_dummy_link,
675 .mdo_unlink = mdd_dummy_unlink
678 static struct md_object *mdo_locate(const struct lu_env *env,
679 struct md_device *md,
680 const struct lu_fid *fid)
682 struct lu_object *obj;
683 struct md_object *mdo;
685 obj = lu_object_find(env, &md->md_lu_dev, fid, NULL);
687 obj = lu_object_locate(obj->lo_header, md->md_lu_dev.ld_type);
688 LASSERT(obj != NULL);
691 mdo = ERR_PTR(PTR_ERR(obj));
696 static int mdd_lpf_setup(const struct lu_env *env, struct mdd_device *m)
698 struct md_object *mdo;
699 struct mdd_object *mdd_lpf;
700 struct lu_fid fid = LU_LPF_FID;
704 rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
705 mdd_lpf_dir_name, S_IFDIR | S_IRUSR | S_IXUSR,
710 mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
712 RETURN(PTR_ERR(mdo));
714 LASSERT(lu_object_exists(&mdo->mo_lu));
716 mdd_lpf = md2mdd_obj(mdo);
717 mdd_lpf->mod_obj.mo_dir_ops = &mdd_lpf_dir_ops;
718 m->mdd_dot_lustre_objs.mdd_lpf = mdd_lpf;
724 * Create special in-memory "fid" object for open-by-fid.
726 static int mdd_obf_setup(const struct lu_env *env, struct mdd_device *m)
728 struct md_object *mdo;
729 struct mdd_object *mdd_obf;
730 struct lu_fid fid = LU_OBF_FID;
733 rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
734 mdd_obf_dir_name, S_IFDIR | S_IXUSR, &fid);
738 mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
740 RETURN(PTR_ERR(mdo));
742 LASSERT(lu_object_exists(&mdo->mo_lu));
744 mdd_obf = md2mdd_obj(mdo);
745 mdd_obf->mod_obj.mo_dir_ops = &mdd_obf_dir_ops;
746 m->mdd_dot_lustre_objs.mdd_obf = mdd_obf;
751 static void mdd_dot_lustre_cleanup(const struct lu_env *env,
752 struct mdd_device *m)
754 if (m->mdd_dot_lustre_objs.mdd_lpf != NULL) {
755 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_lpf);
756 m->mdd_dot_lustre_objs.mdd_lpf = NULL;
758 if (m->mdd_dot_lustre_objs.mdd_obf != NULL) {
759 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_obf);
760 m->mdd_dot_lustre_objs.mdd_obf = NULL;
762 if (m->mdd_dot_lustre != NULL) {
763 mdd_object_put(env, m->mdd_dot_lustre);
764 m->mdd_dot_lustre = NULL;
768 /** Setup ".lustre" directory object */
769 static int mdd_dot_lustre_setup(const struct lu_env *env, struct mdd_device *m)
771 struct md_object *mdo;
776 /* Create ".lustre" directory in ROOT. */
777 fid = LU_DOT_LUSTRE_FID;
778 rc = mdd_local_file_create(env, m, &m->mdd_root_fid,
780 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO,
784 mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
786 RETURN(PTR_ERR(mdo));
787 LASSERT(lu_object_exists(&mdo->mo_lu));
789 m->mdd_dot_lustre = md2mdd_obj(mdo);
791 rc = mdd_obf_setup(env, m);
793 CERROR("%s: error initializing \"fid\" object: rc = %d.\n",
794 mdd2obd_dev(m)->obd_name, rc);
798 rc = mdd_lpf_setup(env, m);
800 CERROR("%s: error initializing \"lost+found\": rc = %d.\n",
801 mdd2obd_dev(m)->obd_name, rc);
808 mdd_dot_lustre_cleanup(env, m);
814 static struct llog_operations hsm_actions_logops;
817 * set llog methods and create LLOG_AGENT_ORIG_CTXT llog
818 * object in obd_device
820 static int mdd_hsm_actions_llog_init(const struct lu_env *env,
821 struct mdd_device *m)
823 struct obd_device *obd = mdd2obd_dev(m);
824 struct llog_ctxt *ctxt = NULL;
828 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
829 obd->obd_lvfs_ctxt.dt = m->mdd_bottom;
831 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_AGENT_ORIG_CTXT,
832 obd, &hsm_actions_logops);
834 CERROR("%s: hsm actions llog setup failed: rc = %d\n",
839 ctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
842 rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
845 CERROR("%s: hsm actions llog open_create failed: rc = %d\n",
847 GOTO(out_cleanup, rc);
850 rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
858 llog_cat_close(env, ctxt->loc_handle);
859 ctxt->loc_handle = NULL;
861 llog_cleanup(env, ctxt);
867 * cleanup the context created by llog_setup_named()
869 static int mdd_hsm_actions_llog_fini(const struct lu_env *env,
870 struct mdd_device *m)
872 struct obd_device *obd = mdd2obd_dev(m);
873 struct llog_ctxt *lctxt;
876 lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
878 llog_cat_close(env, lctxt->loc_handle);
879 lctxt->loc_handle = NULL;
880 llog_cleanup(env, lctxt);
886 static void mdd_device_shutdown(const struct lu_env *env, struct mdd_device *m,
887 struct lustre_cfg *cfg)
889 barrier_deregister(m->mdd_bottom);
890 lfsck_degister(env, m->mdd_bottom);
891 mdd_hsm_actions_llog_fini(env, m);
892 mdd_changelog_fini(env, m);
893 orph_index_fini(env, m);
894 mdd_dot_lustre_cleanup(env, m);
895 if (mdd2obd_dev(m)->u.obt.obt_nodemap_config_file) {
896 nm_config_file_deregister_tgt(env,
897 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file);
898 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file = NULL;
900 if (m->mdd_los != NULL) {
901 local_oid_storage_fini(env, m->mdd_los);
904 lu_site_purge(env, mdd2lu_dev(m)->ld_site, ~0);
906 if (m->mdd_child_exp)
907 obd_disconnect(m->mdd_child_exp);
910 static int mdd_process_config(const struct lu_env *env,
911 struct lu_device *d, struct lustre_cfg *cfg)
913 struct mdd_device *m = lu2mdd_dev(d);
914 struct dt_device *dt = m->mdd_child;
915 struct lu_device *next = &dt->dd_lu_dev;
919 switch (cfg->lcfg_command) {
921 struct obd_device *obd = mdd2obd_dev(m);
923 rc = class_process_proc_param(PARAM_MDD, obd->obd_vars, cfg, m);
924 if (rc > 0 || rc == -ENOSYS)
925 /* we don't understand; pass it on */
926 rc = next->ld_ops->ldo_process_config(env, next, cfg);
930 rc = next->ld_ops->ldo_process_config(env, next, cfg);
933 dt_conf_get(env, dt, &m->mdd_dt_conf);
935 case LCFG_PRE_CLEANUP:
936 rc = next->ld_ops->ldo_process_config(env, next, cfg);
937 mdd_generic_thread_stop(&m->mdd_orph_cleanup_thread);
940 rc = next->ld_ops->ldo_process_config(env, next, cfg);
941 lu_dev_del_linkage(d->ld_site, d);
942 mdd_device_shutdown(env, m, cfg);
945 rc = next->ld_ops->ldo_process_config(env, next, cfg);
952 static int mdd_recovery_complete(const struct lu_env *env,
955 struct mdd_device *mdd = lu2mdd_dev(d);
956 struct lu_device *next;
960 LASSERT(mdd != NULL);
961 next = &mdd->mdd_child->dd_lu_dev;
963 /* XXX: orphans handling. */
964 if (!mdd->mdd_bottom->dd_rdonly)
965 mdd_orphan_cleanup(env, mdd);
966 rc = next->ld_ops->ldo_recovery_complete(env, next);
971 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
972 const struct lu_fid *pfid, const char *name,
973 __u32 mode, struct lu_fid *fid)
975 struct dt_object *parent, *dto;
980 LASSERT(!fid_is_zero(pfid));
981 parent = dt_locate(env, mdd->mdd_bottom, pfid);
982 if (unlikely(IS_ERR(parent)))
983 RETURN(PTR_ERR(parent));
985 /* create local file/dir, if @fid is passed then try to use it */
986 if (fid_is_zero(fid))
987 dto = local_file_find_or_create(env, mdd->mdd_los, parent,
990 dto = local_file_find_or_create_with_fid(env, mdd->mdd_bottom,
994 GOTO(out_put, rc = PTR_ERR(dto));
995 *fid = *lu_object_fid(&dto->do_lu);
996 /* since stack is not fully set up the local_storage uses own stack
997 * and we should drop its object from cache */
998 dt_object_put_nocache(env, dto);
1001 dt_object_put(env, parent);
1005 static int mdd_lfsck_out_notify(const struct lu_env *env, void *data,
1006 enum lfsck_events event)
1011 static int mdd_prepare(const struct lu_env *env,
1012 struct lu_device *pdev,
1013 struct lu_device *cdev)
1015 struct mdd_device *mdd = lu2mdd_dev(cdev);
1016 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
1017 struct nm_config_file *nodemap_config;
1018 struct obd_device_target *obt = &mdd2obd_dev(mdd)->u.obt;
1024 rc = next->ld_ops->ldo_prepare(env, cdev, next);
1028 /* Setup local dirs */
1029 fid.f_seq = FID_SEQ_LOCAL_NAME;
1032 rc = local_oid_storage_init(env, mdd->mdd_bottom, &fid,
1037 rc = dt_root_get(env, mdd->mdd_child, &mdd->mdd_local_root_fid);
1042 if (mdd_seq_site(mdd)->ss_node_id == 0) {
1043 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
1044 mdd_root_dir_name, S_IFDIR |
1045 S_IRUGO | S_IWUSR | S_IXUGO, &fid);
1047 CERROR("%s: create root fid failed: rc = %d\n",
1048 mdd2obd_dev(mdd)->obd_name, rc);
1052 mdd->mdd_root_fid = fid;
1053 rc = mdd_dot_lustre_setup(env, mdd);
1055 CERROR("%s: initializing .lustre failed: rc = %d\n",
1056 mdd2obd_dev(mdd)->obd_name, rc);
1060 /* Normal client usually send root access to MDT0 directly,
1061 * the root FID on non-MDT0 will only be used by echo client. */
1062 mdd->mdd_root_fid = fid;
1065 rc = orph_index_init(env, mdd);
1069 rc = mdd_changelog_init(env, mdd);
1071 CERROR("%s: failed to initialize changelog: rc = %d\n",
1072 mdd2obd_dev(mdd)->obd_name, rc);
1076 rc = mdd_hsm_actions_llog_init(env, mdd);
1078 GOTO(out_changelog, rc);
1080 nodemap_config = nm_config_file_register_tgt(env, mdd->mdd_bottom,
1082 if (IS_ERR(nodemap_config)) {
1083 rc = PTR_ERR(nodemap_config);
1087 obt->obt_nodemap_config_file = nodemap_config;
1090 rc = lfsck_register(env, mdd->mdd_bottom, mdd->mdd_child,
1091 mdd2obd_dev(mdd), mdd_lfsck_out_notify,
1094 CERROR("%s: failed to initialize lfsck: rc = %d\n",
1095 mdd2obd_dev(mdd)->obd_name, rc);
1096 GOTO(out_nodemap, rc);
1099 rc = barrier_register(mdd->mdd_bottom, mdd->mdd_child);
1101 CERROR("%s: failed to register to barrier: rc = %d\n",
1102 mdd2obd_dev(mdd)->obd_name, rc);
1103 GOTO(out_lfsck, rc);
1109 lfsck_degister(env, mdd->mdd_bottom);
1111 nm_config_file_deregister_tgt(env, obt->obt_nodemap_config_file);
1112 obt->obt_nodemap_config_file = NULL;
1114 mdd_hsm_actions_llog_fini(env, mdd);
1116 mdd_changelog_fini(env, mdd);
1118 orph_index_fini(env, mdd);
1120 if (mdd_seq_site(mdd)->ss_node_id == 0)
1121 mdd_dot_lustre_cleanup(env, mdd);
1123 local_oid_storage_fini(env, mdd->mdd_los);
1124 mdd->mdd_los = NULL;
1129 const struct lu_device_operations mdd_lu_ops = {
1130 .ldo_object_alloc = mdd_object_alloc,
1131 .ldo_process_config = mdd_process_config,
1132 .ldo_recovery_complete = mdd_recovery_complete,
1133 .ldo_prepare = mdd_prepare,
1136 static int mdd_root_get(const struct lu_env *env,
1137 struct md_device *m, struct lu_fid *f)
1139 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1142 *f = mdd->mdd_root_fid;
1147 * No permission check is needed.
1149 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1150 struct obd_statfs *sfs)
1152 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1157 rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
1159 sfs->os_namelen = min_t(__u32, sfs->os_namelen, NAME_MAX);
1164 static const struct dt_device_param *mdd_dtconf_get(const struct lu_env *env,
1165 struct md_device *m)
1167 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1169 return &mdd->mdd_dt_conf;
1172 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1175 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1177 *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1178 return (*h == NULL ? -ENOENT : 0);
1181 static struct lu_device *mdd_device_free(const struct lu_env *env,
1182 struct lu_device *lu)
1184 struct mdd_device *m = lu2mdd_dev(lu);
1187 LASSERT(atomic_read(&lu->ld_ref) == 0);
1188 md_device_fini(&m->mdd_md_dev);
1193 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1194 struct lu_device_type *t,
1195 struct lustre_cfg *lcfg)
1197 struct lu_device *l;
1198 struct mdd_device *m;
1202 l = ERR_PTR(-ENOMEM);
1207 md_device_init(&m->mdd_md_dev, t);
1208 rc = mdd_init0(env, m, t, lcfg);
1210 mdd_device_free(env, l);
1219 * we use exports to track all mdd users
1221 static int mdd_obd_connect(const struct lu_env *env, struct obd_export **exp,
1222 struct obd_device *obd, struct obd_uuid *cluuid,
1223 struct obd_connect_data *data, void *localdata)
1225 struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1226 struct lustre_handle conn;
1230 CDEBUG(D_CONFIG, "connect #%d\n", mdd->mdd_connects);
1232 rc = class_connect(&conn, obd, cluuid);
1236 *exp = class_conn2export(&conn);
1238 /* Why should there ever be more than 1 connect? */
1239 LASSERT(mdd->mdd_connects == 0);
1240 mdd->mdd_connects++;
1246 * once last export (we don't count self-export) disappeared
1247 * mdd can be released
1249 static int mdd_obd_disconnect(struct obd_export *exp)
1251 struct obd_device *obd = exp->exp_obd;
1252 struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1253 int rc, release = 0;
1256 mdd->mdd_connects--;
1257 if (mdd->mdd_connects == 0)
1260 rc = class_disconnect(exp);
1262 if (rc == 0 && release)
1263 class_manual_cleanup(obd);
1267 static int mdd_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1268 __u32 keylen, void *key, __u32 *vallen, void *val)
1272 if (KEY_IS(KEY_OSP_CONNECTED)) {
1273 struct obd_device *obd = exp->exp_obd;
1274 struct mdd_device *mdd;
1276 if (!obd->obd_set_up || obd->obd_stopping)
1279 mdd = lu2mdd_dev(obd->obd_lu_dev);
1281 rc = obd_get_info(env, mdd->mdd_child_exp, keylen, key, vallen,
1289 static int mdd_obd_set_info_async(const struct lu_env *env,
1290 struct obd_export *exp,
1291 __u32 keylen, void *key,
1292 __u32 vallen, void *val,
1293 struct ptlrpc_request_set *set)
1295 struct obd_device *obd = exp->exp_obd;
1296 struct mdd_device *mdd;
1299 if (!obd->obd_set_up || obd->obd_stopping)
1302 mdd = lu2mdd_dev(obd->obd_lu_dev);
1304 rc = obd_set_info_async(env, mdd->mdd_child_exp, keylen, key,
1309 static struct obd_ops mdd_obd_device_ops = {
1310 .o_owner = THIS_MODULE,
1311 .o_connect = mdd_obd_connect,
1312 .o_disconnect = mdd_obd_disconnect,
1313 .o_get_info = mdd_obd_get_info,
1314 .o_set_info_async = mdd_obd_set_info_async,
1317 static int mdd_changelog_user_register(const struct lu_env *env,
1318 struct mdd_device *mdd, int *id)
1320 struct llog_ctxt *ctxt;
1321 struct llog_changelog_user_rec *rec;
1325 ctxt = llog_get_context(mdd2obd_dev(mdd),
1326 LLOG_CHANGELOG_USER_ORIG_CTXT);
1332 llog_ctxt_put(ctxt);
1336 /* Assume we want it on since somebody registered */
1337 rc = mdd_changelog_on(env, mdd);
1341 rec->cur_hdr.lrh_len = sizeof(*rec);
1342 rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1343 spin_lock(&mdd->mdd_cl.mc_user_lock);
1344 if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1345 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1346 CERROR("Maximum number of changelog users exceeded!\n");
1347 GOTO(out, rc = -EOVERFLOW);
1349 *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1350 rec->cur_endrec = mdd->mdd_cl.mc_index;
1352 rec->cur_time = (__u32)get_seconds();
1353 if (OBD_FAIL_CHECK(OBD_FAIL_TIME_IN_CHLOG_USER))
1356 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1358 rc = llog_cat_add(env, ctxt->loc_handle, &rec->cur_hdr, NULL);
1360 CDEBUG(D_IOCTL, "Registered changelog user %d\n", *id);
1363 llog_ctxt_put(ctxt);
1367 struct mdd_changelog_user_purge {
1369 __u32 mcup_usercount;
1375 * changelog_user_purge callback
1377 * Is called once per user.
1379 * Check to see the user requested is available from rec.
1380 * Truncate the changelog.
1381 * Keep track of the total number of users (calls).
1383 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1384 struct llog_handle *llh,
1385 struct llog_rec_hdr *hdr, void *data)
1387 struct llog_changelog_user_rec *rec;
1388 struct mdd_changelog_user_purge *mcup = data;
1389 struct llog_cookie cookie;
1394 if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
1397 rec = container_of(hdr, struct llog_changelog_user_rec, cur_hdr);
1399 mcup->mcup_usercount++;
1401 if (rec->cur_id != mcup->mcup_id) {
1402 /* truncate to the lowest endrec that is not this user */
1403 mcup->mcup_minrec = min(mcup->mcup_minrec,
1408 /* Unregister this user */
1409 cookie.lgc_lgl = llh->lgh_id;
1410 cookie.lgc_index = hdr->lrh_index;
1412 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1415 mcup->mcup_found = true;
1416 mcup->mcup_usercount--;
1422 int mdd_changelog_user_purge(const struct lu_env *env,
1423 struct mdd_device *mdd, __u32 id)
1425 struct mdd_changelog_user_purge mcup = {
1427 .mcup_found = false,
1428 .mcup_usercount = 0,
1429 .mcup_minrec = ULLONG_MAX,
1431 struct llog_ctxt *ctxt;
1436 CDEBUG(D_IOCTL, "%s: Purge request: id=%u\n",
1437 mdd2obd_dev(mdd)->obd_name, id);
1439 ctxt = llog_get_context(mdd2obd_dev(mdd),
1440 LLOG_CHANGELOG_USER_ORIG_CTXT);
1442 (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
1443 GOTO(out, rc = -ENXIO);
1445 rc = llog_cat_process(env, ctxt->loc_handle,
1446 mdd_changelog_user_purge_cb, &mcup,
1449 if ((rc == 0) && mcup.mcup_found) {
1450 CDEBUG(D_IOCTL, "%s: Purging changelog entries for user %d "
1452 mdd2obd_dev(mdd)->obd_name, id, mcup.mcup_minrec);
1453 /* Cancelling record 0 destroys the entire changelog, make sure
1454 we don't do that unless we mean it. */
1455 if (mcup.mcup_minrec != 0 || mcup.mcup_usercount == 0) {
1456 rc = mdd_changelog_llog_cancel(env, mdd,
1460 CWARN("%s: No changelog for user %u; rc=%d\n",
1461 mdd2obd_dev(mdd)->obd_name, id, rc);
1462 GOTO(out, rc = -ENOENT);
1465 if ((rc == 0) && (mcup.mcup_usercount == 0)) {
1466 /* No more users; turn changelogs off */
1467 CDEBUG(D_IOCTL, "turning off changelogs\n");
1468 rc = mdd_changelog_off(env, mdd);
1474 llog_ctxt_put(ctxt);
1479 struct mdd_changelog_user_clear {
1487 * changelog_clear callback
1489 * Is called once per user.
1491 * Check to see the user requested is available from rec.
1492 * Check the oldest (smallest) record for boundary conditions.
1493 * Truncate the changelog.
1495 static int mdd_changelog_clear_cb(const struct lu_env *env,
1496 struct llog_handle *llh,
1497 struct llog_rec_hdr *hdr,
1500 struct llog_changelog_user_rec *rec;
1501 struct mdd_changelog_user_clear *mcuc = data;
1506 if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
1509 rec = container_of(hdr, struct llog_changelog_user_rec, cur_hdr);
1511 /* Does the changelog id match the requested id? */
1512 if (rec->cur_id != mcuc->mcuc_id) {
1513 mcuc->mcuc_minrec = min(mcuc->mcuc_minrec,
1518 /* cur_endrec is the oldest purgeable record, make sure we're newer */
1519 if (rec->cur_endrec > mcuc->mcuc_endrec) {
1520 CDEBUG(D_IOCTL, "Request %llu out of range: %llu\n",
1521 mcuc->mcuc_endrec, rec->cur_endrec);
1525 /* Flag that we've met all the range and user checks.
1526 * We now know the record to flush.
1528 rec->cur_endrec = mcuc->mcuc_endrec;
1530 rec->cur_time = (__u32)get_seconds();
1531 if (OBD_FAIL_CHECK(OBD_FAIL_TIME_IN_CHLOG_USER))
1534 mcuc->mcuc_flush = true;
1536 CDEBUG(D_IOCTL, "Rewriting changelog user %u endrec to %llu\n",
1537 mcuc->mcuc_id, rec->cur_endrec);
1539 /* Update the endrec */
1540 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1546 * Clear a changelog up to entry specified by endrec for user id.
1548 static int mdd_changelog_clear(const struct lu_env *env,
1549 struct mdd_device *mdd, __u32 id,
1552 struct mdd_changelog_user_clear mcuc = {
1554 .mcuc_minrec = endrec,
1555 .mcuc_flush = false,
1557 struct llog_ctxt *ctxt;
1563 CDEBUG(D_IOCTL, "%s: Purge request: id=%u, endrec=%llu\n",
1564 mdd2obd_dev(mdd)->obd_name, id, endrec);
1565 /* start_rec is the newest (largest value) entry in the changelogs*/
1566 spin_lock(&mdd->mdd_cl.mc_lock);
1567 start_rec = mdd->mdd_cl.mc_index;
1568 spin_unlock(&mdd->mdd_cl.mc_lock);
1570 if (start_rec < endrec) {
1571 CDEBUG(D_IOCTL, "%s: Could not clear changelog, requested "\
1572 "address out of range\n", mdd2obd_dev(mdd)->obd_name);
1577 mcuc.mcuc_endrec = start_rec;
1578 mcuc.mcuc_minrec = ULLONG_MAX;
1580 mcuc.mcuc_endrec = endrec;
1583 ctxt = llog_get_context(mdd2obd_dev(mdd),
1584 LLOG_CHANGELOG_USER_ORIG_CTXT);
1586 (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
1587 GOTO(out, rc = -ENXIO);
1589 rc = llog_cat_process(env, ctxt->loc_handle,
1590 mdd_changelog_clear_cb, (void *)&mcuc,
1594 CWARN("%s: Failure to clear the changelog for user %d: %d\n",
1595 mdd2obd_dev(mdd)->obd_name, id, rc);
1596 } else if (mcuc.mcuc_flush) {
1597 /* Cancelling record 0 destroys the entire changelog, make sure
1598 we don't do that unless we mean it. */
1599 if (mcuc.mcuc_minrec != 0) {
1600 CDEBUG(D_IOCTL, "%s: Purging changelog entries up "\
1601 "to %llu\n", mdd2obd_dev(mdd)->obd_name,
1604 rc = mdd_changelog_llog_cancel(env, mdd,
1608 CWARN("%s: No entry for user %d\n",
1609 mdd2obd_dev(mdd)->obd_name, id);
1616 llog_ctxt_put(ctxt);
1622 * May be called remotely from mdt_iocontrol_handle or locally from
1623 * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
1624 * an obd_ioctl_data format (but local ioctl handler does).
1626 * \param len - data len
1627 * \param karg - ioctl data, in kernel space
1629 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
1630 unsigned int cmd, int len, void *karg)
1632 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1633 struct obd_ioctl_data *data = karg;
1637 /* Doesn't use obd_ioctl_data */
1639 case OBD_IOC_CHANGELOG_CLEAR: {
1640 struct changelog_setinfo *cs = karg;
1642 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1643 RETURN(-EINPROGRESS);
1645 rc = mdd_changelog_clear(env, mdd, cs->cs_id,
1647 barrier_exit(mdd->mdd_bottom);
1650 case OBD_IOC_START_LFSCK: {
1651 rc = lfsck_start(env, mdd->mdd_bottom,
1652 (struct lfsck_start_param *)karg);
1655 case OBD_IOC_STOP_LFSCK: {
1656 rc = lfsck_stop(env, mdd->mdd_bottom,
1657 (struct lfsck_stop *)karg);
1660 case OBD_IOC_QUERY_LFSCK: {
1661 rc = lfsck_query(env, mdd->mdd_bottom, NULL, NULL,
1662 (struct lfsck_query *)karg);
1667 /* Below ioctls use obd_ioctl_data */
1668 if (len != sizeof(*data)) {
1669 CERROR("Bad ioctl size %d\n", len);
1672 if (data->ioc_version != OBD_IOCTL_VERSION) {
1673 CERROR("Bad magic %x != %x\n", data->ioc_version,
1679 case OBD_IOC_CHANGELOG_REG:
1680 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1681 RETURN(-EINPROGRESS);
1683 rc = mdd_changelog_user_register(env, mdd, &data->ioc_u32_1);
1684 barrier_exit(mdd->mdd_bottom);
1686 case OBD_IOC_CHANGELOG_DEREG:
1687 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1688 RETURN(-EINPROGRESS);
1690 rc = mdd_changelog_user_purge(env, mdd, data->ioc_u32_1);
1691 barrier_exit(mdd->mdd_bottom);
1700 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
1701 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key);
1703 static const struct md_device_operations mdd_ops = {
1704 .mdo_statfs = mdd_statfs,
1705 .mdo_root_get = mdd_root_get,
1706 .mdo_llog_ctxt_get = mdd_llog_ctxt_get,
1707 .mdo_iocontrol = mdd_iocontrol,
1708 .mdo_dtconf_get = mdd_dtconf_get,
1711 static struct lu_device_type_operations mdd_device_type_ops = {
1712 .ldto_init = mdd_type_init,
1713 .ldto_fini = mdd_type_fini,
1715 .ldto_start = mdd_type_start,
1716 .ldto_stop = mdd_type_stop,
1718 .ldto_device_alloc = mdd_device_alloc,
1719 .ldto_device_free = mdd_device_free,
1721 .ldto_device_fini = mdd_device_fini
1724 static struct lu_device_type mdd_device_type = {
1725 .ldt_tags = LU_DEVICE_MD,
1726 .ldt_name = LUSTRE_MDD_NAME,
1727 .ldt_ops = &mdd_device_type_ops,
1728 .ldt_ctx_tags = LCT_MD_THREAD
1731 /* context key constructor: mdd_key_init */
1732 LU_KEY_INIT(mdd, struct mdd_thread_info);
1734 static void mdd_key_fini(const struct lu_context *ctx,
1735 struct lu_context_key *key, void *data)
1737 struct mdd_thread_info *info = data;
1739 lu_buf_free(&info->mti_big_buf);
1740 lu_buf_free(&info->mti_link_buf);
1741 lu_buf_free(&info->mti_xattr_buf);
1746 /* context key: mdd_thread_key */
1747 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
1749 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
1750 int (*func)(void *), void *data, char *name)
1752 struct task_struct *task;
1754 LASSERT(thread->mgt_init == false);
1755 init_completion(&thread->mgt_started);
1756 init_completion(&thread->mgt_finished);
1757 thread->mgt_data = data;
1758 thread->mgt_abort = false;
1759 thread->mgt_init = true;
1761 task = kthread_run(func, thread, name);
1763 complete(&thread->mgt_finished);
1764 return PTR_ERR(task);
1766 wait_for_completion(&thread->mgt_started);
1770 void mdd_generic_thread_stop(struct mdd_generic_thread *thread)
1772 if (thread->mgt_init == true) {
1773 thread->mgt_abort = true;
1774 wait_for_completion(&thread->mgt_finished);
1778 static int __init mdd_init(void)
1782 rc = lu_kmem_init(mdd_caches);
1786 changelog_orig_logops = llog_osd_ops;
1787 changelog_orig_logops.lop_cancel = llog_changelog_cancel;
1788 changelog_orig_logops.lop_add = llog_cat_add_rec;
1789 changelog_orig_logops.lop_declare_add = llog_cat_declare_add_rec;
1791 hsm_actions_logops = llog_osd_ops;
1792 hsm_actions_logops.lop_add = llog_cat_add_rec;
1793 hsm_actions_logops.lop_declare_add = llog_cat_declare_add_rec;
1795 rc = class_register_type(&mdd_obd_device_ops, NULL, true, NULL,
1796 LUSTRE_MDD_NAME, &mdd_device_type);
1798 lu_kmem_fini(mdd_caches);
1802 static void __exit mdd_exit(void)
1804 class_unregister_type(LUSTRE_MDD_NAME);
1805 lu_kmem_fini(mdd_caches);
1808 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1809 MODULE_DESCRIPTION("Lustre Meta-data Device Driver ("LUSTRE_MDD_NAME")");
1810 MODULE_VERSION(LUSTRE_VERSION_STRING);
1811 MODULE_LICENSE("GPL");
1813 module_init(mdd_init);
1814 module_exit(mdd_exit);