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