Whamcloud - gitweb
LU-8906 mdd: Return rc in mdd_local_file_create()
[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, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdd/mdd_device.c
33  *
34  * Lustre Metadata Server (mdd) routines
35  *
36  * Author: Wang Di <wangdi@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include <linux/module.h>
42 #include <linux/kthread.h>
43 #include <obd_class.h>
44 #include <lustre_ioctl.h>
45 #include <lustre_mds.h>
46 #include <obd_support.h>
47 #include <lu_object.h>
48 #include <lustre_param.h>
49 #include <lustre_fid.h>
50 #include <lustre_nodemap.h>
51
52 #include "mdd_internal.h"
53
54 static const struct md_device_operations mdd_ops;
55 static struct lu_device_type mdd_device_type;
56
57 static const char mdd_root_dir_name[] = "ROOT";
58 static const char mdd_obf_dir_name[] = "fid";
59 static const char mdd_lpf_dir_name[] = "lost+found";
60
61 /* Slab for MDD object allocation */
62 struct kmem_cache *mdd_object_kmem;
63
64 static struct lu_kmem_descr mdd_caches[] = {
65         {
66                 .ckd_cache = &mdd_object_kmem,
67                 .ckd_name  = "mdd_obj",
68                 .ckd_size  = sizeof(struct mdd_object)
69         },
70         {
71                 .ckd_cache = NULL
72         }
73 };
74
75 static int mdd_connect_to_next(const struct lu_env *env, struct mdd_device *m,
76                                const char *nextdev)
77 {
78         struct obd_connect_data *data = NULL;
79         struct lu_device        *lud = mdd2lu_dev(m);
80         struct obd_device       *obd;
81         int                      rc;
82         ENTRY;
83
84         LASSERT(m->mdd_child_exp == NULL);
85
86         OBD_ALLOC(data, sizeof(*data));
87         if (data == NULL)
88                 GOTO(out, rc = -ENOMEM);
89
90         obd = class_name2obd(nextdev);
91         if (obd == NULL) {
92                 CERROR("can't locate next device: %s\n", nextdev);
93                 GOTO(out, rc = -ENOTCONN);
94         }
95
96         data->ocd_connect_flags = OBD_CONNECT_VERSION;
97         data->ocd_version = LUSTRE_VERSION_CODE;
98
99         rc = obd_connect(NULL, &m->mdd_child_exp, obd, &obd->obd_uuid, data, NULL);
100         if (rc) {
101                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
102                 GOTO(out, rc);
103         }
104
105         lud->ld_site = m->mdd_child_exp->exp_obd->obd_lu_dev->ld_site;
106         LASSERT(lud->ld_site);
107         m->mdd_child = lu2dt_dev(m->mdd_child_exp->exp_obd->obd_lu_dev);
108         m->mdd_bottom = lu2dt_dev(lud->ld_site->ls_bottom_dev);
109         lu_dev_add_linkage(lud->ld_site, lud);
110
111 out:
112         if (data)
113                 OBD_FREE(data, sizeof(*data));
114         RETURN(rc);
115 }
116
117 static int mdd_init0(const struct lu_env *env, struct mdd_device *mdd,
118                 struct lu_device_type *t, struct lustre_cfg *lcfg)
119 {
120         int rc = -EINVAL;
121         const char *dev;
122         ENTRY;
123
124         /* LU-8040 Set defaults here, before values configs */
125         mdd->mdd_cl.mc_flags = 0; /* off by default */
126         mdd->mdd_cl.mc_mask = CHANGELOG_DEFMASK;
127
128         dev = lustre_cfg_string(lcfg, 0);
129         if (dev == NULL)
130                 RETURN(rc);
131
132         mdd->mdd_md_dev.md_lu_dev.ld_obd = class_name2obd(dev);
133         if (mdd->mdd_md_dev.md_lu_dev.ld_obd == NULL)
134                 RETURN(rc);
135         mdd->mdd_md_dev.md_lu_dev.ld_ops = &mdd_lu_ops;
136         mdd->mdd_md_dev.md_ops = &mdd_ops;
137
138         rc = mdd_connect_to_next(env, mdd, lustre_cfg_string(lcfg, 3));
139         if (rc != 0)
140                 RETURN(rc);
141
142         mdd->mdd_atime_diff = MAX_ATIME_DIFF;
143         /* sync permission changes */
144         mdd->mdd_sync_permission = 1;
145
146         dt_conf_get(env, mdd->mdd_child, &mdd->mdd_dt_conf);
147
148         /* we are using service name but not mdd obd name
149          * for compatibility reasons.
150          * It is passed from MDT in lustre_cfg[2] buffer */
151         rc = mdd_procfs_init(mdd, lustre_cfg_string(lcfg, 2));
152         if (rc < 0)
153                 obd_disconnect(mdd->mdd_child_exp);
154
155         RETURN(rc);
156 }
157
158 static struct lu_device *mdd_device_fini(const struct lu_env *env,
159                                          struct lu_device *d)
160 {
161         struct mdd_device *mdd = lu2mdd_dev(d);
162
163         if (d->ld_site)
164                 lu_dev_del_linkage(d->ld_site, d);
165
166         mdd_procfs_fini(mdd);
167         return NULL;
168 }
169
170 static int changelog_init_cb(const struct lu_env *env, struct llog_handle *llh,
171                              struct llog_rec_hdr *hdr, void *data)
172 {
173         struct mdd_device *mdd = (struct mdd_device *)data;
174         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
175
176         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
177         LASSERT(rec->cr_hdr.lrh_type == CHANGELOG_REC);
178
179         CDEBUG(D_INFO,
180                "seeing record at index %d/%d/%llu t=%x %.*s in log"
181                DOSTID"\n", hdr->lrh_index, rec->cr_hdr.lrh_index,
182                rec->cr.cr_index, rec->cr.cr_type, rec->cr.cr_namelen,
183                changelog_rec_name(&rec->cr), POSTID(&llh->lgh_id.lgl_oi));
184
185         mdd->mdd_cl.mc_index = rec->cr.cr_index;
186         return LLOG_PROC_BREAK;
187 }
188
189 static int changelog_user_init_cb(const struct lu_env *env,
190                                   struct llog_handle *llh,
191                                   struct llog_rec_hdr *hdr, void *data)
192 {
193         struct mdd_device *mdd = (struct mdd_device *)data;
194         struct llog_changelog_user_rec *rec =
195                 (struct llog_changelog_user_rec *)hdr;
196
197         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
198         LASSERT(rec->cur_hdr.lrh_type == CHANGELOG_USER_REC);
199
200         CDEBUG(D_INFO, "seeing user at index %d/%d id=%d endrec=%llu"
201                " in log "DOSTID"\n", hdr->lrh_index, rec->cur_hdr.lrh_index,
202                rec->cur_id, rec->cur_endrec, POSTID(&llh->lgh_id.lgl_oi));
203
204         spin_lock(&mdd->mdd_cl.mc_user_lock);
205         mdd->mdd_cl.mc_lastuser = rec->cur_id;
206         if (rec->cur_endrec > mdd->mdd_cl.mc_index)
207                 mdd->mdd_cl.mc_index = rec->cur_endrec;
208         spin_unlock(&mdd->mdd_cl.mc_user_lock);
209
210         return LLOG_PROC_BREAK;
211 }
212
213 static int llog_changelog_cancel_cb(const struct lu_env *env,
214                                     struct llog_handle *llh,
215                                     struct llog_rec_hdr *hdr, void *data)
216 {
217         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
218         struct llog_cookie       cookie;
219         long long                endrec = *(long long *)data;
220         int                      rc;
221
222         ENTRY;
223
224         /* This is always a (sub)log, not the catalog */
225         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
226
227         if (rec->cr.cr_index > endrec)
228                 /* records are in order, so we're done */
229                 RETURN(LLOG_PROC_BREAK);
230
231         cookie.lgc_lgl = llh->lgh_id;
232         cookie.lgc_index = hdr->lrh_index;
233
234         /* cancel them one at a time.  I suppose we could store up the cookies
235          * and cancel them all at once; probably more efficient, but this is
236          * done as a user call, so who cares... */
237         rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1,
238                                      &cookie);
239         RETURN(rc < 0 ? rc : 0);
240 }
241
242 static int llog_changelog_cancel(const struct lu_env *env,
243                                  struct llog_ctxt *ctxt,
244                                  struct llog_cookie *cookies, int flags)
245 {
246         struct llog_handle      *cathandle = ctxt->loc_handle;
247         int                      rc;
248
249         ENTRY;
250
251         /* This should only be called with the catalog handle */
252         LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
253
254         rc = llog_cat_process(env, cathandle, llog_changelog_cancel_cb,
255                               (void *)cookies, 0, 0);
256         if (rc >= 0)
257                 /* 0 or 1 means we're done */
258                 rc = 0;
259         else
260                 CERROR("%s: cancel idx %u of catalog "DOSTID" rc=%d\n",
261                        ctxt->loc_obd->obd_name, cathandle->lgh_last_idx,
262                        POSTID(&cathandle->lgh_id.lgl_oi), rc);
263
264         RETURN(rc);
265 }
266
267 static struct llog_operations changelog_orig_logops;
268
269 static int
270 mdd_changelog_write_header(const struct lu_env *env, struct mdd_device *mdd,
271                            int markerflags);
272
273 static int
274 mdd_changelog_on(const struct lu_env *env, struct mdd_device *mdd)
275 {
276         int rc = 0;
277
278         if ((mdd->mdd_cl.mc_flags & CLM_ON) != 0)
279                 return rc;
280
281         LCONSOLE_INFO("%s: changelog on\n", mdd2obd_dev(mdd)->obd_name);
282         if (mdd->mdd_cl.mc_flags & CLM_ERR) {
283                 CERROR("Changelogs cannot be enabled due to error "
284                        "condition (see %s log).\n",
285                        mdd2obd_dev(mdd)->obd_name);
286                 rc = -ESRCH;
287         } else {
288                 spin_lock(&mdd->mdd_cl.mc_lock);
289                 mdd->mdd_cl.mc_flags |= CLM_ON;
290                 spin_unlock(&mdd->mdd_cl.mc_lock);
291                 rc = mdd_changelog_write_header(env, mdd, CLM_START);
292         }
293         return rc;
294 }
295
296 static int
297 mdd_changelog_off(const struct lu_env *env, struct mdd_device *mdd)
298 {
299         int rc = 0;
300
301         if ((mdd->mdd_cl.mc_flags & CLM_ON) != CLM_ON)
302                 return rc;
303
304         LCONSOLE_INFO("%s: changelog off\n", mdd2obd_dev(mdd)->obd_name);
305         rc = mdd_changelog_write_header(env, mdd, CLM_FINI);
306         spin_lock(&mdd->mdd_cl.mc_lock);
307         mdd->mdd_cl.mc_flags &= ~CLM_ON;
308         spin_unlock(&mdd->mdd_cl.mc_lock);
309
310         return rc;
311 }
312
313 static int mdd_changelog_llog_init(const struct lu_env *env,
314                                    struct mdd_device *mdd)
315 {
316         struct obd_device       *obd = mdd2obd_dev(mdd);
317         struct llog_ctxt        *ctxt = NULL, *uctxt = NULL;
318         int                      rc;
319
320         ENTRY;
321
322         /* LU-2844 mdd setup failure should not cause umount oops */
323         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_INIT))
324                 RETURN(-EIO);
325
326         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
327         obd->obd_lvfs_ctxt.dt = mdd->mdd_bottom;
328         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
329                         obd, &changelog_orig_logops);
330         if (rc) {
331                 CERROR("%s: changelog llog setup failed: rc = %d\n",
332                        obd->obd_name, rc);
333                 RETURN(rc);
334         }
335
336         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
337         LASSERT(ctxt);
338
339         rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
340                               CHANGELOG_CATALOG);
341         if (rc)
342                 GOTO(out_cleanup, rc);
343
344         rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
345         if (rc)
346                 GOTO(out_close, rc);
347
348         rc = llog_cat_reverse_process(env, ctxt->loc_handle,
349                                       changelog_init_cb, mdd);
350
351         if (rc < 0) {
352                 CERROR("%s: changelog init failed: rc = %d\n", obd->obd_name,
353                        rc);
354                 GOTO(out_close, rc);
355         }
356
357         CDEBUG(D_IOCTL, "changelog starting index=%llu\n",
358                mdd->mdd_cl.mc_index);
359
360         /* setup user changelog */
361         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
362                         obd, &changelog_orig_logops);
363         if (rc) {
364                 CERROR("%s: changelog users llog setup failed: rc = %d\n",
365                        obd->obd_name, rc);
366                 GOTO(out_close, rc);
367         }
368
369         uctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
370         LASSERT(ctxt);
371
372         rc = llog_open_create(env, uctxt, &uctxt->loc_handle, NULL,
373                               CHANGELOG_USERS);
374         if (rc)
375                 GOTO(out_ucleanup, rc);
376
377         uctxt->loc_handle->lgh_logops->lop_add = llog_cat_add_rec;
378         uctxt->loc_handle->lgh_logops->lop_declare_add = llog_cat_declare_add_rec;
379
380         rc = llog_init_handle(env, uctxt->loc_handle, LLOG_F_IS_CAT, NULL);
381         if (rc)
382                 GOTO(out_uclose, rc);
383
384         rc = llog_cat_reverse_process(env, uctxt->loc_handle,
385                                       changelog_user_init_cb, mdd);
386         if (rc < 0) {
387                 CERROR("%s: changelog user init failed: rc = %d\n",
388                        obd->obd_name, rc);
389                 GOTO(out_uclose, rc);
390         }
391
392         /* If we have registered users, assume we want changelogs on */
393         if (mdd->mdd_cl.mc_lastuser > 0) {
394                 rc = mdd_changelog_on(env, mdd);
395                 if (rc < 0)
396                         GOTO(out_uclose, rc);
397         }
398         llog_ctxt_put(ctxt);
399         llog_ctxt_put(uctxt);
400         RETURN(0);
401 out_uclose:
402         llog_cat_close(env, uctxt->loc_handle);
403 out_ucleanup:
404         llog_cleanup(env, uctxt);
405 out_close:
406         llog_cat_close(env, ctxt->loc_handle);
407 out_cleanup:
408         llog_cleanup(env, ctxt);
409         return rc;
410 }
411
412 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
413 {
414         struct obd_device       *obd = mdd2obd_dev(mdd);
415         int                      rc;
416
417         mdd->mdd_cl.mc_index = 0;
418         spin_lock_init(&mdd->mdd_cl.mc_lock);
419         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
420         spin_lock_init(&mdd->mdd_cl.mc_user_lock);
421         mdd->mdd_cl.mc_lastuser = 0;
422
423         rc = mdd_changelog_llog_init(env, mdd);
424         if (rc) {
425                 CERROR("%s: changelog setup during init failed: rc = %d\n",
426                        obd->obd_name, rc);
427                 mdd->mdd_cl.mc_flags |= CLM_ERR;
428         }
429
430         return rc;
431 }
432
433 static void mdd_changelog_fini(const struct lu_env *env,
434                                struct mdd_device *mdd)
435 {
436         struct obd_device       *obd = mdd2obd_dev(mdd);
437         struct llog_ctxt        *ctxt;
438
439         mdd->mdd_cl.mc_flags = 0;
440
441         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
442         if (ctxt) {
443                 llog_cat_close(env, ctxt->loc_handle);
444                 llog_cleanup(env, ctxt);
445         }
446         ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
447         if (ctxt) {
448                 llog_cat_close(env, ctxt->loc_handle);
449                 llog_cleanup(env, ctxt);
450         }
451 }
452
453 /** Remove entries with indicies up to and including \a endrec from the
454  *  changelog
455  * \param mdd
456  * \param endrec
457  * \retval 0 ok
458  */
459 static int
460 mdd_changelog_llog_cancel(const struct lu_env *env, struct mdd_device *mdd,
461                           long long endrec)
462 {
463         struct obd_device *obd = mdd2obd_dev(mdd);
464         struct llog_ctxt *ctxt;
465         long long unsigned cur;
466         int rc;
467
468         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
469         if (ctxt == NULL)
470                 return -ENXIO;
471
472         spin_lock(&mdd->mdd_cl.mc_lock);
473         cur = (long long)mdd->mdd_cl.mc_index;
474         spin_unlock(&mdd->mdd_cl.mc_lock);
475         if (endrec > cur)
476                 endrec = cur;
477
478         /* purge to "0" is shorthand for everything */
479         if (endrec == 0)
480                 endrec = cur;
481
482         /* If purging all records, write a header entry so we don't have an
483            empty catalog and we're sure to have a valid starting index next
484            time.  In case of crash, we just restart with old log so we're
485            allright. */
486         if (endrec == cur) {
487                 /* XXX: transaction is started by llog itself */
488                 rc = mdd_changelog_write_header(env, mdd, CLM_PURGE);
489                 if (rc)
490                       goto out;
491         }
492
493         /* Some records were purged, so reset repeat-access time (so we
494            record new mtime update records, so users can see a file has been
495            changed since the last purge) */
496         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
497
498         rc = llog_cancel(env, ctxt, (struct llog_cookie *)&endrec, 0);
499 out:
500         llog_ctxt_put(ctxt);
501         return rc;
502 }
503
504 /** Add a CL_MARK record to the changelog
505  * \param mdd
506  * \param markerflags - CLM_*
507  * \retval 0 ok
508  */
509 int mdd_changelog_write_header(const struct lu_env *env,
510                                struct mdd_device *mdd, int markerflags)
511 {
512         struct obd_device               *obd = mdd2obd_dev(mdd);
513         struct llog_changelog_rec       *rec;
514         struct lu_buf                   *buf;
515         struct llog_ctxt                *ctxt;
516         int                              reclen;
517         int                              len = strlen(obd->obd_name);
518         int                              rc;
519
520         ENTRY;
521
522         if (mdd->mdd_cl.mc_mask & (1 << CL_MARK)) {
523                 mdd->mdd_cl.mc_starttime = cfs_time_current_64();
524                 RETURN(0);
525         }
526
527         reclen = llog_data_len(sizeof(*rec) + len);
528         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
529         if (buf->lb_buf == NULL)
530                 RETURN(-ENOMEM);
531         rec = buf->lb_buf;
532
533         rec->cr.cr_flags = CLF_VERSION;
534         rec->cr.cr_type = CL_MARK;
535         rec->cr.cr_namelen = len;
536         memcpy(changelog_rec_name(&rec->cr), obd->obd_name, rec->cr.cr_namelen);
537         /* Status and action flags */
538         rec->cr.cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
539         rec->cr_hdr.lrh_len = llog_data_len(changelog_rec_size(&rec->cr) +
540                                             rec->cr.cr_namelen);
541         rec->cr_hdr.lrh_type = CHANGELOG_REC;
542         rec->cr.cr_time = cl_time();
543         spin_lock(&mdd->mdd_cl.mc_lock);
544         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
545         spin_unlock(&mdd->mdd_cl.mc_lock);
546
547         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
548         LASSERT(ctxt);
549
550         rc = llog_cat_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL);
551         if (rc > 0)
552                 rc = 0;
553         llog_ctxt_put(ctxt);
554
555         /* assume on or off event; reset repeat-access time */
556         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
557         RETURN(rc);
558 }
559
560 /**
561  * Lookup method for "fid" object. Only filenames with correct SEQ:OID format
562  * are valid. We also check if object with passed fid exists or not.
563  */
564 static int obf_lookup(const struct lu_env *env, struct md_object *p,
565                       const struct lu_name *lname, struct lu_fid *f,
566                       struct md_op_spec *spec)
567 {
568         char *name = (char *)lname->ln_name;
569         struct mdd_device *mdd = mdo2mdd(p);
570         struct mdd_object *child;
571         int rc = 0;
572
573         while (*name == '[')
574                 name++;
575
576         sscanf(name, SFID, RFID(f));
577         if (!fid_is_sane(f)) {
578                 CWARN("%s: Trying to lookup invalid FID [%s] in %s/%s, FID "
579                       "format should be "DFID"\n", mdd2obd_dev(mdd)->obd_name,
580                       lname->ln_name, dot_lustre_name, mdd_obf_dir_name,
581                       (__u64)FID_SEQ_NORMAL, 1, 0);
582                 GOTO(out, rc = -EINVAL);
583         }
584
585         if (!fid_is_norm(f) && !fid_is_igif(f) && !fid_is_root(f) &&
586             !fid_seq_is_dot(f->f_seq)) {
587                 CWARN("%s: Trying to lookup invalid FID "DFID" in %s/%s, "
588                       "sequence should be >= %#llx or within [%#llx,"
589                       "%#llx].\n", mdd2obd_dev(mdd)->obd_name, PFID(f),
590                       dot_lustre_name, mdd_obf_dir_name, (__u64)FID_SEQ_NORMAL,
591                       (__u64)FID_SEQ_IGIF, (__u64)FID_SEQ_IGIF_MAX);
592                 GOTO(out, rc = -EINVAL);
593         }
594
595         /* Check if object with this fid exists */
596         child = mdd_object_find(env, mdd, f);
597         if (IS_ERR(child))
598                 GOTO(out, rc = PTR_ERR(child));
599
600         if (mdd_object_exists(child) == 0)
601                 rc = -ENOENT;
602
603         mdd_object_put(env, child);
604
605 out:
606         return rc;
607 }
608
609 static int mdd_dummy_create(const struct lu_env *env,
610                             struct md_object *pobj,
611                             const struct lu_name *lname,
612                             struct md_object *child,
613                             struct md_op_spec *spec,
614                             struct md_attr* ma)
615 {
616         return -EPERM;
617 }
618
619 static int mdd_dummy_rename(const struct lu_env *env,
620                             struct md_object *src_pobj,
621                             struct md_object *tgt_pobj,
622                             const struct lu_fid *lf,
623                             const struct lu_name *lsname,
624                             struct md_object *tobj,
625                             const struct lu_name *ltname,
626                             struct md_attr *ma)
627 {
628         return -EPERM;
629 }
630
631 static int mdd_dummy_link(const struct lu_env *env,
632                           struct md_object *tgt_obj,
633                           struct md_object *src_obj,
634                           const struct lu_name *lname,
635                           struct md_attr *ma)
636 {
637         return -EPERM;
638 }
639
640 static int mdd_dummy_unlink(const struct lu_env *env,
641                             struct md_object *pobj,
642                             struct md_object *cobj,
643                             const struct lu_name *lname,
644                             struct md_attr *ma,
645                             int no_name)
646 {
647         return -EPERM;
648 }
649
650 static struct md_dir_operations mdd_obf_dir_ops = {
651         .mdo_lookup = obf_lookup,
652         .mdo_create = mdd_dummy_create,
653         .mdo_rename = mdd_dummy_rename,
654         .mdo_link   = mdd_dummy_link,
655         .mdo_unlink = mdd_dummy_unlink
656 };
657
658 static struct md_dir_operations mdd_lpf_dir_ops = {
659         .mdo_lookup = mdd_lookup,
660         .mdo_create = mdd_dummy_create,
661         .mdo_rename = mdd_dummy_rename,
662         .mdo_link   = mdd_dummy_link,
663         .mdo_unlink = mdd_dummy_unlink
664 };
665
666 static struct md_object *mdo_locate(const struct lu_env *env,
667                                     struct md_device *md,
668                                     const struct lu_fid *fid)
669 {
670         struct lu_object *obj;
671         struct md_object *mdo;
672
673         obj = lu_object_find(env, &md->md_lu_dev, fid, NULL);
674         if (!IS_ERR(obj)) {
675                 obj = lu_object_locate(obj->lo_header, md->md_lu_dev.ld_type);
676                 LASSERT(obj != NULL);
677                 mdo = lu2md(obj);
678         } else {
679                 mdo = ERR_PTR(PTR_ERR(obj));
680         }
681         return mdo;
682 }
683
684 static int mdd_lpf_setup(const struct lu_env *env, struct mdd_device *m)
685 {
686         struct md_object        *mdo;
687         struct mdd_object       *mdd_lpf;
688         struct lu_fid            fid    = LU_LPF_FID;
689         int                      rc;
690         ENTRY;
691
692         rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
693                                    mdd_lpf_dir_name, S_IFDIR | S_IRUSR | S_IXUSR,
694                                    &fid);
695         if (rc != 0)
696                 RETURN(rc);
697
698         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
699         if (IS_ERR(mdo))
700                 RETURN(PTR_ERR(mdo));
701
702         LASSERT(lu_object_exists(&mdo->mo_lu));
703
704         mdd_lpf = md2mdd_obj(mdo);
705         mdd_lpf->mod_obj.mo_dir_ops = &mdd_lpf_dir_ops;
706         m->mdd_dot_lustre_objs.mdd_lpf = mdd_lpf;
707
708         RETURN(0);
709 }
710
711 /**
712  * Create special in-memory "fid" object for open-by-fid.
713  */
714 static int mdd_obf_setup(const struct lu_env *env, struct mdd_device *m)
715 {
716         struct md_object        *mdo;
717         struct mdd_object       *mdd_obf;
718         struct lu_fid            fid = LU_OBF_FID;
719         int                      rc;
720
721         rc = mdd_local_file_create(env, m, mdd_object_fid(m->mdd_dot_lustre),
722                                    mdd_obf_dir_name, S_IFDIR | S_IXUSR, &fid);
723         if (rc < 0)
724                 RETURN(rc);
725
726         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
727         if (IS_ERR(mdo))
728                 RETURN(PTR_ERR(mdo));
729
730         LASSERT(lu_object_exists(&mdo->mo_lu));
731
732         mdd_obf = md2mdd_obj(mdo);
733         mdd_obf->mod_obj.mo_dir_ops = &mdd_obf_dir_ops;
734         m->mdd_dot_lustre_objs.mdd_obf = mdd_obf;
735
736         return 0;
737 }
738
739 static void mdd_dot_lustre_cleanup(const struct lu_env *env,
740                                    struct mdd_device *m)
741 {
742         if (m->mdd_dot_lustre_objs.mdd_lpf != NULL) {
743                 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_lpf);
744                 m->mdd_dot_lustre_objs.mdd_lpf = NULL;
745         }
746         if (m->mdd_dot_lustre_objs.mdd_obf != NULL) {
747                 mdd_object_put(env, m->mdd_dot_lustre_objs.mdd_obf);
748                 m->mdd_dot_lustre_objs.mdd_obf = NULL;
749         }
750         if (m->mdd_dot_lustre != NULL) {
751                 mdd_object_put(env, m->mdd_dot_lustre);
752                 m->mdd_dot_lustre = NULL;
753         }
754 }
755
756 /** Setup ".lustre" directory object */
757 static int mdd_dot_lustre_setup(const struct lu_env *env, struct mdd_device *m)
758 {
759         struct md_object        *mdo;
760         struct lu_fid            fid;
761         int                      rc;
762
763         ENTRY;
764         /* Create ".lustre" directory in ROOT. */
765         fid = LU_DOT_LUSTRE_FID;
766         rc = mdd_local_file_create(env, m, &m->mdd_root_fid,
767                                    dot_lustre_name,
768                                    S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO,
769                                    &fid);
770         if (rc < 0)
771                 RETURN(rc);
772         mdo = mdo_locate(env, &m->mdd_md_dev, &fid);
773         if (IS_ERR(mdo))
774                 RETURN(PTR_ERR(mdo));
775         LASSERT(lu_object_exists(&mdo->mo_lu));
776
777         m->mdd_dot_lustre = md2mdd_obj(mdo);
778
779         rc = mdd_obf_setup(env, m);
780         if (rc) {
781                 CERROR("%s: error initializing \"fid\" object: rc = %d.\n",
782                        mdd2obd_dev(m)->obd_name, rc);
783                 GOTO(out, rc);
784         }
785
786         rc = mdd_lpf_setup(env, m);
787         if (rc != 0) {
788                 CERROR("%s: error initializing \"lost+found\": rc = %d.\n",
789                        mdd2obd_dev(m)->obd_name, rc);
790                 GOTO(out, rc);
791         }
792
793         RETURN(0);
794
795 out:
796         mdd_dot_lustre_cleanup(env, m);
797
798         return rc;
799 }
800
801
802 static struct llog_operations hsm_actions_logops;
803
804 /**
805  * set llog methods and create LLOG_AGENT_ORIG_CTXT llog
806  * object in obd_device
807  */
808 static int mdd_hsm_actions_llog_init(const struct lu_env *env,
809                                      struct mdd_device *m)
810 {
811         struct obd_device       *obd = mdd2obd_dev(m);
812         struct llog_ctxt        *ctxt = NULL;
813         int                      rc;
814         ENTRY;
815
816         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
817         obd->obd_lvfs_ctxt.dt = m->mdd_bottom;
818
819         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_AGENT_ORIG_CTXT,
820                         obd, &hsm_actions_logops);
821         if (rc) {
822                 CERROR("%s: hsm actions llog setup failed: rc = %d\n",
823                         obd->obd_name, rc);
824                 RETURN(rc);
825         }
826
827         ctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
828         LASSERT(ctxt);
829
830         rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
831                               HSM_ACTIONS);
832         if (rc) {
833                 CERROR("%s: hsm actions llog open_create failed: rc = %d\n",
834                         obd->obd_name, rc);
835                 GOTO(out_cleanup, rc);
836         }
837
838         rc = llog_init_handle(env, ctxt->loc_handle, LLOG_F_IS_CAT, NULL);
839         if (rc)
840                 GOTO(out_close, rc);
841
842         llog_ctxt_put(ctxt);
843         RETURN(0);
844
845 out_close:
846         llog_cat_close(env, ctxt->loc_handle);
847         ctxt->loc_handle = NULL;
848 out_cleanup:
849         llog_cleanup(env, ctxt);
850
851         return rc;
852 }
853
854 /**
855  * cleanup the context created by llog_setup_named()
856  */
857 static int mdd_hsm_actions_llog_fini(const struct lu_env *env,
858                                      struct mdd_device *m)
859 {
860         struct obd_device       *obd = mdd2obd_dev(m);
861         struct llog_ctxt        *lctxt;
862         ENTRY;
863
864         lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
865         if (lctxt) {
866                 llog_cat_close(env, lctxt->loc_handle);
867                 lctxt->loc_handle = NULL;
868                 llog_cleanup(env, lctxt);
869         }
870
871         RETURN(0);
872 }
873
874 static void mdd_device_shutdown(const struct lu_env *env, struct mdd_device *m,
875                                 struct lustre_cfg *cfg)
876 {
877         lfsck_degister(env, m->mdd_bottom);
878         mdd_hsm_actions_llog_fini(env, m);
879         mdd_changelog_fini(env, m);
880         orph_index_fini(env, m);
881         mdd_dot_lustre_cleanup(env, m);
882         nm_config_file_deregister_tgt(env, mdd2obd_dev(m)->u.obt.obt_nodemap_config_file);
883         if (m->mdd_los != NULL) {
884                 local_oid_storage_fini(env, m->mdd_los);
885                 m->mdd_los = NULL;
886         }
887         lu_site_purge(env, mdd2lu_dev(m)->ld_site, ~0);
888
889         if (m->mdd_child_exp)
890                 obd_disconnect(m->mdd_child_exp);
891 }
892
893 static int mdd_process_config(const struct lu_env *env,
894                               struct lu_device *d, struct lustre_cfg *cfg)
895 {
896         struct mdd_device *m    = lu2mdd_dev(d);
897         struct dt_device  *dt   = m->mdd_child;
898         struct lu_device  *next = &dt->dd_lu_dev;
899         int rc;
900         ENTRY;
901
902         switch (cfg->lcfg_command) {
903         case LCFG_PARAM: {
904                 struct obd_device *obd = mdd2obd_dev(m);
905
906                 rc = class_process_proc_param(PARAM_MDD, obd->obd_vars, cfg, m);
907                 if (rc > 0 || rc == -ENOSYS)
908                         /* we don't understand; pass it on */
909                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
910                 break;
911         }
912         case LCFG_SETUP:
913                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
914                 if (rc)
915                         GOTO(out, rc);
916                 dt_conf_get(env, dt, &m->mdd_dt_conf);
917                 break;
918         case LCFG_PRE_CLEANUP:
919                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
920                 mdd_generic_thread_stop(&m->mdd_orph_cleanup_thread);
921                 break;
922         case LCFG_CLEANUP:
923                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
924                 lu_dev_del_linkage(d->ld_site, d);
925                 mdd_device_shutdown(env, m, cfg);
926                 break;
927         default:
928                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
929                 break;
930         }
931 out:
932         RETURN(rc);
933 }
934
935 static int mdd_recovery_complete(const struct lu_env *env,
936                                  struct lu_device *d)
937 {
938         struct mdd_device *mdd = lu2mdd_dev(d);
939         struct lu_device *next;
940         int rc;
941         ENTRY;
942
943         LASSERT(mdd != NULL);
944         next = &mdd->mdd_child->dd_lu_dev;
945
946         /* XXX: orphans handling. */
947         mdd_orphan_cleanup(env, mdd);
948         rc = next->ld_ops->ldo_recovery_complete(env, next);
949
950         RETURN(rc);
951 }
952
953 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
954                           const struct lu_fid *pfid, const char *name,
955                           __u32 mode, struct lu_fid *fid)
956 {
957         struct dt_object *parent, *dto;
958         int rc = 0;
959
960         ENTRY;
961
962         LASSERT(!fid_is_zero(pfid));
963         parent = dt_locate(env, mdd->mdd_bottom, pfid);
964         if (unlikely(IS_ERR(parent)))
965                 RETURN(PTR_ERR(parent));
966
967         /* create local file/dir, if @fid is passed then try to use it */
968         if (fid_is_zero(fid))
969                 dto = local_file_find_or_create(env, mdd->mdd_los, parent,
970                                                 name, mode);
971         else
972                 dto = local_file_find_or_create_with_fid(env, mdd->mdd_bottom,
973                                                          fid, parent, name,
974                                                          mode);
975         if (IS_ERR(dto))
976                 GOTO(out_put, rc = PTR_ERR(dto));
977         *fid = *lu_object_fid(&dto->do_lu);
978         /* since stack is not fully set up the local_storage uses own stack
979          * and we should drop its object from cache */
980         lu_object_put_nocache(env, &dto->do_lu);
981         EXIT;
982 out_put:
983         lu_object_put(env, &parent->do_lu);
984         return rc;
985 }
986
987 static int mdd_lfsck_out_notify(const struct lu_env *env, void *data,
988                                 enum lfsck_events event)
989 {
990         return 0;
991 }
992
993 static int mdd_prepare(const struct lu_env *env,
994                        struct lu_device *pdev,
995                        struct lu_device *cdev)
996 {
997         struct mdd_device *mdd = lu2mdd_dev(cdev);
998         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
999         struct nm_config_file *nodemap_config;
1000         struct lu_fid fid;
1001         int rc;
1002
1003         ENTRY;
1004
1005         rc = next->ld_ops->ldo_prepare(env, cdev, next);
1006         if (rc)
1007                 RETURN(rc);
1008
1009         /* Setup local dirs */
1010         fid.f_seq = FID_SEQ_LOCAL_NAME;
1011         fid.f_oid = 1;
1012         fid.f_ver = 0;
1013         rc = local_oid_storage_init(env, mdd->mdd_bottom, &fid,
1014                                     &mdd->mdd_los);
1015         if (rc)
1016                 RETURN(rc);
1017
1018         rc = dt_root_get(env, mdd->mdd_child, &mdd->mdd_local_root_fid);
1019         if (rc < 0)
1020                 GOTO(out_los, rc);
1021
1022         lu_root_fid(&fid);
1023         if (mdd_seq_site(mdd)->ss_node_id == 0) {
1024                 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
1025                                            mdd_root_dir_name, S_IFDIR |
1026                                            S_IRUGO | S_IWUSR | S_IXUGO, &fid);
1027                 if (rc != 0) {
1028                         CERROR("%s: create root fid failed: rc = %d\n",
1029                                mdd2obd_dev(mdd)->obd_name, rc);
1030                         GOTO(out_los, rc);
1031                 }
1032
1033                 mdd->mdd_root_fid = fid;
1034                 rc = mdd_dot_lustre_setup(env, mdd);
1035                 if (rc != 0) {
1036                         CERROR("%s: initializing .lustre failed: rc = %d\n",
1037                                mdd2obd_dev(mdd)->obd_name, rc);
1038                         GOTO(out_los, rc);
1039                 }
1040         } else {
1041                 /* Normal client usually send root access to MDT0 directly,
1042                  * the root FID on non-MDT0 will only be used by echo client. */
1043                 mdd->mdd_root_fid = fid;
1044         }
1045
1046         rc = orph_index_init(env, mdd);
1047         if (rc < 0)
1048                 GOTO(out_dot, rc);
1049
1050         rc = mdd_changelog_init(env, mdd);
1051         if (rc != 0) {
1052                 CERROR("%s: failed to initialize changelog: rc = %d\n",
1053                        mdd2obd_dev(mdd)->obd_name, rc);
1054                 GOTO(out_orph, rc);
1055         }
1056
1057         rc = mdd_hsm_actions_llog_init(env, mdd);
1058         if (rc != 0)
1059                 GOTO(out_changelog, rc);
1060
1061         nodemap_config = nm_config_file_register_tgt(env, mdd->mdd_bottom,
1062                                                      mdd->mdd_los);
1063         if (IS_ERR(nodemap_config))
1064                 GOTO(out_hsm, rc = PTR_ERR(nodemap_config));
1065
1066         mdd2obd_dev(mdd)->u.obt.obt_nodemap_config_file = nodemap_config;
1067
1068         rc = lfsck_register(env, mdd->mdd_bottom, mdd->mdd_child,
1069                             mdd2obd_dev(mdd), mdd_lfsck_out_notify,
1070                             mdd, true);
1071         if (rc != 0) {
1072                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
1073                        mdd2obd_dev(mdd)->obd_name, rc);
1074                 GOTO(out_nodemap, rc);
1075         }
1076
1077         RETURN(0);
1078
1079 out_nodemap:
1080         nm_config_file_deregister_tgt(env, mdd2obd_dev(mdd)->u.obt.obt_nodemap_config_file);
1081         mdd2obd_dev(mdd)->u.obt.obt_nodemap_config_file = NULL;
1082 out_hsm:
1083         mdd_hsm_actions_llog_fini(env, mdd);
1084 out_changelog:
1085         mdd_changelog_fini(env, mdd);
1086 out_orph:
1087         orph_index_fini(env, mdd);
1088 out_dot:
1089         if (mdd_seq_site(mdd)->ss_node_id == 0)
1090                 mdd_dot_lustre_cleanup(env, mdd);
1091 out_los:
1092         local_oid_storage_fini(env, mdd->mdd_los);
1093         mdd->mdd_los = NULL;
1094
1095         return rc;
1096 }
1097
1098 const struct lu_device_operations mdd_lu_ops = {
1099         .ldo_object_alloc      = mdd_object_alloc,
1100         .ldo_process_config    = mdd_process_config,
1101         .ldo_recovery_complete = mdd_recovery_complete,
1102         .ldo_prepare           = mdd_prepare,
1103 };
1104
1105 static int mdd_root_get(const struct lu_env *env,
1106                         struct md_device *m, struct lu_fid *f)
1107 {
1108         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1109
1110         ENTRY;
1111         *f = mdd->mdd_root_fid;
1112         RETURN(0);
1113 }
1114
1115 /*
1116  * No permission check is needed.
1117  */
1118 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1119                       struct obd_statfs *sfs)
1120 {
1121         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1122         int rc;
1123
1124         ENTRY;
1125
1126         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
1127
1128         sfs->os_namelen = min_t(__u32, sfs->os_namelen, NAME_MAX);
1129
1130         RETURN(rc);
1131 }
1132
1133 static int mdd_maxeasize_get(const struct lu_env *env, struct md_device *m,
1134                                 int *easize)
1135 {
1136         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1137         ENTRY;
1138
1139         *easize = mdd->mdd_dt_conf.ddp_max_ea_size;
1140
1141         RETURN(0);
1142 }
1143
1144 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1145                              int idx, void **h)
1146 {
1147         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1148
1149         *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1150         return (*h == NULL ? -ENOENT : 0);
1151 }
1152
1153 static struct lu_device *mdd_device_free(const struct lu_env *env,
1154                                          struct lu_device *lu)
1155 {
1156         struct mdd_device *m = lu2mdd_dev(lu);
1157         ENTRY;
1158
1159         LASSERT(atomic_read(&lu->ld_ref) == 0);
1160         md_device_fini(&m->mdd_md_dev);
1161         OBD_FREE_PTR(m);
1162         RETURN(NULL);
1163 }
1164
1165 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1166                                           struct lu_device_type *t,
1167                                           struct lustre_cfg *lcfg)
1168 {
1169         struct lu_device  *l;
1170         struct mdd_device *m;
1171
1172         OBD_ALLOC_PTR(m);
1173         if (m == NULL) {
1174                 l = ERR_PTR(-ENOMEM);
1175         } else {
1176                 int rc;
1177
1178                 l = mdd2lu_dev(m);
1179                 md_device_init(&m->mdd_md_dev, t);
1180                 rc = mdd_init0(env, m, t, lcfg);
1181                 if (rc != 0) {
1182                         mdd_device_free(env, l);
1183                         l = ERR_PTR(rc);
1184                 }
1185         }
1186
1187         return l;
1188 }
1189
1190 /*
1191  * we use exports to track all mdd users
1192  */
1193 static int mdd_obd_connect(const struct lu_env *env, struct obd_export **exp,
1194                            struct obd_device *obd, struct obd_uuid *cluuid,
1195                            struct obd_connect_data *data, void *localdata)
1196 {
1197         struct mdd_device    *mdd = lu2mdd_dev(obd->obd_lu_dev);
1198         struct lustre_handle  conn;
1199         int                   rc;
1200         ENTRY;
1201
1202         CDEBUG(D_CONFIG, "connect #%d\n", mdd->mdd_connects);
1203
1204         rc = class_connect(&conn, obd, cluuid);
1205         if (rc)
1206                 RETURN(rc);
1207
1208         *exp = class_conn2export(&conn);
1209
1210         /* Why should there ever be more than 1 connect? */
1211         LASSERT(mdd->mdd_connects == 0);
1212         mdd->mdd_connects++;
1213
1214         RETURN(0);
1215 }
1216
1217 /*
1218  * once last export (we don't count self-export) disappeared
1219  * mdd can be released
1220  */
1221 static int mdd_obd_disconnect(struct obd_export *exp)
1222 {
1223         struct obd_device *obd = exp->exp_obd;
1224         struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1225         int                rc, release = 0;
1226         ENTRY;
1227
1228         mdd->mdd_connects--;
1229         if (mdd->mdd_connects == 0)
1230                 release = 1;
1231
1232         rc = class_disconnect(exp);
1233
1234         if (rc == 0 && release)
1235                 class_manual_cleanup(obd);
1236         RETURN(rc);
1237 }
1238
1239 static int mdd_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1240                             __u32 keylen, void *key, __u32 *vallen, void *val)
1241 {
1242         int rc = -EINVAL;
1243
1244         if (KEY_IS(KEY_OSP_CONNECTED)) {
1245                 struct obd_device       *obd = exp->exp_obd;
1246                 struct mdd_device       *mdd;
1247
1248                 if (!obd->obd_set_up || obd->obd_stopping)
1249                         RETURN(-EAGAIN);
1250
1251                 mdd = lu2mdd_dev(obd->obd_lu_dev);
1252                 LASSERT(mdd);
1253                 rc = obd_get_info(env, mdd->mdd_child_exp, keylen, key, vallen,
1254                                   val);
1255                 RETURN(rc);
1256         }
1257
1258         RETURN(rc);
1259 }
1260
1261 static int mdd_obd_set_info_async(const struct lu_env *env,
1262                                   struct obd_export *exp,
1263                                   __u32 keylen, void *key,
1264                                   __u32 vallen, void *val,
1265                                   struct ptlrpc_request_set *set)
1266 {
1267         struct obd_device       *obd = exp->exp_obd;
1268         struct mdd_device       *mdd;
1269         int                      rc;
1270
1271         if (!obd->obd_set_up || obd->obd_stopping)
1272                 RETURN(-EAGAIN);
1273
1274         mdd = lu2mdd_dev(obd->obd_lu_dev);
1275         LASSERT(mdd);
1276         rc = obd_set_info_async(env, mdd->mdd_child_exp, keylen, key,
1277                                 vallen, val, set);
1278         RETURN(rc);
1279 }
1280
1281 static struct obd_ops mdd_obd_device_ops = {
1282         .o_owner        = THIS_MODULE,
1283         .o_connect      = mdd_obd_connect,
1284         .o_disconnect   = mdd_obd_disconnect,
1285         .o_get_info     = mdd_obd_get_info,
1286         .o_set_info_async = mdd_obd_set_info_async,
1287 };
1288
1289 static int mdd_changelog_user_register(const struct lu_env *env,
1290                                        struct mdd_device *mdd, int *id)
1291 {
1292         struct llog_ctxt *ctxt;
1293         struct llog_changelog_user_rec *rec;
1294         int rc;
1295         ENTRY;
1296
1297         ctxt = llog_get_context(mdd2obd_dev(mdd),
1298                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1299         if (ctxt == NULL)
1300                 RETURN(-ENXIO);
1301
1302         OBD_ALLOC_PTR(rec);
1303         if (rec == NULL) {
1304                 llog_ctxt_put(ctxt);
1305                 RETURN(-ENOMEM);
1306         }
1307
1308         /* Assume we want it on since somebody registered */
1309         rc = mdd_changelog_on(env, mdd);
1310         if (rc)
1311                 GOTO(out, rc);
1312
1313         rec->cur_hdr.lrh_len = sizeof(*rec);
1314         rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1315         spin_lock(&mdd->mdd_cl.mc_user_lock);
1316         if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1317                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1318                 CERROR("Maximum number of changelog users exceeded!\n");
1319                 GOTO(out, rc = -EOVERFLOW);
1320         }
1321         *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1322         rec->cur_endrec = mdd->mdd_cl.mc_index;
1323         spin_unlock(&mdd->mdd_cl.mc_user_lock);
1324
1325         rc = llog_cat_add(env, ctxt->loc_handle, &rec->cur_hdr, NULL);
1326
1327         CDEBUG(D_IOCTL, "Registered changelog user %d\n", *id);
1328 out:
1329         OBD_FREE_PTR(rec);
1330         llog_ctxt_put(ctxt);
1331         RETURN(rc);
1332 }
1333
1334 struct mdd_changelog_user_data {
1335         __u64 mcud_endrec; /**< purge record for this user */
1336         __u64 mcud_minrec; /**< lowest changelog recno still referenced */
1337         __u32 mcud_id;
1338         __u32 mcud_minid;  /**< user id with lowest rec reference */
1339         __u32 mcud_usercount;
1340         unsigned int mcud_found:1;
1341 };
1342 #define MCUD_UNREGISTER -1LL
1343
1344 /** Two things:
1345  * 1. Find the smallest record everyone is willing to purge
1346  * 2. Update the last purgeable record for this user
1347  */
1348 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1349                                        struct llog_handle *llh,
1350                                        struct llog_rec_hdr *hdr, void *data)
1351 {
1352         struct llog_changelog_user_rec  *rec;
1353         struct mdd_changelog_user_data  *mcud = data;
1354         int                              rc;
1355
1356         ENTRY;
1357
1358         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
1359
1360         rec = (struct llog_changelog_user_rec *)hdr;
1361
1362         mcud->mcud_usercount++;
1363
1364         /* If we have a new endrec for this id, use it for the following
1365            min check instead of its old value */
1366         if (rec->cur_id == mcud->mcud_id)
1367                 rec->cur_endrec = max(rec->cur_endrec, mcud->mcud_endrec);
1368
1369         /* Track the minimum referenced record */
1370         if (mcud->mcud_minid == 0 || mcud->mcud_minrec > rec->cur_endrec) {
1371                 mcud->mcud_minid = rec->cur_id;
1372                 mcud->mcud_minrec = rec->cur_endrec;
1373         }
1374
1375         if (rec->cur_id != mcud->mcud_id)
1376                 RETURN(0);
1377
1378         /* Update this user's record */
1379         mcud->mcud_found = 1;
1380
1381         /* Special case: unregister this user */
1382         if (mcud->mcud_endrec == MCUD_UNREGISTER) {
1383                 struct llog_cookie cookie;
1384
1385                 cookie.lgc_lgl = llh->lgh_id;
1386                 cookie.lgc_index = hdr->lrh_index;
1387
1388                 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1389                                              1, &cookie);
1390                 if (rc == 0)
1391                         mcud->mcud_usercount--;
1392
1393                 RETURN(rc);
1394         }
1395
1396         /* Update the endrec */
1397         CDEBUG(D_IOCTL, "Rewriting changelog user %d endrec to %llu\n",
1398                mcud->mcud_id, rec->cur_endrec);
1399
1400         rc = llog_write(env, llh, hdr, hdr->lrh_index);
1401
1402         RETURN(rc);
1403 }
1404
1405 static int mdd_changelog_user_purge(const struct lu_env *env,
1406                                     struct mdd_device *mdd, int id,
1407                                     long long endrec)
1408 {
1409         struct mdd_changelog_user_data data;
1410         struct llog_ctxt *ctxt;
1411         int rc;
1412         ENTRY;
1413
1414         CDEBUG(D_IOCTL, "Purge request: id=%d, endrec=%lld\n", id, endrec);
1415
1416         data.mcud_id = id;
1417         data.mcud_minid = 0;
1418         data.mcud_minrec = 0;
1419         data.mcud_usercount = 0;
1420         data.mcud_endrec = endrec;
1421         spin_lock(&mdd->mdd_cl.mc_lock);
1422         endrec = mdd->mdd_cl.mc_index;
1423         spin_unlock(&mdd->mdd_cl.mc_lock);
1424         if ((data.mcud_endrec == 0) ||
1425             ((data.mcud_endrec > endrec) &&
1426              (data.mcud_endrec != MCUD_UNREGISTER)))
1427                 data.mcud_endrec = endrec;
1428
1429         ctxt = llog_get_context(mdd2obd_dev(mdd),
1430                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1431         if (ctxt == NULL)
1432                 return -ENXIO;
1433
1434         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
1435
1436         rc = llog_cat_process(env, ctxt->loc_handle,
1437                               mdd_changelog_user_purge_cb, (void *)&data,
1438                               0, 0);
1439         if ((rc >= 0) && (data.mcud_minrec > 0)) {
1440                 CDEBUG(D_IOCTL, "Purging changelog entries up to %lld"
1441                        ", referenced by "CHANGELOG_USER_PREFIX"%d\n",
1442                        data.mcud_minrec, data.mcud_minid);
1443                 rc = mdd_changelog_llog_cancel(env, mdd, data.mcud_minrec);
1444         } else {
1445                 CWARN("Could not determine changelog records to purge; rc=%d\n",
1446                       rc);
1447         }
1448
1449         llog_ctxt_put(ctxt);
1450
1451         if (!data.mcud_found) {
1452                 CWARN("No entry for user %d.  Last changelog reference is "
1453                       "%lld by changelog user %d\n", data.mcud_id,
1454                       data.mcud_minrec, data.mcud_minid);
1455                rc = -ENOENT;
1456         }
1457
1458         if (!rc && data.mcud_usercount == 0)
1459                 /* No more users; turn changelogs off */
1460                 rc = mdd_changelog_off(env, mdd);
1461
1462         RETURN(rc);
1463 }
1464
1465 /** mdd_iocontrol
1466  * May be called remotely from mdt_iocontrol_handle or locally from
1467  * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
1468  * an obd_ioctl_data format (but local ioctl handler does).
1469  * \param cmd - ioc
1470  * \param len - data len
1471  * \param karg - ioctl data, in kernel space
1472  */
1473 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
1474                          unsigned int cmd, int len, void *karg)
1475 {
1476         struct mdd_device *mdd;
1477         struct obd_ioctl_data *data = karg;
1478         int rc;
1479         ENTRY;
1480
1481         mdd = lu2mdd_dev(&m->md_lu_dev);
1482
1483         /* Doesn't use obd_ioctl_data */
1484         switch (cmd) {
1485         case OBD_IOC_CHANGELOG_CLEAR: {
1486                 struct changelog_setinfo *cs = karg;
1487                 rc = mdd_changelog_user_purge(env, mdd, cs->cs_id,
1488                                               cs->cs_recno);
1489                 RETURN(rc);
1490         }
1491         case OBD_IOC_GET_MNTOPT: {
1492                 mntopt_t *mntopts = (mntopt_t *)karg;
1493                 *mntopts = mdd->mdd_dt_conf.ddp_mntopts;
1494                 RETURN(0);
1495         }
1496         case OBD_IOC_START_LFSCK: {
1497                 rc = lfsck_start(env, mdd->mdd_bottom,
1498                                  (struct lfsck_start_param *)karg);
1499                 RETURN(rc);
1500         }
1501         case OBD_IOC_STOP_LFSCK: {
1502                 rc = lfsck_stop(env, mdd->mdd_bottom,
1503                                 (struct lfsck_stop *)karg);
1504                 RETURN(rc);
1505         }
1506         case OBD_IOC_QUERY_LFSCK: {
1507                 rc = lfsck_query(env, mdd->mdd_bottom, NULL, NULL,
1508                                  (struct lfsck_query *)karg);
1509                 RETURN(rc);
1510         }
1511         }
1512
1513         /* Below ioctls use obd_ioctl_data */
1514         if (len != sizeof(*data)) {
1515                 CERROR("Bad ioctl size %d\n", len);
1516                 RETURN(-EINVAL);
1517         }
1518         if (data->ioc_version != OBD_IOCTL_VERSION) {
1519                 CERROR("Bad magic %x != %x\n", data->ioc_version,
1520                        OBD_IOCTL_VERSION);
1521                 RETURN(-EINVAL);
1522         }
1523
1524         switch (cmd) {
1525         case OBD_IOC_CHANGELOG_REG:
1526                 rc = mdd_changelog_user_register(env, mdd, &data->ioc_u32_1);
1527                 break;
1528         case OBD_IOC_CHANGELOG_DEREG:
1529                 rc = mdd_changelog_user_purge(env, mdd, data->ioc_u32_1,
1530                                               MCUD_UNREGISTER);
1531                 break;
1532         default:
1533                 rc = -ENOTTY;
1534         }
1535
1536         RETURN (rc);
1537 }
1538
1539 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
1540 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key);
1541
1542 static const struct md_device_operations mdd_ops = {
1543         .mdo_statfs         = mdd_statfs,
1544         .mdo_root_get       = mdd_root_get,
1545         .mdo_llog_ctxt_get  = mdd_llog_ctxt_get,
1546         .mdo_iocontrol      = mdd_iocontrol,
1547         .mdo_maxeasize_get  = mdd_maxeasize_get,
1548 };
1549
1550 static struct lu_device_type_operations mdd_device_type_ops = {
1551         .ldto_init = mdd_type_init,
1552         .ldto_fini = mdd_type_fini,
1553
1554         .ldto_start = mdd_type_start,
1555         .ldto_stop  = mdd_type_stop,
1556
1557         .ldto_device_alloc = mdd_device_alloc,
1558         .ldto_device_free  = mdd_device_free,
1559
1560         .ldto_device_fini    = mdd_device_fini
1561 };
1562
1563 static struct lu_device_type mdd_device_type = {
1564         .ldt_tags     = LU_DEVICE_MD,
1565         .ldt_name     = LUSTRE_MDD_NAME,
1566         .ldt_ops      = &mdd_device_type_ops,
1567         .ldt_ctx_tags = LCT_MD_THREAD
1568 };
1569
1570 /* context key constructor: mdd_key_init */
1571 LU_KEY_INIT(mdd, struct mdd_thread_info);
1572
1573 static void mdd_key_fini(const struct lu_context *ctx,
1574                          struct lu_context_key *key, void *data)
1575 {
1576         struct mdd_thread_info *info = data;
1577
1578         lu_buf_free(&info->mti_big_buf);
1579         lu_buf_free(&info->mti_link_buf);
1580
1581         OBD_FREE_PTR(info);
1582 }
1583
1584 /* context key: mdd_thread_key */
1585 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
1586
1587 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
1588                              int (*func)(void *), void *data, char *name)
1589 {
1590         struct task_struct      *task;
1591
1592         LASSERT(thread->mgt_init == false);
1593         init_completion(&thread->mgt_started);
1594         init_completion(&thread->mgt_finished);
1595         thread->mgt_data = data;
1596         thread->mgt_abort = false;
1597         thread->mgt_init = true;
1598
1599         task = kthread_run(func, thread, name);
1600         if (IS_ERR(task)) {
1601                 complete(&thread->mgt_finished);
1602                 return PTR_ERR(task);
1603         }
1604         wait_for_completion(&thread->mgt_started);
1605         return 0;
1606 }
1607
1608 void mdd_generic_thread_stop(struct mdd_generic_thread *thread)
1609 {
1610         if (thread->mgt_init == true) {
1611                 thread->mgt_abort = true;
1612                 wait_for_completion(&thread->mgt_finished);
1613         }
1614 }
1615
1616 static int __init mdd_init(void)
1617 {
1618         int rc;
1619
1620         rc = lu_kmem_init(mdd_caches);
1621         if (rc)
1622                 return rc;
1623
1624         changelog_orig_logops = llog_osd_ops;
1625         changelog_orig_logops.lop_cancel = llog_changelog_cancel;
1626         changelog_orig_logops.lop_add = llog_cat_add_rec;
1627         changelog_orig_logops.lop_declare_add = llog_cat_declare_add_rec;
1628
1629         hsm_actions_logops = llog_osd_ops;
1630         hsm_actions_logops.lop_add = llog_cat_add_rec;
1631         hsm_actions_logops.lop_declare_add = llog_cat_declare_add_rec;
1632
1633         rc = class_register_type(&mdd_obd_device_ops, NULL, true, NULL,
1634                                  LUSTRE_MDD_NAME, &mdd_device_type);
1635         if (rc)
1636                 lu_kmem_fini(mdd_caches);
1637         return rc;
1638 }
1639
1640 static void __exit mdd_exit(void)
1641 {
1642         class_unregister_type(LUSTRE_MDD_NAME);
1643         lu_kmem_fini(mdd_caches);
1644 }
1645
1646 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1647 MODULE_DESCRIPTION("Lustre Meta-data Device Driver ("LUSTRE_MDD_NAME")");
1648 MODULE_VERSION(LUSTRE_VERSION_STRING);
1649 MODULE_LICENSE("GPL");
1650
1651 module_init(mdd_init);
1652 module_exit(mdd_exit);