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