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