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