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