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