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