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