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