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/
31 * lustre/mdd/mdd_device.c
33 * Lustre Metadata Server (mdd) routines
35 * Author: Wang Di <wangdi@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <linux/module.h>
41 #include <linux/kthread.h>
42 #include <obd_class.h>
43 #include <uapi/linux/lustre/lustre_ioctl.h>
44 #include <lustre_mds.h>
45 #include <obd_support.h>
46 #include <lu_object.h>
47 #include <uapi/linux/lustre/lustre_param.h>
48 #include <lustre_fid.h>
49 #include <lustre_nodemap.h>
50 #include <lustre_barrier.h>
52 #include "mdd_internal.h"
54 static const struct md_device_operations mdd_ops;
55 static struct lu_device_type mdd_device_type;
57 static const char mdd_root_dir_name[] = "ROOT";
58 static const char mdd_obf_dir_name[] = "fid";
59 static const char mdd_lpf_dir_name[] = "lost+found";
61 /* Slab for MDD object allocation */
62 struct kmem_cache *mdd_object_kmem;
64 static struct lu_kmem_descr mdd_caches[] = {
66 .ckd_cache = &mdd_object_kmem,
67 .ckd_name = "mdd_obj",
68 .ckd_size = sizeof(struct mdd_object)
75 static int mdd_connect_to_next(const struct lu_env *env, struct mdd_device *m,
78 struct obd_connect_data *data = NULL;
79 struct lu_device *lud = mdd2lu_dev(m);
80 struct obd_device *obd;
84 LASSERT(m->mdd_child_exp == NULL);
86 OBD_ALLOC(data, sizeof(*data));
88 GOTO(out, rc = -ENOMEM);
90 obd = class_name2obd(nextdev);
92 CERROR("can't locate next device: %s\n", nextdev);
93 GOTO(out, rc = -ENOTCONN);
96 data->ocd_connect_flags = OBD_CONNECT_VERSION;
97 data->ocd_version = LUSTRE_VERSION_CODE;
99 rc = obd_connect(NULL, &m->mdd_child_exp, obd, &obd->obd_uuid, data, NULL);
101 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
105 lud->ld_site = m->mdd_child_exp->exp_obd->obd_lu_dev->ld_site;
106 LASSERT(lud->ld_site);
107 m->mdd_child = lu2dt_dev(m->mdd_child_exp->exp_obd->obd_lu_dev);
108 m->mdd_bottom = lu2dt_dev(lud->ld_site->ls_bottom_dev);
109 lu_dev_add_linkage(lud->ld_site, lud);
113 OBD_FREE(data, sizeof(*data));
117 static int mdd_init0(const struct lu_env *env, struct mdd_device *mdd,
118 struct lu_device_type *t, struct lustre_cfg *lcfg)
124 /* LU-8040 Set defaults here, before values configs */
125 mdd->mdd_cl.mc_flags = 0; /* off by default */
126 mdd->mdd_cl.mc_mask = CHANGELOG_DEFMASK;
127 mdd->mdd_cl.mc_deniednext = 60; /* 60 secs by default */
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;
146 /* enable changelog garbage collection */
147 mdd->mdd_changelog_gc = 1;
148 /* with a significant amount of idle time */
149 mdd->mdd_changelog_max_idle_time = CHLOG_MAX_IDLE_TIME;
150 /* or a significant amount of late indexes */
151 mdd->mdd_changelog_max_idle_indexes = CHLOG_MAX_IDLE_INDEXES;
152 /* with a reasonable interval between each check */
153 mdd->mdd_changelog_min_gc_interval = CHLOG_MIN_GC_INTERVAL;
154 /* with a very few number of free catalog entries */
155 mdd->mdd_changelog_min_free_cat_entries = CHLOG_MIN_FREE_CAT_ENTRIES;
156 /* special default striping for files created with O_APPEND */
157 mdd->mdd_append_stripe_count = 1;
158 mdd->mdd_append_pool[0] = '\0';
160 dt_conf_get(env, mdd->mdd_child, &mdd->mdd_dt_conf);
162 /* we are using service name but not mdd obd name
163 * for compatibility reasons.
164 * It is passed from MDT in lustre_cfg[2] buffer */
165 rc = mdd_procfs_init(mdd, lustre_cfg_string(lcfg, 2));
167 obd_disconnect(mdd->mdd_child_exp);
172 static struct lu_device *mdd_device_fini(const struct lu_env *env,
175 struct mdd_device *mdd = lu2mdd_dev(d);
178 lu_dev_del_linkage(d->ld_site, d);
180 mdd_procfs_fini(mdd);
184 static int changelog_init_cb(const struct lu_env *env, struct llog_handle *llh,
185 struct llog_rec_hdr *hdr, void *data)
187 struct mdd_device *mdd = (struct mdd_device *)data;
188 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
190 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
191 LASSERT(rec->cr_hdr.lrh_type == CHANGELOG_REC);
194 "seeing record at index %d/%d/%llu t=%x %.*s in log"
195 DFID"\n", hdr->lrh_index, rec->cr_hdr.lrh_index,
196 rec->cr.cr_index, rec->cr.cr_type, rec->cr.cr_namelen,
197 changelog_rec_name(&rec->cr), PFID(&llh->lgh_id.lgl_oi.oi_fid));
199 mdd->mdd_cl.mc_index = rec->cr.cr_index;
200 return LLOG_PROC_BREAK;
203 static int changelog_user_init_cb(const struct lu_env *env,
204 struct llog_handle *llh,
205 struct llog_rec_hdr *hdr, void *data)
207 struct mdd_device *mdd = (struct mdd_device *)data;
208 struct llog_changelog_user_rec *rec =
209 (struct llog_changelog_user_rec *)hdr;
211 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
212 LASSERT(rec->cur_hdr.lrh_type == CHANGELOG_USER_REC);
214 CDEBUG(D_INFO, "seeing user at index %d/%d id=%d endrec=%llu"
215 " in log "DFID"\n", hdr->lrh_index, rec->cur_hdr.lrh_index,
216 rec->cur_id, rec->cur_endrec, PFID(&llh->lgh_id.lgl_oi.oi_fid));
218 spin_lock(&mdd->mdd_cl.mc_user_lock);
219 mdd->mdd_cl.mc_lastuser = rec->cur_id;
220 mdd->mdd_cl.mc_users++;
221 if (rec->cur_endrec > mdd->mdd_cl.mc_index)
222 mdd->mdd_cl.mc_index = rec->cur_endrec;
223 spin_unlock(&mdd->mdd_cl.mc_user_lock);
225 return LLOG_PROC_BREAK;
228 struct changelog_orphan_data {
230 struct mdd_device *mdd;
233 /* find oldest changelog record index */
234 static int changelog_detect_orphan_cb(const struct lu_env *env,
235 struct llog_handle *llh,
236 struct llog_rec_hdr *hdr, void *data)
238 struct mdd_device *mdd = ((struct changelog_orphan_data *)data)->mdd;
239 struct llog_changelog_rec *rec = container_of(hdr,
240 struct llog_changelog_rec,
243 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
245 if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
246 CWARN("%s: invalid record at index %d in log "DFID"\n",
247 mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
248 PFID(&llh->lgh_id.lgl_oi.oi_fid));
249 /* try to find some next valid record and thus allow to recover
250 * from a corrupted LLOG, instead to assert and force a crash
255 CDEBUG(D_INFO, "%s: seeing record at index %d/%d/%llu t=%x %.*s in log "
256 DFID"\n", mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
257 rec->cr_hdr.lrh_index, rec->cr.cr_index, rec->cr.cr_type,
258 rec->cr.cr_namelen, changelog_rec_name(&rec->cr),
259 PFID(&llh->lgh_id.lgl_oi.oi_fid));
261 ((struct changelog_orphan_data *)data)->index = rec->cr.cr_index;
262 return LLOG_PROC_BREAK;
265 /* find oldest changelog user index */
266 static int changelog_user_detect_orphan_cb(const struct lu_env *env,
267 struct llog_handle *llh,
268 struct llog_rec_hdr *hdr, void *data)
270 struct mdd_device *mdd = ((struct changelog_orphan_data *)data)->mdd;
271 struct llog_changelog_user_rec *rec = container_of(hdr,
272 struct llog_changelog_user_rec,
275 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
277 if (rec->cur_hdr.lrh_type != CHANGELOG_USER_REC) {
278 CWARN("%s: invalid user at index %d in log "DFID"\n",
279 mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
280 PFID(&llh->lgh_id.lgl_oi.oi_fid));
281 /* try to find some next valid record and thus allow to recover
282 * from a corrupted LLOG, instead to assert and force a crash
287 CDEBUG(D_INFO, "%s: seeing user at index %d/%d id=%d endrec=%llu in "
288 "log "DFID"\n", mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
289 rec->cur_hdr.lrh_index, rec->cur_id, rec->cur_endrec,
290 PFID(&llh->lgh_id.lgl_oi.oi_fid));
292 if (((struct changelog_orphan_data *)data)->index == 0 ||
293 rec->cur_endrec < ((struct changelog_orphan_data *)data)->index)
294 ((struct changelog_orphan_data *)data)->index = rec->cur_endrec;
299 struct changelog_cancel_cookie {
301 struct mdd_device *mdd;
304 static int llog_changelog_cancel_cb(const struct lu_env *env,
305 struct llog_handle *llh,
306 struct llog_rec_hdr *hdr, void *data)
308 struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
309 struct llog_cookie cookie;
310 struct changelog_cancel_cookie *cl_cookie =
311 (struct changelog_cancel_cookie *)data;
316 /* This is always a (sub)log, not the catalog */
317 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
319 /* if current context is GC-thread allow it to stop upon umount
320 * remaining records cleanup will occur upon next mount
322 * also during testing, wait for GC-thread to be released
324 * XXX this requires the GC-thread to not fork a sub-thread via
325 * llog[_cat]_process_or_fork() and we may think to also implement
326 * this shutdown mechanism for manually started user unregister which
327 * can also take a long time if huge backlog of records
329 if (unlikely(cl_cookie->mdd->mdd_cl.mc_gc_task == current)) {
330 /* wait to be released */
331 while (CFS_FAIL_CHECK_QUIET(OBD_FAIL_FORCE_GC_THREAD))
334 if (kthread_should_stop())
335 RETURN(LLOG_PROC_BREAK);
338 if (rec->cr.cr_index > cl_cookie->endrec)
339 /* records are in order, so we're done */
340 RETURN(LLOG_PROC_BREAK);
342 cookie.lgc_lgl = llh->lgh_id;
343 cookie.lgc_index = hdr->lrh_index;
345 /* cancel them one at a time. I suppose we could store up the cookies
346 * and cancel them all at once; probably more efficient, but this is
347 * done as a user call, so who cares... */
348 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1,
350 RETURN(rc < 0 ? rc : 0);
353 static int llog_changelog_cancel(const struct lu_env *env,
354 struct llog_ctxt *ctxt,
355 struct changelog_cancel_cookie *cookie)
357 struct llog_handle *cathandle = ctxt->loc_handle;
362 /* This should only be called with the catalog handle */
363 LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
365 rc = llog_cat_process(env, cathandle, llog_changelog_cancel_cb,
368 /* 0 or 1 means we're done */
371 CERROR("%s: cancel idx %u of catalog "DFID": rc = %d\n",
372 ctxt->loc_obd->obd_name, cathandle->lgh_last_idx,
373 PFID(&cathandle->lgh_id.lgl_oi.oi_fid), rc);
378 static struct llog_operations changelog_orig_logops;
381 mdd_changelog_write_header(const struct lu_env *env, struct mdd_device *mdd,
385 mdd_changelog_on(const struct lu_env *env, struct mdd_device *mdd)
389 if ((mdd->mdd_cl.mc_flags & CLM_ON) != 0)
392 LCONSOLE_INFO("%s: changelog on\n", mdd2obd_dev(mdd)->obd_name);
393 if (mdd->mdd_cl.mc_flags & CLM_ERR) {
394 CERROR("Changelogs cannot be enabled due to error "
395 "condition (see %s log).\n",
396 mdd2obd_dev(mdd)->obd_name);
399 spin_lock(&mdd->mdd_cl.mc_lock);
400 mdd->mdd_cl.mc_flags |= CLM_ON;
401 spin_unlock(&mdd->mdd_cl.mc_lock);
402 rc = mdd_changelog_write_header(env, mdd, CLM_START);
408 mdd_changelog_off(const struct lu_env *env, struct mdd_device *mdd)
412 if ((mdd->mdd_cl.mc_flags & CLM_ON) != CLM_ON)
415 LCONSOLE_INFO("%s: changelog off\n", mdd2obd_dev(mdd)->obd_name);
416 rc = mdd_changelog_write_header(env, mdd, CLM_FINI);
417 spin_lock(&mdd->mdd_cl.mc_lock);
418 mdd->mdd_cl.mc_flags &= ~CLM_ON;
419 spin_unlock(&mdd->mdd_cl.mc_lock);
424 static int mdd_changelog_llog_init(const struct lu_env *env,
425 struct mdd_device *mdd)
427 struct obd_device *obd = mdd2obd_dev(mdd);
428 struct llog_ctxt *ctxt = NULL, *uctxt = NULL;
429 struct changelog_orphan_data changelog_orphan = { .index = 0,
431 user_orphan = { .index = 0,
437 /* LU-2844 mdd setup failure should not cause umount oops */
438 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_INIT))
441 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
442 obd->obd_lvfs_ctxt.dt = mdd->mdd_bottom;
443 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
444 obd, &changelog_orig_logops);
446 CERROR("%s: changelog llog setup failed: rc = %d\n",
451 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
454 rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
457 GOTO(out_cleanup, rc);
459 rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
463 rc = llog_cat_reverse_process(env, ctxt->loc_handle,
464 changelog_init_cb, mdd);
467 CERROR("%s: changelog init failed: rc = %d\n", obd->obd_name,
472 CDEBUG(D_IOCTL, "changelog starting index=%llu\n",
473 mdd->mdd_cl.mc_index);
475 /* setup user changelog */
476 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
477 obd, &changelog_orig_logops);
479 CERROR("%s: changelog users llog setup failed: rc = %d\n",
484 uctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
487 rc = llog_open_create(env, uctxt, &uctxt->loc_handle, NULL,
490 GOTO(out_ucleanup, rc);
492 rc = llog_init_handle(env, uctxt->loc_handle, LLOG_F_IS_CAT, NULL);
494 GOTO(out_uclose, rc);
496 rc = llog_cat_reverse_process(env, uctxt->loc_handle,
497 changelog_user_init_cb, mdd);
499 CERROR("%s: changelog user init failed: rc = %d\n",
501 GOTO(out_uclose, rc);
504 /* If we have registered users, assume we want changelogs on */
505 if (mdd->mdd_cl.mc_lastuser > 0) {
506 rc = mdd_changelog_on(env, mdd);
508 GOTO(out_uclose, rc);
511 /* find and clear any orphan changelog records (1st record index <
512 * smallest of all users current index), likely to come from an
513 * interrupted manual or GC-thread purge, as its user record had
515 * XXX we may wait for a still registered user clear operation to
516 * do the job, but it may then take a long time to reach the user's
517 * real targetted records if a huge purge backlog is still to be
518 * processed as a long time idle user record could have been deleted
519 * XXX we may need to run end of purge as a separate thread
521 rc = llog_cat_process(env, ctxt->loc_handle, changelog_detect_orphan_cb,
522 &changelog_orphan, 0, 0);
524 CERROR("%s: changelog detect orphan failed: rc = %d\n",
526 GOTO(out_uclose, rc);
528 rc = llog_cat_process(env, uctxt->loc_handle,
529 changelog_user_detect_orphan_cb,
532 CERROR("%s: changelog user detect orphan failed: rc = %d\n",
534 GOTO(out_uclose, rc);
536 if (unlikely(changelog_orphan.index < user_orphan.index)) {
537 struct changelog_cancel_cookie cl_cookie = {
538 .endrec = user_orphan.index,
542 CWARN("%s : orphan changelog records found, starting from "
543 "index %llu to index %llu, being cleared now\n",
544 obd->obd_name, changelog_orphan.index, user_orphan.index);
546 /* XXX we may need to run end of purge as a separate thread */
547 rc = llog_changelog_cancel(env, ctxt, &cl_cookie);
549 CERROR("%s: purge of changelog orphan records failed: "
550 "rc = %d\n", obd->obd_name, rc);
551 GOTO(out_uclose, rc);
556 llog_ctxt_put(uctxt);
559 llog_cat_close(env, uctxt->loc_handle);
561 llog_cleanup(env, uctxt);
563 llog_cat_close(env, ctxt->loc_handle);
565 llog_cleanup(env, ctxt);
569 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
571 struct obd_device *obd = mdd2obd_dev(mdd);
574 mdd->mdd_cl.mc_index = 0;
575 spin_lock_init(&mdd->mdd_cl.mc_lock);
576 mdd->mdd_cl.mc_starttime = ktime_get();
577 spin_lock_init(&mdd->mdd_cl.mc_user_lock);
578 mdd->mdd_cl.mc_lastuser = 0;
580 /* ensure a GC check will, and a thread run may, occur upon start */
581 mdd->mdd_cl.mc_gc_time = 0;
582 mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NONE;
584 rc = mdd_changelog_llog_init(env, mdd);
586 CERROR("%s: changelog setup during init failed: rc = %d\n",
588 mdd->mdd_cl.mc_flags |= CLM_ERR;
594 static void mdd_changelog_fini(const struct lu_env *env,
595 struct mdd_device *mdd)
597 struct obd_device *obd = mdd2obd_dev(mdd);
598 struct llog_ctxt *ctxt;
600 if (mdd->mdd_cl.mc_flags & CLM_CLEANUP_DONE)
602 mdd->mdd_cl.mc_flags = CLM_CLEANUP_DONE;
605 /* stop GC-thread if running */
606 spin_lock(&mdd->mdd_cl.mc_lock);
607 if (likely(mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE)) {
608 /* avoid any attempt to run a GC-thread */
609 mdd->mdd_cl.mc_gc_task = current;
610 spin_unlock(&mdd->mdd_cl.mc_lock);
612 struct task_struct *gc_task;
614 if (unlikely(mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NEED ||
615 mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_START)) {
616 /* need to wait for birthing GC-thread to be started
617 * and to have set mc_gc_task to itself
619 spin_unlock(&mdd->mdd_cl.mc_lock);
620 /* Add a tiny sleep */
621 schedule_timeout_uninterruptible(1);
622 /* go back to fully check if GC-thread has started or
623 * even already exited or if a new one is starting...
627 /* take a reference on task_struct to avoid it to be freed
630 gc_task = mdd->mdd_cl.mc_gc_task;
631 get_task_struct(gc_task);
632 spin_unlock(&mdd->mdd_cl.mc_lock);
633 kthread_stop(gc_task);
634 put_task_struct(gc_task);
637 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
639 llog_cat_close(env, ctxt->loc_handle);
640 llog_cleanup(env, ctxt);
642 ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
644 llog_cat_close(env, ctxt->loc_handle);
645 llog_cleanup(env, ctxt);
649 /** Remove entries with indicies up to and including \a endrec from the
656 mdd_changelog_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
659 struct obd_device *obd = mdd2obd_dev(mdd);
660 struct llog_ctxt *ctxt;
661 long long unsigned cur;
662 struct changelog_cancel_cookie cookie;
665 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
669 spin_lock(&mdd->mdd_cl.mc_lock);
670 cur = (long long)mdd->mdd_cl.mc_index;
671 spin_unlock(&mdd->mdd_cl.mc_lock);
675 /* purge to "0" is shorthand for everything */
679 /* If purging all records, write a header entry so we don't have an
680 empty catalog and we're sure to have a valid starting index next
681 time. In case of crash, we just restart with old log so we're
684 /* XXX: transaction is started by llog itself */
685 rc = mdd_changelog_write_header(env, mdd, CLM_PURGE);
690 /* Some records were purged, so reset repeat-access time (so we
691 record new mtime update records, so users can see a file has been
692 changed since the last purge) */
693 mdd->mdd_cl.mc_starttime = ktime_get();
695 cookie.endrec = endrec;
697 rc = llog_changelog_cancel(env, ctxt, &cookie);
703 /** Add a CL_MARK record to the changelog
705 * \param markerflags - CLM_*
708 int mdd_changelog_write_header(const struct lu_env *env,
709 struct mdd_device *mdd, int markerflags)
711 struct obd_device *obd = mdd2obd_dev(mdd);
712 struct llog_changelog_rec *rec;
714 struct llog_ctxt *ctxt;
716 int len = strlen(obd->obd_name);
721 if (mdd->mdd_cl.mc_mask & BIT(CL_MARK)) {
722 mdd->mdd_cl.mc_starttime = ktime_get();
726 reclen = llog_data_len(sizeof(*rec) + len);
727 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
728 if (buf->lb_buf == NULL)
732 rec->cr.cr_flags = CLF_VERSION;
733 rec->cr.cr_type = CL_MARK;
734 rec->cr.cr_namelen = len;
735 memcpy(changelog_rec_name(&rec->cr), obd->obd_name, rec->cr.cr_namelen);
736 /* Status and action flags */
737 rec->cr.cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
738 rec->cr_hdr.lrh_len = llog_data_len(changelog_rec_size(&rec->cr) +
740 rec->cr_hdr.lrh_type = CHANGELOG_REC;
741 rec->cr.cr_time = cl_time();
743 ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
746 rc = llog_cat_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL);
751 /* assume on or off event; reset repeat-access time */
752 mdd->mdd_cl.mc_starttime = ktime_get();
757 * Lookup method for "fid" object. Only filenames with correct SEQ:OID format
758 * are valid. We also check if object with passed fid exists or not.
760 static int obf_lookup(const struct lu_env *env, struct md_object *p,
761 const struct lu_name *lname, struct lu_fid *f,
762 struct md_op_spec *spec)
764 char *name = (char *)lname->ln_name;
765 struct mdd_device *mdd = mdo2mdd(p);
766 struct mdd_object *child;
772 sscanf(name, SFID, RFID(f));
774 GOTO(out, rc = -ENOENT);
776 if (!fid_is_norm(f) && !fid_is_igif(f) && !fid_is_root(f) &&
777 !fid_seq_is_dot(f->f_seq))
778 GOTO(out, rc = -ENOENT);
780 /* Check if object with this fid exists */
781 child = mdd_object_find(env, mdd, f);
783 GOTO(out, rc = PTR_ERR(child));
785 if (mdd_object_exists(child) == 0)
788 mdd_object_put(env, child);
794 static int mdd_dummy_create(const struct lu_env *env,
795 struct md_object *pobj,
796 const struct lu_name *lname,
797 struct md_object *child,
798 struct md_op_spec *spec,
804 static int mdd_dummy_rename(const struct lu_env *env,
805 struct md_object *src_pobj,
806 struct md_object *tgt_pobj,
807 const struct lu_fid *lf,
808 const struct lu_name *lsname,
809 struct md_object *tobj,
810 const struct lu_name *ltname,
816 static int mdd_dummy_link(const struct lu_env *env,
817 struct md_object *tgt_obj,
818 struct md_object *src_obj,
819 const struct lu_name *lname,
825 static int mdd_dummy_unlink(const struct lu_env *env,
826 struct md_object *pobj,
827 struct md_object *cobj,
828 const struct lu_name *lname,
835 int mdd_create(const struct lu_env *env, struct md_object *pobj,
836 const struct lu_name *lname, struct md_object *child,
837 struct md_op_spec *spec, struct md_attr *ma);
838 static int mdd_obf_create(const struct lu_env *env, struct md_object *pobj,
839 const struct lu_name *lname, struct md_object *child,
840 struct md_op_spec *spec, struct md_attr *ma)
842 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
843 return mdd_create(env, pobj, lname, child, spec, ma);
847 static const struct md_dir_operations mdd_obf_dir_ops = {
848 .mdo_lookup = obf_lookup,
849 .mdo_create = mdd_obf_create,
850 .mdo_rename = mdd_dummy_rename,
851 .mdo_link = mdd_dummy_link,
852 .mdo_unlink = mdd_dummy_unlink
855 static const struct md_dir_operations mdd_lpf_dir_ops = {
856 .mdo_lookup = mdd_lookup,
857 .mdo_create = mdd_dummy_create,
858 .mdo_rename = mdd_dummy_rename,
859 .mdo_link = mdd_dummy_link,
860 .mdo_unlink = mdd_dummy_unlink
863 static struct md_object *mdo_locate(const struct lu_env *env,
864 struct md_device *md,
865 const struct lu_fid *fid)
867 struct lu_object *obj;
868 struct md_object *mdo;
870 obj = lu_object_find(env, &md->md_lu_dev, fid, NULL);
872 obj = lu_object_locate(obj->lo_header, md->md_lu_dev.ld_type);
873 LASSERT(obj != NULL);
881 static int mdd_lpf_setup(const struct lu_env *env, struct mdd_device *m)
883 struct md_object *mdo;
884 struct mdd_object *mdd_lpf;
885 struct lu_fid fid = LU_LPF_FID;
889 rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
890 mdd_lpf_dir_name, S_IFDIR | S_IRUSR | S_IXUSR,
895 mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
897 RETURN(PTR_ERR(mdo));
899 LASSERT(lu_object_exists(&mdo->mo_lu));
901 mdd_lpf = md2mdd_obj(mdo);
902 mdd_lpf->mod_obj.mo_dir_ops = &mdd_lpf_dir_ops;
903 m->mdd_dot_lustre_objs.mdd_lpf = mdd_lpf;
909 * Create special in-memory "fid" object for open-by-fid.
911 static int mdd_obf_setup(const struct lu_env *env, struct mdd_device *m)
913 struct md_object *mdo;
914 struct mdd_object *mdd_obf;
915 struct lu_fid fid = LU_OBF_FID;
918 rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
919 mdd_obf_dir_name, S_IFDIR | S_IXUSR, &fid);
923 mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
925 RETURN(PTR_ERR(mdo));
927 LASSERT(lu_object_exists(&mdo->mo_lu));
929 mdd_obf = md2mdd_obj(mdo);
930 mdd_obf->mod_obj.mo_dir_ops = &mdd_obf_dir_ops;
931 m->mdd_dot_lustre_objs.mdd_obf = mdd_obf;
936 static void mdd_dot_lustre_cleanup(const struct lu_env *env,
937 struct mdd_device *m)
939 if (m->mdd_dot_lustre_objs.mdd_lpf != NULL) {
940 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_lpf);
941 m->mdd_dot_lustre_objs.mdd_lpf = NULL;
943 if (m->mdd_dot_lustre_objs.mdd_obf != NULL) {
944 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_obf);
945 m->mdd_dot_lustre_objs.mdd_obf = NULL;
947 if (m->mdd_dot_lustre != NULL) {
948 mdd_object_put(env, m->mdd_dot_lustre);
949 m->mdd_dot_lustre = NULL;
953 /** Setup ".lustre" directory object */
954 static int mdd_dot_lustre_setup(const struct lu_env *env, struct mdd_device *m)
956 struct md_object *mdo;
961 /* Create ".lustre" directory in ROOT. */
962 fid = LU_DOT_LUSTRE_FID;
963 rc = mdd_local_file_create(env, m, &m->mdd_root_fid,
965 S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO,
969 mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
971 RETURN(PTR_ERR(mdo));
972 LASSERT(lu_object_exists(&mdo->mo_lu));
974 m->mdd_dot_lustre = md2mdd_obj(mdo);
976 rc = mdd_obf_setup(env, m);
978 CERROR("%s: error initializing \"fid\" object: rc = %d.\n",
979 mdd2obd_dev(m)->obd_name, rc);
983 rc = mdd_lpf_setup(env, m);
985 CERROR("%s: error initializing \"lost+found\": rc = %d.\n",
986 mdd2obd_dev(m)->obd_name, rc);
993 mdd_dot_lustre_cleanup(env, m);
999 * set llog methods and create LLOG_AGENT_ORIG_CTXT llog
1000 * object in obd_device
1002 static int mdd_hsm_actions_llog_init(const struct lu_env *env,
1003 struct mdd_device *m)
1005 struct obd_device *obd = mdd2obd_dev(m);
1006 struct llog_ctxt *ctxt = NULL;
1010 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1011 obd->obd_lvfs_ctxt.dt = m->mdd_bottom;
1013 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_AGENT_ORIG_CTXT,
1014 obd, &llog_common_cat_ops);
1016 CERROR("%s: hsm actions llog setup failed: rc = %d\n",
1021 ctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
1024 rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
1027 CERROR("%s: hsm actions llog open_create failed: rc = %d\n",
1029 GOTO(out_cleanup, rc);
1032 rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
1034 GOTO(out_close, rc);
1036 llog_ctxt_put(ctxt);
1040 llog_cat_close(env, ctxt->loc_handle);
1041 ctxt->loc_handle = NULL;
1043 llog_cleanup(env, ctxt);
1049 * cleanup the context created by llog_setup_named()
1051 static int mdd_hsm_actions_llog_fini(const struct lu_env *env,
1052 struct mdd_device *m)
1054 struct obd_device *obd = mdd2obd_dev(m);
1055 struct llog_ctxt *lctxt;
1058 lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
1060 llog_cat_close(env, lctxt->loc_handle);
1061 lctxt->loc_handle = NULL;
1062 llog_cleanup(env, lctxt);
1068 static void mdd_device_shutdown(const struct lu_env *env, struct mdd_device *m,
1069 struct lustre_cfg *cfg)
1071 barrier_deregister(m->mdd_bottom);
1072 lfsck_degister(env, m->mdd_bottom);
1073 mdd_hsm_actions_llog_fini(env, m);
1074 mdd_changelog_fini(env, m);
1075 mdd_orphan_index_fini(env, m);
1076 mdd_dot_lustre_cleanup(env, m);
1077 if (mdd2obd_dev(m)->u.obt.obt_nodemap_config_file) {
1078 nm_config_file_deregister_tgt(env,
1079 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file);
1080 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file = NULL;
1082 if (m->mdd_los != NULL) {
1083 local_oid_storage_fini(env, m->mdd_los);
1086 lu_site_purge(env, mdd2lu_dev(m)->ld_site, ~0);
1088 if (m->mdd_child_exp)
1089 obd_disconnect(m->mdd_child_exp);
1092 static int mdd_process_config(const struct lu_env *env,
1093 struct lu_device *d, struct lustre_cfg *cfg)
1095 struct mdd_device *m = lu2mdd_dev(d);
1096 struct dt_device *dt = m->mdd_child;
1097 struct lu_device *next = &dt->dd_lu_dev;
1101 switch (cfg->lcfg_command) {
1105 count = class_modify_config(cfg, PARAM_MDD, &m->mdd_kobj);
1106 rc = count > 0 ? 0 : count;
1108 /* we don't understand; pass it on */
1109 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1113 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1116 dt_conf_get(env, dt, &m->mdd_dt_conf);
1118 case LCFG_PRE_CLEANUP:
1119 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1120 mdd_generic_thread_stop(&m->mdd_orphan_cleanup_thread);
1123 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1124 lu_dev_del_linkage(d->ld_site, d);
1125 mdd_device_shutdown(env, m, cfg);
1128 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1135 static int mdd_recovery_complete(const struct lu_env *env,
1136 struct lu_device *d)
1138 struct mdd_device *mdd = lu2mdd_dev(d);
1139 struct lu_device *next;
1143 LASSERT(mdd != NULL);
1144 next = &mdd->mdd_child->dd_lu_dev;
1146 if (!mdd->mdd_bottom->dd_rdonly)
1147 mdd_orphan_cleanup(env, mdd);
1148 rc = next->ld_ops->ldo_recovery_complete(env, next);
1153 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
1154 const struct lu_fid *pfid, const char *name,
1155 __u32 mode, struct lu_fid *fid)
1157 struct dt_object *parent, *dto;
1162 LASSERT(!fid_is_zero(pfid));
1163 parent = dt_locate(env, mdd->mdd_bottom, pfid);
1164 if (unlikely(IS_ERR(parent)))
1165 RETURN(PTR_ERR(parent));
1167 /* create local file/dir, if @fid is passed then try to use it */
1168 if (fid_is_zero(fid))
1169 dto = local_file_find_or_create(env, mdd->mdd_los, parent,
1172 dto = local_file_find_or_create_with_fid(env, mdd->mdd_bottom,
1176 GOTO(out_put, rc = PTR_ERR(dto));
1177 *fid = *lu_object_fid(&dto->do_lu);
1178 /* since stack is not fully set up the local_storage uses own stack
1179 * and we should drop its object from cache */
1180 dt_object_put_nocache(env, dto);
1183 dt_object_put(env, parent);
1187 static int mdd_lfsck_out_notify(const struct lu_env *env, void *data,
1188 enum lfsck_events event)
1193 static int mdd_prepare(const struct lu_env *env,
1194 struct lu_device *pdev,
1195 struct lu_device *cdev)
1197 struct mdd_device *mdd = lu2mdd_dev(cdev);
1198 struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
1199 struct nm_config_file *nodemap_config;
1200 struct obd_device_target *obt = &mdd2obd_dev(mdd)->u.obt;
1206 rc = next->ld_ops->ldo_prepare(env, cdev, next);
1210 /* Setup local dirs */
1211 fid.f_seq = FID_SEQ_LOCAL_NAME;
1214 rc = local_oid_storage_init(env, mdd->mdd_bottom, &fid,
1219 rc = dt_root_get(env, mdd->mdd_child, &mdd->mdd_local_root_fid);
1224 if (mdd_seq_site(mdd)->ss_node_id == 0) {
1225 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
1226 mdd_root_dir_name, S_IFDIR |
1227 S_IRUGO | S_IWUSR | S_IXUGO, &fid);
1229 CERROR("%s: create root fid failed: rc = %d\n",
1230 mdd2obd_dev(mdd)->obd_name, rc);
1234 mdd->mdd_root_fid = fid;
1235 rc = mdd_dot_lustre_setup(env, mdd);
1237 CERROR("%s: initializing .lustre failed: rc = %d\n",
1238 mdd2obd_dev(mdd)->obd_name, rc);
1242 /* Normal client usually send root access to MDT0 directly,
1243 * the root FID on non-MDT0 will only be used by echo client. */
1244 mdd->mdd_root_fid = fid;
1247 rc = mdd_orphan_index_init(env, mdd);
1251 rc = mdd_changelog_init(env, mdd);
1253 CERROR("%s: failed to initialize changelog: rc = %d\n",
1254 mdd2obd_dev(mdd)->obd_name, rc);
1258 rc = mdd_hsm_actions_llog_init(env, mdd);
1260 GOTO(out_changelog, rc);
1262 nodemap_config = nm_config_file_register_tgt(env, mdd->mdd_bottom,
1264 if (IS_ERR(nodemap_config)) {
1265 rc = PTR_ERR(nodemap_config);
1269 obt->obt_nodemap_config_file = nodemap_config;
1272 rc = lfsck_register(env, mdd->mdd_bottom, mdd->mdd_child,
1273 mdd2obd_dev(mdd), mdd_lfsck_out_notify,
1276 CERROR("%s: failed to initialize lfsck: rc = %d\n",
1277 mdd2obd_dev(mdd)->obd_name, rc);
1278 GOTO(out_nodemap, rc);
1281 rc = barrier_register(mdd->mdd_bottom, mdd->mdd_child);
1283 CERROR("%s: failed to register to barrier: rc = %d\n",
1284 mdd2obd_dev(mdd)->obd_name, rc);
1285 GOTO(out_lfsck, rc);
1291 lfsck_degister(env, mdd->mdd_bottom);
1293 nm_config_file_deregister_tgt(env, obt->obt_nodemap_config_file);
1294 obt->obt_nodemap_config_file = NULL;
1296 mdd_hsm_actions_llog_fini(env, mdd);
1298 mdd_changelog_fini(env, mdd);
1300 mdd_orphan_index_fini(env, mdd);
1302 if (mdd_seq_site(mdd)->ss_node_id == 0)
1303 mdd_dot_lustre_cleanup(env, mdd);
1305 local_oid_storage_fini(env, mdd->mdd_los);
1306 mdd->mdd_los = NULL;
1312 * Implementation of lu_device_operations::ldo_fid_alloc() for MDD.
1314 * Find corresponding device by passed parent and name, and allocate FID from
1317 * see include/lu_object.h for the details.
1319 static int mdd_fid_alloc(const struct lu_env *env, struct lu_device *d,
1320 struct lu_fid *fid, struct lu_object *parent,
1321 const struct lu_name *name)
1323 struct mdd_device *mdd = lu2mdd_dev(d);
1324 struct lu_object *o = lu_object_next(parent);
1326 return dt_fid_alloc(env, mdd->mdd_child, fid, o, name);
1329 const struct lu_device_operations mdd_lu_ops = {
1330 .ldo_object_alloc = mdd_object_alloc,
1331 .ldo_process_config = mdd_process_config,
1332 .ldo_recovery_complete = mdd_recovery_complete,
1333 .ldo_prepare = mdd_prepare,
1334 .ldo_fid_alloc = mdd_fid_alloc,
1337 static int mdd_root_get(const struct lu_env *env,
1338 struct md_device *m, struct lu_fid *f)
1340 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1343 *f = mdd->mdd_root_fid;
1348 * No permission check is needed.
1350 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1351 struct obd_statfs *sfs)
1353 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1358 rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs, NULL);
1360 sfs->os_namelen = min_t(__u32, sfs->os_namelen, NAME_MAX);
1365 static const struct dt_device_param *mdd_dtconf_get(const struct lu_env *env,
1366 struct md_device *m)
1368 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1370 return &mdd->mdd_dt_conf;
1373 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1376 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1378 *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1379 return (*h == NULL ? -ENOENT : 0);
1382 static struct lu_device *mdd_device_free(const struct lu_env *env,
1383 struct lu_device *lu)
1385 struct mdd_device *m = lu2mdd_dev(lu);
1388 LASSERT(atomic_read(&lu->ld_ref) == 0);
1389 md_device_fini(&m->mdd_md_dev);
1394 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1395 struct lu_device_type *t,
1396 struct lustre_cfg *lcfg)
1398 struct lu_device *l;
1399 struct mdd_device *m;
1403 l = ERR_PTR(-ENOMEM);
1408 md_device_init(&m->mdd_md_dev, t);
1409 rc = mdd_init0(env, m, t, lcfg);
1411 mdd_device_free(env, l);
1420 * we use exports to track all mdd users
1422 static int mdd_obd_connect(const struct lu_env *env, struct obd_export **exp,
1423 struct obd_device *obd, struct obd_uuid *cluuid,
1424 struct obd_connect_data *data, void *localdata)
1426 struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1427 struct lustre_handle conn;
1431 CDEBUG(D_CONFIG, "connect #%d\n", mdd->mdd_connects);
1433 rc = class_connect(&conn, obd, cluuid);
1437 *exp = class_conn2export(&conn);
1439 /* Why should there ever be more than 1 connect? */
1440 LASSERT(mdd->mdd_connects == 0);
1441 mdd->mdd_connects++;
1447 * once last export (we don't count self-export) disappeared
1448 * mdd can be released
1450 static int mdd_obd_disconnect(struct obd_export *exp)
1452 struct obd_device *obd = exp->exp_obd;
1453 struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1454 int rc, release = 0;
1457 mdd->mdd_connects--;
1458 if (mdd->mdd_connects == 0)
1461 rc = class_disconnect(exp);
1463 if (rc == 0 && release)
1464 class_manual_cleanup(obd);
1468 static int mdd_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1469 __u32 keylen, void *key, __u32 *vallen, void *val)
1473 if (KEY_IS(KEY_OSP_CONNECTED)) {
1474 struct obd_device *obd = exp->exp_obd;
1475 struct mdd_device *mdd;
1477 if (!obd->obd_set_up || obd->obd_stopping)
1480 mdd = lu2mdd_dev(obd->obd_lu_dev);
1482 rc = obd_get_info(env, mdd->mdd_child_exp, keylen, key, vallen,
1490 static int mdd_obd_set_info_async(const struct lu_env *env,
1491 struct obd_export *exp,
1492 __u32 keylen, void *key,
1493 __u32 vallen, void *val,
1494 struct ptlrpc_request_set *set)
1496 struct obd_device *obd = exp->exp_obd;
1497 struct mdd_device *mdd;
1500 if (!obd->obd_set_up || obd->obd_stopping)
1503 mdd = lu2mdd_dev(obd->obd_lu_dev);
1505 rc = obd_set_info_async(env, mdd->mdd_child_exp, keylen, key,
1510 static const struct obd_ops mdd_obd_device_ops = {
1511 .o_owner = THIS_MODULE,
1512 .o_connect = mdd_obd_connect,
1513 .o_disconnect = mdd_obd_disconnect,
1514 .o_get_info = mdd_obd_get_info,
1515 .o_set_info_async = mdd_obd_set_info_async,
1518 static int mdd_changelog_user_register(const struct lu_env *env,
1519 struct mdd_device *mdd, int *id)
1521 struct llog_ctxt *ctxt;
1522 struct llog_changelog_user_rec *rec;
1526 ctxt = llog_get_context(mdd2obd_dev(mdd),
1527 LLOG_CHANGELOG_USER_ORIG_CTXT);
1533 llog_ctxt_put(ctxt);
1537 CFS_RACE(CFS_FAIL_CHLOG_USER_REG_UNREG_RACE);
1539 rec->cur_hdr.lrh_len = sizeof(*rec);
1540 rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1541 spin_lock(&mdd->mdd_cl.mc_user_lock);
1542 if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1543 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1544 CERROR("Maximum number of changelog users exceeded!\n");
1545 GOTO(out, rc = -EOVERFLOW);
1547 *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1548 mdd->mdd_cl.mc_users++;
1549 rec->cur_endrec = mdd->mdd_cl.mc_index;
1551 rec->cur_time = (__u32)ktime_get_real_seconds();
1552 if (OBD_FAIL_CHECK(OBD_FAIL_TIME_IN_CHLOG_USER))
1555 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1557 rc = llog_cat_add(env, ctxt->loc_handle, &rec->cur_hdr, NULL);
1559 CWARN("%s: Failed to register changelog user %d: rc=%d\n",
1560 mdd2obd_dev(mdd)->obd_name, *id, rc);
1561 spin_lock(&mdd->mdd_cl.mc_user_lock);
1562 mdd->mdd_cl.mc_users--;
1563 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1567 CDEBUG(D_IOCTL, "Registered changelog user %d\n", *id);
1569 /* Assume we want it on since somebody registered */
1570 rc = mdd_changelog_on(env, mdd);
1576 llog_ctxt_put(ctxt);
1580 struct mdd_changelog_user_purge {
1581 struct mdd_device *mcup_mdd;
1583 __u32 mcup_usercount;
1589 * changelog_user_purge callback
1591 * Is called once per user.
1593 * Check to see the user requested is available from rec.
1594 * Truncate the changelog.
1595 * Keep track of the total number of users (calls).
1597 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1598 struct llog_handle *llh,
1599 struct llog_rec_hdr *hdr, void *data)
1601 struct llog_changelog_user_rec *rec;
1602 struct mdd_changelog_user_purge *mcup = data;
1603 struct llog_cookie cookie;
1608 if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
1611 rec = container_of(hdr, struct llog_changelog_user_rec, cur_hdr);
1613 mcup->mcup_usercount++;
1615 if (rec->cur_id != mcup->mcup_id) {
1616 /* truncate to the lowest endrec that is not this user */
1617 mcup->mcup_minrec = min(mcup->mcup_minrec,
1622 /* Unregister this user */
1623 cookie.lgc_lgl = llh->lgh_id;
1624 cookie.lgc_index = hdr->lrh_index;
1626 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1629 mcup->mcup_found = true;
1630 mcup->mcup_usercount--;
1631 spin_lock(&mcup->mcup_mdd->mdd_cl.mc_user_lock);
1632 mcup->mcup_mdd->mdd_cl.mc_users--;
1633 spin_unlock(&mcup->mcup_mdd->mdd_cl.mc_user_lock);
1639 int mdd_changelog_user_purge(const struct lu_env *env,
1640 struct mdd_device *mdd, __u32 id)
1642 struct mdd_changelog_user_purge mcup = {
1645 .mcup_found = false,
1646 .mcup_usercount = 0,
1647 .mcup_minrec = ULLONG_MAX,
1649 struct llog_ctxt *ctxt;
1654 CDEBUG(D_IOCTL, "%s: Purge request: id=%u\n",
1655 mdd2obd_dev(mdd)->obd_name, id);
1657 ctxt = llog_get_context(mdd2obd_dev(mdd),
1658 LLOG_CHANGELOG_USER_ORIG_CTXT);
1660 (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
1661 GOTO(out, rc = -ENXIO);
1663 rc = llog_cat_process(env, ctxt->loc_handle,
1664 mdd_changelog_user_purge_cb, &mcup,
1667 OBD_FAIL_TIMEOUT(OBD_FAIL_LLOG_PURGE_DELAY, cfs_fail_val);
1669 if ((rc == 0) && (mcup.mcup_usercount == 0)) {
1670 spin_lock(&mdd->mdd_cl.mc_user_lock);
1671 if (mdd->mdd_cl.mc_users == 0) {
1672 /* No more users; turn changelogs off */
1673 CDEBUG(D_IOCTL, "turning off changelogs\n");
1674 rc = mdd_changelog_off(env, mdd);
1676 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1679 if ((rc == 0) && mcup.mcup_found) {
1680 CDEBUG(D_IOCTL, "%s: Purging changelog entries for user %d "
1682 mdd2obd_dev(mdd)->obd_name, id, mcup.mcup_minrec);
1683 /* Cancelling record 0 destroys the entire changelog, make sure
1684 we don't do that unless we mean it. */
1685 if (mcup.mcup_minrec != 0 || mcup.mcup_usercount == 0) {
1686 rc = mdd_changelog_llog_cancel(env, mdd,
1690 CWARN("%s: No changelog for user %u; rc=%d\n",
1691 mdd2obd_dev(mdd)->obd_name, id, rc);
1692 GOTO(out, rc = -ENOENT);
1695 CFS_RACE(CFS_FAIL_CHLOG_USER_REG_UNREG_RACE);
1700 llog_ctxt_put(ctxt);
1705 struct mdd_changelog_user_clear {
1713 * changelog_clear callback
1715 * Is called once per user.
1717 * Check to see the user requested is available from rec.
1718 * Check the oldest (smallest) record for boundary conditions.
1719 * Truncate the changelog.
1721 static int mdd_changelog_clear_cb(const struct lu_env *env,
1722 struct llog_handle *llh,
1723 struct llog_rec_hdr *hdr,
1726 struct llog_changelog_user_rec *rec;
1727 struct mdd_changelog_user_clear *mcuc = data;
1732 if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
1735 rec = container_of(hdr, struct llog_changelog_user_rec, cur_hdr);
1737 /* Does the changelog id match the requested id? */
1738 if (rec->cur_id != mcuc->mcuc_id) {
1739 mcuc->mcuc_minrec = min(mcuc->mcuc_minrec,
1744 /* cur_endrec is the oldest purgeable record, make sure we're newer */
1745 if (rec->cur_endrec > mcuc->mcuc_endrec) {
1746 CDEBUG(D_IOCTL, "Request %llu out of range: %llu\n",
1747 mcuc->mcuc_endrec, rec->cur_endrec);
1751 /* Flag that we've met all the range and user checks.
1752 * We now know the record to flush.
1754 rec->cur_endrec = mcuc->mcuc_endrec;
1756 rec->cur_time = (__u32)ktime_get_real_seconds();
1757 if (OBD_FAIL_CHECK(OBD_FAIL_TIME_IN_CHLOG_USER))
1760 mcuc->mcuc_flush = true;
1762 CDEBUG(D_IOCTL, "Rewriting changelog user %u endrec to %llu\n",
1763 mcuc->mcuc_id, rec->cur_endrec);
1765 /* Update the endrec */
1766 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1772 * Clear a changelog up to entry specified by endrec for user id.
1774 static int mdd_changelog_clear(const struct lu_env *env,
1775 struct mdd_device *mdd, __u32 id,
1778 struct mdd_changelog_user_clear mcuc = {
1780 .mcuc_minrec = endrec,
1781 .mcuc_flush = false,
1783 struct llog_ctxt *ctxt;
1789 CDEBUG(D_IOCTL, "%s: Purge request: id=%u, endrec=%llu\n",
1790 mdd2obd_dev(mdd)->obd_name, id, endrec);
1791 /* start_rec is the newest (largest value) entry in the changelogs*/
1792 spin_lock(&mdd->mdd_cl.mc_lock);
1793 start_rec = mdd->mdd_cl.mc_index;
1794 spin_unlock(&mdd->mdd_cl.mc_lock);
1796 if (start_rec < endrec) {
1797 CDEBUG(D_IOCTL, "%s: Could not clear changelog, requested "\
1798 "address out of range\n", mdd2obd_dev(mdd)->obd_name);
1803 mcuc.mcuc_endrec = start_rec;
1804 mcuc.mcuc_minrec = ULLONG_MAX;
1806 mcuc.mcuc_endrec = endrec;
1809 ctxt = llog_get_context(mdd2obd_dev(mdd),
1810 LLOG_CHANGELOG_USER_ORIG_CTXT);
1812 (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
1813 GOTO(out, rc = -ENXIO);
1815 rc = llog_cat_process(env, ctxt->loc_handle,
1816 mdd_changelog_clear_cb, (void *)&mcuc,
1820 CWARN("%s: Failure to clear the changelog for user %d: %d\n",
1821 mdd2obd_dev(mdd)->obd_name, id, rc);
1822 } else if (mcuc.mcuc_flush) {
1823 /* Cancelling record 0 destroys the entire changelog, make sure
1824 we don't do that unless we mean it. */
1825 if (mcuc.mcuc_minrec != 0) {
1826 CDEBUG(D_IOCTL, "%s: Purging changelog entries up "\
1827 "to %llu\n", mdd2obd_dev(mdd)->obd_name,
1830 rc = mdd_changelog_llog_cancel(env, mdd,
1834 CWARN("%s: No entry for user %d\n",
1835 mdd2obd_dev(mdd)->obd_name, id);
1842 llog_ctxt_put(ctxt);
1848 * May be called remotely from mdt_iocontrol_handle or locally from
1849 * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
1850 * an obd_ioctl_data format (but local ioctl handler does).
1852 * \param len - data len
1853 * \param karg - ioctl data, in kernel space
1855 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
1856 unsigned int cmd, int len, void *karg)
1858 struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1859 struct obd_ioctl_data *data = karg;
1863 /* Doesn't use obd_ioctl_data */
1865 case OBD_IOC_CHANGELOG_CLEAR: {
1866 struct changelog_setinfo *cs = karg;
1868 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1869 RETURN(-EINPROGRESS);
1871 rc = mdd_changelog_clear(env, mdd, cs->cs_id,
1873 barrier_exit(mdd->mdd_bottom);
1876 case OBD_IOC_START_LFSCK: {
1877 rc = lfsck_start(env, mdd->mdd_bottom,
1878 (struct lfsck_start_param *)karg);
1881 case OBD_IOC_STOP_LFSCK: {
1882 rc = lfsck_stop(env, mdd->mdd_bottom,
1883 (struct lfsck_stop *)karg);
1886 case OBD_IOC_QUERY_LFSCK: {
1887 rc = lfsck_query(env, mdd->mdd_bottom, NULL, NULL,
1888 (struct lfsck_query *)karg);
1893 /* Below ioctls use obd_ioctl_data */
1894 if (len != sizeof(*data)) {
1895 CERROR("Bad ioctl size %d\n", len);
1898 if (data->ioc_version != OBD_IOCTL_VERSION) {
1899 CERROR("Bad magic %x != %x\n", data->ioc_version,
1905 case OBD_IOC_CHANGELOG_REG:
1906 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1907 RETURN(-EINPROGRESS);
1909 rc = mdd_changelog_user_register(env, mdd, &data->ioc_u32_1);
1910 barrier_exit(mdd->mdd_bottom);
1912 case OBD_IOC_CHANGELOG_DEREG:
1913 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1914 RETURN(-EINPROGRESS);
1916 /* explicitly clear changelog first, to protect from crash in
1917 * the middle of purge that would lead to unregistered consumer
1918 * but pending changelog entries
1920 rc = mdd_changelog_clear(env, mdd, data->ioc_u32_1, 0);
1922 rc = mdd_changelog_user_purge(env,
1923 mdd, data->ioc_u32_1);
1925 barrier_exit(mdd->mdd_bottom);
1934 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
1935 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key);
1937 static const struct md_device_operations mdd_ops = {
1938 .mdo_statfs = mdd_statfs,
1939 .mdo_root_get = mdd_root_get,
1940 .mdo_llog_ctxt_get = mdd_llog_ctxt_get,
1941 .mdo_iocontrol = mdd_iocontrol,
1942 .mdo_dtconf_get = mdd_dtconf_get,
1945 static const struct lu_device_type_operations mdd_device_type_ops = {
1946 .ldto_init = mdd_type_init,
1947 .ldto_fini = mdd_type_fini,
1949 .ldto_start = mdd_type_start,
1950 .ldto_stop = mdd_type_stop,
1952 .ldto_device_alloc = mdd_device_alloc,
1953 .ldto_device_free = mdd_device_free,
1955 .ldto_device_fini = mdd_device_fini
1958 static struct lu_device_type mdd_device_type = {
1959 .ldt_tags = LU_DEVICE_MD,
1960 .ldt_name = LUSTRE_MDD_NAME,
1961 .ldt_ops = &mdd_device_type_ops,
1962 .ldt_ctx_tags = LCT_MD_THREAD
1965 /* context key constructor: mdd_key_init */
1966 LU_KEY_INIT(mdd, struct mdd_thread_info);
1968 static void mdd_key_fini(const struct lu_context *ctx,
1969 struct lu_context_key *key, void *data)
1971 struct mdd_thread_info *info = data;
1973 lu_buf_free(&info->mti_big_buf);
1974 lu_buf_free(&info->mti_link_buf);
1975 lu_buf_free(&info->mti_xattr_buf);
1980 /* context key: mdd_thread_key */
1981 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
1983 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
1984 int (*func)(void *), void *data, char *name)
1986 struct task_struct *task;
1988 LASSERT(thread->mgt_init == false);
1989 init_completion(&thread->mgt_started);
1990 init_completion(&thread->mgt_finished);
1991 thread->mgt_data = data;
1992 thread->mgt_abort = false;
1993 thread->mgt_init = true;
1995 task = kthread_run(func, thread, name);
1997 complete(&thread->mgt_finished);
1998 return PTR_ERR(task);
2000 wait_for_completion(&thread->mgt_started);
2004 void mdd_generic_thread_stop(struct mdd_generic_thread *thread)
2006 if (thread->mgt_init == true) {
2007 thread->mgt_abort = true;
2008 wait_for_completion(&thread->mgt_finished);
2012 static int __init mdd_init(void)
2016 rc = lu_kmem_init(mdd_caches);
2020 changelog_orig_logops = llog_common_cat_ops;
2021 changelog_orig_logops.lop_write_rec = mdd_changelog_write_rec;
2023 rc = class_register_type(&mdd_obd_device_ops, NULL, false,
2024 LUSTRE_MDD_NAME, &mdd_device_type);
2026 lu_kmem_fini(mdd_caches);
2030 static void __exit mdd_exit(void)
2032 class_unregister_type(LUSTRE_MDD_NAME);
2033 lu_kmem_fini(mdd_caches);
2036 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2037 MODULE_DESCRIPTION("Lustre Meta-data Device Driver ("LUSTRE_MDD_NAME")");
2038 MODULE_VERSION(LUSTRE_VERSION_STRING);
2039 MODULE_LICENSE("GPL");
2041 module_init(mdd_init);
2042 module_exit(mdd_exit);