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