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