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