Whamcloud - gitweb
LU-7724 mdd: create mdd_changelog_on and _off functions
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdd/mdd_device.c
37  *
38  * Lustre Metadata Server (mdd) routines
39  *
40  * Author: Wang Di <wangdi@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <linux/module.h>
46 #include <linux/kthread.h>
47 #include <obd_class.h>
48 #include <lustre_ioctl.h>
49 #include <lustre_mds.h>
50 #include <obd_support.h>
51 #include <lu_object.h>
52 #include <lustre_param.h>
53 #include <lustre_fid.h>
54
55 #include "mdd_internal.h"
56
57 static const struct md_device_operations mdd_ops;
58 static struct lu_device_type mdd_device_type;
59
60 static const char mdd_root_dir_name[] = "ROOT";
61 static const char mdd_obf_dir_name[] = "fid";
62 static const char mdd_lpf_dir_name[] = "lost+found";
63
64 /* Slab for MDD object allocation */
65 struct kmem_cache *mdd_object_kmem;
66
67 static struct lu_kmem_descr mdd_caches[] = {
68         {
69                 .ckd_cache = &mdd_object_kmem,
70                 .ckd_name  = "mdd_obj",
71                 .ckd_size  = sizeof(struct mdd_object)
72         },
73         {
74                 .ckd_cache = NULL
75         }
76 };
77
78 static int mdd_connect_to_next(const struct lu_env *env, struct mdd_device *m,
79                                const char *nextdev)
80 {
81         struct obd_connect_data *data = NULL;
82         struct lu_device        *lud = mdd2lu_dev(m);
83         struct obd_device       *obd;
84         int                      rc;
85         ENTRY;
86
87         LASSERT(m->mdd_child_exp == NULL);
88
89         OBD_ALLOC(data, sizeof(*data));
90         if (data == NULL)
91                 GOTO(out, rc = -ENOMEM);
92
93         obd = class_name2obd(nextdev);
94         if (obd == NULL) {
95                 CERROR("can't locate next device: %s\n", nextdev);
96                 GOTO(out, rc = -ENOTCONN);
97         }
98
99         data->ocd_connect_flags = OBD_CONNECT_VERSION;
100         data->ocd_version = LUSTRE_VERSION_CODE;
101
102         rc = obd_connect(NULL, &m->mdd_child_exp, obd, &obd->obd_uuid, data, NULL);
103         if (rc) {
104                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
105                 GOTO(out, rc);
106         }
107
108         lud->ld_site = m->mdd_child_exp->exp_obd->obd_lu_dev->ld_site;
109         LASSERT(lud->ld_site);
110         m->mdd_child = lu2dt_dev(m->mdd_child_exp->exp_obd->obd_lu_dev);
111         m->mdd_bottom = lu2dt_dev(lud->ld_site->ls_bottom_dev);
112         lu_dev_add_linkage(lud->ld_site, lud);
113
114 out:
115         if (data)
116                 OBD_FREE(data, sizeof(*data));
117         RETURN(rc);
118 }
119
120 static int mdd_init0(const struct lu_env *env, struct mdd_device *mdd,
121                 struct lu_device_type *t, struct lustre_cfg *lcfg)
122 {
123         int rc = -EINVAL;
124         const char *dev;
125         ENTRY;
126
127         dev = lustre_cfg_string(lcfg, 0);
128         if (dev == NULL)
129                 RETURN(rc);
130
131         mdd->mdd_md_dev.md_lu_dev.ld_obd = class_name2obd(dev);
132         if (mdd->mdd_md_dev.md_lu_dev.ld_obd == NULL)
133                 RETURN(rc);
134         mdd->mdd_md_dev.md_lu_dev.ld_ops = &mdd_lu_ops;
135         mdd->mdd_md_dev.md_ops = &mdd_ops;
136
137         rc = mdd_connect_to_next(env, mdd, lustre_cfg_string(lcfg, 3));
138         if (rc != 0)
139                 RETURN(rc);
140
141         mdd->mdd_atime_diff = MAX_ATIME_DIFF;
142         /* sync permission changes */
143         mdd->mdd_sync_permission = 1;
144
145         dt_conf_get(env, mdd->mdd_child, &mdd->mdd_dt_conf);
146
147         /* we are using service name but not mdd obd name
148          * for compatibility reasons.
149          * It is passed from MDT in lustre_cfg[2] buffer */
150         rc = mdd_procfs_init(mdd, lustre_cfg_string(lcfg, 2));
151         if (rc < 0)
152                 obd_disconnect(mdd->mdd_child_exp);
153
154         RETURN(rc);
155 }
156
157 static struct lu_device *mdd_device_fini(const struct lu_env *env,
158                                          struct lu_device *d)
159 {
160         struct mdd_device *mdd = lu2mdd_dev(d);
161
162         if (d->ld_site)
163                 lu_dev_del_linkage(d->ld_site, d);
164
165         mdd_procfs_fini(mdd);
166         return NULL;
167 }
168
169 static int changelog_init_cb(const struct lu_env *env, struct llog_handle *llh,
170                              struct llog_rec_hdr *hdr, void *data)
171 {
172         struct mdd_device *mdd = (struct mdd_device *)data;
173         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
174
175         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
176         LASSERT(rec->cr_hdr.lrh_type == CHANGELOG_REC);
177
178         CDEBUG(D_INFO,
179                "seeing record at index %d/%d/"LPU64" t=%x %.*s in log"
180                DOSTID"\n", hdr->lrh_index, rec->cr_hdr.lrh_index,
181                rec->cr.cr_index, rec->cr.cr_type, rec->cr.cr_namelen,
182                changelog_rec_name(&rec->cr), POSTID(&llh->lgh_id.lgl_oi));
183
184         mdd->mdd_cl.mc_index = rec->cr.cr_index;
185         return LLOG_PROC_BREAK;
186 }
187
188 static int changelog_user_init_cb(const struct lu_env *env,
189                                   struct llog_handle *llh,
190                                   struct llog_rec_hdr *hdr, void *data)
191 {
192         struct mdd_device *mdd = (struct mdd_device *)data;
193         struct llog_changelog_user_rec *rec =
194                 (struct llog_changelog_user_rec *)hdr;
195
196         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
197         LASSERT(rec->cur_hdr.lrh_type == CHANGELOG_USER_REC);
198
199         CDEBUG(D_INFO, "seeing user at index %d/%d id=%d endrec="LPU64
200                " in log "DOSTID"\n", hdr->lrh_index, rec->cur_hdr.lrh_index,
201                rec->cur_id, rec->cur_endrec, POSTID(&llh->lgh_id.lgl_oi));
202
203         spin_lock(&mdd->mdd_cl.mc_user_lock);
204         mdd->mdd_cl.mc_lastuser = rec->cur_id;
205         if (rec->cur_endrec > mdd->mdd_cl.mc_index)
206                 mdd->mdd_cl.mc_index = rec->cur_endrec;
207         spin_unlock(&mdd->mdd_cl.mc_user_lock);
208
209         return LLOG_PROC_BREAK;
210 }
211
212 static int llog_changelog_cancel_cb(const struct lu_env *env,
213                                     struct llog_handle *llh,
214                                     struct llog_rec_hdr *hdr, void *data)
215 {
216         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
217         struct llog_cookie       cookie;
218         long long                endrec = *(long long *)data;
219         int                      rc;
220
221         ENTRY;
222
223         /* This is always a (sub)log, not the catalog */
224         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
225
226         if (rec->cr.cr_index > endrec)
227                 /* records are in order, so we're done */
228                 RETURN(LLOG_PROC_BREAK);
229
230         cookie.lgc_lgl = llh->lgh_id;
231         cookie.lgc_index = hdr->lrh_index;
232
233         /* cancel them one at a time.  I suppose we could store up the cookies
234          * and cancel them all at once; probably more efficient, but this is
235          * done as a user call, so who cares... */
236         rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle, 1,
237                                      &cookie);
238         RETURN(rc < 0 ? rc : 0);
239 }
240
241 static int llog_changelog_cancel(const struct lu_env *env,
242                                  struct llog_ctxt *ctxt,
243                                  struct llog_cookie *cookies, int flags)
244 {
245         struct llog_handle      *cathandle = ctxt->loc_handle;
246         int                      rc;
247
248         ENTRY;
249
250         /* This should only be called with the catalog handle */
251         LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
252
253         rc = llog_cat_process(env, cathandle, llog_changelog_cancel_cb,
254                               (void *)cookies, 0, 0);
255         if (rc >= 0)
256                 /* 0 or 1 means we're done */
257                 rc = 0;
258         else
259                 CERROR("%s: cancel idx %u of catalog "DOSTID" rc=%d\n",
260                        ctxt->loc_obd->obd_name, cathandle->lgh_last_idx,
261                        POSTID(&cathandle->lgh_id.lgl_oi), rc);
262
263         RETURN(rc);
264 }
265
266 static struct llog_operations changelog_orig_logops;
267
268 static int
269 mdd_changelog_write_header(const struct lu_env *env, struct mdd_device *mdd,
270                            int markerflags);
271
272 static int
273 mdd_changelog_on(const struct lu_env *env, struct mdd_device *mdd)
274 {
275         int rc = 0;
276
277         if ((mdd->mdd_cl.mc_flags & CLM_ON) != 0)
278                 return rc;
279
280         LCONSOLE_INFO("%s: changelog on\n", mdd2obd_dev(mdd)->obd_name);
281         if (mdd->mdd_cl.mc_flags & CLM_ERR) {
282                 CERROR("Changelogs cannot be enabled due to error "
283                        "condition (see %s log).\n",
284                        mdd2obd_dev(mdd)->obd_name);
285                 rc = -ESRCH;
286         } else {
287                 spin_lock(&mdd->mdd_cl.mc_lock);
288                 mdd->mdd_cl.mc_flags |= CLM_ON;
289                 spin_unlock(&mdd->mdd_cl.mc_lock);
290                 rc = mdd_changelog_write_header(env, mdd, CLM_START);
291         }
292         return rc;
293 }
294
295 static int
296 mdd_changelog_off(const struct lu_env *env, struct mdd_device *mdd)
297 {
298         int rc = 0;
299
300         if ((mdd->mdd_cl.mc_flags & CLM_ON) != CLM_ON)
301                 return rc;
302
303         LCONSOLE_INFO("%s: changelog off\n", mdd2obd_dev(mdd)->obd_name);
304         rc = mdd_changelog_write_header(env, mdd, CLM_FINI);
305         spin_lock(&mdd->mdd_cl.mc_lock);
306         mdd->mdd_cl.mc_flags &= ~CLM_ON;
307         spin_unlock(&mdd->mdd_cl.mc_lock);
308
309         return rc;
310 }
311
312 static int mdd_changelog_llog_init(const struct lu_env *env,
313                                    struct mdd_device *mdd)
314 {
315         struct obd_device       *obd = mdd2obd_dev(mdd);
316         struct llog_ctxt        *ctxt = NULL, *uctxt = NULL;
317         int                      rc;
318
319         ENTRY;
320
321         /* LU-2844 mdd setup failure should not cause umount oops */
322         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CHANGELOG_INIT))
323                 RETURN(-EIO);
324
325         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
326         obd->obd_lvfs_ctxt.dt = mdd->mdd_bottom;
327         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
328                         obd, &changelog_orig_logops);
329         if (rc) {
330                 CERROR("%s: changelog llog setup failed: rc = %d\n",
331                        obd->obd_name, rc);
332                 RETURN(rc);
333         }
334
335         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
336         LASSERT(ctxt);
337
338         rc = llog_open_create(env, ctxt, &ctxt->loc_handle, NULL,
339                               CHANGELOG_CATALOG);
340         if (rc)
341                 GOTO(out_cleanup, rc);
342
343         rc = llog_cat_init_and_process(env, ctxt->loc_handle);
344         if (rc)
345                 GOTO(out_close, rc);
346
347         rc = llog_cat_reverse_process(env, ctxt->loc_handle,
348                                       changelog_init_cb, mdd);
349
350         if (rc < 0) {
351                 CERROR("%s: changelog init failed: rc = %d\n", obd->obd_name,
352                        rc);
353                 GOTO(out_close, rc);
354         }
355
356         CDEBUG(D_IOCTL, "changelog starting index="LPU64"\n",
357                mdd->mdd_cl.mc_index);
358
359         /* setup user changelog */
360         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
361                         obd, &changelog_orig_logops);
362         if (rc) {
363                 CERROR("%s: changelog users llog setup failed: rc = %d\n",
364                        obd->obd_name, rc);
365                 GOTO(out_close, rc);
366         }
367
368         uctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
369         LASSERT(ctxt);
370
371         rc = llog_open_create(env, uctxt, &uctxt->loc_handle, NULL,
372                               CHANGELOG_USERS);
373         if (rc)
374                 GOTO(out_ucleanup, rc);
375
376         uctxt->loc_handle->lgh_logops->lop_add = llog_cat_add_rec;
377         uctxt->loc_handle->lgh_logops->lop_declare_add = llog_cat_declare_add_rec;
378
379         rc = llog_cat_init_and_process(env, uctxt->loc_handle);
380         if (rc)
381                 GOTO(out_uclose, rc);
382
383         rc = llog_cat_reverse_process(env, uctxt->loc_handle,
384                                       changelog_user_init_cb, mdd);
385         if (rc < 0) {
386                 CERROR("%s: changelog user init failed: rc = %d\n",
387                        obd->obd_name, rc);
388                 GOTO(out_uclose, rc);
389         }
390
391         /* If we have registered users, assume we want changelogs on */
392         if (mdd->mdd_cl.mc_lastuser > 0) {
393                 rc = mdd_changelog_on(env, mdd);
394                 if (rc < 0)
395                         GOTO(out_uclose, rc);
396         }
397         llog_ctxt_put(ctxt);
398         llog_ctxt_put(uctxt);
399         RETURN(0);
400 out_uclose:
401         llog_cat_close(env, uctxt->loc_handle);
402 out_ucleanup:
403         llog_cleanup(env, uctxt);
404 out_close:
405         llog_cat_close(env, ctxt->loc_handle);
406 out_cleanup:
407         llog_cleanup(env, ctxt);
408         return rc;
409 }
410
411 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
412 {
413         struct obd_device       *obd = mdd2obd_dev(mdd);
414         int                      rc;
415
416         mdd->mdd_cl.mc_index = 0;
417         spin_lock_init(&mdd->mdd_cl.mc_lock);
418         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
419         mdd->mdd_cl.mc_flags = 0; /* off by default */
420         mdd->mdd_cl.mc_mask = CHANGELOG_DEFMASK;
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 >= "LPX64" or within ["LPX64","
590                       ""LPX64"].\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_cat_init_and_process(env, ctxt->loc_handle);
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         mdd_generic_thread_stop(&m->mdd_orph_cleanup_thread);
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 (m->mdd_los != NULL) {
885                 local_oid_storage_fini(env, m->mdd_los);
886                 m->mdd_los = NULL;
887         }
888         lu_site_purge(env, mdd2lu_dev(m)->ld_site, ~0);
889
890         if (m->mdd_child_exp)
891                 obd_disconnect(m->mdd_child_exp);
892 }
893
894 static int mdd_process_config(const struct lu_env *env,
895                               struct lu_device *d, struct lustre_cfg *cfg)
896 {
897         struct mdd_device *m    = lu2mdd_dev(d);
898         struct dt_device  *dt   = m->mdd_child;
899         struct lu_device  *next = &dt->dd_lu_dev;
900         int rc;
901         ENTRY;
902
903         switch (cfg->lcfg_command) {
904         case LCFG_PARAM: {
905                 struct obd_device *obd = mdd2obd_dev(m);
906
907                 rc = class_process_proc_param(PARAM_MDD, obd->obd_vars, cfg, m);
908                 if (rc > 0 || rc == -ENOSYS)
909                         /* we don't understand; pass it on */
910                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
911                 break;
912         }
913         case LCFG_SETUP:
914                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
915                 if (rc)
916                         GOTO(out, rc);
917                 dt_conf_get(env, dt, &m->mdd_dt_conf);
918                 break;
919         case LCFG_CLEANUP:
920                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
921                 lu_dev_del_linkage(d->ld_site, d);
922                 mdd_device_shutdown(env, m, cfg);
923                 break;
924         default:
925                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
926                 break;
927         }
928 out:
929         RETURN(rc);
930 }
931
932 static int mdd_recovery_complete(const struct lu_env *env,
933                                  struct lu_device *d)
934 {
935         struct mdd_device *mdd = lu2mdd_dev(d);
936         struct lu_device *next;
937         int rc;
938         ENTRY;
939
940         LASSERT(mdd != NULL);
941         next = &mdd->mdd_child->dd_lu_dev;
942
943         /* XXX: orphans handling. */
944         mdd_orphan_cleanup(env, mdd);
945         rc = next->ld_ops->ldo_recovery_complete(env, next);
946
947         RETURN(rc);
948 }
949
950 int mdd_local_file_create(const struct lu_env *env, struct mdd_device *mdd,
951                           const struct lu_fid *pfid, const char *name,
952                           __u32 mode, struct lu_fid *fid)
953 {
954         struct dt_object *parent, *dto;
955         int rc;
956
957         ENTRY;
958
959         LASSERT(!fid_is_zero(pfid));
960         parent = dt_locate(env, mdd->mdd_bottom, pfid);
961         if (unlikely(IS_ERR(parent)))
962                 RETURN(PTR_ERR(parent));
963
964         /* create local file/dir, if @fid is passed then try to use it */
965         if (fid_is_zero(fid))
966                 dto = local_file_find_or_create(env, mdd->mdd_los, parent,
967                                                 name, mode);
968         else
969                 dto = local_file_find_or_create_with_fid(env, mdd->mdd_bottom,
970                                                          fid, parent, name,
971                                                          mode);
972         if (IS_ERR(dto))
973                 GOTO(out_put, rc = PTR_ERR(dto));
974         *fid = *lu_object_fid(&dto->do_lu);
975         /* since stack is not fully set up the local_storage uses own stack
976          * and we should drop its object from cache */
977         lu_object_put_nocache(env, &dto->do_lu);
978         EXIT;
979 out_put:
980         lu_object_put(env, &parent->do_lu);
981         return 0;
982 }
983
984 static int mdd_lfsck_out_notify(const struct lu_env *env, void *data,
985                                 enum lfsck_events event)
986 {
987         return 0;
988 }
989
990 static int mdd_prepare(const struct lu_env *env,
991                        struct lu_device *pdev,
992                        struct lu_device *cdev)
993 {
994         struct mdd_device       *mdd = lu2mdd_dev(cdev);
995         struct lu_device        *next = &mdd->mdd_child->dd_lu_dev;
996         struct lu_fid            fid;
997         int                      rc;
998
999         ENTRY;
1000
1001         rc = next->ld_ops->ldo_prepare(env, cdev, next);
1002         if (rc)
1003                 RETURN(rc);
1004
1005         /* Setup local dirs */
1006         fid.f_seq = FID_SEQ_LOCAL_NAME;
1007         fid.f_oid = 1;
1008         fid.f_ver = 0;
1009         rc = local_oid_storage_init(env, mdd->mdd_bottom, &fid,
1010                                     &mdd->mdd_los);
1011         if (rc)
1012                 RETURN(rc);
1013
1014         rc = dt_root_get(env, mdd->mdd_child, &mdd->mdd_local_root_fid);
1015         if (rc < 0)
1016                 GOTO(out_los, rc);
1017
1018         lu_root_fid(&fid);
1019         if (mdd_seq_site(mdd)->ss_node_id == 0) {
1020                 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
1021                                            mdd_root_dir_name, S_IFDIR |
1022                                            S_IRUGO | S_IWUSR | S_IXUGO, &fid);
1023                 if (rc != 0) {
1024                         CERROR("%s: create root fid failed: rc = %d\n",
1025                                mdd2obd_dev(mdd)->obd_name, rc);
1026                         GOTO(out_los, rc);
1027                 }
1028
1029                 mdd->mdd_root_fid = fid;
1030                 rc = mdd_dot_lustre_setup(env, mdd);
1031                 if (rc != 0) {
1032                         CERROR("%s: initializing .lustre failed: rc = %d\n",
1033                                mdd2obd_dev(mdd)->obd_name, rc);
1034                         GOTO(out_los, rc);
1035                 }
1036         } else {
1037                 /* Normal client usually send root access to MDT0 directly,
1038                  * the root FID on non-MDT0 will only be used by echo client. */
1039                 mdd->mdd_root_fid = fid;
1040         }
1041
1042         rc = orph_index_init(env, mdd);
1043         if (rc < 0)
1044                 GOTO(out_dot, rc);
1045
1046         rc = mdd_changelog_init(env, mdd);
1047         if (rc != 0) {
1048                 CERROR("%s: failed to initialize changelog: rc = %d\n",
1049                        mdd2obd_dev(mdd)->obd_name, rc);
1050                 GOTO(out_orph, rc);
1051         }
1052
1053         rc = mdd_hsm_actions_llog_init(env, mdd);
1054         if (rc != 0)
1055                 GOTO(out_changelog, rc);
1056
1057         rc = lfsck_register(env, mdd->mdd_bottom, mdd->mdd_child,
1058                             mdd2obd_dev(mdd), mdd_lfsck_out_notify,
1059                             mdd, true);
1060         if (rc != 0) {
1061                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
1062                        mdd2obd_dev(mdd)->obd_name, rc);
1063                 GOTO(out_hsm, rc);
1064         }
1065         RETURN(0);
1066 out_hsm:
1067         mdd_hsm_actions_llog_fini(env, mdd);
1068 out_changelog:
1069         mdd_changelog_fini(env, mdd);
1070 out_orph:
1071         orph_index_fini(env, mdd);
1072 out_dot:
1073         if (mdd_seq_site(mdd)->ss_node_id == 0)
1074                 mdd_dot_lustre_cleanup(env, mdd);
1075 out_los:
1076         local_oid_storage_fini(env, mdd->mdd_los);
1077         mdd->mdd_los = NULL;
1078
1079         return rc;
1080 }
1081
1082 const struct lu_device_operations mdd_lu_ops = {
1083         .ldo_object_alloc      = mdd_object_alloc,
1084         .ldo_process_config    = mdd_process_config,
1085         .ldo_recovery_complete = mdd_recovery_complete,
1086         .ldo_prepare           = mdd_prepare,
1087 };
1088
1089 static int mdd_root_get(const struct lu_env *env,
1090                         struct md_device *m, struct lu_fid *f)
1091 {
1092         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1093
1094         ENTRY;
1095         *f = mdd->mdd_root_fid;
1096         RETURN(0);
1097 }
1098
1099 /*
1100  * No permission check is needed.
1101  */
1102 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
1103                       struct obd_statfs *sfs)
1104 {
1105         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1106         int rc;
1107
1108         ENTRY;
1109
1110         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
1111
1112         sfs->os_namelen = min_t(__u32, sfs->os_namelen, NAME_MAX);
1113
1114         RETURN(rc);
1115 }
1116
1117 static int mdd_maxeasize_get(const struct lu_env *env, struct md_device *m,
1118                                 int *easize)
1119 {
1120         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1121         ENTRY;
1122
1123         *easize = mdd->mdd_dt_conf.ddp_max_ea_size;
1124
1125         RETURN(0);
1126 }
1127
1128 static int mdd_llog_ctxt_get(const struct lu_env *env, struct md_device *m,
1129                              int idx, void **h)
1130 {
1131         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
1132
1133         *h = llog_group_get_ctxt(&mdd2obd_dev(mdd)->obd_olg, idx);
1134         return (*h == NULL ? -ENOENT : 0);
1135 }
1136
1137 static struct lu_device *mdd_device_free(const struct lu_env *env,
1138                                          struct lu_device *lu)
1139 {
1140         struct mdd_device *m = lu2mdd_dev(lu);
1141         ENTRY;
1142
1143         LASSERT(atomic_read(&lu->ld_ref) == 0);
1144         md_device_fini(&m->mdd_md_dev);
1145         OBD_FREE_PTR(m);
1146         RETURN(NULL);
1147 }
1148
1149 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
1150                                           struct lu_device_type *t,
1151                                           struct lustre_cfg *lcfg)
1152 {
1153         struct lu_device  *l;
1154         struct mdd_device *m;
1155
1156         OBD_ALLOC_PTR(m);
1157         if (m == NULL) {
1158                 l = ERR_PTR(-ENOMEM);
1159         } else {
1160                 int rc;
1161
1162                 l = mdd2lu_dev(m);
1163                 md_device_init(&m->mdd_md_dev, t);
1164                 rc = mdd_init0(env, m, t, lcfg);
1165                 if (rc != 0) {
1166                         mdd_device_free(env, l);
1167                         l = ERR_PTR(rc);
1168                 }
1169         }
1170
1171         return l;
1172 }
1173
1174 /*
1175  * we use exports to track all mdd users
1176  */
1177 static int mdd_obd_connect(const struct lu_env *env, struct obd_export **exp,
1178                            struct obd_device *obd, struct obd_uuid *cluuid,
1179                            struct obd_connect_data *data, void *localdata)
1180 {
1181         struct mdd_device    *mdd = lu2mdd_dev(obd->obd_lu_dev);
1182         struct lustre_handle  conn;
1183         int                   rc;
1184         ENTRY;
1185
1186         CDEBUG(D_CONFIG, "connect #%d\n", mdd->mdd_connects);
1187
1188         rc = class_connect(&conn, obd, cluuid);
1189         if (rc)
1190                 RETURN(rc);
1191
1192         *exp = class_conn2export(&conn);
1193
1194         /* Why should there ever be more than 1 connect? */
1195         LASSERT(mdd->mdd_connects == 0);
1196         mdd->mdd_connects++;
1197
1198         RETURN(0);
1199 }
1200
1201 /*
1202  * once last export (we don't count self-export) disappeared
1203  * mdd can be released
1204  */
1205 static int mdd_obd_disconnect(struct obd_export *exp)
1206 {
1207         struct obd_device *obd = exp->exp_obd;
1208         struct mdd_device *mdd = lu2mdd_dev(obd->obd_lu_dev);
1209         int                rc, release = 0;
1210         ENTRY;
1211
1212         mdd->mdd_connects--;
1213         if (mdd->mdd_connects == 0)
1214                 release = 1;
1215
1216         rc = class_disconnect(exp);
1217
1218         if (rc == 0 && release)
1219                 class_manual_cleanup(obd);
1220         RETURN(rc);
1221 }
1222
1223 static int mdd_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1224                             __u32 keylen, void *key, __u32 *vallen, void *val)
1225 {
1226         int rc = -EINVAL;
1227
1228         if (KEY_IS(KEY_OSP_CONNECTED)) {
1229                 struct obd_device       *obd = exp->exp_obd;
1230                 struct mdd_device       *mdd;
1231
1232                 if (!obd->obd_set_up || obd->obd_stopping)
1233                         RETURN(-EAGAIN);
1234
1235                 mdd = lu2mdd_dev(obd->obd_lu_dev);
1236                 LASSERT(mdd);
1237                 rc = obd_get_info(env, mdd->mdd_child_exp, keylen, key, vallen,
1238                                   val);
1239                 RETURN(rc);
1240         }
1241
1242         RETURN(rc);
1243 }
1244
1245 static int mdd_obd_set_info_async(const struct lu_env *env,
1246                                   struct obd_export *exp,
1247                                   __u32 keylen, void *key,
1248                                   __u32 vallen, void *val,
1249                                   struct ptlrpc_request_set *set)
1250 {
1251         struct obd_device       *obd = exp->exp_obd;
1252         struct mdd_device       *mdd;
1253         int                      rc;
1254
1255         if (!obd->obd_set_up || obd->obd_stopping)
1256                 RETURN(-EAGAIN);
1257
1258         mdd = lu2mdd_dev(obd->obd_lu_dev);
1259         LASSERT(mdd);
1260         rc = obd_set_info_async(env, mdd->mdd_child_exp, keylen, key,
1261                                 vallen, val, set);
1262         RETURN(rc);
1263 }
1264
1265 static struct obd_ops mdd_obd_device_ops = {
1266         .o_owner        = THIS_MODULE,
1267         .o_connect      = mdd_obd_connect,
1268         .o_disconnect   = mdd_obd_disconnect,
1269         .o_get_info     = mdd_obd_get_info,
1270         .o_set_info_async = mdd_obd_set_info_async,
1271 };
1272
1273 static int mdd_changelog_user_register(const struct lu_env *env,
1274                                        struct mdd_device *mdd, int *id)
1275 {
1276         struct llog_ctxt *ctxt;
1277         struct llog_changelog_user_rec *rec;
1278         int rc;
1279         ENTRY;
1280
1281         ctxt = llog_get_context(mdd2obd_dev(mdd),
1282                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1283         if (ctxt == NULL)
1284                 RETURN(-ENXIO);
1285
1286         OBD_ALLOC_PTR(rec);
1287         if (rec == NULL) {
1288                 llog_ctxt_put(ctxt);
1289                 RETURN(-ENOMEM);
1290         }
1291
1292         /* Assume we want it on since somebody registered */
1293         rc = mdd_changelog_on(env, mdd);
1294         if (rc)
1295                 GOTO(out, rc);
1296
1297         rec->cur_hdr.lrh_len = sizeof(*rec);
1298         rec->cur_hdr.lrh_type = CHANGELOG_USER_REC;
1299         spin_lock(&mdd->mdd_cl.mc_user_lock);
1300         if (mdd->mdd_cl.mc_lastuser == (unsigned int)(-1)) {
1301                 spin_unlock(&mdd->mdd_cl.mc_user_lock);
1302                 CERROR("Maximum number of changelog users exceeded!\n");
1303                 GOTO(out, rc = -EOVERFLOW);
1304         }
1305         *id = rec->cur_id = ++mdd->mdd_cl.mc_lastuser;
1306         rec->cur_endrec = mdd->mdd_cl.mc_index;
1307         spin_unlock(&mdd->mdd_cl.mc_user_lock);
1308
1309         rc = llog_cat_add(env, ctxt->loc_handle, &rec->cur_hdr, NULL);
1310
1311         CDEBUG(D_IOCTL, "Registered changelog user %d\n", *id);
1312 out:
1313         OBD_FREE_PTR(rec);
1314         llog_ctxt_put(ctxt);
1315         RETURN(rc);
1316 }
1317
1318 struct mdd_changelog_user_data {
1319         __u64 mcud_endrec; /**< purge record for this user */
1320         __u64 mcud_minrec; /**< lowest changelog recno still referenced */
1321         __u32 mcud_id;
1322         __u32 mcud_minid;  /**< user id with lowest rec reference */
1323         __u32 mcud_usercount;
1324         unsigned int mcud_found:1;
1325 };
1326 #define MCUD_UNREGISTER -1LL
1327
1328 /** Two things:
1329  * 1. Find the smallest record everyone is willing to purge
1330  * 2. Update the last purgeable record for this user
1331  */
1332 static int mdd_changelog_user_purge_cb(const struct lu_env *env,
1333                                        struct llog_handle *llh,
1334                                        struct llog_rec_hdr *hdr, void *data)
1335 {
1336         struct llog_changelog_user_rec  *rec;
1337         struct mdd_changelog_user_data  *mcud = data;
1338         int                              rc;
1339
1340         ENTRY;
1341
1342         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
1343
1344         rec = (struct llog_changelog_user_rec *)hdr;
1345
1346         mcud->mcud_usercount++;
1347
1348         /* If we have a new endrec for this id, use it for the following
1349            min check instead of its old value */
1350         if (rec->cur_id == mcud->mcud_id)
1351                 rec->cur_endrec = max(rec->cur_endrec, mcud->mcud_endrec);
1352
1353         /* Track the minimum referenced record */
1354         if (mcud->mcud_minid == 0 || mcud->mcud_minrec > rec->cur_endrec) {
1355                 mcud->mcud_minid = rec->cur_id;
1356                 mcud->mcud_minrec = rec->cur_endrec;
1357         }
1358
1359         if (rec->cur_id != mcud->mcud_id)
1360                 RETURN(0);
1361
1362         /* Update this user's record */
1363         mcud->mcud_found = 1;
1364
1365         /* Special case: unregister this user */
1366         if (mcud->mcud_endrec == MCUD_UNREGISTER) {
1367                 struct llog_cookie cookie;
1368
1369                 cookie.lgc_lgl = llh->lgh_id;
1370                 cookie.lgc_index = hdr->lrh_index;
1371
1372                 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
1373                                              1, &cookie);
1374                 if (rc == 0)
1375                         mcud->mcud_usercount--;
1376
1377                 RETURN(rc);
1378         }
1379
1380         /* Update the endrec */
1381         CDEBUG(D_IOCTL, "Rewriting changelog user %d endrec to "LPU64"\n",
1382                mcud->mcud_id, rec->cur_endrec);
1383
1384         rc = llog_write(env, llh, hdr, hdr->lrh_index);
1385
1386         RETURN(rc);
1387 }
1388
1389 static int mdd_changelog_user_purge(const struct lu_env *env,
1390                                     struct mdd_device *mdd, int id,
1391                                     long long endrec)
1392 {
1393         struct mdd_changelog_user_data data;
1394         struct llog_ctxt *ctxt;
1395         int rc;
1396         ENTRY;
1397
1398         CDEBUG(D_IOCTL, "Purge request: id=%d, endrec=%lld\n", id, endrec);
1399
1400         data.mcud_id = id;
1401         data.mcud_minid = 0;
1402         data.mcud_minrec = 0;
1403         data.mcud_usercount = 0;
1404         data.mcud_endrec = endrec;
1405         spin_lock(&mdd->mdd_cl.mc_lock);
1406         endrec = mdd->mdd_cl.mc_index;
1407         spin_unlock(&mdd->mdd_cl.mc_lock);
1408         if ((data.mcud_endrec == 0) ||
1409             ((data.mcud_endrec > endrec) &&
1410              (data.mcud_endrec != MCUD_UNREGISTER)))
1411                 data.mcud_endrec = endrec;
1412
1413         ctxt = llog_get_context(mdd2obd_dev(mdd),
1414                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
1415         if (ctxt == NULL)
1416                 return -ENXIO;
1417
1418         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
1419
1420         rc = llog_cat_process(env, ctxt->loc_handle,
1421                               mdd_changelog_user_purge_cb, (void *)&data,
1422                               0, 0);
1423         if ((rc >= 0) && (data.mcud_minrec > 0)) {
1424                 CDEBUG(D_IOCTL, "Purging changelog entries up to "LPD64
1425                        ", referenced by "CHANGELOG_USER_PREFIX"%d\n",
1426                        data.mcud_minrec, data.mcud_minid);
1427                 rc = mdd_changelog_llog_cancel(env, mdd, data.mcud_minrec);
1428         } else {
1429                 CWARN("Could not determine changelog records to purge; rc=%d\n",
1430                       rc);
1431         }
1432
1433         llog_ctxt_put(ctxt);
1434
1435         if (!data.mcud_found) {
1436                 CWARN("No entry for user %d.  Last changelog reference is "
1437                       LPD64" by changelog user %d\n", data.mcud_id,
1438                       data.mcud_minrec, data.mcud_minid);
1439                rc = -ENOENT;
1440         }
1441
1442         if (!rc && data.mcud_usercount == 0)
1443                 /* No more users; turn changelogs off */
1444                 rc = mdd_changelog_off(env, mdd);
1445
1446         RETURN(rc);
1447 }
1448
1449 /** mdd_iocontrol
1450  * May be called remotely from mdt_iocontrol_handle or locally from
1451  * mdt_iocontrol. Data may be freeform - remote handling doesn't enforce
1452  * an obd_ioctl_data format (but local ioctl handler does).
1453  * \param cmd - ioc
1454  * \param len - data len
1455  * \param karg - ioctl data, in kernel space
1456  */
1457 static int mdd_iocontrol(const struct lu_env *env, struct md_device *m,
1458                          unsigned int cmd, int len, void *karg)
1459 {
1460         struct mdd_device *mdd;
1461         struct obd_ioctl_data *data = karg;
1462         int rc;
1463         ENTRY;
1464
1465         mdd = lu2mdd_dev(&m->md_lu_dev);
1466
1467         /* Doesn't use obd_ioctl_data */
1468         switch (cmd) {
1469         case OBD_IOC_CHANGELOG_CLEAR: {
1470                 struct changelog_setinfo *cs = karg;
1471                 rc = mdd_changelog_user_purge(env, mdd, cs->cs_id,
1472                                               cs->cs_recno);
1473                 RETURN(rc);
1474         }
1475         case OBD_IOC_GET_MNTOPT: {
1476                 mntopt_t *mntopts = (mntopt_t *)karg;
1477                 *mntopts = mdd->mdd_dt_conf.ddp_mntopts;
1478                 RETURN(0);
1479         }
1480         case OBD_IOC_START_LFSCK: {
1481                 rc = lfsck_start(env, mdd->mdd_bottom,
1482                                  (struct lfsck_start_param *)karg);
1483                 RETURN(rc);
1484         }
1485         case OBD_IOC_STOP_LFSCK: {
1486                 rc = lfsck_stop(env, mdd->mdd_bottom,
1487                                 (struct lfsck_stop *)karg);
1488                 RETURN(rc);
1489         }
1490         }
1491
1492         /* Below ioctls use obd_ioctl_data */
1493         if (len != sizeof(*data)) {
1494                 CERROR("Bad ioctl size %d\n", len);
1495                 RETURN(-EINVAL);
1496         }
1497         if (data->ioc_version != OBD_IOCTL_VERSION) {
1498                 CERROR("Bad magic %x != %x\n", data->ioc_version,
1499                        OBD_IOCTL_VERSION);
1500                 RETURN(-EINVAL);
1501         }
1502
1503         switch (cmd) {
1504         case OBD_IOC_CHANGELOG_REG:
1505                 rc = mdd_changelog_user_register(env, mdd, &data->ioc_u32_1);
1506                 break;
1507         case OBD_IOC_CHANGELOG_DEREG:
1508                 rc = mdd_changelog_user_purge(env, mdd, data->ioc_u32_1,
1509                                               MCUD_UNREGISTER);
1510                 break;
1511         default:
1512                 rc = -ENOTTY;
1513         }
1514
1515         RETURN (rc);
1516 }
1517
1518 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
1519 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key);
1520
1521 static const struct md_device_operations mdd_ops = {
1522         .mdo_statfs         = mdd_statfs,
1523         .mdo_root_get       = mdd_root_get,
1524         .mdo_llog_ctxt_get  = mdd_llog_ctxt_get,
1525         .mdo_iocontrol      = mdd_iocontrol,
1526         .mdo_maxeasize_get  = mdd_maxeasize_get,
1527 };
1528
1529 static struct lu_device_type_operations mdd_device_type_ops = {
1530         .ldto_init = mdd_type_init,
1531         .ldto_fini = mdd_type_fini,
1532
1533         .ldto_start = mdd_type_start,
1534         .ldto_stop  = mdd_type_stop,
1535
1536         .ldto_device_alloc = mdd_device_alloc,
1537         .ldto_device_free  = mdd_device_free,
1538
1539         .ldto_device_fini    = mdd_device_fini
1540 };
1541
1542 static struct lu_device_type mdd_device_type = {
1543         .ldt_tags     = LU_DEVICE_MD,
1544         .ldt_name     = LUSTRE_MDD_NAME,
1545         .ldt_ops      = &mdd_device_type_ops,
1546         .ldt_ctx_tags = LCT_MD_THREAD
1547 };
1548
1549 /* context key constructor: mdd_key_init */
1550 LU_KEY_INIT(mdd, struct mdd_thread_info);
1551
1552 static void mdd_key_fini(const struct lu_context *ctx,
1553                          struct lu_context_key *key, void *data)
1554 {
1555         struct mdd_thread_info *info = data;
1556
1557         lu_buf_free(&info->mti_big_buf);
1558         lu_buf_free(&info->mti_link_buf);
1559
1560         OBD_FREE_PTR(info);
1561 }
1562
1563 /* context key: mdd_thread_key */
1564 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
1565
1566 int mdd_generic_thread_start(struct mdd_generic_thread *thread,
1567                              int (*func)(void *), void *data, char *name)
1568 {
1569         struct task_struct      *task;
1570
1571         LASSERT(thread->mgt_init == false);
1572         init_completion(&thread->mgt_started);
1573         init_completion(&thread->mgt_finished);
1574         thread->mgt_data = data;
1575         thread->mgt_abort = false;
1576         thread->mgt_init = true;
1577
1578         task = kthread_run(func, thread, name);
1579         if (IS_ERR(task)) {
1580                 complete(&thread->mgt_finished);
1581                 return PTR_ERR(task);
1582         }
1583         wait_for_completion(&thread->mgt_started);
1584         return 0;
1585 }
1586
1587 void mdd_generic_thread_stop(struct mdd_generic_thread *thread)
1588 {
1589         if (thread->mgt_init == true) {
1590                 thread->mgt_abort = true;
1591                 wait_for_completion(&thread->mgt_finished);
1592         }
1593 }
1594
1595 static int __init mdd_init(void)
1596 {
1597         int rc;
1598
1599         rc = lu_kmem_init(mdd_caches);
1600         if (rc)
1601                 return rc;
1602
1603         changelog_orig_logops = llog_osd_ops;
1604         changelog_orig_logops.lop_cancel = llog_changelog_cancel;
1605         changelog_orig_logops.lop_add = llog_cat_add_rec;
1606         changelog_orig_logops.lop_declare_add = llog_cat_declare_add_rec;
1607
1608         hsm_actions_logops = llog_osd_ops;
1609         hsm_actions_logops.lop_add = llog_cat_add_rec;
1610         hsm_actions_logops.lop_declare_add = llog_cat_declare_add_rec;
1611
1612         rc = class_register_type(&mdd_obd_device_ops, NULL, true, NULL,
1613                                  LUSTRE_MDD_NAME, &mdd_device_type);
1614         if (rc)
1615                 lu_kmem_fini(mdd_caches);
1616         return rc;
1617 }
1618
1619 static void __exit mdd_exit(void)
1620 {
1621         class_unregister_type(LUSTRE_MDD_NAME);
1622         lu_kmem_fini(mdd_caches);
1623 }
1624
1625 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
1626 MODULE_DESCRIPTION("Lustre Meta-data Device Driver ("LUSTRE_MDD_NAME")");
1627 MODULE_VERSION(LUSTRE_VERSION_STRING);
1628 MODULE_LICENSE("GPL");
1629
1630 module_init(mdd_init);
1631 module_exit(mdd_exit);