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