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