Whamcloud - gitweb
LU-10680 mdd: create gc thread when no current transaction
[fs/lustre-release.git] / lustre / mdd / mdd_device.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdd/mdd_device.c
33  *
34  * Lustre Metadata Server (mdd) routines
35  *
36  * Author: Wang Di <wangdi@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_MDS
40
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>
52
53 #include "mdd_internal.h"
54
55 static const struct md_device_operations mdd_ops;
56 static struct lu_device_type mdd_device_type;
57
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";
61
62 /* Slab for MDD object allocation */
63 struct kmem_cache *mdd_object_kmem;
64
65 static struct lu_kmem_descr mdd_caches[] = {
66         {
67                 .ckd_cache = &mdd_object_kmem,
68                 .ckd_name  = "mdd_obj",
69                 .ckd_size  = sizeof(struct mdd_object)
70         },
71         {
72                 .ckd_cache = NULL
73         }
74 };
75
76 static int mdd_connect_to_next(const struct lu_env *env, struct mdd_device *m,
77                                const char *nextdev)
78 {
79         struct obd_connect_data *data = NULL;
80         struct lu_device        *lud = mdd2lu_dev(m);
81         struct obd_device       *obd;
82         int                      rc;
83         ENTRY;
84
85         LASSERT(m->mdd_child_exp == NULL);
86
87         OBD_ALLOC(data, sizeof(*data));
88         if (data == NULL)
89                 GOTO(out, rc = -ENOMEM);
90
91         obd = class_name2obd(nextdev);
92         if (obd == NULL) {
93                 CERROR("can't locate next device: %s\n", nextdev);
94                 GOTO(out, rc = -ENOTCONN);
95         }
96
97         data->ocd_connect_flags = OBD_CONNECT_VERSION;
98         data->ocd_version = LUSTRE_VERSION_CODE;
99
100         rc = obd_connect(NULL, &m->mdd_child_exp, obd, &obd->obd_uuid, data, NULL);
101         if (rc) {
102                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
103                 GOTO(out, rc);
104         }
105
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);
111
112 out:
113         if (data)
114                 OBD_FREE(data, sizeof(*data));
115         RETURN(rc);
116 }
117
118 static int mdd_init0(const struct lu_env *env, struct mdd_device *mdd,
119                 struct lu_device_type *t, struct lustre_cfg *lcfg)
120 {
121         int rc = -EINVAL;
122         const char *dev;
123         ENTRY;
124
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 */
129
130         dev = lustre_cfg_string(lcfg, 0);
131         if (dev == NULL)
132                 RETURN(rc);
133
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)
136                 RETURN(rc);
137         mdd->mdd_md_dev.md_lu_dev.ld_ops = &mdd_lu_ops;
138         mdd->mdd_md_dev.md_ops = &mdd_ops;
139
140         rc = mdd_connect_to_next(env, mdd, lustre_cfg_string(lcfg, 3));
141         if (rc != 0)
142                 RETURN(rc);
143
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 = 0;
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 catalog entries */
156         mdd->mdd_changelog_min_free_cat_entries = CHLOG_MIN_FREE_CAT_ENTRIES;
157
158         dt_conf_get(env, mdd->mdd_child, &mdd->mdd_dt_conf);
159
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));
164         if (rc < 0)
165                 obd_disconnect(mdd->mdd_child_exp);
166
167         RETURN(rc);
168 }
169
170 static struct lu_device *mdd_device_fini(const struct lu_env *env,
171                                          struct lu_device *d)
172 {
173         struct mdd_device *mdd = lu2mdd_dev(d);
174
175         if (d->ld_site)
176                 lu_dev_del_linkage(d->ld_site, d);
177
178         mdd_procfs_fini(mdd);
179         return NULL;
180 }
181
182 static int changelog_init_cb(const struct lu_env *env, struct llog_handle *llh,
183                              struct llog_rec_hdr *hdr, void *data)
184 {
185         struct mdd_device *mdd = (struct mdd_device *)data;
186         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
187
188         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
189         LASSERT(rec->cr_hdr.lrh_type == CHANGELOG_REC);
190
191         CDEBUG(D_INFO,
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));
196
197         mdd->mdd_cl.mc_index = rec->cr.cr_index;
198         return LLOG_PROC_BREAK;
199 }
200
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)
204 {
205         struct mdd_device *mdd = (struct mdd_device *)data;
206         struct llog_changelog_user_rec *rec =
207                 (struct llog_changelog_user_rec *)hdr;
208
209         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
210         LASSERT(rec->cur_hdr.lrh_type == CHANGELOG_USER_REC);
211
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));
215
216         spin_lock(&mdd->mdd_cl.mc_user_lock);
217         mdd->mdd_cl.mc_lastuser = rec->cur_id;
218         mdd->mdd_cl.mc_users++;
219         if (rec->cur_endrec > mdd->mdd_cl.mc_index)
220                 mdd->mdd_cl.mc_index = rec->cur_endrec;
221         spin_unlock(&mdd->mdd_cl.mc_user_lock);
222
223         return LLOG_PROC_BREAK;
224 }
225
226 struct changelog_orphan_data {
227         __u64 index;
228         struct mdd_device *mdd;
229 };
230
231 /* find oldest changelog record index */
232 static int changelog_detect_orphan_cb(const struct lu_env *env,
233                                       struct llog_handle *llh,
234                                       struct llog_rec_hdr *hdr, void *data)
235 {
236         struct mdd_device *mdd = ((struct changelog_orphan_data *)data)->mdd;
237         struct llog_changelog_rec *rec = container_of(hdr,
238                                                       struct llog_changelog_rec,
239                                                       cr_hdr);
240
241         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
242
243         if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
244                 CWARN("%s: invalid record at index %d in log "DFID"\n",
245                       mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
246                       PFID(&llh->lgh_id.lgl_oi.oi_fid));
247                 /* try to find some next valid record and thus allow to recover
248                  * from a corrupted LLOG, instead to assert and force a crash
249                  */
250                 return 0;
251         }
252
253         CDEBUG(D_INFO, "%s: seeing record at index %d/%d/%llu t=%x %.*s in log "
254                DFID"\n", mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
255                rec->cr_hdr.lrh_index, rec->cr.cr_index, rec->cr.cr_type,
256                rec->cr.cr_namelen, changelog_rec_name(&rec->cr),
257                PFID(&llh->lgh_id.lgl_oi.oi_fid));
258
259         ((struct changelog_orphan_data *)data)->index = rec->cr.cr_index;
260         return LLOG_PROC_BREAK;
261 }
262
263 /* find oldest changelog user index */
264 static int changelog_user_detect_orphan_cb(const struct lu_env *env,
265                                            struct llog_handle *llh,
266                                            struct llog_rec_hdr *hdr, void *data)
267 {
268         struct mdd_device *mdd = ((struct changelog_orphan_data *)data)->mdd;
269         struct llog_changelog_user_rec *rec = container_of(hdr,
270                                                 struct llog_changelog_user_rec,
271                                                 cur_hdr);
272
273         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
274
275         if (rec->cur_hdr.lrh_type != CHANGELOG_USER_REC) {
276                 CWARN("%s: invalid user at index %d in log "DFID"\n",
277                       mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
278                       PFID(&llh->lgh_id.lgl_oi.oi_fid));
279                 /* try to find some next valid record and thus allow to recover
280                  * from a corrupted LLOG, instead to assert and force a crash
281                  */
282                 return 0;
283         }
284
285         CDEBUG(D_INFO, "%s: seeing user at index %d/%d id=%d endrec=%llu in "
286                "log "DFID"\n", mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
287                rec->cur_hdr.lrh_index, rec->cur_id, rec->cur_endrec,
288                PFID(&llh->lgh_id.lgl_oi.oi_fid));
289
290         if (((struct changelog_orphan_data *)data)->index == 0 ||
291             rec->cur_endrec < ((struct changelog_orphan_data *)data)->index)
292                 ((struct changelog_orphan_data *)data)->index = rec->cur_endrec;
293
294         return 0;
295 }
296
297 struct changelog_cancel_cookie {
298         long long endrec;
299         struct mdd_device *mdd;
300 };
301
302 static int llog_changelog_cancel_cb(const struct lu_env *env,
303                                     struct llog_handle *llh,
304                                     struct llog_rec_hdr *hdr, void *data)
305 {
306         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
307         struct llog_cookie       cookie;
308         struct changelog_cancel_cookie *cl_cookie =
309                 (struct changelog_cancel_cookie *)data;
310         int                      rc;
311
312         ENTRY;
313
314         /* This is always a (sub)log, not the catalog */
315         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
316
317         /* if current context is GC-thread allow it to stop upon umount
318          * remaining records cleanup will occur upon next mount
319          *
320          * also during testing, wait for GC-thread to be released
321          *
322          * XXX this requires the GC-thread to not fork a sub-thread via
323          * llog[_cat]_process_or_fork() and we may think to also implement
324          * this shutdown mechanism for manually started user unregister which
325          * can also take a long time if huge backlog of records
326          */
327         if (unlikely(cl_cookie->mdd->mdd_cl.mc_gc_task == current)) {
328                 /* wait to be released */
329                 while (CFS_FAIL_CHECK_QUIET(OBD_FAIL_FORCE_GC_THREAD))
330                         schedule();
331
332                 if (kthread_should_stop())
333                         RETURN(LLOG_PROC_BREAK);
334         }
335
336         if (rec->cr.cr_index > cl_cookie->endrec)
337                 /* records are in order, so we're done */
338                 RETURN(LLOG_PROC_BREAK);
339
340         cookie.lgc_lgl = llh->lgh_id;
341         cookie.lgc_index = hdr->lrh_index;
342
343         /* cancel them one at a time.  I suppose we could store up the cookies
344          * and cancel them all at once; probably more efficient, but this is
345          * done as a user call, so who cares... */
346         rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1,
347                                      &cookie);
348         RETURN(rc < 0 ? rc : 0);
349 }
350
351 static int llog_changelog_cancel(const struct lu_env *env,
352                                  struct llog_ctxt *ctxt,
353                                  struct changelog_cancel_cookie *cookie)
354 {
355         struct llog_handle      *cathandle = ctxt->loc_handle;
356         int                      rc;
357
358         ENTRY;
359
360         /* This should only be called with the catalog handle */
361         LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
362
363         rc = llog_cat_process(env, cathandle, llog_changelog_cancel_cb,
364                               cookie, 0, 0);
365         if (rc >= 0)
366                 /* 0 or 1 means we're done */
367                 rc = 0;
368         else
369                 CERROR("%s: cancel idx %u of catalog "DFID": rc = %d\n",
370                        ctxt->loc_obd->obd_name, cathandle->lgh_last_idx,
371                        PFID(&cathandle->lgh_id.lgl_oi.oi_fid), rc);
372
373         RETURN(rc);
374 }
375
376 static struct llog_operations changelog_orig_logops;
377
378 static int
379 mdd_changelog_write_header(const struct lu_env *env, struct mdd_device *mdd,
380                            int markerflags);
381
382 static int
383 mdd_changelog_on(const struct lu_env *env, struct mdd_device *mdd)
384 {
385         int rc = 0;
386
387         if ((mdd->mdd_cl.mc_flags & CLM_ON) != 0)
388                 return rc;
389
390         LCONSOLE_INFO("%s: changelog on\n", mdd2obd_dev(mdd)->obd_name);
391         if (mdd->mdd_cl.mc_flags & CLM_ERR) {
392                 CERROR("Changelogs cannot be enabled due to error "
393                        "condition (see %s log).\n",
394                        mdd2obd_dev(mdd)->obd_name);
395                 rc = -ESRCH;
396         } else {
397                 spin_lock(&mdd->mdd_cl.mc_lock);
398                 mdd->mdd_cl.mc_flags |= CLM_ON;
399                 spin_unlock(&mdd->mdd_cl.mc_lock);
400                 rc = mdd_changelog_write_header(env, mdd, CLM_START);
401         }
402         return rc;
403 }
404
405 static int
406 mdd_changelog_off(const struct lu_env *env, struct mdd_device *mdd)
407 {
408         int rc = 0;
409
410         if ((mdd->mdd_cl.mc_flags & CLM_ON) != CLM_ON)
411                 return rc;
412
413         LCONSOLE_INFO("%s: changelog off\n", mdd2obd_dev(mdd)->obd_name);
414         rc = mdd_changelog_write_header(env, mdd, CLM_FINI);
415         spin_lock(&mdd->mdd_cl.mc_lock);
416         mdd->mdd_cl.mc_flags &= ~CLM_ON;
417         spin_unlock(&mdd->mdd_cl.mc_lock);
418
419         return rc;
420 }
421
422 static int mdd_changelog_llog_init(const struct lu_env *env,
423                                    struct mdd_device *mdd)
424 {
425         struct obd_device       *obd = mdd2obd_dev(mdd);
426         struct llog_ctxt        *ctxt = NULL, *uctxt = NULL;
427         struct changelog_orphan_data changelog_orphan = { .index = 0,
428                                                           .mdd = mdd },
429                                      user_orphan = { .index = 0,
430                                                      .mdd = mdd };
431         int                      rc;
432
433         ENTRY;
434
435         /* LU-2844 mdd setup failure should not cause umount oops */
436         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_INIT))
437                 RETURN(-EIO);
438
439         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
440         obd->obd_lvfs_ctxt.dt = mdd->mdd_bottom;
441         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
442                         obd, &changelog_orig_logops);
443         if (rc) {
444                 CERROR("%s: changelog llog setup failed: rc = %d\n",
445                        obd->obd_name, rc);
446                 RETURN(rc);
447         }
448
449         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
450         LASSERT(ctxt);
451
452         rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
453                               CHANGELOG_CATALOG);
454         if (rc)
455                 GOTO(out_cleanup, rc);
456
457         rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
458         if (rc)
459                 GOTO(out_close, rc);
460
461         rc = llog_cat_reverse_process(env, ctxt->loc_handle,
462                                       changelog_init_cb, mdd);
463
464         if (rc < 0) {
465                 CERROR("%s: changelog init failed: rc = %d\n", obd->obd_name,
466                        rc);
467                 GOTO(out_close, rc);
468         }
469
470         CDEBUG(D_IOCTL, "changelog starting index=%llu\n",
471                mdd->mdd_cl.mc_index);
472
473         /* setup user changelog */
474         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
475                         obd, &changelog_orig_logops);
476         if (rc) {
477                 CERROR("%s: changelog users llog setup failed: rc = %d\n",
478                        obd->obd_name, rc);
479                 GOTO(out_close, rc);
480         }
481
482         uctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
483         LASSERT(ctxt);
484
485         rc = llog_open_create(env, uctxt, &uctxt->loc_handle, NULL,
486                               CHANGELOG_USERS);
487         if (rc)
488                 GOTO(out_ucleanup, rc);
489
490         uctxt->loc_handle->lgh_logops->lop_add = llog_cat_add_rec;
491         uctxt->loc_handle->lgh_logops->lop_declare_add = llog_cat_declare_add_rec;
492
493         rc = llog_init_handle(env, uctxt->loc_handle, LLOG_F_IS_CAT, NULL);
494         if (rc)
495                 GOTO(out_uclose, rc);
496
497         rc = llog_cat_reverse_process(env, uctxt->loc_handle,
498                                       changelog_user_init_cb, mdd);
499         if (rc < 0) {
500                 CERROR("%s: changelog user init failed: rc = %d\n",
501                        obd->obd_name, rc);
502                 GOTO(out_uclose, rc);
503         }
504
505         /* If we have registered users, assume we want changelogs on */
506         if (mdd->mdd_cl.mc_lastuser > 0) {
507                 rc = mdd_changelog_on(env, mdd);
508                 if (rc < 0)
509                         GOTO(out_uclose, rc);
510         }
511
512         /* find and clear any orphan changelog records (1st record index <
513          * smallest of all users current index), likely to come from an
514          * interrupted manual or GC-thread purge, as its user record had
515          * been deleted first
516          * XXX we may wait for a still registered user clear operation to
517          * do the job, but it may then take a long time to reach the user's
518          * real targetted records if a huge purge backlog is still to be
519          * processed as a long time idle user record could have been deleted
520          * XXX we may need to run end of purge as a separate thread
521          */
522         rc = llog_cat_process(env, ctxt->loc_handle, changelog_detect_orphan_cb,
523                               &changelog_orphan, 0, 0);
524         if (rc < 0) {
525                 CERROR("%s: changelog detect orphan failed: rc = %d\n",
526                        obd->obd_name, rc);
527                 GOTO(out_uclose, rc);
528         }
529         rc = llog_cat_process(env, uctxt->loc_handle,
530                               changelog_user_detect_orphan_cb,
531                               &user_orphan, 0, 0);
532         if (rc < 0) {
533                 CERROR("%s: changelog user detect orphan failed: rc = %d\n",
534                        obd->obd_name, rc);
535                 GOTO(out_uclose, rc);
536         }
537         if (unlikely(changelog_orphan.index < user_orphan.index)) {
538                 struct changelog_cancel_cookie cl_cookie = {
539                         .endrec = user_orphan.index,
540                         .mdd = mdd,
541                 };
542
543                 CWARN("%s : orphan changelog records found, starting from "
544                       "index %llu to index %llu, being cleared now\n",
545                       obd->obd_name, changelog_orphan.index, user_orphan.index);
546
547                 /* XXX we may need to run end of purge as a separate thread */
548                 rc = llog_changelog_cancel(env, ctxt, &cl_cookie);
549                 if (rc < 0) {
550                         CERROR("%s: purge of changelog orphan records failed: "
551                                "rc = %d\n", obd->obd_name, rc);
552                         GOTO(out_uclose, rc);
553                 }
554         }
555
556         llog_ctxt_put(ctxt);
557         llog_ctxt_put(uctxt);
558         RETURN(0);
559 out_uclose:
560         llog_cat_close(env, uctxt->loc_handle);
561 out_ucleanup:
562         llog_cleanup(env, uctxt);
563 out_close:
564         llog_cat_close(env, ctxt->loc_handle);
565 out_cleanup:
566         llog_cleanup(env, ctxt);
567         return rc;
568 }
569
570 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
571 {
572         struct obd_device       *obd = mdd2obd_dev(mdd);
573         int                      rc;
574
575         mdd->mdd_cl.mc_index = 0;
576         spin_lock_init(&mdd->mdd_cl.mc_lock);
577         mdd->mdd_cl.mc_starttime = ktime_get();
578         spin_lock_init(&mdd->mdd_cl.mc_user_lock);
579         mdd->mdd_cl.mc_lastuser = 0;
580
581         /* ensure a GC check will, and a thread run may, occur upon start */
582         mdd->mdd_cl.mc_gc_time = 0;
583         mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NONE;
584
585         rc = mdd_changelog_llog_init(env, mdd);
586         if (rc) {
587                 CERROR("%s: changelog setup during init failed: rc = %d\n",
588                        obd->obd_name, rc);
589                 mdd->mdd_cl.mc_flags |= CLM_ERR;
590         }
591
592         return rc;
593 }
594
595 static void mdd_changelog_fini(const struct lu_env *env,
596                                struct mdd_device *mdd)
597 {
598         struct obd_device       *obd = mdd2obd_dev(mdd);
599         struct llog_ctxt        *ctxt;
600
601         mdd->mdd_cl.mc_flags = 0;
602
603 again:
604         /* stop GC-thread if running */
605         spin_lock(&mdd->mdd_cl.mc_lock);
606         if (likely(mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE)) {
607                 /* avoid any attempt to run a GC-thread */
608                 mdd->mdd_cl.mc_gc_task = current;
609                 spin_unlock(&mdd->mdd_cl.mc_lock);
610         } else {
611                 struct task_struct *gc_task;
612
613                 if (unlikely(mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NEED ||
614                              mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_START)) {
615                         /* need to wait for birthing GC-thread to be started
616                          * and to have set mc_gc_task to itself
617                          */
618                         spin_unlock(&mdd->mdd_cl.mc_lock);
619                         schedule_timeout(usecs_to_jiffies(10));
620                         /* go back to fully check if GC-thread has started or
621                          * even already exited or if a new one is starting...
622                          */
623                         goto again;
624                 }
625                 /* take a reference on task_struct to avoid it to be freed
626                  * upon exit
627                  */
628                 gc_task = mdd->mdd_cl.mc_gc_task;
629                 get_task_struct(gc_task);
630                 spin_unlock(&mdd->mdd_cl.mc_lock);
631                 kthread_stop(gc_task);
632                 put_task_struct(gc_task);
633         }
634
635         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
636         if (ctxt) {
637                 llog_cat_close(env, ctxt->loc_handle);
638                 llog_cleanup(env, ctxt);
639         }
640         ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
641         if (ctxt) {
642                 llog_cat_close(env, ctxt->loc_handle);
643                 llog_cleanup(env, ctxt);
644         }
645 }
646
647 /** Remove entries with indicies up to and including \a endrec from the
648  *  changelog
649  * \param mdd
650  * \param endrec
651  * \retval 0 ok
652  */
653 static int
654 mdd_changelog_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
655                           long long endrec)
656 {
657         struct obd_device *obd = mdd2obd_dev(mdd);
658         struct llog_ctxt *ctxt;
659         long long unsigned cur;
660         struct changelog_cancel_cookie cookie;
661         int rc;
662
663         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
664         if (ctxt == NULL)
665                 return -ENXIO;
666
667         spin_lock(&mdd->mdd_cl.mc_lock);
668         cur = (long long)mdd->mdd_cl.mc_index;
669         spin_unlock(&mdd->mdd_cl.mc_lock);
670         if (endrec > cur)
671                 endrec = cur;
672
673         /* purge to "0" is shorthand for everything */
674         if (endrec == 0)
675                 endrec = cur;
676
677         /* If purging all records, write a header entry so we don't have an
678            empty catalog and we're sure to have a valid starting index next
679            time.  In case of crash, we just restart with old log so we're
680            allright. */
681         if (endrec == cur) {
682                 /* XXX: transaction is started by llog itself */
683                 rc = mdd_changelog_write_header(env, mdd, CLM_PURGE);
684                 if (rc)
685                       goto out;
686         }
687
688         /* Some records were purged, so reset repeat-access time (so we
689            record new mtime update records, so users can see a file has been
690            changed since the last purge) */
691         mdd->mdd_cl.mc_starttime = ktime_get();
692
693         cookie.endrec = endrec;
694         cookie.mdd = mdd;
695         rc = llog_changelog_cancel(env, ctxt, &cookie);
696 out:
697         llog_ctxt_put(ctxt);
698         return rc;
699 }
700
701 /** Add a CL_MARK record to the changelog
702  * \param mdd
703  * \param markerflags - CLM_*
704  * \retval 0 ok
705  */
706 int mdd_changelog_write_header(const struct lu_env *env,
707                                struct mdd_device *mdd, int markerflags)
708 {
709         struct obd_device               *obd = mdd2obd_dev(mdd);
710         struct llog_changelog_rec       *rec;
711         struct lu_buf                   *buf;
712         struct llog_ctxt                *ctxt;
713         int                              reclen;
714         int                              len = strlen(obd->obd_name);
715         int                              rc;
716
717         ENTRY;
718
719         if (mdd->mdd_cl.mc_mask & (1 << CL_MARK)) {
720                 mdd->mdd_cl.mc_starttime = ktime_get();
721                 RETURN(0);
722         }
723
724         reclen = llog_data_len(sizeof(*rec) + len);
725         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
726         if (buf->lb_buf == NULL)
727                 RETURN(-ENOMEM);
728         rec = buf->lb_buf;
729
730         rec->cr.cr_flags = CLF_VERSION;
731         rec->cr.cr_type = CL_MARK;
732         rec->cr.cr_namelen = len;
733         memcpy(changelog_rec_name(&rec->cr), obd->obd_name, rec->cr.cr_namelen);
734         /* Status and action flags */
735         rec->cr.cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
736         rec->cr_hdr.lrh_len = llog_data_len(changelog_rec_size(&rec->cr) +
737                                             rec->cr.cr_namelen);
738         rec->cr_hdr.lrh_type = CHANGELOG_REC;
739         rec->cr.cr_time = cl_time();
740         spin_lock(&mdd->mdd_cl.mc_lock);
741         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
742         spin_unlock(&mdd->mdd_cl.mc_lock);
743
744         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
745         LASSERT(ctxt);
746
747         rc = llog_cat_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL);
748         if (rc > 0)
749                 rc = 0;
750         llog_ctxt_put(ctxt);
751
752         /* assume on or off event; reset repeat-access time */
753         mdd->mdd_cl.mc_starttime = ktime_get();
754         RETURN(rc);
755 }
756
757 /**
758  * Lookup method for "fid" object. Only filenames with correct SEQ:OID format
759  * are valid. We also check if object with passed fid exists or not.
760  */
761 static int obf_lookup(const struct lu_env *env, struct md_object *p,
762                       const struct lu_name *lname, struct lu_fid *f,
763                       struct md_op_spec *spec)
764 {
765         char *name = (char *)lname->ln_name;
766         struct mdd_device *mdd = mdo2mdd(p);
767         struct mdd_object *child;
768         int rc = 0;
769
770         while (*name == '[')
771                 name++;
772
773         sscanf(name, SFID, RFID(f));
774         if (!fid_is_sane(f)) {
775                 CWARN("%s: Trying to lookup invalid FID [%s] in %s/%s, FID "
776                       "format should be "DFID"\n", mdd2obd_dev(mdd)->obd_name,
777                       lname->ln_name, dot_lustre_name, mdd_obf_dir_name,
778                       (__u64)FID_SEQ_NORMAL, 1, 0);
779                 GOTO(out, rc = -EINVAL);
780         }
781
782         if (!fid_is_norm(f) && !fid_is_igif(f) && !fid_is_root(f) &&
783             !fid_seq_is_dot(f->f_seq)) {
784                 CWARN("%s: Trying to lookup invalid FID "DFID" in %s/%s, "
785                       "sequence should be >= %#llx or within [%#llx,"
786                       "%#llx].\n", mdd2obd_dev(mdd)->obd_name, PFID(f),
787                       dot_lustre_name, mdd_obf_dir_name, (__u64)FID_SEQ_NORMAL,
788                       (__u64)FID_SEQ_IGIF, (__u64)FID_SEQ_IGIF_MAX);
789                 GOTO(out, rc = -EINVAL);
790         }
791
792         /* Check if object with this fid exists */
793         child = mdd_object_find(env, mdd, f);
794         if (IS_ERR(child))
795                 GOTO(out, rc = PTR_ERR(child));
796
797         if (mdd_object_exists(child) == 0)
798                 rc = -ENOENT;
799
800         mdd_object_put(env, child);
801
802 out:
803         return rc;
804 }
805
806 static int mdd_dummy_create(const struct lu_env *env,
807                             struct md_object *pobj,
808                             const struct lu_name *lname,
809                             struct md_object *child,
810                             struct md_op_spec *spec,
811                             struct md_attr* ma)
812 {
813         return -EPERM;
814 }
815
816 static int mdd_dummy_rename(const struct lu_env *env,
817                             struct md_object *src_pobj,
818                             struct md_object *tgt_pobj,
819                             const struct lu_fid *lf,
820                             const struct lu_name *lsname,
821                             struct md_object *tobj,
822                             const struct lu_name *ltname,
823                             struct md_attr *ma)
824 {
825         return -EPERM;
826 }
827
828 static int mdd_dummy_link(const struct lu_env *env,
829                           struct md_object *tgt_obj,
830                           struct md_object *src_obj,
831                           const struct lu_name *lname,
832                           struct md_attr *ma)
833 {
834         return -EPERM;
835 }
836
837 static int mdd_dummy_unlink(const struct lu_env *env,
838                             struct md_object *pobj,
839                             struct md_object *cobj,
840                             const struct lu_name *lname,
841                             struct md_attr *ma,
842                             int no_name)
843 {
844         return -EPERM;
845 }
846
847 static struct md_dir_operations mdd_obf_dir_ops = {
848         .mdo_lookup = obf_lookup,
849         .mdo_create = mdd_dummy_create,
850         .mdo_rename = mdd_dummy_rename,
851         .mdo_link   = mdd_dummy_link,
852         .mdo_unlink = mdd_dummy_unlink
853 };
854
855 static 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
861 };
862
863 static struct md_object *mdo_locate(const struct lu_env *env,
864                                     struct md_device *md,
865                                     const struct lu_fid *fid)
866 {
867         struct lu_object *obj;
868         struct md_object *mdo;
869
870         obj = lu_object_find(env, &md->md_lu_dev, fid, NULL);
871         if (!IS_ERR(obj)) {
872                 obj = lu_object_locate(obj->lo_header, md->md_lu_dev.ld_type);
873                 LASSERT(obj != NULL);
874                 mdo = lu2md(obj);
875         } else {
876                 mdo = ERR_PTR(PTR_ERR(obj));
877         }
878         return mdo;
879 }
880
881 static int mdd_lpf_setup(const struct lu_env *env, struct mdd_device *m)
882 {
883         struct md_object        *mdo;
884         struct mdd_object       *mdd_lpf;
885         struct lu_fid            fid    = LU_LPF_FID;
886         int                      rc;
887         ENTRY;
888
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,
891                                    &fid);
892         if (rc != 0)
893                 RETURN(rc);
894
895         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
896         if (IS_ERR(mdo))
897                 RETURN(PTR_ERR(mdo));
898
899         LASSERT(lu_object_exists(&mdo->mo_lu));
900
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;
904
905         RETURN(0);
906 }
907
908 /**
909  * Create special in-memory "fid" object for open-by-fid.
910  */
911 static int mdd_obf_setup(const struct lu_env *env, struct mdd_device *m)
912 {
913         struct md_object        *mdo;
914         struct mdd_object       *mdd_obf;
915         struct lu_fid            fid = LU_OBF_FID;
916         int                      rc;
917
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);
920         if (rc < 0)
921                 RETURN(rc);
922
923         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
924         if (IS_ERR(mdo))
925                 RETURN(PTR_ERR(mdo));
926
927         LASSERT(lu_object_exists(&mdo->mo_lu));
928
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;
932
933         return 0;
934 }
935
936 static void mdd_dot_lustre_cleanup(const struct lu_env *env,
937                                    struct mdd_device *m)
938 {
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;
942         }
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;
946         }
947         if (m->mdd_dot_lustre != NULL) {
948                 mdd_object_put(env, m->mdd_dot_lustre);
949                 m->mdd_dot_lustre = NULL;
950         }
951 }
952
953 /** Setup ".lustre" directory object */
954 static int mdd_dot_lustre_setup(const struct lu_env *env, struct mdd_device *m)
955 {
956         struct md_object        *mdo;
957         struct lu_fid            fid;
958         int                      rc;
959
960         ENTRY;
961         /* Create ".lustre" directory in ROOT. */
962         fid = LU_DOT_LUSTRE_FID;
963         rc = mdd_local_file_create(env, m, &m->mdd_root_fid,
964                                    dot_lustre_name,
965                                    S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO,
966                                    &fid);
967         if (rc < 0)
968                 RETURN(rc);
969         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
970         if (IS_ERR(mdo))
971                 RETURN(PTR_ERR(mdo));
972         LASSERT(lu_object_exists(&mdo->mo_lu));
973
974         m->mdd_dot_lustre = md2mdd_obj(mdo);
975
976         rc = mdd_obf_setup(env, m);
977         if (rc) {
978                 CERROR("%s: error initializing \"fid\" object: rc = %d.\n",
979                        mdd2obd_dev(m)->obd_name, rc);
980                 GOTO(out, rc);
981         }
982
983         rc = mdd_lpf_setup(env, m);
984         if (rc != 0) {
985                 CERROR("%s: error initializing \"lost+found\": rc = %d.\n",
986                        mdd2obd_dev(m)->obd_name, rc);
987                 GOTO(out, rc);
988         }
989
990         RETURN(0);
991
992 out:
993         mdd_dot_lustre_cleanup(env, m);
994
995         return rc;
996 }
997
998
999 static struct llog_operations hsm_actions_logops;
1000
1001 /**
1002  * set llog methods and create LLOG_AGENT_ORIG_CTXT llog
1003  * object in obd_device
1004  */
1005 static int mdd_hsm_actions_llog_init(const struct lu_env *env,
1006                                      struct mdd_device *m)
1007 {
1008         struct obd_device       *obd = mdd2obd_dev(m);
1009         struct llog_ctxt        *ctxt = NULL;
1010         int                      rc;
1011         ENTRY;
1012
1013         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1014         obd->obd_lvfs_ctxt.dt = m->mdd_bottom;
1015
1016         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_AGENT_ORIG_CTXT,
1017                         obd, &hsm_actions_logops);
1018         if (rc) {
1019                 CERROR("%s: hsm actions llog setup failed: rc = %d\n",
1020                         obd->obd_name, rc);
1021                 RETURN(rc);
1022         }
1023
1024         ctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
1025         LASSERT(ctxt);
1026
1027         rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
1028                               HSM_ACTIONS);
1029         if (rc) {
1030                 CERROR("%s: hsm actions llog open_create failed: rc = %d\n",
1031                         obd->obd_name, rc);
1032                 GOTO(out_cleanup, rc);
1033         }
1034
1035         rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
1036         if (rc)
1037                 GOTO(out_close, rc);
1038
1039         llog_ctxt_put(ctxt);
1040         RETURN(0);
1041
1042 out_close:
1043         llog_cat_close(env, ctxt->loc_handle);
1044         ctxt->loc_handle = NULL;
1045 out_cleanup:
1046         llog_cleanup(env, ctxt);
1047
1048         return rc;
1049 }
1050
1051 /**
1052  * cleanup the context created by llog_setup_named()
1053  */
1054 static int mdd_hsm_actions_llog_fini(const struct lu_env *env,
1055                                      struct mdd_device *m)
1056 {
1057         struct obd_device       *obd = mdd2obd_dev(m);
1058         struct llog_ctxt        *lctxt;
1059         ENTRY;
1060
1061         lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
1062         if (lctxt) {
1063                 llog_cat_close(env, lctxt->loc_handle);
1064                 lctxt->loc_handle = NULL;
1065                 llog_cleanup(env, lctxt);
1066         }
1067
1068         RETURN(0);
1069 }
1070
1071 static void mdd_device_shutdown(const struct lu_env *env, struct mdd_device *m,
1072                                 struct lustre_cfg *cfg)
1073 {
1074         barrier_deregister(m->mdd_bottom);
1075         lfsck_degister(env, m->mdd_bottom);
1076         mdd_hsm_actions_llog_fini(env, m);
1077         mdd_changelog_fini(env, m);
1078         mdd_orphan_index_fini(env, m);
1079         mdd_dot_lustre_cleanup(env, m);
1080         if (mdd2obd_dev(m)->u.obt.obt_nodemap_config_file) {
1081                 nm_config_file_deregister_tgt(env,
1082                                 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file);
1083                 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file = NULL;
1084         }
1085         if (m->mdd_los != NULL) {
1086                 local_oid_storage_fini(env, m->mdd_los);
1087                 m->mdd_los = NULL;
1088         }
1089         lu_site_purge(env, mdd2lu_dev(m)->ld_site, ~0);
1090
1091         if (m->mdd_child_exp)
1092                 obd_disconnect(m->mdd_child_exp);
1093 }
1094
1095 static int mdd_process_config(const struct lu_env *env,
1096                               struct lu_device *d, struct lustre_cfg *cfg)
1097 {
1098         struct mdd_device *m    = lu2mdd_dev(d);
1099         struct dt_device  *dt   = m->mdd_child;
1100         struct lu_device  *next = &dt->dd_lu_dev;
1101         int rc;
1102         ENTRY;
1103
1104         switch (cfg->lcfg_command) {
1105         case LCFG_PARAM: {
1106                 struct obd_device *obd = mdd2obd_dev(m);
1107
1108                 rc = class_process_proc_param(PARAM_MDD, obd->obd_vars, cfg, m);
1109                 if (rc > 0 || rc == -ENOSYS)
1110                         /* we don't understand; pass it on */
1111                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
1112                 break;
1113         }
1114         case LCFG_SETUP:
1115                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1116                 if (rc)
1117                         GOTO(out, rc);
1118                 dt_conf_get(env, dt, &m->mdd_dt_conf);
1119                 break;
1120         case LCFG_PRE_CLEANUP:
1121                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1122                 mdd_generic_thread_stop(&m->mdd_orphan_cleanup_thread);
1123                 break;
1124         case LCFG_CLEANUP:
1125                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1126                 lu_dev_del_linkage(d->ld_site, d);
1127                 mdd_device_shutdown(env, m, cfg);
1128                 break;
1129         default:
1130                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1131                 break;
1132         }
1133 out:
1134         RETURN(rc);
1135 }
1136
1137 static int mdd_recovery_complete(const struct lu_env *env,
1138                                  struct lu_device *d)
1139 {
1140         struct mdd_device *mdd = lu2mdd_dev(d);
1141         struct lu_device *next;
1142         int rc;
1143         ENTRY;
1144
1145         LASSERT(mdd != NULL);
1146         next = &mdd->mdd_child->dd_lu_dev;
1147
1148         if (!mdd->mdd_bottom->dd_rdonly)
1149                 mdd_orphan_cleanup(env, mdd);
1150         rc = next->ld_ops->ldo_recovery_complete(env, next);
1151
1152         RETURN(rc);
1153 }
1154
1155 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
1156                           const struct lu_fid *pfid, const char *name,
1157                           __u32 mode, struct lu_fid *fid)
1158 {
1159         struct dt_object *parent, *dto;
1160         int rc = 0;
1161
1162         ENTRY;
1163
1164         LASSERT(!fid_is_zero(pfid));
1165         parent = dt_locate(env, mdd->mdd_bottom, pfid);
1166         if (unlikely(IS_ERR(parent)))
1167                 RETURN(PTR_ERR(parent));
1168
1169         /* create local file/dir, if @fid is passed then try to use it */
1170         if (fid_is_zero(fid))
1171                 dto = local_file_find_or_create(env, mdd->mdd_los, parent,
1172                                                 name, mode);
1173         else
1174                 dto = local_file_find_or_create_with_fid(env, mdd->mdd_bottom,
1175                                                          fid, parent, name,
1176                                                          mode);
1177         if (IS_ERR(dto))
1178                 GOTO(out_put, rc = PTR_ERR(dto));
1179         *fid = *lu_object_fid(&dto->do_lu);
1180         /* since stack is not fully set up the local_storage uses own stack
1181          * and we should drop its object from cache */
1182         dt_object_put_nocache(env, dto);
1183         EXIT;
1184 out_put:
1185         dt_object_put(env, parent);
1186         return rc;
1187 }
1188
1189 static int mdd_lfsck_out_notify(const struct lu_env *env, void *data,
1190                                 enum lfsck_events event)
1191 {
1192         return 0;
1193 }
1194
1195 static int mdd_prepare(const struct lu_env *env,
1196                        struct lu_device *pdev,
1197                        struct lu_device *cdev)
1198 {
1199         struct mdd_device *mdd = lu2mdd_dev(cdev);
1200         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
1201         struct nm_config_file *nodemap_config;
1202         struct obd_device_target *obt = &mdd2obd_dev(mdd)->u.obt;
1203         struct lu_fid fid;
1204         int rc;
1205
1206         ENTRY;
1207
1208         rc = next->ld_ops->ldo_prepare(env, cdev, next);
1209         if (rc)
1210                 RETURN(rc);
1211
1212         /* Setup local dirs */
1213         fid.f_seq = FID_SEQ_LOCAL_NAME;
1214         fid.f_oid = 1;
1215         fid.f_ver = 0;
1216         rc = local_oid_storage_init(env, mdd->mdd_bottom, &fid,
1217                                     &mdd->mdd_los);
1218         if (rc)
1219                 RETURN(rc);
1220
1221         rc = dt_root_get(env, mdd->mdd_child, &mdd->mdd_local_root_fid);
1222         if (rc < 0)
1223                 GOTO(out_los, rc);
1224
1225         lu_root_fid(&fid);
1226         if (mdd_seq_site(mdd)->ss_node_id == 0) {
1227                 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
1228                                            mdd_root_dir_name, S_IFDIR |
1229                                            S_IRUGO | S_IWUSR | S_IXUGO, &fid);
1230                 if (rc != 0) {
1231                         CERROR("%s: create root fid failed: rc = %d\n",
1232                                mdd2obd_dev(mdd)->obd_name, rc);
1233                         GOTO(out_los, rc);
1234                 }
1235
1236                 mdd->mdd_root_fid = fid;
1237                 rc = mdd_dot_lustre_setup(env, mdd);
1238                 if (rc != 0) {
1239                         CERROR("%s: initializing .lustre failed: rc = %d\n",
1240                                mdd2obd_dev(mdd)->obd_name, rc);
1241                         GOTO(out_los, rc);
1242                 }
1243         } else {
1244                 /* Normal client usually send root access to MDT0 directly,
1245                  * the root FID on non-MDT0 will only be used by echo client. */
1246                 mdd->mdd_root_fid = fid;
1247         }
1248
1249         rc = mdd_orphan_index_init(env, mdd);
1250         if (rc < 0)
1251                 GOTO(out_dot, rc);
1252
1253         rc = mdd_changelog_init(env, mdd);
1254         if (rc != 0) {
1255                 CERROR("%s: failed to initialize changelog: rc = %d\n",
1256                        mdd2obd_dev(mdd)->obd_name, rc);
1257                 GOTO(out_orph, rc);
1258         }
1259
1260         rc = mdd_hsm_actions_llog_init(env, mdd);
1261         if (rc != 0)
1262                 GOTO(out_changelog, rc);
1263
1264         nodemap_config = nm_config_file_register_tgt(env, mdd->mdd_bottom,
1265                                                      mdd->mdd_los);
1266         if (IS_ERR(nodemap_config)) {
1267                 rc = PTR_ERR(nodemap_config);
1268                 if (rc != -EROFS)
1269                         GOTO(out_hsm, rc);
1270         } else {
1271                 obt->obt_nodemap_config_file = nodemap_config;
1272         }
1273
1274         rc = lfsck_register(env, mdd->mdd_bottom, mdd->mdd_child,
1275                             mdd2obd_dev(mdd), mdd_lfsck_out_notify,
1276                             mdd, true);
1277         if (rc != 0) {
1278                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
1279                        mdd2obd_dev(mdd)->obd_name, rc);
1280                 GOTO(out_nodemap, rc);
1281         }
1282
1283         rc = barrier_register(mdd->mdd_bottom, mdd->mdd_child);
1284         if (rc) {
1285                 CERROR("%s: failed to register to barrier: rc = %d\n",
1286                        mdd2obd_dev(mdd)->obd_name, rc);
1287                 GOTO(out_lfsck, rc);
1288         }
1289
1290         RETURN(0);
1291
1292 out_lfsck:
1293         lfsck_degister(env, mdd->mdd_bottom);
1294 out_nodemap:
1295         nm_config_file_deregister_tgt(env, obt->obt_nodemap_config_file);
1296         obt->obt_nodemap_config_file = NULL;
1297 out_hsm:
1298         mdd_hsm_actions_llog_fini(env, mdd);
1299 out_changelog:
1300         mdd_changelog_fini(env, mdd);
1301 out_orph:
1302         mdd_orphan_index_fini(env, mdd);
1303 out_dot:
1304         if (mdd_seq_site(mdd)->ss_node_id == 0)
1305                 mdd_dot_lustre_cleanup(env, mdd);
1306 out_los:
1307         local_oid_storage_fini(env, mdd->mdd_los);
1308         mdd->mdd_los = NULL;
1309
1310         return rc;
1311 }
1312
1313 const struct lu_device_operations mdd_lu_ops = {
1314         .ldo_object_alloc      = mdd_object_alloc,
1315         .ldo_process_config    = mdd_process_config,
1316         .ldo_recovery_complete = mdd_recovery_complete,
1317         .ldo_prepare           = mdd_prepare,
1318 };
1319
1320 static int mdd_root_get(const struct lu_env *env,
1321                         struct md_device *m, struct lu_fid *f)
1322 {
1323         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1324
1325         ENTRY;
1326         *f = mdd->mdd_root_fid;
1327         RETURN(0);
1328 }
1329
1330 /*
1331  * No permission check is needed.
1332  */
1333 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1334                       struct obd_statfs *sfs)
1335 {
1336         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1337         int rc;
1338
1339         ENTRY;
1340
1341         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
1342
1343         sfs->os_namelen = min_t(__u32, sfs->os_namelen, NAME_MAX);
1344
1345         RETURN(rc);
1346 }
1347
1348 static const struct dt_device_param *mdd_dtconf_get(const struct lu_env *env,
1349                                                     struct md_device *m)
1350 {
1351         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1352
1353         return &mdd->mdd_dt_conf;
1354 }
1355
1356 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1357                              int idx, void **h)
1358 {
1359         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1360
1361         *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1362         return (*h == NULL ? -ENOENT : 0);
1363 }
1364
1365 static struct lu_device *mdd_device_free(const struct lu_env *env,
1366                                          struct lu_device *lu)
1367 {
1368         struct mdd_device *m = lu2mdd_dev(lu);
1369         ENTRY;
1370
1371         LASSERT(atomic_read(&lu->ld_ref) == 0);
1372         md_device_fini(&m->mdd_md_dev);
1373         OBD_FREE_PTR(m);
1374         RETURN(NULL);
1375 }
1376
1377 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1378                                           struct lu_device_type *t,
1379                                           struct lustre_cfg *lcfg)
1380 {
1381         struct lu_device  *l;
1382         struct mdd_device *m;
1383
1384         OBD_ALLOC_PTR(m);
1385         if (m == NULL) {
1386                 l = ERR_PTR(-ENOMEM);
1387         } else {
1388                 int rc;
1389
1390                 l = mdd2lu_dev(m);
1391                 md_device_init(&m->mdd_md_dev, t);
1392                 rc = mdd_init0(env, m, t, lcfg);
1393                 if (rc != 0) {
1394                         mdd_device_free(env, l);
1395                         l = ERR_PTR(rc);
1396                 }
1397         }
1398
1399         return l;
1400 }
1401
1402 /*
1403  * we use exports to track all mdd users
1404  */
1405 static int mdd_obd_connect(const struct lu_env *env, struct obd_export **exp,
1406                            struct obd_device *obd, struct obd_uuid *cluuid,
1407                            struct obd_connect_data *data, void *localdata)
1408 {
1409         struct mdd_device    *mdd = lu2mdd_dev(obd->obd_lu_dev);
1410         struct lustre_handle  conn;
1411         int                   rc;
1412         ENTRY;
1413
1414         CDEBUG(D_CONFIG, "connect #%d\n", mdd->mdd_connects);
1415
1416         rc = class_connect(&conn, obd, cluuid);
1417         if (rc)
1418                 RETURN(rc);
1419
1420         *exp = class_conn2export(&conn);
1421
1422         /* Why should there ever be more than 1 connect? */
1423         LASSERT(mdd->mdd_connects == 0);
1424         mdd->mdd_connects++;
1425
1426         RETURN(0);
1427 }
1428
1429 /*
1430  * once last export (we don't count self-export) disappeared
1431  * mdd can be released
1432  */
1433 static int mdd_obd_disconnect(struct obd_export *exp)
1434 {
1435         struct obd_device *obd = exp->exp_obd;
1436         struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1437         int                rc, release = 0;
1438         ENTRY;
1439
1440         mdd->mdd_connects--;
1441         if (mdd->mdd_connects == 0)
1442                 release = 1;
1443
1444         rc = class_disconnect(exp);
1445
1446         if (rc == 0 && release)
1447                 class_manual_cleanup(obd);
1448         RETURN(rc);
1449 }
1450
1451 static int mdd_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1452                             __u32 keylen, void *key, __u32 *vallen, void *val)
1453 {
1454         int rc = -EINVAL;
1455
1456         if (KEY_IS(KEY_OSP_CONNECTED)) {
1457                 struct obd_device       *obd = exp->exp_obd;
1458                 struct mdd_device       *mdd;
1459
1460                 if (!obd->obd_set_up || obd->obd_stopping)
1461                         RETURN(-EAGAIN);
1462
1463                 mdd = lu2mdd_dev(obd->obd_lu_dev);
1464                 LASSERT(mdd);
1465                 rc = obd_get_info(env, mdd->mdd_child_exp, keylen, key, vallen,
1466                                   val);
1467                 RETURN(rc);
1468         }
1469
1470         RETURN(rc);
1471 }
1472
1473 static int mdd_obd_set_info_async(const struct lu_env *env,
1474                                   struct obd_export *exp,
1475                                   __u32 keylen, void *key,
1476                                   __u32 vallen, void *val,
1477                                   struct ptlrpc_request_set *set)
1478 {
1479         struct obd_device       *obd = exp->exp_obd;
1480         struct mdd_device       *mdd;
1481         int                      rc;
1482
1483         if (!obd->obd_set_up || obd->obd_stopping)
1484                 RETURN(-EAGAIN);
1485
1486         mdd = lu2mdd_dev(obd->obd_lu_dev);
1487         LASSERT(mdd);
1488         rc = obd_set_info_async(env, mdd->mdd_child_exp, keylen, key,
1489                                 vallen, val, set);
1490         RETURN(rc);
1491 }
1492
1493 static struct obd_ops mdd_obd_device_ops = {
1494         .o_owner        = THIS_MODULE,
1495         .o_connect      = mdd_obd_connect,
1496         .o_disconnect   = mdd_obd_disconnect,
1497         .o_get_info     = mdd_obd_get_info,
1498         .o_set_info_async = mdd_obd_set_info_async,
1499 };
1500
1501 static int mdd_changelog_user_register(const struct lu_env *env,
1502                                        struct mdd_device *mdd, int *id)
1503 {
1504         struct llog_ctxt *ctxt;
1505         struct llog_changelog_user_rec *rec;
1506         int rc;
1507         ENTRY;
1508
1509         ctxt = llog_get_context(mdd2obd_dev(mdd),
1510                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1511         if (ctxt == NULL)
1512                 RETURN(-ENXIO);
1513
1514         OBD_ALLOC_PTR(rec);
1515         if (rec == NULL) {
1516                 llog_ctxt_put(ctxt);
1517                 RETURN(-ENOMEM);
1518         }
1519
1520         CFS_RACE(CFS_FAIL_CHLOG_USER_REG_UNREG_RACE);
1521
1522         rec->cur_hdr.lrh_len = sizeof(*rec);
1523         rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1524         spin_lock(&mdd->mdd_cl.mc_user_lock);
1525         if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1526                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1527                 CERROR("Maximum number of changelog users exceeded!\n");
1528                 GOTO(out, rc = -EOVERFLOW);
1529         }
1530         *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1531         mdd->mdd_cl.mc_users++;
1532         rec->cur_endrec = mdd->mdd_cl.mc_index;
1533
1534         rec->cur_time = (__u32)ktime_get_real_seconds();
1535         if (OBD_FAIL_CHECK(OBD_FAIL_TIME_IN_CHLOG_USER))
1536                 rec->cur_time = 0;
1537
1538         spin_unlock(&mdd->mdd_cl.mc_user_lock);
1539
1540         rc = llog_cat_add(env, ctxt->loc_handle, &rec->cur_hdr, NULL);
1541         if (rc) {
1542                 CWARN("%s: Failed to register changelog user %d: rc=%d\n",
1543                       mdd2obd_dev(mdd)->obd_name, *id, rc);
1544                 spin_lock(&mdd->mdd_cl.mc_user_lock);
1545                 mdd->mdd_cl.mc_users--;
1546                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1547                 GOTO(out, rc);
1548         }
1549
1550         CDEBUG(D_IOCTL, "Registered changelog user %d\n", *id);
1551
1552         /* Assume we want it on since somebody registered */
1553         rc = mdd_changelog_on(env, mdd);
1554         if (rc)
1555                 GOTO(out, rc);
1556
1557 out:
1558         OBD_FREE_PTR(rec);
1559         llog_ctxt_put(ctxt);
1560         RETURN(rc);
1561 }
1562
1563 struct mdd_changelog_user_purge {
1564         struct mdd_device *mcup_mdd;
1565         __u32 mcup_id;
1566         __u32 mcup_usercount;
1567         __u64 mcup_minrec;
1568         bool mcup_found;
1569 };
1570
1571 /**
1572  * changelog_user_purge callback
1573  *
1574  * Is called once per user.
1575  *
1576  * Check to see the user requested is available from rec.
1577  * Truncate the changelog.
1578  * Keep track of the total number of users (calls).
1579  */
1580 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1581                                        struct llog_handle *llh,
1582                                        struct llog_rec_hdr *hdr, void *data)
1583 {
1584         struct llog_changelog_user_rec  *rec;
1585         struct mdd_changelog_user_purge *mcup = data;
1586         struct llog_cookie               cookie;
1587         int                              rc;
1588
1589         ENTRY;
1590
1591         if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
1592                 RETURN(-ENXIO);
1593
1594         rec = container_of(hdr, struct llog_changelog_user_rec, cur_hdr);
1595
1596         mcup->mcup_usercount++;
1597
1598         if (rec->cur_id != mcup->mcup_id) {
1599                 /* truncate to the lowest endrec that is not this user */
1600                 mcup->mcup_minrec = min(mcup->mcup_minrec,
1601                                         rec->cur_endrec);
1602                 RETURN(0);
1603         }
1604
1605         /* Unregister this user */
1606         cookie.lgc_lgl = llh->lgh_id;
1607         cookie.lgc_index = hdr->lrh_index;
1608
1609         rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1610                                      1, &cookie);
1611         if (rc == 0) {
1612                 mcup->mcup_found = true;
1613                 mcup->mcup_usercount--;
1614                 spin_lock(&mcup->mcup_mdd->mdd_cl.mc_user_lock);
1615                 mcup->mcup_mdd->mdd_cl.mc_users--;
1616                 spin_unlock(&mcup->mcup_mdd->mdd_cl.mc_user_lock);
1617         }
1618
1619         RETURN(rc);
1620 }
1621
1622 int mdd_changelog_user_purge(const struct lu_env *env,
1623                              struct mdd_device *mdd, __u32 id)
1624 {
1625         struct mdd_changelog_user_purge mcup = {
1626                 .mcup_mdd = mdd,
1627                 .mcup_id = id,
1628                 .mcup_found = false,
1629                 .mcup_usercount = 0,
1630                 .mcup_minrec = ULLONG_MAX,
1631         };
1632         struct llog_ctxt *ctxt;
1633         int rc;
1634
1635         ENTRY;
1636
1637         CDEBUG(D_IOCTL, "%s: Purge request: id=%u\n",
1638                mdd2obd_dev(mdd)->obd_name, id);
1639
1640         ctxt = llog_get_context(mdd2obd_dev(mdd),
1641                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1642         if (ctxt == NULL ||
1643             (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
1644                 GOTO(out, rc = -ENXIO);
1645
1646         rc = llog_cat_process(env, ctxt->loc_handle,
1647                               mdd_changelog_user_purge_cb, &mcup,
1648                               0, 0);
1649
1650         if ((rc == 0) && (mcup.mcup_usercount == 0)) {
1651                 spin_lock(&mdd->mdd_cl.mc_user_lock);
1652                 if (mdd->mdd_cl.mc_users == 0) {
1653                         /* No more users; turn changelogs off */
1654                         CDEBUG(D_IOCTL, "turning off changelogs\n");
1655                         rc = mdd_changelog_off(env, mdd);
1656                 }
1657                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1658         }
1659
1660         if ((rc == 0) && mcup.mcup_found) {
1661                 CDEBUG(D_IOCTL, "%s: Purging changelog entries for user %d "
1662                        "record=%llu\n",
1663                        mdd2obd_dev(mdd)->obd_name, id, mcup.mcup_minrec);
1664                 /* Cancelling record 0 destroys the entire changelog, make sure
1665                    we don't do that unless we mean it. */
1666                 if (mcup.mcup_minrec != 0 || mcup.mcup_usercount == 0) {
1667                         rc = mdd_changelog_llog_cancel(env, mdd,
1668                                                        mcup.mcup_minrec);
1669                 }
1670         } else {
1671                 CWARN("%s: No changelog for user %u; rc=%d\n",
1672                       mdd2obd_dev(mdd)->obd_name, id, rc);
1673                 GOTO(out, rc = -ENOENT);
1674         }
1675
1676         CFS_RACE(CFS_FAIL_CHLOG_USER_REG_UNREG_RACE);
1677
1678         EXIT;
1679 out:
1680         if (ctxt != NULL)
1681                 llog_ctxt_put(ctxt);
1682
1683         return rc;
1684 }
1685
1686 struct mdd_changelog_user_clear {
1687         __u64 mcuc_endrec;
1688         __u64 mcuc_minrec;
1689         __u32 mcuc_id;
1690         bool mcuc_flush;
1691 };
1692
1693 /**
1694  * changelog_clear callback
1695  *
1696  * Is called once per user.
1697  *
1698  * Check to see the user requested is available from rec.
1699  * Check the oldest (smallest) record for boundary conditions.
1700  * Truncate the changelog.
1701  */
1702 static int mdd_changelog_clear_cb(const struct lu_env *env,
1703                                   struct llog_handle *llh,
1704                                   struct llog_rec_hdr *hdr,
1705                                   void *data)
1706 {
1707         struct llog_changelog_user_rec *rec;
1708         struct mdd_changelog_user_clear *mcuc = data;
1709         int rc;
1710
1711         ENTRY;
1712
1713         if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
1714                 RETURN(-ENXIO);
1715
1716         rec = container_of(hdr, struct llog_changelog_user_rec, cur_hdr);
1717
1718         /* Does the changelog id match the requested id? */
1719         if (rec->cur_id != mcuc->mcuc_id) {
1720                 mcuc->mcuc_minrec = min(mcuc->mcuc_minrec,
1721                                         rec->cur_endrec);
1722                 RETURN(0);
1723         }
1724
1725         /* cur_endrec is the oldest purgeable record, make sure we're newer */
1726         if (rec->cur_endrec > mcuc->mcuc_endrec) {
1727                 CDEBUG(D_IOCTL, "Request %llu out of range: %llu\n",
1728                        mcuc->mcuc_endrec, rec->cur_endrec);
1729                 RETURN(-EINVAL);
1730         }
1731
1732         /* Flag that we've met all the range and user checks.
1733          * We now know the record to flush.
1734          */
1735         rec->cur_endrec = mcuc->mcuc_endrec;
1736
1737         rec->cur_time = (__u32)ktime_get_real_seconds();
1738         if (OBD_FAIL_CHECK(OBD_FAIL_TIME_IN_CHLOG_USER))
1739                 rec->cur_time = 0;
1740
1741         mcuc->mcuc_flush = true;
1742
1743         CDEBUG(D_IOCTL, "Rewriting changelog user %u endrec to %llu\n",
1744                mcuc->mcuc_id, rec->cur_endrec);
1745
1746         /* Update the endrec */
1747         rc = llog_write(env, llh, hdr, hdr->lrh_index);
1748
1749         RETURN(rc);
1750 }
1751
1752 /**
1753  * Clear a changelog up to entry specified by endrec for user id.
1754  */
1755 static int mdd_changelog_clear(const struct lu_env *env,
1756                                struct mdd_device *mdd, __u32 id,
1757                                __u64 endrec)
1758 {
1759         struct mdd_changelog_user_clear mcuc = {
1760                 .mcuc_id = id,
1761                 .mcuc_minrec = endrec,
1762                 .mcuc_flush = false,
1763         };
1764         struct llog_ctxt *ctxt;
1765         __u64 start_rec;
1766         int rc;
1767
1768         ENTRY;
1769
1770         CDEBUG(D_IOCTL, "%s: Purge request: id=%u, endrec=%llu\n",
1771                mdd2obd_dev(mdd)->obd_name, id, endrec);
1772         /* start_rec is the newest (largest value) entry in the changelogs*/
1773         spin_lock(&mdd->mdd_cl.mc_lock);
1774         start_rec = mdd->mdd_cl.mc_index;
1775         spin_unlock(&mdd->mdd_cl.mc_lock);
1776
1777         if (start_rec < endrec) {
1778                 CDEBUG(D_IOCTL, "%s: Could not clear changelog, requested "\
1779                        "address out of range\n", mdd2obd_dev(mdd)->obd_name);
1780                 RETURN(-EINVAL);
1781         }
1782
1783         if (endrec == 0) {
1784                 mcuc.mcuc_endrec = start_rec;
1785                 mcuc.mcuc_minrec = ULLONG_MAX;
1786         } else {
1787                 mcuc.mcuc_endrec = endrec;
1788         }
1789
1790         ctxt = llog_get_context(mdd2obd_dev(mdd),
1791                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1792         if (ctxt == NULL ||
1793             (ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) == 0)
1794                 GOTO(out, rc = -ENXIO);
1795
1796         rc = llog_cat_process(env, ctxt->loc_handle,
1797                               mdd_changelog_clear_cb, (void *)&mcuc,
1798                               0, 0);
1799
1800         if (rc < 0) {
1801                 CWARN("%s: Failure to clear the changelog for user %d: %d\n",
1802                       mdd2obd_dev(mdd)->obd_name, id, rc);
1803         } else if (mcuc.mcuc_flush) {
1804                 /* Cancelling record 0 destroys the entire changelog, make sure
1805                    we don't do that unless we mean it. */
1806                 if (mcuc.mcuc_minrec != 0) {
1807                         CDEBUG(D_IOCTL, "%s: Purging changelog entries up "\
1808                                "to %llu\n", mdd2obd_dev(mdd)->obd_name,
1809                                mcuc.mcuc_minrec);
1810
1811                         rc = mdd_changelog_llog_cancel(env, mdd,
1812                                                       mcuc.mcuc_minrec);
1813                 }
1814         } else {
1815                 CWARN("%s: No entry for user %d\n",
1816                       mdd2obd_dev(mdd)->obd_name, id);
1817                 rc = -ENOENT;
1818         }
1819
1820         EXIT;
1821 out:
1822         if (ctxt != NULL)
1823                 llog_ctxt_put(ctxt);
1824
1825         return rc;
1826 }
1827
1828 /** mdd_iocontrol
1829  * May be called remotely from mdt_iocontrol_handle or locally from
1830  * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
1831  * an obd_ioctl_data format (but local ioctl handler does).
1832  * \param cmd - ioc
1833  * \param len - data len
1834  * \param karg - ioctl data, in kernel space
1835  */
1836 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
1837                          unsigned int cmd, int len, void *karg)
1838 {
1839         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1840         struct obd_ioctl_data *data = karg;
1841         int rc;
1842         ENTRY;
1843
1844         /* Doesn't use obd_ioctl_data */
1845         switch (cmd) {
1846         case OBD_IOC_CHANGELOG_CLEAR: {
1847                 struct changelog_setinfo *cs = karg;
1848
1849                 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1850                         RETURN(-EINPROGRESS);
1851
1852                 rc = mdd_changelog_clear(env, mdd, cs->cs_id,
1853                                          cs->cs_recno);
1854                 barrier_exit(mdd->mdd_bottom);
1855                 RETURN(rc);
1856         }
1857         case OBD_IOC_START_LFSCK: {
1858                 rc = lfsck_start(env, mdd->mdd_bottom,
1859                                  (struct lfsck_start_param *)karg);
1860                 RETURN(rc);
1861         }
1862         case OBD_IOC_STOP_LFSCK: {
1863                 rc = lfsck_stop(env, mdd->mdd_bottom,
1864                                 (struct lfsck_stop *)karg);
1865                 RETURN(rc);
1866         }
1867         case OBD_IOC_QUERY_LFSCK: {
1868                 rc = lfsck_query(env, mdd->mdd_bottom, NULL, NULL,
1869                                  (struct lfsck_query *)karg);
1870                 RETURN(rc);
1871         }
1872         }
1873
1874         /* Below ioctls use obd_ioctl_data */
1875         if (len != sizeof(*data)) {
1876                 CERROR("Bad ioctl size %d\n", len);
1877                 RETURN(-EINVAL);
1878         }
1879         if (data->ioc_version != OBD_IOCTL_VERSION) {
1880                 CERROR("Bad magic %x != %x\n", data->ioc_version,
1881                        OBD_IOCTL_VERSION);
1882                 RETURN(-EINVAL);
1883         }
1884
1885         switch (cmd) {
1886         case OBD_IOC_CHANGELOG_REG:
1887                 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1888                         RETURN(-EINPROGRESS);
1889
1890                 rc = mdd_changelog_user_register(env, mdd, &data->ioc_u32_1);
1891                 barrier_exit(mdd->mdd_bottom);
1892                 break;
1893         case OBD_IOC_CHANGELOG_DEREG:
1894                 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
1895                         RETURN(-EINPROGRESS);
1896
1897                 rc = mdd_changelog_user_purge(env, mdd, data->ioc_u32_1);
1898                 barrier_exit(mdd->mdd_bottom);
1899                 break;
1900         default:
1901                 rc = -ENOTTY;
1902         }
1903
1904         RETURN(rc);
1905 }
1906
1907 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
1908 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key);
1909
1910 static const struct md_device_operations mdd_ops = {
1911         .mdo_statfs         = mdd_statfs,
1912         .mdo_root_get       = mdd_root_get,
1913         .mdo_llog_ctxt_get  = mdd_llog_ctxt_get,
1914         .mdo_iocontrol      = mdd_iocontrol,
1915         .mdo_dtconf_get     = mdd_dtconf_get,
1916 };
1917
1918 static struct lu_device_type_operations mdd_device_type_ops = {
1919         .ldto_init = mdd_type_init,
1920         .ldto_fini = mdd_type_fini,
1921
1922         .ldto_start = mdd_type_start,
1923         .ldto_stop  = mdd_type_stop,
1924
1925         .ldto_device_alloc = mdd_device_alloc,
1926         .ldto_device_free  = mdd_device_free,
1927
1928         .ldto_device_fini    = mdd_device_fini
1929 };
1930
1931 static struct lu_device_type mdd_device_type = {
1932         .ldt_tags     = LU_DEVICE_MD,
1933         .ldt_name     = LUSTRE_MDD_NAME,
1934         .ldt_ops      = &mdd_device_type_ops,
1935         .ldt_ctx_tags = LCT_MD_THREAD
1936 };
1937
1938 /* context key constructor: mdd_key_init */
1939 LU_KEY_INIT(mdd, struct mdd_thread_info);
1940
1941 static void mdd_key_fini(const struct lu_context *ctx,
1942                          struct lu_context_key *key, void *data)
1943 {
1944         struct mdd_thread_info *info = data;
1945
1946         lu_buf_free(&info->mti_big_buf);
1947         lu_buf_free(&info->mti_link_buf);
1948         lu_buf_free(&info->mti_xattr_buf);
1949
1950         OBD_FREE_PTR(info);
1951 }
1952
1953 /* context key: mdd_thread_key */
1954 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
1955
1956 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
1957                              int (*func)(void *), void *data, char *name)
1958 {
1959         struct task_struct      *task;
1960
1961         LASSERT(thread->mgt_init == false);
1962         init_completion(&thread->mgt_started);
1963         init_completion(&thread->mgt_finished);
1964         thread->mgt_data = data;
1965         thread->mgt_abort = false;
1966         thread->mgt_init = true;
1967
1968         task = kthread_run(func, thread, name);
1969         if (IS_ERR(task)) {
1970                 complete(&thread->mgt_finished);
1971                 return PTR_ERR(task);
1972         }
1973         wait_for_completion(&thread->mgt_started);
1974         return 0;
1975 }
1976
1977 void mdd_generic_thread_stop(struct mdd_generic_thread *thread)
1978 {
1979         if (thread->mgt_init == true) {
1980                 thread->mgt_abort = true;
1981                 wait_for_completion(&thread->mgt_finished);
1982         }
1983 }
1984
1985 static int __init mdd_init(void)
1986 {
1987         int rc;
1988
1989         rc = lu_kmem_init(mdd_caches);
1990         if (rc)
1991                 return rc;
1992
1993         changelog_orig_logops = llog_osd_ops;
1994         changelog_orig_logops.lop_add = llog_cat_add_rec;
1995         changelog_orig_logops.lop_declare_add = llog_cat_declare_add_rec;
1996
1997         hsm_actions_logops = llog_osd_ops;
1998         hsm_actions_logops.lop_add = llog_cat_add_rec;
1999         hsm_actions_logops.lop_declare_add = llog_cat_declare_add_rec;
2000
2001         rc = class_register_type(&mdd_obd_device_ops, NULL, true, NULL,
2002                                  LUSTRE_MDD_NAME, &mdd_device_type);
2003         if (rc)
2004                 lu_kmem_fini(mdd_caches);
2005         return rc;
2006 }
2007
2008 static void __exit mdd_exit(void)
2009 {
2010         class_unregister_type(LUSTRE_MDD_NAME);
2011         lu_kmem_fini(mdd_caches);
2012 }
2013
2014 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2015 MODULE_DESCRIPTION("Lustre Meta-data Device Driver ("LUSTRE_MDD_NAME")");
2016 MODULE_VERSION(LUSTRE_VERSION_STRING);
2017 MODULE_LICENSE("GPL");
2018
2019 module_init(mdd_init);
2020 module_exit(mdd_exit);