Whamcloud - gitweb
37667afde0e4f0c10fb1ea22dc38fc71b20e56a8
[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  *
31  * lustre/mdd/mdd_device.c
32  *
33  * Lustre Metadata Server (mdd) routines
34  *
35  * Author: Wang Di <wangdi@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/module.h>
41 #include <linux/kthread.h>
42 #include <obd_class.h>
43 #include <uapi/linux/lustre/lustre_ioctl.h>
44 #include <lustre_mds.h>
45 #include <obd_support.h>
46 #include <lu_object.h>
47 #include <uapi/linux/lustre/lustre_param.h>
48 #include <lustre_fid.h>
49 #include <lustre_nodemap.h>
50 #include <lustre_barrier.h>
51
52 #include "mdd_internal.h"
53
54 static const struct md_device_operations mdd_ops;
55 static struct lu_device_type mdd_device_type;
56
57 static const char mdd_root_dir_name[] = "ROOT";
58 static const char mdd_obf_dir_name[] = "fid";
59 static const char mdd_lpf_dir_name[] = "lost+found";
60
61 /* Slab for MDD object allocation */
62 struct kmem_cache *mdd_object_kmem;
63
64 static struct lu_kmem_descr mdd_caches[] = {
65         {
66                 .ckd_cache = &mdd_object_kmem,
67                 .ckd_name  = "mdd_obj",
68                 .ckd_size  = sizeof(struct mdd_object)
69         },
70         {
71                 .ckd_cache = NULL
72         }
73 };
74
75 static int mdd_connect_to_next(const struct lu_env *env, struct mdd_device *m,
76                                const char *nextdev)
77 {
78         struct obd_connect_data *data = NULL;
79         struct lu_device        *lud = mdd2lu_dev(m);
80         struct obd_device       *obd;
81         int                      rc;
82         ENTRY;
83
84         LASSERT(m->mdd_child_exp == NULL);
85
86         OBD_ALLOC(data, sizeof(*data));
87         if (data == NULL)
88                 GOTO(out, rc = -ENOMEM);
89
90         obd = class_name2obd(nextdev);
91         if (obd == NULL) {
92                 CERROR("can't locate next device: %s\n", nextdev);
93                 GOTO(out, rc = -ENOTCONN);
94         }
95
96         data->ocd_connect_flags = OBD_CONNECT_VERSION;
97         data->ocd_version = LUSTRE_VERSION_CODE;
98
99         rc = obd_connect(NULL, &m->mdd_child_exp, obd, &obd->obd_uuid, data, NULL);
100         if (rc) {
101                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
102                 GOTO(out, rc);
103         }
104
105         lud->ld_site = m->mdd_child_exp->exp_obd->obd_lu_dev->ld_site;
106         LASSERT(lud->ld_site);
107         m->mdd_child = lu2dt_dev(m->mdd_child_exp->exp_obd->obd_lu_dev);
108         m->mdd_bottom = lu2dt_dev(lud->ld_site->ls_bottom_dev);
109         lu_dev_add_linkage(lud->ld_site, lud);
110
111 out:
112         if (data)
113                 OBD_FREE(data, sizeof(*data));
114         RETURN(rc);
115 }
116
117 static int mdd_init0(const struct lu_env *env, struct mdd_device *mdd,
118                 struct lu_device_type *t, struct lustre_cfg *lcfg)
119 {
120         int rc = -EINVAL;
121         const char *dev;
122
123         ENTRY;
124
125         /* LU-8040 Set defaults here, before values configs */
126         mdd->mdd_cl.mc_flags = 0; /* off by default */
127         /* per-server mask is set via parameters if needed */
128         mdd->mdd_cl.mc_proc_mask = CHANGELOG_MINMASK;
129         /* current mask is calculated from mask above and users masks */
130         mdd->mdd_cl.mc_current_mask = CHANGELOG_MINMASK;
131         mdd->mdd_cl.mc_deniednext = 60; /* 60 secs by default */
132
133         dev = lustre_cfg_string(lcfg, 0);
134         if (dev == NULL)
135                 RETURN(rc);
136
137         mdd->mdd_md_dev.md_lu_dev.ld_obd = class_name2obd(dev);
138         if (mdd->mdd_md_dev.md_lu_dev.ld_obd == NULL)
139                 RETURN(rc);
140         mdd->mdd_md_dev.md_lu_dev.ld_ops = &mdd_lu_ops;
141         mdd->mdd_md_dev.md_ops = &mdd_ops;
142
143         rc = mdd_connect_to_next(env, mdd, lustre_cfg_string(lcfg, 3));
144         if (rc != 0)
145                 RETURN(rc);
146
147         mdd->mdd_atime_diff = MAX_ATIME_DIFF;
148         /* sync permission changes */
149         mdd->mdd_sync_permission = 1;
150         /* enable changelog garbage collection */
151         mdd->mdd_changelog_gc = 1;
152         /* with a significant amount of idle time */
153         mdd->mdd_changelog_max_idle_time = CHLOG_MAX_IDLE_TIME;
154         /* or a significant amount of late indexes */
155         mdd->mdd_changelog_max_idle_indexes = CHLOG_MAX_IDLE_INDEXES;
156         /* with a reasonable interval between each check */
157         mdd->mdd_changelog_min_gc_interval = CHLOG_MIN_GC_INTERVAL;
158         /* with a very few number of free catalog entries */
159         mdd->mdd_changelog_min_free_cat_entries = CHLOG_MIN_FREE_CAT_ENTRIES;
160         /* special default striping for files created with O_APPEND */
161         mdd->mdd_append_stripe_count = 1;
162         mdd->mdd_append_pool[0] = '\0';
163
164         dt_conf_get(env, mdd->mdd_child, &mdd->mdd_dt_conf);
165
166         /* we are using service name but not mdd obd name
167          * for compatibility reasons.
168          * It is passed from MDT in lustre_cfg[2] buffer */
169         rc = mdd_procfs_init(mdd, lustre_cfg_string(lcfg, 2));
170         if (rc < 0)
171                 obd_disconnect(mdd->mdd_child_exp);
172
173         RETURN(rc);
174 }
175
176 static struct lu_device *mdd_device_fini(const struct lu_env *env,
177                                          struct lu_device *d)
178 {
179         struct mdd_device *mdd = lu2mdd_dev(d);
180
181         if (d->ld_site)
182                 lu_dev_del_linkage(d->ld_site, d);
183
184         mdd_procfs_fini(mdd);
185         return NULL;
186 }
187
188 static int changelog_init_cb(const struct lu_env *env, struct llog_handle *llh,
189                              struct llog_rec_hdr *hdr, void *data)
190 {
191         struct mdd_device *mdd = (struct mdd_device *)data;
192         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
193
194         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
195         LASSERT(rec->cr_hdr.lrh_type == CHANGELOG_REC);
196
197         CDEBUG(D_INFO,
198                "seeing record at index %d/%d/%llu t=%x %.*s in log"
199                DFID"\n", hdr->lrh_index, rec->cr_hdr.lrh_index,
200                rec->cr.cr_index, rec->cr.cr_type, rec->cr.cr_namelen,
201                changelog_rec_name(&rec->cr), PFID(&llh->lgh_id.lgl_oi.oi_fid));
202         spin_lock(&mdd->mdd_cl.mc_lock);
203         mdd->mdd_cl.mc_index = rec->cr.cr_index;
204         spin_unlock(&mdd->mdd_cl.mc_lock);
205         return LLOG_PROC_BREAK;
206 }
207
208 char *mdd_chlg_username(struct llog_changelog_user_rec2 *rec, char *buf,
209                         size_t len)
210 {
211         if (rec->cur_hdr.lrh_type == CHANGELOG_USER_REC2 &&
212             rec->cur_name[0])
213                 snprintf(buf, len, "%s%u-%s",  CHANGELOG_USER_PREFIX,
214                          rec->cur_id, rec->cur_name);
215         else
216                 snprintf(buf, len, "%s%u", CHANGELOG_USER_PREFIX, rec->cur_id);
217         return buf;
218 }
219
220 __u32 mdd_chlg_usermask(struct llog_changelog_user_rec2 *rec)
221 {
222         return rec->cur_hdr.lrh_type == CHANGELOG_USER_REC2 ?
223                rec->cur_mask : 0;
224 }
225
226 static int changelog_user_init_cb(const struct lu_env *env,
227                                   struct llog_handle *llh,
228                                   struct llog_rec_hdr *hdr, void *data)
229 {
230         struct mdd_device *mdd = data;
231         struct llog_changelog_user_rec2 *rec;
232         char user_name[CHANGELOG_USER_NAMELEN_FULL];
233
234         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
235
236         rec = container_of(hdr, typeof(*rec), cur_hdr);
237         if (rec->cur_hdr.lrh_type != CHANGELOG_USER_REC &&
238             rec->cur_hdr.lrh_type != CHANGELOG_USER_REC2) {
239                 CWARN("%s: unknown user type %x at index %u in log "DFID"\n",
240                       mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
241                       rec->cur_hdr.lrh_type, PFID(&llh->lgh_id.lgl_oi.oi_fid));
242
243                 return 0;
244         }
245
246         CDEBUG(D_INFO, "%s: user %s at index %u/%u endrec=%llu in log "DFID"\n",
247                mdd2obd_dev(mdd)->obd_name, mdd_chlg_username(rec, user_name,
248                                                              sizeof(user_name)),
249                hdr->lrh_index, rec->cur_hdr.lrh_index, rec->cur_endrec,
250                PFID(&llh->lgh_id.lgl_oi.oi_fid));
251
252         spin_lock(&mdd->mdd_cl.mc_user_lock);
253         mdd->mdd_cl.mc_lastuser = rec->cur_id;
254         mdd->mdd_cl.mc_users++;
255         if (rec->cur_hdr.lrh_type == CHANGELOG_USER_REC2 && rec->cur_mask)
256                 mdd->mdd_cl.mc_current_mask |= rec->cur_mask;
257         else if (mdd->mdd_cl.mc_proc_mask == CHANGELOG_MINMASK)
258                 mdd->mdd_cl.mc_current_mask |= CHANGELOG_DEFMASK;
259         mdd->mdd_cl.mc_mintime = min(mdd->mdd_cl.mc_mintime, rec->cur_time);
260         mdd->mdd_cl.mc_minrec = min(mdd->mdd_cl.mc_minrec, rec->cur_endrec);
261         spin_unlock(&mdd->mdd_cl.mc_user_lock);
262         spin_lock(&mdd->mdd_cl.mc_lock);
263         if (rec->cur_endrec > mdd->mdd_cl.mc_index)
264                 mdd->mdd_cl.mc_index = rec->cur_endrec;
265         spin_unlock(&mdd->mdd_cl.mc_lock);
266
267         return LLOG_PROC_BREAK;
268 }
269
270 struct changelog_orphan_data {
271         __u64                   clod_index;
272         struct mdd_device       *clod_mdd;
273 };
274
275 /* find oldest changelog record index */
276 static int changelog_detect_orphan_cb(const struct lu_env *env,
277                                       struct llog_handle *llh,
278                                       struct llog_rec_hdr *hdr, void *data)
279 {
280         struct changelog_orphan_data *clod = data;
281         struct mdd_device *mdd = clod->clod_mdd;
282         struct llog_changelog_rec *rec;
283
284         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
285
286         rec = container_of(hdr, typeof(*rec), cr_hdr);
287         if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
288                 CWARN("%s: invalid record at index %d in log "DFID"\n",
289                       mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
290                       PFID(&llh->lgh_id.lgl_oi.oi_fid));
291                 /* try to find some next valid record and thus allow to recover
292                  * from a corrupted LLOG, instead to assert and force a crash
293                  */
294                 return 0;
295         }
296
297         CDEBUG(D_INFO,
298                "%s: record at index %d/%d/%llu t=%x %.*s in log "DFID"\n",
299                mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
300                rec->cr_hdr.lrh_index, rec->cr.cr_index, rec->cr.cr_type,
301                rec->cr.cr_namelen, changelog_rec_name(&rec->cr),
302                PFID(&llh->lgh_id.lgl_oi.oi_fid));
303
304         clod->clod_index = rec->cr.cr_index;
305
306         return LLOG_PROC_BREAK;
307 }
308
309 /* find oldest changelog user index */
310 static int changelog_user_detect_orphan_cb(const struct lu_env *env,
311                                            struct llog_handle *llh,
312                                            struct llog_rec_hdr *hdr, void *data)
313 {
314         struct changelog_orphan_data *clod = data;
315         struct mdd_device *mdd = clod->clod_mdd;
316         struct llog_changelog_user_rec2 *rec;
317         char user_name[CHANGELOG_USER_NAMELEN_FULL];
318
319         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
320
321         rec = container_of(hdr, typeof(*rec), cur_hdr);
322         if (rec->cur_hdr.lrh_type != CHANGELOG_USER_REC &&
323             rec->cur_hdr.lrh_type != CHANGELOG_USER_REC2) {
324                 CWARN("%s: unknown user type %u at index %u in log "DFID"\n",
325                       mdd2obd_dev(mdd)->obd_name, hdr->lrh_index,
326                       rec->cur_hdr.lrh_type, PFID(&llh->lgh_id.lgl_oi.oi_fid));
327                 /* try to find some next valid record and thus allow to recover
328                  * from a corrupted LLOG, instead to assert and force a crash
329                  */
330                 return 0;
331         }
332
333         CDEBUG(D_INFO, "%s: user %s at index %u/%u endrec=%llu in log "DFID"\n",
334                mdd2obd_dev(mdd)->obd_name, mdd_chlg_username(rec, user_name,
335                                                              sizeof(user_name)),
336                hdr->lrh_index, rec->cur_hdr.lrh_index,
337                rec->cur_endrec, PFID(&llh->lgh_id.lgl_oi.oi_fid));
338
339         clod->clod_index = min_t(__u64, clod->clod_index, rec->cur_endrec);
340
341         return 0;
342 }
343
344 struct changelog_cancel_cookie {
345         long long endrec;
346         struct mdd_device *mdd;
347 };
348
349 static int llog_changelog_cancel_cb(const struct lu_env *env,
350                                     struct llog_handle *llh,
351                                     struct llog_rec_hdr *hdr, void *data)
352 {
353         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
354         struct changelog_cancel_cookie *cl_cookie =
355                 (struct changelog_cancel_cookie *)data;
356
357         ENTRY;
358
359         /* This is always a (sub)log, not the catalog */
360         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
361
362         /* if current context is GC-thread allow it to stop upon umount
363          * remaining records cleanup will occur upon next mount
364          *
365          * also during testing, wait for GC-thread to be released
366          *
367          * XXX this requires the GC-thread to not fork a sub-thread via
368          * llog[_cat]_process_or_fork() and we may think to also implement
369          * this shutdown mechanism for manually started user unregister which
370          * can also take a long time if huge backlog of records
371          */
372         if (unlikely(cl_cookie->mdd->mdd_cl.mc_gc_task == current)) {
373                 /* wait to be released */
374                 while (CFS_FAIL_CHECK_QUIET(OBD_FAIL_FORCE_GC_THREAD))
375                         schedule();
376
377                 if (kthread_should_stop())
378                         RETURN(LLOG_PROC_BREAK);
379         }
380
381         if (rec->cr.cr_index > cl_cookie->endrec)
382                 /* records are in order, so we're done */
383                 RETURN(LLOG_PROC_BREAK);
384
385         if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_RACE))) {
386                 if (cfs_fail_val == 0)
387                         cfs_fail_val = hdr->lrh_index;
388                 if (cfs_fail_val == hdr->lrh_index)
389                         OBD_RACE(OBD_FAIL_MDS_CHANGELOG_RACE);
390         }
391
392         /* Records folow one by one, cr_index++. We could calculate the
393          * last cr_index at this plain llog. And if it less then cookie endrec
394          * cancel the whole file.
395          */
396         if ((LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - hdr->lrh_index +
397              rec->cr.cr_index) < cl_cookie->endrec) {
398                 int rc;
399
400                 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_DEL))) {
401                         if (cfs_fail_val == 0) {
402                                 cfs_fail_val = (unsigned long)llh & 0xFFFFFFFF;
403                                 OBD_RACE(OBD_FAIL_MDS_CHANGELOG_DEL);
404                         }
405                 }
406                 rc = llog_destroy(env, llh);
407                 if (!rc) {
408                         CDEBUG(D_HA, "Changelog destroyed plain "DFID"\n",
409                                PFID(&llh->lgh_id.lgl_oi.oi_fid));
410                         RETURN(LLOG_DEL_PLAIN);
411                 }
412         }
413
414         /* cancel them one at a time.  I suppose we could store up the cookies
415          * and cancel them all at once; probably more efficient, but this is
416          * done as a user call, so who cares... */
417
418         RETURN(LLOG_DEL_RECORD);
419 }
420
421 static int llog_changelog_cancel(const struct lu_env *env,
422                                  struct llog_ctxt *ctxt,
423                                  struct changelog_cancel_cookie *cookie)
424 {
425         struct llog_handle      *cathandle = ctxt->loc_handle;
426         int                      rc;
427
428         ENTRY;
429
430         /* This should only be called with the catalog handle */
431         LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
432
433         rc = llog_cat_process(env, cathandle, llog_changelog_cancel_cb,
434                               cookie, 0, 0);
435         if (rc >= 0)
436                 /* 0 or 1 means we're done */
437                 rc = 0;
438         else
439                 CERROR("%s: cancel idx %u of catalog "DFID": rc = %d\n",
440                        ctxt->loc_obd->obd_name, cathandle->lgh_last_idx,
441                        PFID(&cathandle->lgh_id.lgl_oi.oi_fid), rc);
442
443         RETURN(rc);
444 }
445
446 static struct llog_operations changelog_orig_logops;
447
448 static int
449 mdd_changelog_write_header(const struct lu_env *env, struct mdd_device *mdd,
450                            int markerflags);
451
452 static int
453 mdd_changelog_on(const struct lu_env *env, struct mdd_device *mdd)
454 {
455         int rc = 0;
456
457         if ((mdd->mdd_cl.mc_flags & CLM_ON) != 0)
458                 return rc;
459
460         LCONSOLE_INFO("%s: changelog on\n", mdd2obd_dev(mdd)->obd_name);
461         if (mdd->mdd_cl.mc_flags & CLM_ERR) {
462                 CERROR("Changelogs cannot be enabled due to error "
463                        "condition (see %s log).\n",
464                        mdd2obd_dev(mdd)->obd_name);
465                 rc = -ESRCH;
466         } else {
467                 spin_lock(&mdd->mdd_cl.mc_lock);
468                 mdd->mdd_cl.mc_flags |= CLM_ON;
469                 spin_unlock(&mdd->mdd_cl.mc_lock);
470                 rc = mdd_changelog_write_header(env, mdd, CLM_START);
471         }
472         return rc;
473 }
474
475 static int
476 mdd_changelog_off(const struct lu_env *env, struct mdd_device *mdd)
477 {
478         int rc = 0;
479
480         if ((mdd->mdd_cl.mc_flags & CLM_ON) != CLM_ON)
481                 return rc;
482
483         LCONSOLE_INFO("%s: changelog off\n", mdd2obd_dev(mdd)->obd_name);
484         rc = mdd_changelog_write_header(env, mdd, CLM_FINI);
485         spin_lock(&mdd->mdd_cl.mc_lock);
486         mdd->mdd_cl.mc_flags &= ~CLM_ON;
487         spin_unlock(&mdd->mdd_cl.mc_lock);
488
489         return rc;
490 }
491
492 static int mdd_changelog_llog_init(const struct lu_env *env,
493                                    struct mdd_device *mdd)
494 {
495         struct obd_device *obd = mdd2obd_dev(mdd);
496         struct llog_ctxt *ctxt = NULL, *uctxt = NULL;
497         struct changelog_orphan_data clod = {
498                 .clod_mdd = mdd,
499                 .clod_index = -1,
500         }, user_orphan = {
501                 .clod_mdd = mdd,
502                 .clod_index = -1,
503         };
504         int rc;
505
506         ENTRY;
507
508         /* LU-2844 mdd setup failure should not cause umount oops */
509         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_INIT))
510                 RETURN(-EIO);
511
512         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
513         obd->obd_lvfs_ctxt.dt = mdd->mdd_bottom;
514         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
515                         obd, &changelog_orig_logops);
516         if (rc) {
517                 CERROR("%s: changelog llog setup failed: rc = %d\n",
518                        obd->obd_name, rc);
519                 RETURN(rc);
520         }
521
522         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
523         LASSERT(ctxt);
524
525         rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
526                               CHANGELOG_CATALOG);
527         if (rc)
528                 GOTO(out_cleanup, rc);
529
530         rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
531         if (rc)
532                 GOTO(out_close, rc);
533
534         rc = llog_cat_reverse_process(env, ctxt->loc_handle,
535                                       changelog_init_cb, mdd);
536
537         if (rc < 0) {
538                 CERROR("%s: changelog init failed: rc = %d\n", obd->obd_name,
539                        rc);
540                 GOTO(out_close, rc);
541         }
542
543         CDEBUG(D_IOCTL, "changelog starting index=%llu\n",
544                mdd->mdd_cl.mc_index);
545
546         /* setup user changelog */
547         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
548                         obd, &changelog_orig_logops);
549         if (rc) {
550                 CERROR("%s: changelog users llog setup failed: rc = %d\n",
551                        obd->obd_name, rc);
552                 GOTO(out_close, rc);
553         }
554
555         uctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
556         LASSERT(ctxt);
557
558         rc = llog_open_create(env, uctxt, &uctxt->loc_handle, NULL,
559                               CHANGELOG_USERS);
560         if (rc)
561                 GOTO(out_ucleanup, rc);
562
563         rc = llog_init_handle(env, uctxt->loc_handle, LLOG_F_IS_CAT, NULL);
564         if (rc)
565                 GOTO(out_uclose, rc);
566
567         rc = llog_cat_reverse_process(env, uctxt->loc_handle,
568                                       changelog_user_init_cb, mdd);
569         if (rc < 0) {
570                 CERROR("%s: changelog user init failed: rc = %d\n",
571                        obd->obd_name, rc);
572                 GOTO(out_uclose, rc);
573         }
574
575         /* Finally apply per-server mask */
576         mdd->mdd_cl.mc_current_mask |= mdd->mdd_cl.mc_proc_mask;
577
578         /* If we have registered users, assume we want changelogs on */
579         if (mdd->mdd_cl.mc_lastuser > 0) {
580                 rc = mdd_changelog_on(env, mdd);
581                 if (rc < 0)
582                         GOTO(out_uclose, rc);
583         }
584
585         /* find and clear any orphan changelog records (1st record index <
586          * smallest of all users current index), likely to come from an
587          * interrupted manual or GC-thread purge, as its user record had
588          * been deleted first
589          * XXX we may wait for a still registered user clear operation to
590          * do the job, but it may then take a long time to reach the user's
591          * real targetted records if a huge purge backlog is still to be
592          * processed as a long time idle user record could have been deleted
593          * XXX we may need to run end of purge as a separate thread
594          */
595         rc = llog_cat_process(env, ctxt->loc_handle, changelog_detect_orphan_cb,
596                               &clod, 0, 0);
597         if (rc < 0) {
598                 CERROR("%s: changelog detect orphan failed: rc = %d\n",
599                        obd->obd_name, rc);
600                 GOTO(out_uclose, rc);
601         }
602         rc = llog_cat_process(env, uctxt->loc_handle,
603                               changelog_user_detect_orphan_cb,
604                               &user_orphan, 0, 0);
605         if (rc < 0) {
606                 CERROR("%s: changelog user detect orphan failed: rc = %d\n",
607                        obd->obd_name, rc);
608                 GOTO(out_uclose, rc);
609         }
610         if (unlikely(clod.clod_index < user_orphan.clod_index)) {
611                 struct changelog_cancel_cookie cl_cookie = {
612                         .endrec = user_orphan.clod_index,
613                         .mdd = mdd,
614                 };
615
616                 CWARN("%s : orphan changelog records found, starting from "
617                       "index %llu to index %llu, being cleared now\n",
618                       obd->obd_name, clod.clod_index, user_orphan.clod_index);
619
620                 /* XXX we may need to run end of purge as a separate thread */
621                 rc = llog_changelog_cancel(env, ctxt, &cl_cookie);
622                 if (rc < 0) {
623                         CERROR("%s: purge of changelog orphan records failed: "
624                                "rc = %d\n", obd->obd_name, rc);
625                         GOTO(out_uclose, rc);
626                 }
627         }
628
629         llog_ctxt_put(ctxt);
630         llog_ctxt_put(uctxt);
631         RETURN(0);
632 out_uclose:
633         llog_cat_close(env, uctxt->loc_handle);
634 out_ucleanup:
635         llog_cleanup(env, uctxt);
636 out_close:
637         llog_cat_close(env, ctxt->loc_handle);
638 out_cleanup:
639         llog_cleanup(env, ctxt);
640         return rc;
641 }
642
643 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
644 {
645         struct obd_device       *obd = mdd2obd_dev(mdd);
646         int                      rc;
647
648         mdd->mdd_cl.mc_index = 0;
649         spin_lock_init(&mdd->mdd_cl.mc_lock);
650         mdd->mdd_cl.mc_starttime = ktime_get();
651         spin_lock_init(&mdd->mdd_cl.mc_user_lock);
652         mdd->mdd_cl.mc_lastuser = 0;
653
654         /* ensure a GC check will, and a thread run may, occur upon start */
655         mdd->mdd_cl.mc_gc_time = 0;
656         mdd->mdd_cl.mc_gc_task = MDD_CHLG_GC_NONE;
657         mdd->mdd_cl.mc_mintime = (__u32)ktime_get_real_seconds();
658         mdd->mdd_cl.mc_minrec = ULLONG_MAX;
659
660         rc = mdd_changelog_llog_init(env, mdd);
661         if (rc) {
662                 CERROR("%s: changelog setup during init failed: rc = %d\n",
663                        obd->obd_name, rc);
664                 mdd->mdd_cl.mc_flags |= CLM_ERR;
665         }
666
667         return rc;
668 }
669
670 static void mdd_changelog_fini(const struct lu_env *env,
671                                struct mdd_device *mdd)
672 {
673         struct obd_device       *obd = mdd2obd_dev(mdd);
674         struct llog_ctxt        *ctxt;
675
676         if (mdd->mdd_cl.mc_flags & CLM_CLEANUP_DONE)
677                 return;
678         mdd->mdd_cl.mc_flags = CLM_CLEANUP_DONE;
679
680 again:
681         /* stop GC-thread if running */
682         spin_lock(&mdd->mdd_cl.mc_lock);
683         if (likely(mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NONE)) {
684                 /* avoid any attempt to run a GC-thread */
685                 mdd->mdd_cl.mc_gc_task = current;
686                 spin_unlock(&mdd->mdd_cl.mc_lock);
687         } else {
688                 struct task_struct *gc_task;
689
690                 if (unlikely(mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_NEED ||
691                              mdd->mdd_cl.mc_gc_task == MDD_CHLG_GC_START)) {
692                         /* need to wait for birthing GC-thread to be started
693                          * and to have set mc_gc_task to itself
694                          */
695                         spin_unlock(&mdd->mdd_cl.mc_lock);
696                         /* Add a tiny sleep */
697                         schedule_timeout_uninterruptible(1);
698                         /* go back to fully check if GC-thread has started or
699                          * even already exited or if a new one is starting...
700                          */
701                         goto again;
702                 }
703                 /* take a reference on task_struct to avoid it to be freed
704                  * upon exit
705                  */
706                 gc_task = mdd->mdd_cl.mc_gc_task;
707                 get_task_struct(gc_task);
708                 spin_unlock(&mdd->mdd_cl.mc_lock);
709                 kthread_stop(gc_task);
710                 put_task_struct(gc_task);
711         }
712
713         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
714         if (ctxt) {
715                 llog_cat_close(env, ctxt->loc_handle);
716                 llog_cleanup(env, ctxt);
717         }
718         ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
719         if (ctxt) {
720                 llog_cat_close(env, ctxt->loc_handle);
721                 llog_cleanup(env, ctxt);
722         }
723 }
724
725 /**
726  * Remove entries with indicies up to and including \a endrec from the
727  *  changelog
728  * \param mdd
729  * \param endrec
730  * \retval 0 ok
731  */
732 static int
733 mdd_changelog_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
734                           unsigned long long endrec)
735 {
736         struct obd_device *obd = mdd2obd_dev(mdd);
737         struct llog_ctxt *ctxt;
738         unsigned long long cur;
739         struct changelog_cancel_cookie cookie;
740         int rc;
741
742         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
743         if (!ctxt)
744                 return -ENXIO;
745
746         spin_lock(&mdd->mdd_cl.mc_lock);
747         cur = (long long)mdd->mdd_cl.mc_index;
748         spin_unlock(&mdd->mdd_cl.mc_lock);
749
750         /*
751          * If purging all records, write a header entry so we don't have an
752          * empty catalog and we're sure to have a valid starting index next
753          * time. In a case of crash, we just restart with old log so we're
754          * allright.
755          */
756         if (endrec >= cur) {
757                 rc = mdd_changelog_write_header(env, mdd, CLM_PURGE);
758                 if (rc)
759                         goto out;
760                 endrec = cur;
761         }
762
763         /*
764          * Some records were purged, so reset repeat-access time (so we
765          * record new mtime update records, so users can see a file has been
766          * changed since the last purge)
767          */
768         mdd->mdd_cl.mc_starttime = ktime_get();
769
770         cookie.endrec = endrec;
771         cookie.mdd = mdd;
772         rc = llog_changelog_cancel(env, ctxt, &cookie);
773 out:
774         llog_ctxt_put(ctxt);
775         return rc;
776 }
777
778 /** Add a CL_MARK record to the changelog
779  * \param mdd
780  * \param markerflags - CLM_*
781  * \retval 0 ok
782  */
783 int mdd_changelog_write_header(const struct lu_env *env,
784                                struct mdd_device *mdd, int markerflags)
785 {
786         struct obd_device               *obd = mdd2obd_dev(mdd);
787         struct llog_changelog_rec       *rec;
788         struct lu_buf                   *buf;
789         struct llog_ctxt                *ctxt;
790         int                              reclen;
791         int                              len = strlen(obd->obd_name);
792         int                              rc;
793
794         ENTRY;
795
796         if (mdd->mdd_cl.mc_current_mask & BIT(CL_MARK)) {
797                 mdd->mdd_cl.mc_starttime = ktime_get();
798                 RETURN(0);
799         }
800
801         reclen = llog_data_len(sizeof(*rec) + len);
802         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mdi_chlg_buf, reclen);
803         if (buf->lb_buf == NULL)
804                 RETURN(-ENOMEM);
805         rec = buf->lb_buf;
806
807         rec->cr.cr_flags = CLF_VERSION;
808         rec->cr.cr_type = CL_MARK;
809         rec->cr.cr_namelen = len;
810         memcpy(changelog_rec_name(&rec->cr), obd->obd_name, rec->cr.cr_namelen);
811         /* Status and action flags */
812         rec->cr.cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
813         rec->cr_hdr.lrh_len = llog_data_len(changelog_rec_size(&rec->cr) +
814                                             rec->cr.cr_namelen);
815         rec->cr_hdr.lrh_type = CHANGELOG_REC;
816         rec->cr.cr_time = cl_time();
817
818         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
819         LASSERT(ctxt);
820
821         rc = llog_cat_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL);
822         if (rc > 0)
823                 rc = 0;
824         llog_ctxt_put(ctxt);
825
826         /* assume on or off event; reset repeat-access time */
827         mdd->mdd_cl.mc_starttime = ktime_get();
828         RETURN(rc);
829 }
830
831 /**
832  * Lookup method for "fid" object. Only filenames with correct SEQ:OID format
833  * are valid. We also check if object with passed fid exists or not.
834  */
835 static int obf_lookup(const struct lu_env *env, struct md_object *p,
836                       const struct lu_name *lname, struct lu_fid *f,
837                       struct md_op_spec *spec)
838 {
839         char *name = (char *)lname->ln_name;
840         struct mdd_device *mdd = mdo2mdd(p);
841         struct mdd_object *child;
842         int rc = 0;
843
844         while (*name == '[')
845                 name++;
846
847         sscanf(name, SFID, RFID(f));
848         if (!fid_is_sane(f))
849                 GOTO(out, rc = -ENOENT);
850
851         if (!fid_is_norm(f) && !fid_is_igif(f) && !fid_is_root(f) &&
852             !fid_seq_is_dot(f->f_seq))
853                 GOTO(out, rc = -ENOENT);
854
855         /* Check if object with this fid exists */
856         child = mdd_object_find(env, mdd, f);
857         if (IS_ERR(child))
858                 GOTO(out, rc = PTR_ERR(child));
859
860         if (mdd_object_exists(child) == 0)
861                 rc = -ENOENT;
862
863         mdd_object_put(env, child);
864
865 out:
866         return rc;
867 }
868
869 static int mdd_dummy_create(const struct lu_env *env,
870                             struct md_object *pobj,
871                             const struct lu_name *lname,
872                             struct md_object *child,
873                             struct md_op_spec *spec,
874                             struct md_attr* ma)
875 {
876         return -EPERM;
877 }
878
879 static int mdd_dummy_rename(const struct lu_env *env,
880                             struct md_object *src_pobj,
881                             struct md_object *tgt_pobj,
882                             const struct lu_fid *lf,
883                             const struct lu_name *lsname,
884                             struct md_object *tobj,
885                             const struct lu_name *ltname,
886                             struct md_attr *ma)
887 {
888         return -EPERM;
889 }
890
891 static int mdd_dummy_link(const struct lu_env *env,
892                           struct md_object *tgt_obj,
893                           struct md_object *src_obj,
894                           const struct lu_name *lname,
895                           struct md_attr *ma)
896 {
897         return -EPERM;
898 }
899
900 static int mdd_dummy_unlink(const struct lu_env *env,
901                             struct md_object *pobj,
902                             struct md_object *cobj,
903                             const struct lu_name *lname,
904                             struct md_attr *ma,
905                             int no_name)
906 {
907         return -EPERM;
908 }
909
910 int mdd_create(const struct lu_env *env, struct md_object *pobj,
911                       const struct lu_name *lname, struct md_object *child,
912                       struct md_op_spec *spec, struct md_attr *ma);
913 static int mdd_obf_create(const struct lu_env *env, struct md_object *pobj,
914                       const struct lu_name *lname, struct md_object *child,
915                       struct md_op_spec *spec, struct md_attr *ma)
916 {
917         if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
918                 return mdd_create(env, pobj, lname, child, spec, ma);
919         RETURN(-EPERM);
920 }
921
922 static const struct md_dir_operations mdd_obf_dir_ops = {
923         .mdo_lookup = obf_lookup,
924         .mdo_create = mdd_obf_create,
925         .mdo_rename = mdd_dummy_rename,
926         .mdo_link   = mdd_dummy_link,
927         .mdo_unlink = mdd_dummy_unlink
928 };
929
930 static const struct md_dir_operations mdd_lpf_dir_ops = {
931         .mdo_lookup = mdd_lookup,
932         .mdo_create = mdd_dummy_create,
933         .mdo_rename = mdd_dummy_rename,
934         .mdo_link   = mdd_dummy_link,
935         .mdo_unlink = mdd_dummy_unlink
936 };
937
938 static struct md_object *mdo_locate(const struct lu_env *env,
939                                     struct md_device *md,
940                                     const struct lu_fid *fid)
941 {
942         struct lu_object *obj;
943         struct md_object *mdo;
944
945         obj = lu_object_find(env, &md->md_lu_dev, fid, NULL);
946         if (!IS_ERR(obj)) {
947                 obj = lu_object_locate(obj->lo_header, md->md_lu_dev.ld_type);
948                 LASSERT(obj != NULL);
949                 mdo = lu2md(obj);
950         } else {
951                 mdo = ERR_CAST(obj);
952         }
953         return mdo;
954 }
955
956 static int mdd_lpf_setup(const struct lu_env *env, struct mdd_device *m)
957 {
958         struct md_object        *mdo;
959         struct mdd_object       *mdd_lpf;
960         struct lu_fid            fid    = LU_LPF_FID;
961         int                      rc;
962         ENTRY;
963
964         rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
965                                    mdd_lpf_dir_name, S_IFDIR | S_IRUSR | S_IXUSR,
966                                    &fid);
967         if (rc != 0)
968                 RETURN(rc);
969
970         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
971         if (IS_ERR(mdo))
972                 RETURN(PTR_ERR(mdo));
973
974         LASSERT(lu_object_exists(&mdo->mo_lu));
975
976         mdd_lpf = md2mdd_obj(mdo);
977         mdd_lpf->mod_obj.mo_dir_ops = &mdd_lpf_dir_ops;
978         m->mdd_dot_lustre_objs.mdd_lpf = mdd_lpf;
979
980         RETURN(0);
981 }
982
983 /**
984  * Create special in-memory "fid" object for open-by-fid.
985  */
986 static int mdd_obf_setup(const struct lu_env *env, struct mdd_device *m)
987 {
988         struct md_object        *mdo;
989         struct mdd_object       *mdd_obf;
990         struct lu_fid            fid = LU_OBF_FID;
991         int                      rc;
992
993         rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
994                                    mdd_obf_dir_name, S_IFDIR | S_IXUSR, &fid);
995         if (rc < 0)
996                 RETURN(rc);
997
998         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
999         if (IS_ERR(mdo))
1000                 RETURN(PTR_ERR(mdo));
1001
1002         LASSERT(lu_object_exists(&mdo->mo_lu));
1003
1004         mdd_obf = md2mdd_obj(mdo);
1005         mdd_obf->mod_obj.mo_dir_ops = &mdd_obf_dir_ops;
1006         m->mdd_dot_lustre_objs.mdd_obf = mdd_obf;
1007
1008         return 0;
1009 }
1010
1011 static void mdd_dot_lustre_cleanup(const struct lu_env *env,
1012                                    struct mdd_device *m)
1013 {
1014         if (m->mdd_dot_lustre_objs.mdd_lpf != NULL) {
1015                 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_lpf);
1016                 m->mdd_dot_lustre_objs.mdd_lpf = NULL;
1017         }
1018         if (m->mdd_dot_lustre_objs.mdd_obf != NULL) {
1019                 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_obf);
1020                 m->mdd_dot_lustre_objs.mdd_obf = NULL;
1021         }
1022         if (m->mdd_dot_lustre != NULL) {
1023                 mdd_object_put(env, m->mdd_dot_lustre);
1024                 m->mdd_dot_lustre = NULL;
1025         }
1026 }
1027
1028 /** Setup ".lustre" directory object */
1029 static int mdd_dot_lustre_setup(const struct lu_env *env, struct mdd_device *m)
1030 {
1031         struct md_object        *mdo;
1032         struct lu_fid            fid;
1033         int                      rc;
1034
1035         ENTRY;
1036         /* Create ".lustre" directory in ROOT. */
1037         fid = LU_DOT_LUSTRE_FID;
1038         rc = mdd_local_file_create(env, m, &m->mdd_root_fid,
1039                                    dot_lustre_name,
1040                                    S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO,
1041                                    &fid);
1042         if (rc < 0)
1043                 RETURN(rc);
1044         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
1045         if (IS_ERR(mdo))
1046                 RETURN(PTR_ERR(mdo));
1047         LASSERT(lu_object_exists(&mdo->mo_lu));
1048
1049         m->mdd_dot_lustre = md2mdd_obj(mdo);
1050
1051         rc = mdd_obf_setup(env, m);
1052         if (rc) {
1053                 CERROR("%s: error initializing \"fid\" object: rc = %d.\n",
1054                        mdd2obd_dev(m)->obd_name, rc);
1055                 GOTO(out, rc);
1056         }
1057
1058         rc = mdd_lpf_setup(env, m);
1059         if (rc != 0) {
1060                 CERROR("%s: error initializing \"lost+found\": rc = %d.\n",
1061                        mdd2obd_dev(m)->obd_name, rc);
1062                 GOTO(out, rc);
1063         }
1064
1065         RETURN(0);
1066
1067 out:
1068         mdd_dot_lustre_cleanup(env, m);
1069
1070         return rc;
1071 }
1072
1073 /**
1074  * set llog methods and create LLOG_AGENT_ORIG_CTXT llog
1075  * object in obd_device
1076  */
1077 static int mdd_hsm_actions_llog_init(const struct lu_env *env,
1078                                      struct mdd_device *m)
1079 {
1080         struct obd_device       *obd = mdd2obd_dev(m);
1081         struct llog_ctxt        *ctxt = NULL;
1082         int                      rc;
1083         ENTRY;
1084
1085         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1086         obd->obd_lvfs_ctxt.dt = m->mdd_bottom;
1087
1088         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_AGENT_ORIG_CTXT,
1089                         obd, &llog_common_cat_ops);
1090         if (rc) {
1091                 CERROR("%s: hsm actions llog setup failed: rc = %d\n",
1092                         obd->obd_name, rc);
1093                 RETURN(rc);
1094         }
1095
1096         ctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
1097         LASSERT(ctxt);
1098
1099         rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
1100                               HSM_ACTIONS);
1101         if (rc) {
1102                 CERROR("%s: hsm actions llog open_create failed: rc = %d\n",
1103                         obd->obd_name, rc);
1104                 GOTO(out_cleanup, rc);
1105         }
1106
1107         rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
1108         if (rc)
1109                 GOTO(out_close, rc);
1110
1111         llog_ctxt_put(ctxt);
1112         RETURN(0);
1113
1114 out_close:
1115         llog_cat_close(env, ctxt->loc_handle);
1116         ctxt->loc_handle = NULL;
1117 out_cleanup:
1118         llog_cleanup(env, ctxt);
1119
1120         return rc;
1121 }
1122
1123 /**
1124  * cleanup the context created by llog_setup_named()
1125  */
1126 static int mdd_hsm_actions_llog_fini(const struct lu_env *env,
1127                                      struct mdd_device *m)
1128 {
1129         struct obd_device       *obd = mdd2obd_dev(m);
1130         struct llog_ctxt        *lctxt;
1131         ENTRY;
1132
1133         lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
1134         if (lctxt) {
1135                 llog_cat_close(env, lctxt->loc_handle);
1136                 lctxt->loc_handle = NULL;
1137                 llog_cleanup(env, lctxt);
1138         }
1139
1140         RETURN(0);
1141 }
1142
1143 static void mdd_device_shutdown(const struct lu_env *env, struct mdd_device *m,
1144                                 struct lustre_cfg *cfg)
1145 {
1146         barrier_deregister(m->mdd_bottom);
1147         lfsck_degister(env, m->mdd_bottom);
1148         mdd_hsm_actions_llog_fini(env, m);
1149         mdd_changelog_fini(env, m);
1150         mdd_orphan_index_fini(env, m);
1151         mdd_dot_lustre_cleanup(env, m);
1152         if (mdd2obd_dev(m)->u.obt.obt_nodemap_config_file) {
1153                 nm_config_file_deregister_tgt(env,
1154                                 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file);
1155                 mdd2obd_dev(m)->u.obt.obt_nodemap_config_file = NULL;
1156         }
1157         if (m->mdd_los != NULL) {
1158                 local_oid_storage_fini(env, m->mdd_los);
1159                 m->mdd_los = NULL;
1160         }
1161         lu_site_purge(env, mdd2lu_dev(m)->ld_site, ~0);
1162
1163         if (m->mdd_child_exp)
1164                 obd_disconnect(m->mdd_child_exp);
1165 }
1166
1167 static int mdd_process_config(const struct lu_env *env,
1168                               struct lu_device *d, struct lustre_cfg *cfg)
1169 {
1170         struct mdd_device *m    = lu2mdd_dev(d);
1171         struct dt_device  *dt   = m->mdd_child;
1172         struct lu_device  *next = &dt->dd_lu_dev;
1173         int rc;
1174         ENTRY;
1175
1176         switch (cfg->lcfg_command) {
1177         case LCFG_PARAM: {
1178                 ssize_t count;
1179
1180                 count = class_modify_config(cfg, PARAM_MDD, &m->mdd_kobj);
1181                 rc = count > 0 ? 0 : count;
1182                 if (rc)
1183                         /* we don't understand; pass it on */
1184                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
1185                 break;
1186         }
1187         case LCFG_SETUP:
1188                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1189                 if (rc)
1190                         GOTO(out, rc);
1191                 dt_conf_get(env, dt, &m->mdd_dt_conf);
1192                 break;
1193         case LCFG_PRE_CLEANUP:
1194                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1195                 mdd_generic_thread_stop(&m->mdd_orphan_cleanup_thread);
1196                 break;
1197         case LCFG_CLEANUP:
1198                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1199                 lu_dev_del_linkage(d->ld_site, d);
1200                 mdd_device_shutdown(env, m, cfg);
1201                 break;
1202         default:
1203                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
1204                 break;
1205         }
1206 out:
1207         RETURN(rc);
1208 }
1209
1210 static int mdd_recovery_complete(const struct lu_env *env,
1211                                  struct lu_device *d)
1212 {
1213         struct mdd_device *mdd = lu2mdd_dev(d);
1214         struct lu_device *next;
1215         int rc;
1216         ENTRY;
1217
1218         LASSERT(mdd != NULL);
1219         next = &mdd->mdd_child->dd_lu_dev;
1220
1221         if (!mdd->mdd_bottom->dd_rdonly)
1222                 mdd_orphan_cleanup(env, mdd);
1223         rc = next->ld_ops->ldo_recovery_complete(env, next);
1224
1225         RETURN(rc);
1226 }
1227
1228 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
1229                           const struct lu_fid *pfid, const char *name,
1230                           __u32 mode, struct lu_fid *fid)
1231 {
1232         struct dt_object *parent, *dto;
1233         int rc = 0;
1234
1235         ENTRY;
1236
1237         LASSERT(!fid_is_zero(pfid));
1238         parent = dt_locate(env, mdd->mdd_bottom, pfid);
1239         if (unlikely(IS_ERR(parent)))
1240                 RETURN(PTR_ERR(parent));
1241
1242         /* create local file/dir, if @fid is passed then try to use it */
1243         if (fid_is_zero(fid))
1244                 dto = local_file_find_or_create(env, mdd->mdd_los, parent,
1245                                                 name, mode);
1246         else
1247                 dto = local_file_find_or_create_with_fid(env, mdd->mdd_bottom,
1248                                                          fid, parent, name,
1249                                                          mode);
1250         if (IS_ERR(dto))
1251                 GOTO(out_put, rc = PTR_ERR(dto));
1252         *fid = *lu_object_fid(&dto->do_lu);
1253         /* since stack is not fully set up the local_storage uses own stack
1254          * and we should drop its object from cache */
1255         dt_object_put_nocache(env, dto);
1256         EXIT;
1257 out_put:
1258         dt_object_put(env, parent);
1259         return rc;
1260 }
1261
1262 static int mdd_lfsck_out_notify(const struct lu_env *env, void *data,
1263                                 enum lfsck_events event)
1264 {
1265         return 0;
1266 }
1267
1268 static int mdd_prepare(const struct lu_env *env,
1269                        struct lu_device *pdev,
1270                        struct lu_device *cdev)
1271 {
1272         struct mdd_device *mdd = lu2mdd_dev(cdev);
1273         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
1274         struct nm_config_file *nodemap_config;
1275         struct obd_device_target *obt = &mdd2obd_dev(mdd)->u.obt;
1276         struct dt_object *root = NULL;
1277         struct thandle *th = NULL;
1278         struct lu_fid fid;
1279         int rc;
1280
1281         ENTRY;
1282
1283         rc = next->ld_ops->ldo_prepare(env, cdev, next);
1284         if (rc)
1285                 RETURN(rc);
1286
1287         /* Setup local dirs */
1288         fid.f_seq = FID_SEQ_LOCAL_NAME;
1289         fid.f_oid = 1;
1290         fid.f_ver = 0;
1291         rc = local_oid_storage_init(env, mdd->mdd_bottom, &fid,
1292                                     &mdd->mdd_los);
1293         if (rc)
1294                 RETURN(rc);
1295
1296         rc = dt_root_get(env, mdd->mdd_child, &mdd->mdd_local_root_fid);
1297         if (rc < 0)
1298                 GOTO(out_los, rc);
1299
1300         lu_root_fid(&fid);
1301         if (mdd_seq_site(mdd)->ss_node_id == 0) {
1302                 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
1303                                            mdd_root_dir_name, S_IFDIR |
1304                                            S_IRUGO | S_IWUSR | S_IXUGO, &fid);
1305                 if (rc != 0) {
1306                         CERROR("%s: create root fid failed: rc = %d\n",
1307                                mdd2obd_dev(mdd)->obd_name, rc);
1308                         GOTO(out_los, rc);
1309                 }
1310
1311                 /* store a default directory layout on the root directory if
1312                  * it doesn't already exist to improve MDT space balance.
1313                  */
1314                 root = dt_locate(env, mdd->mdd_bottom, &fid);
1315                 if (unlikely(IS_ERR(root)))
1316                         GOTO(out_los, rc = PTR_ERR(root));
1317
1318                 rc = dt_xattr_get(env, root, &LU_BUF_NULL,
1319                                   XATTR_NAME_DEFAULT_LMV);
1320                 if (rc == -ENODATA) {
1321                         struct lu_buf buf;
1322                         struct lmv_user_md lmv_default = {
1323                                 .lum_magic              = LMV_USER_MAGIC,
1324                                 .lum_stripe_count       = 1,
1325                                 .lum_stripe_offset      = LMV_OFFSET_DEFAULT,
1326                                 .lum_max_inherit        = LMV_INHERIT_UNLIMITED,
1327                                 .lum_max_inherit_rr     = LMV_INHERIT_RR_ROOT,
1328                         };
1329
1330                         th = dt_trans_create(env, mdd->mdd_bottom);
1331                         if (IS_ERR(th))
1332                                 GOTO(out_root_put, rc = PTR_ERR(th));
1333
1334                         buf.lb_buf = &lmv_default;
1335                         buf.lb_len = sizeof(lmv_default);
1336                         rc = dt_declare_xattr_set(env, root, &buf,
1337                                                   XATTR_NAME_DEFAULT_LMV, 0,
1338                                                   th);
1339                         if (rc)
1340                                 GOTO(out_trans_stop, rc);
1341
1342                         rc = dt_trans_start_local(env, mdd->mdd_bottom, th);
1343                         if (rc)
1344                                 GOTO(out_trans_stop, rc);
1345
1346                         rc = dt_xattr_set(env, root, &buf,
1347                                           XATTR_NAME_DEFAULT_LMV, 0, th);
1348                         if (rc)
1349                                 GOTO(out_trans_stop, rc);
1350
1351                         dt_trans_stop(env, mdd->mdd_bottom, th);
1352                         th = NULL;
1353                 } else if (rc < 0 && rc != -ERANGE) {
1354                         CERROR("%s: get default LMV of root failed: rc = %d\n",
1355                                mdd2obd_dev(mdd)->obd_name, rc);
1356
1357                         GOTO(out_root_put, rc);
1358                 }
1359
1360                 dt_object_put(env, root);
1361                 root = NULL;
1362
1363                 mdd->mdd_root_fid = fid;
1364                 rc = mdd_dot_lustre_setup(env, mdd);
1365                 if (rc != 0) {
1366                         CERROR("%s: initializing .lustre failed: rc = %d\n",
1367                                mdd2obd_dev(mdd)->obd_name, rc);
1368                         GOTO(out_los, rc);
1369                 }
1370         } else {
1371                 /* Normal client usually send root access to MDT0 directly,
1372                  * the root FID on non-MDT0 will only be used by echo client. */
1373                 mdd->mdd_root_fid = fid;
1374         }
1375
1376         rc = mdd_orphan_index_init(env, mdd);
1377         if (rc < 0)
1378                 GOTO(out_dot, rc);
1379
1380         rc = mdd_changelog_init(env, mdd);
1381         if (rc != 0) {
1382                 CERROR("%s: failed to initialize changelog: rc = %d\n",
1383                        mdd2obd_dev(mdd)->obd_name, rc);
1384                 GOTO(out_orph, rc);
1385         }
1386
1387         rc = mdd_hsm_actions_llog_init(env, mdd);
1388         if (rc != 0)
1389                 GOTO(out_changelog, rc);
1390
1391         nodemap_config = nm_config_file_register_tgt(env, mdd->mdd_bottom,
1392                                                      mdd->mdd_los);
1393         if (IS_ERR(nodemap_config)) {
1394                 rc = PTR_ERR(nodemap_config);
1395                 if (rc != -EROFS)
1396                         GOTO(out_hsm, rc);
1397         } else {
1398                 obt->obt_nodemap_config_file = nodemap_config;
1399         }
1400
1401         rc = lfsck_register(env, mdd->mdd_bottom, mdd->mdd_child,
1402                             mdd2obd_dev(mdd), mdd_lfsck_out_notify,
1403                             mdd, true);
1404         if (rc != 0) {
1405                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
1406                        mdd2obd_dev(mdd)->obd_name, rc);
1407                 GOTO(out_nodemap, rc);
1408         }
1409
1410         rc = barrier_register(mdd->mdd_bottom, mdd->mdd_child);
1411         if (rc) {
1412                 CERROR("%s: failed to register to barrier: rc = %d\n",
1413                        mdd2obd_dev(mdd)->obd_name, rc);
1414                 GOTO(out_lfsck, rc);
1415         }
1416
1417         RETURN(0);
1418
1419 out_lfsck:
1420         lfsck_degister(env, mdd->mdd_bottom);
1421 out_nodemap:
1422         nm_config_file_deregister_tgt(env, obt->obt_nodemap_config_file);
1423         obt->obt_nodemap_config_file = NULL;
1424 out_hsm:
1425         mdd_hsm_actions_llog_fini(env, mdd);
1426 out_changelog:
1427         mdd_changelog_fini(env, mdd);
1428 out_orph:
1429         mdd_orphan_index_fini(env, mdd);
1430 out_dot:
1431         if (mdd_seq_site(mdd)->ss_node_id == 0)
1432                 mdd_dot_lustre_cleanup(env, mdd);
1433 out_trans_stop:
1434         if (th != NULL)
1435                 dt_trans_stop(env, mdd->mdd_bottom, th);
1436 out_root_put:
1437         if (root != NULL)
1438                 dt_object_put(env, root);
1439 out_los:
1440         local_oid_storage_fini(env, mdd->mdd_los);
1441         mdd->mdd_los = NULL;
1442
1443         return rc;
1444 }
1445
1446 /**
1447  * Implementation of lu_device_operations::ldo_fid_alloc() for MDD.
1448  *
1449  * Find corresponding device by passed parent and name, and allocate FID from
1450  * there.
1451  *
1452  * see include/lu_object.h for the details.
1453  */
1454 static int mdd_fid_alloc(const struct lu_env *env, struct lu_device *d,
1455                          struct lu_fid *fid, struct lu_object *parent,
1456                          const struct lu_name *name)
1457 {
1458         struct mdd_device *mdd = lu2mdd_dev(d);
1459         struct lu_object *o = lu_object_next(parent);
1460
1461         return dt_fid_alloc(env, mdd->mdd_child, fid, o, name);
1462 }
1463
1464 const struct lu_device_operations mdd_lu_ops = {
1465         .ldo_object_alloc      = mdd_object_alloc,
1466         .ldo_process_config    = mdd_process_config,
1467         .ldo_recovery_complete = mdd_recovery_complete,
1468         .ldo_prepare           = mdd_prepare,
1469         .ldo_fid_alloc         = mdd_fid_alloc,
1470 };
1471
1472 static int mdd_root_get(const struct lu_env *env,
1473                         struct md_device *m, struct lu_fid *f)
1474 {
1475         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1476
1477         ENTRY;
1478         *f = mdd->mdd_root_fid;
1479         RETURN(0);
1480 }
1481
1482 /*
1483  * No permission check is needed.
1484  */
1485 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1486                       struct obd_statfs *sfs)
1487 {
1488         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1489         int rc;
1490
1491         ENTRY;
1492
1493         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs, NULL);
1494
1495         sfs->os_namelen = min_t(__u32, sfs->os_namelen, NAME_MAX);
1496
1497         RETURN(rc);
1498 }
1499
1500 static const struct dt_device_param *mdd_dtconf_get(const struct lu_env *env,
1501                                                     struct md_device *m)
1502 {
1503         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1504
1505         return &mdd->mdd_dt_conf;
1506 }
1507
1508 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1509                              int idx, void **h)
1510 {
1511         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1512
1513         *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1514         return (*h == NULL ? -ENOENT : 0);
1515 }
1516
1517 static struct lu_device *mdd_device_free(const struct lu_env *env,
1518                                          struct lu_device *lu)
1519 {
1520         struct mdd_device *m = lu2mdd_dev(lu);
1521         ENTRY;
1522
1523         LASSERT(atomic_read(&lu->ld_ref) == 0);
1524         md_device_fini(&m->mdd_md_dev);
1525         OBD_FREE_PTR(m);
1526         RETURN(NULL);
1527 }
1528
1529 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1530                                           struct lu_device_type *t,
1531                                           struct lustre_cfg *lcfg)
1532 {
1533         struct lu_device  *l;
1534         struct mdd_device *m;
1535
1536         OBD_ALLOC_PTR(m);
1537         if (m == NULL) {
1538                 l = ERR_PTR(-ENOMEM);
1539         } else {
1540                 int rc;
1541
1542                 l = mdd2lu_dev(m);
1543                 md_device_init(&m->mdd_md_dev, t);
1544                 rc = mdd_init0(env, m, t, lcfg);
1545                 if (rc != 0) {
1546                         mdd_device_free(env, l);
1547                         l = ERR_PTR(rc);
1548                 }
1549         }
1550
1551         return l;
1552 }
1553
1554 /*
1555  * we use exports to track all mdd users
1556  */
1557 static int mdd_obd_connect(const struct lu_env *env, struct obd_export **exp,
1558                            struct obd_device *obd, struct obd_uuid *cluuid,
1559                            struct obd_connect_data *data, void *localdata)
1560 {
1561         struct mdd_device    *mdd = lu2mdd_dev(obd->obd_lu_dev);
1562         struct lustre_handle  conn;
1563         int                   rc;
1564         ENTRY;
1565
1566         CDEBUG(D_CONFIG, "connect #%d\n", mdd->mdd_connects);
1567
1568         rc = class_connect(&conn, obd, cluuid);
1569         if (rc)
1570                 RETURN(rc);
1571
1572         *exp = class_conn2export(&conn);
1573
1574         /* Why should there ever be more than 1 connect? */
1575         LASSERT(mdd->mdd_connects == 0);
1576         mdd->mdd_connects++;
1577
1578         RETURN(0);
1579 }
1580
1581 /*
1582  * once last export (we don't count self-export) disappeared
1583  * mdd can be released
1584  */
1585 static int mdd_obd_disconnect(struct obd_export *exp)
1586 {
1587         struct obd_device *obd = exp->exp_obd;
1588         struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1589         int                rc, release = 0;
1590         ENTRY;
1591
1592         mdd->mdd_connects--;
1593         if (mdd->mdd_connects == 0)
1594                 release = 1;
1595
1596         rc = class_disconnect(exp);
1597
1598         if (rc == 0 && release)
1599                 class_manual_cleanup(obd);
1600         RETURN(rc);
1601 }
1602
1603 static int mdd_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1604                             __u32 keylen, void *key, __u32 *vallen, void *val)
1605 {
1606         int rc = -EINVAL;
1607
1608         if (KEY_IS(KEY_OSP_CONNECTED)) {
1609                 struct obd_device       *obd = exp->exp_obd;
1610                 struct mdd_device       *mdd;
1611
1612                 if (!obd->obd_set_up || obd->obd_stopping)
1613                         RETURN(-EAGAIN);
1614
1615                 mdd = lu2mdd_dev(obd->obd_lu_dev);
1616                 LASSERT(mdd);
1617                 rc = obd_get_info(env, mdd->mdd_child_exp, keylen, key, vallen,
1618                                   val);
1619                 RETURN(rc);
1620         }
1621
1622         RETURN(rc);
1623 }
1624
1625 static int mdd_obd_set_info_async(const struct lu_env *env,
1626                                   struct obd_export *exp,
1627                                   __u32 keylen, void *key,
1628                                   __u32 vallen, void *val,
1629                                   struct ptlrpc_request_set *set)
1630 {
1631         struct obd_device       *obd = exp->exp_obd;
1632         struct mdd_device       *mdd;
1633         int                      rc;
1634
1635         if (!obd->obd_set_up || obd->obd_stopping)
1636                 RETURN(-EAGAIN);
1637
1638         mdd = lu2mdd_dev(obd->obd_lu_dev);
1639         LASSERT(mdd);
1640         rc = obd_set_info_async(env, mdd->mdd_child_exp, keylen, key,
1641                                 vallen, val, set);
1642         RETURN(rc);
1643 }
1644
1645 static const struct obd_ops mdd_obd_device_ops = {
1646         .o_owner        = THIS_MODULE,
1647         .o_connect      = mdd_obd_connect,
1648         .o_disconnect   = mdd_obd_disconnect,
1649         .o_get_info     = mdd_obd_get_info,
1650         .o_set_info_async = mdd_obd_set_info_async,
1651 };
1652
1653 struct mdd_changelog_name_check_data {
1654         const char *mcnc_name;
1655         __u32       mcnc_id;
1656 };
1657
1658 /**
1659  * changelog_recalc_mask callback
1660  *
1661  * Is is called per each registered user and calculates combined mask of
1662  * all registered users.
1663  */
1664 static int mdd_changelog_name_check_cb(const struct lu_env *env,
1665                                        struct llog_handle *llh,
1666                                        struct llog_rec_hdr *hdr, void *data)
1667 {
1668         struct llog_changelog_user_rec2 *rec;
1669         struct mdd_changelog_name_check_data *mcnc = data;
1670
1671         rec = container_of(hdr, typeof(*rec), cur_hdr);
1672         if (rec->cur_hdr.lrh_type == CHANGELOG_USER_REC2 &&
1673             !strncmp(rec->cur_name, mcnc->mcnc_name, sizeof(rec->cur_name))) {
1674                 mcnc->mcnc_id = rec->cur_id;
1675                 return -EEXIST;
1676         }
1677         return 0;
1678 }
1679
1680 static int mdd_changelog_name_check(const struct lu_env *env,
1681                                     struct llog_ctxt *ctxt,
1682                                     struct mdd_device *mdd, const char *name)
1683 {
1684         struct mdd_changelog_name_check_data mcnc = { .mcnc_name = name, };
1685         int chr = 0;
1686         int rc;
1687
1688         ENTRY;
1689
1690         /* first symbol is a letter */
1691         if (!isalpha(name[0])) {
1692                 rc = -EINVAL;
1693                 CERROR("%s: first char '%c' in '%s' is not letter: rc = %d\n",
1694                        mdd2obd_dev(mdd)->obd_name, name[0], name, rc);
1695                 RETURN(rc);
1696         }
1697
1698         /* name is valid: contains letters, numbers and '-', '_' only */
1699         while (name[++chr]) {
1700                 if (!(isalnum(name[chr]) || name[chr] == '_' ||
1701                       name[chr] == '-')) {
1702                         rc = -EINVAL;
1703                         CERROR("%s: wrong char '%c' in name '%s': rc = %d\n",
1704                                mdd2obd_dev(mdd)->obd_name, name[chr], name, rc);
1705                         RETURN(rc);
1706                 }
1707         }
1708
1709         if (chr > CHANGELOG_USER_NAMELEN) {
1710                 rc = -ENAMETOOLONG;
1711                 CERROR("%s: name '%s' is over %d symbols limit: rc = %d\n",
1712                        mdd2obd_dev(mdd)->obd_name, name,
1713                        CHANGELOG_USER_NAMELEN, rc);
1714                 RETURN(rc);
1715         }
1716
1717         rc = llog_cat_process(env, ctxt->loc_handle,
1718                               mdd_changelog_name_check_cb, &mcnc, 0, 0);
1719         if (rc == -EEXIST)
1720                 CWARN("%s: changelog name %s exists already: rc = %d\n",
1721                       mdd2obd_dev(mdd)->obd_name, name, rc);
1722         else if (rc < 0)
1723                 CWARN("%s: failed user changelog processing: rc = %d\n",
1724                       mdd2obd_dev(mdd)->obd_name, rc);
1725         RETURN(rc);
1726 }
1727
1728 static int mdd_changelog_user_register(const struct lu_env *env,
1729                                        struct mdd_device *mdd, int *id,
1730                                        const char *name, const char *mask)
1731 {
1732         struct llog_ctxt *ctxt;
1733         struct llog_changelog_user_rec2 *rec;
1734         char user_name[CHANGELOG_USER_NAMELEN_FULL];
1735         int rc;
1736
1737         ENTRY;
1738
1739         ctxt = llog_get_context(mdd2obd_dev(mdd),
1740                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1741         if (ctxt == NULL)
1742                 RETURN(-ENXIO);
1743
1744         OBD_ALLOC_PTR(rec);
1745         if (rec == NULL) {
1746                 llog_ctxt_put(ctxt);
1747                 RETURN(-ENOMEM);
1748         }
1749
1750         CFS_RACE(CFS_FAIL_CHLOG_USER_REG_UNREG_RACE);
1751
1752         rec->cur_hdr.lrh_len = sizeof(*rec);
1753         /* keep old record type for users without mask/name for
1754          * compatibility needs
1755          */
1756         if (mask || (name && name[0]))
1757                 rec->cur_hdr.lrh_type = CHANGELOG_USER_REC2;
1758         else
1759                 rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1760         spin_lock(&mdd->mdd_cl.mc_user_lock);
1761         if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1762                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1763                 rc = -EOVERFLOW;
1764                 CERROR("%s: registering %s user: max ID is exceeded: rc = %d\n",
1765                        mdd2obd_dev(mdd)->obd_name,
1766                        (name && name[0]) ? name : "new", rc);
1767                 GOTO(out, rc);
1768         }
1769         *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1770         mdd->mdd_cl.mc_users++;
1771         spin_unlock(&mdd->mdd_cl.mc_user_lock);
1772
1773         rec->cur_time = (__u32)ktime_get_real_seconds();
1774         if (OBD_FAIL_PRECHECK(OBD_FAIL_TIME_IN_CHLOG_USER)) {
1775                 rec->cur_time -= min(cfs_fail_val, rec->cur_time);
1776                 spin_lock(&mdd->mdd_cl.mc_user_lock);
1777                 mdd->mdd_cl.mc_mintime = rec->cur_time;
1778                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1779         }
1780
1781         spin_lock(&mdd->mdd_cl.mc_lock);
1782         rec->cur_endrec = mdd->mdd_cl.mc_index;
1783         spin_unlock(&mdd->mdd_cl.mc_lock);
1784
1785         if (mask) {
1786                 /* if user will use relative mask apply it on default one */
1787                 rec->cur_mask = CHANGELOG_DEFMASK;
1788                 rc = cfs_str2mask(mask, changelog_type2str, &rec->cur_mask,
1789                                   CHANGELOG_MINMASK, CHANGELOG_ALLMASK,
1790                                   CHANGELOG_DEFMASK);
1791                 if (rc)
1792                         GOTO(out_users, rc);
1793         } else if (mdd->mdd_cl.mc_proc_mask == CHANGELOG_MINMASK) {
1794                 /* a maskless users means default mask but only if server has
1795                  * no specific mask set
1796                  */
1797                 rec->cur_mask = CHANGELOG_DEFMASK;
1798         }
1799
1800         if (name && name[0]) {
1801                 rc = mdd_changelog_name_check(env, ctxt, mdd, name);
1802                 if (rc)
1803                         GOTO(out_users, rc);
1804                 strlcpy(rec->cur_name, name, sizeof(rec->cur_name));
1805         }
1806         mdd_chlg_username(rec, user_name, sizeof(user_name));
1807
1808         rc = llog_cat_add(env, ctxt->loc_handle, &rec->cur_hdr, NULL);
1809         if (rc) {
1810                 CWARN("%s: failed to register changelog user %s: rc = %d\n",
1811                       mdd2obd_dev(mdd)->obd_name, user_name, rc);
1812                 GOTO(out_users, rc);
1813         }
1814
1815         /* apply user mask finally */
1816         spin_lock(&mdd->mdd_cl.mc_user_lock);
1817         mdd->mdd_cl.mc_current_mask |= rec->cur_mask;
1818         spin_unlock(&mdd->mdd_cl.mc_user_lock);
1819
1820         CDEBUG(D_IOCTL, "%s: registered changelog user '%s', mask %#x\n",
1821                mdd2obd_dev(mdd)->obd_name, user_name, rec->cur_mask);
1822
1823         /* Assume we want it on since somebody registered */
1824         rc = mdd_changelog_on(env, mdd);
1825         if (rc)
1826                 /* record is added, so don't decrement users on error */
1827                 GOTO(out, rc);
1828 out_users:
1829         if (rc) {
1830                 spin_lock(&mdd->mdd_cl.mc_user_lock);
1831                 mdd->mdd_cl.mc_users--;
1832                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1833         }
1834 out:
1835         OBD_FREE_PTR(rec);
1836         llog_ctxt_put(ctxt);
1837         RETURN(rc);
1838 }
1839
1840 struct mdd_changelog_recalc_mask_data {
1841         struct mdd_device *mcrm_mdd;
1842         __u32              mcrm_mask;
1843 };
1844
1845 /**
1846  * changelog_recalc_mask callback
1847  *
1848  * Is is called per each registered user and calculates combined mask of
1849  * all registered users.
1850  */
1851 static int mdd_changelog_recalc_mask_cb(const struct lu_env *env,
1852                                        struct llog_handle *llh,
1853                                        struct llog_rec_hdr *hdr, void *data)
1854 {
1855         struct llog_changelog_user_rec2 *rec;
1856         struct mdd_changelog_recalc_mask_data *mcrm = data;
1857
1858         rec = container_of(hdr, typeof(*rec), cur_hdr);
1859         if (rec->cur_hdr.lrh_type == CHANGELOG_USER_REC2 && rec->cur_mask)
1860                 mcrm->mcrm_mask |= rec->cur_mask;
1861         else if (mcrm->mcrm_mdd->mdd_cl.mc_proc_mask == CHANGELOG_MINMASK)
1862                 mcrm->mcrm_mask |= CHANGELOG_DEFMASK;
1863
1864         return 0;
1865 }
1866
1867 int mdd_changelog_recalc_mask(const struct lu_env *env, struct mdd_device *mdd)
1868 {
1869         struct llog_ctxt *ctxt;
1870         struct mdd_changelog_recalc_mask_data mcrm = {
1871                 .mcrm_mdd = mdd,
1872                 .mcrm_mask = mdd->mdd_cl.mc_proc_mask,
1873         };
1874         int rc;
1875
1876         ENTRY;
1877
1878         ctxt = llog_get_context(mdd2obd_dev(mdd),
1879                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1880         if (!ctxt)
1881                 RETURN(-ENXIO);
1882
1883         if (!(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
1884                 GOTO(out, rc = -ENXIO);
1885
1886         rc = llog_cat_process(env, ctxt->loc_handle,
1887                               mdd_changelog_recalc_mask_cb, &mcrm, 0, 0);
1888         if (rc < 0)
1889                 CWARN("%s: failed user changelog processing: rc = %d\n",
1890                       mdd2obd_dev(mdd)->obd_name, rc);
1891
1892         spin_lock(&mdd->mdd_cl.mc_user_lock);
1893         CDEBUG(D_INFO, "%s: recalc changelog mask: %#x -> %#x\n",
1894                mdd2obd_dev(mdd)->obd_name, mdd->mdd_cl.mc_current_mask,
1895                mcrm.mcrm_mask);
1896         mdd->mdd_cl.mc_current_mask = mcrm.mcrm_mask;
1897         spin_unlock(&mdd->mdd_cl.mc_user_lock);
1898
1899         EXIT;
1900 out:
1901         llog_ctxt_put(ctxt);
1902
1903         return rc;
1904 }
1905
1906 struct mdd_changelog_user_purge {
1907         struct mdd_device *mcup_mdd;
1908         __u32 mcup_id;
1909         __u32 mcup_usercount;
1910         __u64 mcup_minrec;
1911         bool mcup_found;
1912         char mcup_name[CHANGELOG_USER_NAMELEN_FULL];
1913 };
1914
1915 /**
1916  * changelog_user_purge callback
1917  *
1918  * Is called once per user.
1919  *
1920  * Check to see the user requested is available from rec.
1921  * Truncate the changelog.
1922  * Keep track of the total number of users (calls).
1923  */
1924 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1925                                        struct llog_handle *llh,
1926                                        struct llog_rec_hdr *hdr, void *data)
1927 {
1928         struct llog_changelog_user_rec2 *rec;
1929         struct mdd_changelog_user_purge *mcup = data;
1930         struct llog_cookie cookie;
1931         int rc;
1932
1933         ENTRY;
1934
1935         if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
1936                 RETURN(-ENXIO);
1937
1938         rec = container_of(hdr, typeof(*rec), cur_hdr);
1939
1940         mcup->mcup_usercount++;
1941
1942         if (rec->cur_id != mcup->mcup_id) {
1943                 /* truncate to the lowest endrec that is not this user */
1944                 mcup->mcup_minrec = min(mcup->mcup_minrec, rec->cur_endrec);
1945                 RETURN(0);
1946         }
1947
1948         mdd_chlg_username(rec, mcup->mcup_name, sizeof(mcup->mcup_name));
1949
1950         /* Unregister this user */
1951         cookie.lgc_lgl = llh->lgh_id;
1952         cookie.lgc_index = hdr->lrh_index;
1953         rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1954                                      1, &cookie);
1955         if (rc == 0) {
1956                 mcup->mcup_found = true;
1957                 mcup->mcup_usercount--;
1958                 spin_lock(&mcup->mcup_mdd->mdd_cl.mc_user_lock);
1959                 mcup->mcup_mdd->mdd_cl.mc_users--;
1960                 spin_unlock(&mcup->mcup_mdd->mdd_cl.mc_user_lock);
1961         }
1962
1963         RETURN(rc);
1964 }
1965
1966 int mdd_changelog_user_purge(const struct lu_env *env,
1967                              struct mdd_device *mdd, __u32 id)
1968 {
1969         struct mdd_changelog_user_purge mcup = {
1970                 .mcup_mdd = mdd,
1971                 .mcup_id = id,
1972                 .mcup_found = false,
1973                 .mcup_usercount = 0,
1974                 .mcup_minrec = ULLONG_MAX,
1975                 .mcup_name = { 0 },
1976         };
1977         struct llog_ctxt *ctxt;
1978         int rc;
1979
1980         ENTRY;
1981
1982         CDEBUG(D_IOCTL, "%s: Purge request: id=%u\n",
1983                mdd2obd_dev(mdd)->obd_name, id);
1984
1985         ctxt = llog_get_context(mdd2obd_dev(mdd),
1986                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1987         if (!ctxt)
1988                 RETURN(-ENXIO);
1989         if (!(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
1990                 GOTO(out, rc = -ENXIO);
1991
1992         rc = llog_cat_process(env, ctxt->loc_handle,
1993                               mdd_changelog_user_purge_cb, &mcup, 0, 0);
1994         if (rc) {
1995                 CWARN("%s: failed to purge changelog for user %s: rc = %d\n",
1996                       mdd2obd_dev(mdd)->obd_name, mcup.mcup_name, rc);
1997                 GOTO(out, rc);
1998         }
1999
2000         OBD_FAIL_TIMEOUT(OBD_FAIL_LLOG_PURGE_DELAY, cfs_fail_val);
2001         if (mcup.mcup_usercount == 0) {
2002                 spin_lock(&mdd->mdd_cl.mc_user_lock);
2003                 if (mdd->mdd_cl.mc_users == 0) {
2004                         /* No more users; turn changelogs off */
2005                         CDEBUG(D_IOCTL, "turning off changelogs\n");
2006                         rc = mdd_changelog_off(env, mdd);
2007                 }
2008                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
2009         }
2010
2011         if (mcup.mcup_found) {
2012                 CDEBUG(D_IOCTL,
2013                        "%s: Purge changelog entries for user %s record=%llu\n",
2014                        mdd2obd_dev(mdd)->obd_name,
2015                        mcup.mcup_name, mcup.mcup_minrec);
2016                 rc = mdd_changelog_llog_cancel(env, mdd, mcup.mcup_minrec);
2017         } else {
2018                 CWARN("%s: No changelog for user id %u: rc = %d\n",
2019                       mdd2obd_dev(mdd)->obd_name, id, rc);
2020                 GOTO(out, rc = -ENOENT);
2021         }
2022
2023         CFS_RACE(CFS_FAIL_CHLOG_USER_REG_UNREG_RACE);
2024
2025         EXIT;
2026 out:
2027         llog_ctxt_put(ctxt);
2028
2029         return rc;
2030 }
2031
2032 struct mdd_changelog_user_clear {
2033         __u64 mcuc_endrec;
2034         __u64 mcuc_minrec;
2035         __u32 mcuc_mintime;
2036         __u32 mcuc_id;
2037         bool mcuc_flush;
2038         struct mdd_device *mcuc_mdd;
2039         char mcuc_name[CHANGELOG_USER_NAMELEN_FULL];
2040 };
2041
2042 /**
2043  * changelog_clear callback
2044  *
2045  * Is called once per user.
2046  *
2047  * Check to see the user requested is available from rec.
2048  * Check the oldest (smallest) record for boundary conditions.
2049  * Truncate the changelog.
2050  */
2051 static int mdd_changelog_clear_cb(const struct lu_env *env,
2052                                   struct llog_handle *llh,
2053                                   struct llog_rec_hdr *hdr,
2054                                   void *data)
2055 {
2056         struct llog_changelog_user_rec2 *rec;
2057         struct mdd_changelog_user_clear *mcuc = data;
2058         struct mdd_device *mdd = mcuc->mcuc_mdd;
2059         int rc;
2060
2061         ENTRY;
2062
2063         if ((llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) == 0)
2064                 RETURN(-ENXIO);
2065
2066         rec = container_of(hdr, typeof(*rec), cur_hdr);
2067         /* Does the changelog id match the requested id? */
2068         if (rec->cur_id != mcuc->mcuc_id) {
2069                 mcuc->mcuc_minrec = min(mcuc->mcuc_minrec, rec->cur_endrec);
2070                 mcuc->mcuc_mintime = min(mcuc->mcuc_mintime, rec->cur_time);
2071                 RETURN(0);
2072         }
2073
2074         mdd_chlg_username(rec, mcuc->mcuc_name, sizeof(mcuc->mcuc_name));
2075         /* cur_endrec is the oldest purgeable record, make sure we're newer */
2076         if (rec->cur_endrec > mcuc->mcuc_endrec) {
2077                 rc = -EINVAL;
2078                 CDEBUG(D_IOCTL,
2079                        "%s: request %llu < endrec %llu for user %s: rc = %d\n",
2080                        mdd2obd_dev(mdd)->obd_name, mcuc->mcuc_endrec,
2081                        rec->cur_endrec, mcuc->mcuc_name, rc);
2082                 RETURN(rc);
2083         }
2084
2085         /*
2086          * Flag that we've met all the range and user checks.
2087          * We now know the record to flush.
2088          */
2089         mcuc->mcuc_flush = true;
2090
2091         rec->cur_endrec = mcuc->mcuc_endrec;
2092         rec->cur_time = (__u32)ktime_get_real_seconds();
2093
2094         CDEBUG(D_IOCTL, "%s: update changelog user %s endrec = %llu\n",
2095                mdd2obd_dev(mdd)->obd_name, mcuc->mcuc_name, rec->cur_endrec);
2096
2097         /* Update the endrec */
2098         rc = llog_write(env, llh, hdr, hdr->lrh_index);
2099
2100         RETURN(rc);
2101 }
2102
2103 /**
2104  * Clear a changelog up to entry specified by endrec for user id.
2105  */
2106 static int mdd_changelog_clear(const struct lu_env *env,
2107                                struct mdd_device *mdd, __u32 id,
2108                                __u64 endrec)
2109 {
2110         struct mdd_changelog_user_clear mcuc = {
2111                 .mcuc_id = id,
2112                 .mcuc_minrec = endrec,
2113                 .mcuc_flush = false,
2114                 .mcuc_mdd = mdd,
2115                 .mcuc_mintime = ktime_get_real_seconds(),
2116                 .mcuc_name = { 0 },
2117         };
2118         struct llog_ctxt *ctxt;
2119         __u64 start_rec;
2120         int rc;
2121
2122         ENTRY;
2123
2124         CDEBUG(D_IOCTL, "%s: Purge request: id=%u, endrec=%llu\n",
2125                mdd2obd_dev(mdd)->obd_name, id, endrec);
2126         /* start_rec is the newest (largest value) entry in the changelogs*/
2127         spin_lock(&mdd->mdd_cl.mc_lock);
2128         start_rec = mdd->mdd_cl.mc_index;
2129         spin_unlock(&mdd->mdd_cl.mc_lock);
2130
2131         if (start_rec < endrec) {
2132                 CDEBUG(D_IOCTL, "%s: Could not clear changelog, requested "\
2133                        "address out of range\n", mdd2obd_dev(mdd)->obd_name);
2134                 RETURN(-EINVAL);
2135         }
2136
2137         if (endrec == 0) {
2138                 mcuc.mcuc_endrec = start_rec;
2139                 mcuc.mcuc_minrec = ULLONG_MAX;
2140         } else {
2141                 mcuc.mcuc_endrec = endrec;
2142         }
2143
2144         ctxt = llog_get_context(mdd2obd_dev(mdd),
2145                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
2146         if (!ctxt)
2147                 RETURN(-ENXIO);
2148
2149         if (!(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
2150                 GOTO(out, rc = -ENXIO);
2151
2152         rc = llog_cat_process(env, ctxt->loc_handle, mdd_changelog_clear_cb,
2153                               &mcuc, 0, 0);
2154         if (rc == -EINVAL) {
2155                 CDEBUG(D_IOCTL, "%s: no changelog recnum <= %llu to clear\n",
2156                        mdd2obd_dev(mdd)->obd_name, (unsigned long long)endrec);
2157                 GOTO(out, rc);
2158         }
2159
2160         if (rc < 0) {
2161                 CWARN("%s: can't clear the changelog for user %s: rc = %d\n",
2162                       mdd2obd_dev(mdd)->obd_name, mcuc.mcuc_name, rc);
2163                 GOTO(out, rc);
2164         }
2165
2166         if (!mcuc.mcuc_flush) {
2167                 CDEBUG(D_IOCTL, "%s: no entry for user %d\n",
2168                        mdd2obd_dev(mdd)->obd_name, id);
2169                 GOTO(out, rc = -ENOENT);
2170         }
2171
2172         CDEBUG(D_IOCTL, "%s: purge changelog user %s entries up to %llu\n",
2173                mdd2obd_dev(mdd)->obd_name, mcuc.mcuc_name, mcuc.mcuc_minrec);
2174
2175         rc = mdd_changelog_llog_cancel(env, mdd, mcuc.mcuc_minrec);
2176         if (rc)
2177                 GOTO(out, rc);
2178
2179         spin_lock(&mdd->mdd_cl.mc_user_lock);
2180         mdd->mdd_cl.mc_minrec = mcuc.mcuc_minrec;
2181         mdd->mdd_cl.mc_mintime = mcuc.mcuc_mintime;
2182         spin_unlock(&mdd->mdd_cl.mc_user_lock);
2183
2184         EXIT;
2185 out:
2186         llog_ctxt_put(ctxt);
2187
2188         return rc;
2189 }
2190
2191 static int mdd_changelog_user_deregister(const struct lu_env *env,
2192                                          struct mdd_device *mdd, int *id,
2193                                          const char *name)
2194 {
2195         struct llog_ctxt *ctxt;
2196         struct mdd_changelog_name_check_data mcnc = {
2197                 .mcnc_name = name,
2198                 .mcnc_id = 0,
2199         };
2200         int rc;
2201
2202         ENTRY;
2203
2204         if (name) {
2205                 ctxt = llog_get_context(mdd2obd_dev(mdd),
2206                                         LLOG_CHANGELOG_USER_ORIG_CTXT);
2207                 if (!ctxt)
2208                         RETURN(-ENXIO);
2209
2210                 rc = llog_cat_process(env, ctxt->loc_handle,
2211                               mdd_changelog_name_check_cb, &mcnc, 0, 0);
2212                 llog_ctxt_put(ctxt);
2213
2214                 if (rc != -EEXIST) {
2215                         CDEBUG(D_IOCTL, "%s: no entry for username %s\n",
2216                                mdd2obd_dev(mdd)->obd_name, name);
2217                         RETURN(-ENOENT);
2218                 }
2219                 *id = mcnc.mcnc_id;
2220         }
2221
2222         /* explicitly clear changelog first, to protect from crash in
2223          * the middle of purge that would lead to unregistered consumer
2224          * but pending changelog entries
2225          */
2226         rc = mdd_changelog_clear(env, mdd, *id, 0);
2227         if (!rc)
2228                 rc = mdd_changelog_user_purge(env, mdd, *id);
2229
2230         /* recalc changelog current mask */
2231         mdd_changelog_recalc_mask(env, mdd);
2232
2233         RETURN(rc);
2234 }
2235
2236 /** mdd_iocontrol
2237  * May be called remotely from mdt_iocontrol_handle or locally from
2238  * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
2239  * an obd_ioctl_data format (but local ioctl handler does).
2240  * \param cmd - ioc
2241  * \param len - data len
2242  * \param karg - ioctl data, in kernel space
2243  */
2244 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
2245                          unsigned int cmd, int len, void *karg)
2246 {
2247         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
2248         struct obd_ioctl_data *data = karg;
2249         int rc;
2250         ENTRY;
2251
2252         /* Doesn't use obd_ioctl_data */
2253         switch (cmd) {
2254         case OBD_IOC_CHANGELOG_CLEAR: {
2255                 struct changelog_setinfo *cs = karg;
2256
2257                 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
2258                         RETURN(-EINPROGRESS);
2259
2260                 rc = mdd_changelog_clear(env, mdd, cs->cs_id,
2261                                          cs->cs_recno);
2262                 barrier_exit(mdd->mdd_bottom);
2263                 RETURN(rc);
2264         }
2265         case OBD_IOC_START_LFSCK: {
2266                 rc = lfsck_start(env, mdd->mdd_bottom,
2267                                  (struct lfsck_start_param *)karg);
2268                 RETURN(rc);
2269         }
2270         case OBD_IOC_STOP_LFSCK: {
2271                 rc = lfsck_stop(env, mdd->mdd_bottom,
2272                                 (struct lfsck_stop *)karg);
2273                 RETURN(rc);
2274         }
2275         case OBD_IOC_QUERY_LFSCK: {
2276                 rc = lfsck_query(env, mdd->mdd_bottom, NULL, NULL,
2277                                  (struct lfsck_query *)karg);
2278                 RETURN(rc);
2279         }
2280         }
2281
2282         /* Below ioctls use obd_ioctl_data */
2283         if (data->ioc_version != OBD_IOCTL_VERSION) {
2284                 CERROR("Bad magic %x != %x\n", data->ioc_version,
2285                        OBD_IOCTL_VERSION);
2286                 RETURN(-EINVAL);
2287         }
2288
2289         switch (cmd) {
2290         case OBD_IOC_CHANGELOG_REG:
2291                 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
2292                         RETURN(-EINPROGRESS);
2293
2294                 rc = mdd_changelog_user_register(env, mdd, &data->ioc_u32_1,
2295                                                  data->ioc_inlbuf1,
2296                                                  data->ioc_inlbuf2);
2297                 barrier_exit(mdd->mdd_bottom);
2298                 break;
2299         case OBD_IOC_CHANGELOG_DEREG:
2300                 if (unlikely(!barrier_entry(mdd->mdd_bottom)))
2301                         RETURN(-EINPROGRESS);
2302
2303                 rc = mdd_changelog_user_deregister(env, mdd, &data->ioc_u32_1,
2304                                                    data->ioc_inlbuf1);
2305
2306                 barrier_exit(mdd->mdd_bottom);
2307                 break;
2308         default:
2309                 rc = -ENOTTY;
2310         }
2311
2312         RETURN(rc);
2313 }
2314
2315 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
2316 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key);
2317
2318 static const struct md_device_operations mdd_ops = {
2319         .mdo_statfs         = mdd_statfs,
2320         .mdo_root_get       = mdd_root_get,
2321         .mdo_llog_ctxt_get  = mdd_llog_ctxt_get,
2322         .mdo_iocontrol      = mdd_iocontrol,
2323         .mdo_dtconf_get     = mdd_dtconf_get,
2324 };
2325
2326 static const struct lu_device_type_operations mdd_device_type_ops = {
2327         .ldto_init              = mdd_type_init,
2328         .ldto_fini              = mdd_type_fini,
2329
2330         .ldto_start             = mdd_type_start,
2331         .ldto_stop              = mdd_type_stop,
2332
2333         .ldto_device_alloc      = mdd_device_alloc,
2334         .ldto_device_free       = mdd_device_free,
2335
2336         .ldto_device_fini       = mdd_device_fini
2337 };
2338
2339 static struct lu_device_type mdd_device_type = {
2340         .ldt_tags     = LU_DEVICE_MD,
2341         .ldt_name     = LUSTRE_MDD_NAME,
2342         .ldt_ops      = &mdd_device_type_ops,
2343         .ldt_ctx_tags = LCT_MD_THREAD
2344 };
2345
2346 /* context key constructor: mdd_key_init */
2347 LU_KEY_INIT(mdd, struct mdd_thread_info);
2348
2349 static void mdd_key_fini(const struct lu_context *ctx,
2350                          struct lu_context_key *key, void *data)
2351 {
2352         struct mdd_thread_info *info = data;
2353
2354         lu_buf_free(&info->mdi_big_buf);
2355         lu_buf_free(&info->mdi_link_buf);
2356         lu_buf_free(&info->mdi_xattr_buf);
2357         lu_buf_free(&info->mdi_chlg_buf);
2358
2359         OBD_FREE_PTR(info);
2360 }
2361
2362 /* context key: mdd_thread_key */
2363 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
2364
2365 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
2366                              int (*func)(void *), void *data, char *name)
2367 {
2368         struct task_struct      *task;
2369
2370         LASSERT(thread->mgt_init == false);
2371         init_completion(&thread->mgt_started);
2372         init_completion(&thread->mgt_finished);
2373         thread->mgt_data = data;
2374         thread->mgt_abort = false;
2375         thread->mgt_init = true;
2376
2377         task = kthread_run(func, thread, name);
2378         if (IS_ERR(task)) {
2379                 complete(&thread->mgt_finished);
2380                 return PTR_ERR(task);
2381         }
2382         wait_for_completion(&thread->mgt_started);
2383         return 0;
2384 }
2385
2386 void mdd_generic_thread_stop(struct mdd_generic_thread *thread)
2387 {
2388         if (thread->mgt_init == true) {
2389                 thread->mgt_abort = true;
2390                 wait_for_completion(&thread->mgt_finished);
2391         }
2392 }
2393
2394 static int __init mdd_init(void)
2395 {
2396         int rc;
2397
2398         rc = lu_kmem_init(mdd_caches);
2399         if (rc)
2400                 return rc;
2401
2402         changelog_orig_logops = llog_common_cat_ops;
2403         changelog_orig_logops.lop_write_rec = mdd_changelog_write_rec;
2404
2405         rc = class_register_type(&mdd_obd_device_ops, NULL, false,
2406                                  LUSTRE_MDD_NAME, &mdd_device_type);
2407         if (rc)
2408                 lu_kmem_fini(mdd_caches);
2409         return rc;
2410 }
2411
2412 static void __exit mdd_exit(void)
2413 {
2414         class_unregister_type(LUSTRE_MDD_NAME);
2415         lu_kmem_fini(mdd_caches);
2416 }
2417
2418 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2419 MODULE_DESCRIPTION("Lustre Meta-data Device Driver ("LUSTRE_MDD_NAME")");
2420 MODULE_VERSION(LUSTRE_VERSION_STRING);
2421 MODULE_LICENSE("GPL");
2422
2423 module_init(mdd_init);
2424 module_exit(mdd_exit);