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