Whamcloud - gitweb
e9bfcfcc1de676aad6ee620bd20363b54a18fc26
[fs/lustre-release.git] / lustre / mdd / mdd_device.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MDS
47
48 #include <linux/module.h>
49 #include <linux/jbd.h>
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
55
56 #include <lustre_disk.h>
57 #include <lustre_fid.h>
58 #include <linux/ldiskfs_fs.h>
59 #include <lustre_mds.h>
60 #include <lustre/lustre_idl.h>
61 #include <lustre_disk.h>      /* for changelogs */
62 #include <lustre_param.h>
63
64 #include "mdd_internal.h"
65
66 const struct md_device_operations mdd_ops;
67
68 static const char mdd_root_dir_name[] = "ROOT";
69
70 static int mdd_device_init(const struct lu_env *env, struct lu_device *d,
71                            const char *name, struct lu_device *next)
72 {
73         struct mdd_device *mdd = lu2mdd_dev(d);
74         int rc;
75         ENTRY;
76
77         mdd->mdd_child = lu2dt_dev(next);
78
79         /* Prepare transactions callbacks. */
80         mdd->mdd_txn_cb.dtc_txn_start = mdd_txn_start_cb;
81         mdd->mdd_txn_cb.dtc_txn_stop = mdd_txn_stop_cb;
82         mdd->mdd_txn_cb.dtc_txn_commit = mdd_txn_commit_cb;
83         mdd->mdd_txn_cb.dtc_cookie = mdd;
84         CFS_INIT_LIST_HEAD(&mdd->mdd_txn_cb.dtc_linkage);
85         mdd->mdd_atime_diff = MAX_ATIME_DIFF;
86
87         rc = mdd_procfs_init(mdd, name);
88         RETURN(rc);
89 }
90
91 static struct lu_device *mdd_device_fini(const struct lu_env *env,
92                                          struct lu_device *d)
93 {
94         struct mdd_device *mdd = lu2mdd_dev(d);
95         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
96         int rc;
97
98         rc = mdd_procfs_fini(mdd);
99         if (rc) {
100                 CERROR("proc fini error %d \n", rc);
101                 return ERR_PTR(rc);
102         }
103         return next;
104 }
105
106 static void mdd_changelog_fini(const struct lu_env *env,
107                                struct mdd_device *mdd);
108
109 static void mdd_device_shutdown(const struct lu_env *env,
110                                 struct mdd_device *m, struct lustre_cfg *cfg)
111 {
112         ENTRY;
113         mdd_changelog_fini(env, m);
114         dt_txn_callback_del(m->mdd_child, &m->mdd_txn_cb);
115         if (m->mdd_obd_dev)
116                 mdd_fini_obd(env, m, cfg);
117         orph_index_fini(env, m);
118         /* remove upcall device*/
119         md_upcall_fini(&m->mdd_md_dev);
120         EXIT;
121 }
122
123 static int changelog_init_cb(struct llog_handle *llh, struct llog_rec_hdr *hdr,
124                              void *data)
125 {
126         struct mdd_device *mdd = (struct mdd_device *)data;
127         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
128         ENTRY;
129
130         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
131                 CERROR("log is not plain\n");
132                 RETURN(-EINVAL);
133         }
134         if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
135                 CERROR("Not a changelog rec? %d\n", rec->cr_hdr.lrh_type);
136                 RETURN(-EINVAL);
137         }
138
139         CDEBUG(D_INODE,
140                "seeing record at index %d/%d/"LPU64" t=%x %.*s in log "LPX64"\n",
141                hdr->lrh_index, rec->cr_hdr.lrh_index, rec->cr_index,
142                rec->cr_type, rec->cr_namelen, rec->cr_name,
143                llh->lgh_id.lgl_oid);
144
145         mdd->mdd_cl.mc_index = rec->cr_index;
146         RETURN(LLOG_PROC_BREAK);
147 }
148
149 static int mdd_changelog_llog_init(struct mdd_device *mdd)
150 {
151         struct obd_device *obd = mdd2obd_dev(mdd);
152         struct llog_ctxt *ctxt;
153         int rc;
154
155         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
156         if (ctxt == NULL) {
157                 CERROR("no context\n");
158                 return -EINVAL;
159         }
160         if (!ctxt->loc_handle) {
161                 CERROR("no handle\n");
162                 return -EINVAL;
163         }
164         rc = llog_cat_reverse_process(ctxt->loc_handle, changelog_init_cb, mdd);
165         llog_ctxt_put(ctxt);
166
167         if (rc < 0)
168                 CERROR("changelog init failed: %d\n", rc);
169         else
170                 rc = 0; /* llog_proc_break is ok */
171
172         CDEBUG(D_INODE, "changelog_init index="LPU64"\n", mdd->mdd_cl.mc_index);
173
174         return rc;
175 }
176
177 static int mdd_changelog_init(const struct lu_env *env, struct mdd_device *mdd)
178 {
179         int rc;
180
181         mdd->mdd_cl.mc_index = 0;
182         spin_lock_init(&mdd->mdd_cl.mc_lock);
183         cfs_waitq_init(&mdd->mdd_cl.mc_waitq);
184
185         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
186         mdd->mdd_cl.mc_flags = 0; /* off by default */
187         mdd->mdd_cl.mc_mask = CL_DEFMASK;
188         rc = mdd_changelog_llog_init(mdd);
189         if (rc) {
190                 CERROR("Changelog setup during init failed %d\n", rc);
191                 mdd->mdd_cl.mc_flags |= CLM_ERR;
192         }
193         return rc;
194 }
195
196 static void mdd_changelog_fini(const struct lu_env *env, struct mdd_device *mdd)
197 {
198         mdd->mdd_cl.mc_flags = 0;
199 }
200
201 /** Add a changelog entry \a rec to the changelog llog
202  * \param mdd
203  * \param rec
204  * \param handle - currently ignored since llogs start their own transaction;
205  *                 this will hopefully be fixed in llog rewrite
206  * \retval 0 ok
207  */
208 int mdd_changelog_llog_write(struct mdd_device         *mdd,
209                              struct llog_changelog_rec *rec,
210                              struct thandle            *handle)
211 {
212         struct obd_device *obd = mdd2obd_dev(mdd);
213         struct llog_ctxt *ctxt;
214         int rc;
215
216         if ((mdd->mdd_cl.mc_mask & (1 << rec->cr_type)) == 0)
217                 return 0;
218
219         rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr_namelen);
220         /* llog_lvfs_write_rec sets the llog tail len */
221         rec->cr_hdr.lrh_type = CHANGELOG_REC;
222         rec->cr_time = cfs_time_current_64();
223         spin_lock(&mdd->mdd_cl.mc_lock);
224         /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
225            but as long as the MDD transactions are ordered correctly for e.g.
226            rename conflicts, I don't think this should matter. */
227         rec->cr_index = ++mdd->mdd_cl.mc_index;
228         spin_unlock(&mdd->mdd_cl.mc_lock);
229         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
230         if (ctxt == NULL)
231                 return -ENXIO;
232
233         /* nested journal transaction */
234         rc = llog_add(ctxt, &rec->cr_hdr, NULL, NULL, 0);
235         llog_ctxt_put(ctxt);
236
237         cfs_waitq_signal(&mdd->mdd_cl.mc_waitq);
238
239         return rc;
240 }
241
242 /** Remove entries with indicies up to and including \a endrec from the
243  *  changelog
244  * \param mdd
245  * \param endrec
246  * \retval 0 ok
247  */
248 int mdd_changelog_llog_cancel(struct mdd_device *mdd, long long endrec)
249 {
250         struct obd_device *obd = mdd2obd_dev(mdd);
251         struct llog_ctxt *ctxt;
252         int rc;
253
254         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
255         if (ctxt == NULL)
256                 return -ENXIO;
257
258         /* Some records purged; reset repeat-access time */
259         mdd->mdd_cl.mc_starttime = cfs_time_current_64();
260
261         rc = llog_cancel(ctxt, NULL, 1, (struct llog_cookie *)&endrec, 0);
262
263         llog_ctxt_put(ctxt);
264
265         return rc;
266 }
267
268 /** Add a CL_MARK record to the changelog
269  * \param mdd
270  * \param markerflags - CLM_*
271  * \retval 0 ok
272  */
273 int mdd_changelog_write_header(struct mdd_device *mdd, int markerflags)
274 {
275         struct obd_device *obd = mdd2obd_dev(mdd);
276         struct llog_changelog_rec *rec;
277         int reclen;
278         int len = strlen(obd->obd_name);
279         int rc;
280         ENTRY;
281
282         reclen = llog_data_len(sizeof(*rec) + len);
283         OBD_ALLOC(rec, reclen);
284         if (rec == NULL)
285                 RETURN(-ENOMEM);
286
287         rec->cr_flags = CLF_VERSION;
288         rec->cr_type = CL_MARK;
289         rec->cr_namelen = len;
290         memcpy(rec->cr_name, obd->obd_name, rec->cr_namelen);
291         /* Status and action flags */
292         rec->cr_markerflags = mdd->mdd_cl.mc_flags | markerflags;
293
294         rc = mdd_changelog_llog_write(mdd, rec, NULL);
295
296         /* assume on or off event; reset repeat-access time */
297         mdd->mdd_cl.mc_starttime = rec->cr_time;
298
299         OBD_FREE(rec, reclen);
300         RETURN(rc);
301 }
302
303 static int mdd_process_config(const struct lu_env *env,
304                               struct lu_device *d, struct lustre_cfg *cfg)
305 {
306         struct mdd_device *m    = lu2mdd_dev(d);
307         struct dt_device  *dt   = m->mdd_child;
308         struct lu_device  *next = &dt->dd_lu_dev;
309         int rc;
310         ENTRY;
311
312         switch (cfg->lcfg_command) {
313         case LCFG_PARAM: {
314                 struct lprocfs_static_vars lvars;
315
316                 lprocfs_mdd_init_vars(&lvars);
317                 rc = class_process_proc_param(PARAM_MDD, lvars.obd_vars, cfg,m);
318                 if (rc > 0 || rc == -ENOSYS)
319                         /* we don't understand; pass it on */
320                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
321                 break;
322         }
323         case LCFG_SETUP:
324                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
325                 if (rc)
326                         GOTO(out, rc);
327                 dt->dd_ops->dt_conf_get(env, dt, &m->mdd_dt_conf);
328
329                 rc = mdd_init_obd(env, m, cfg);
330                 if (rc) {
331                         CERROR("lov init error %d \n", rc);
332                         GOTO(out, rc);
333                 }
334                 rc = mdd_txn_init_credits(env, m);
335                 if (rc)
336                         break;
337
338                 mdd_changelog_init(env, m);
339                 break;
340         case LCFG_CLEANUP:
341                 mdd_device_shutdown(env, m, cfg);
342         default:
343                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
344                 break;
345         }
346 out:
347         RETURN(rc);
348 }
349
350 #if 0
351 static int mdd_lov_set_nextid(const struct lu_env *env,
352                               struct mdd_device *mdd)
353 {
354         struct mds_obd *mds = &mdd->mdd_obd_dev->u.mds;
355         int rc;
356         ENTRY;
357
358         LASSERT(mds->mds_lov_objids != NULL);
359         rc = obd_set_info_async(mds->mds_osc_exp, strlen(KEY_NEXT_ID),
360                                 KEY_NEXT_ID, mds->mds_lov_desc.ld_tgt_count,
361                                 mds->mds_lov_objids, NULL);
362
363         RETURN(rc);
364 }
365
366 static int mdd_cleanup_unlink_llog(const struct lu_env *env,
367                                    struct mdd_device *mdd)
368 {
369         /* XXX: to be implemented! */
370         return 0;
371 }
372 #endif
373
374 static int mdd_recovery_complete(const struct lu_env *env,
375                                  struct lu_device *d)
376 {
377         struct mdd_device *mdd = lu2mdd_dev(d);
378         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
379         struct obd_device *obd = mdd2obd_dev(mdd);
380         int rc;
381         ENTRY;
382
383         LASSERT(mdd != NULL);
384         LASSERT(obd != NULL);
385 #if 0
386         /* XXX: Do we need this in new stack? */
387         rc = mdd_lov_set_nextid(env, mdd);
388         if (rc) {
389                 CERROR("mdd_lov_set_nextid() failed %d\n",
390                        rc);
391                 RETURN(rc);
392         }
393
394         /* XXX: cleanup unlink. */
395         rc = mdd_cleanup_unlink_llog(env, mdd);
396         if (rc) {
397                 CERROR("mdd_cleanup_unlink_llog() failed %d\n",
398                        rc);
399                 RETURN(rc);
400         }
401 #endif
402         /* Call that with obd_recovering = 1 just to update objids */
403         obd_notify(obd->u.mds.mds_osc_obd, NULL, (obd->obd_async_recov ?
404                     OBD_NOTIFY_SYNC_NONBLOCK : OBD_NOTIFY_SYNC), NULL);
405
406         /* Drop obd_recovering to 0 and call o_postrecov to recover mds_lov */
407         obd->obd_recovering = 0;
408         obd->obd_type->typ_dt_ops->o_postrecov(obd);
409
410         /* XXX: orphans handling. */
411         __mdd_orphan_cleanup(env, mdd);
412         rc = next->ld_ops->ldo_recovery_complete(env, next);
413
414         RETURN(rc);
415 }
416
417 static int mdd_prepare(const struct lu_env *env,
418                        struct lu_device *pdev,
419                        struct lu_device *cdev)
420 {
421         struct mdd_device *mdd = lu2mdd_dev(cdev);
422         struct lu_device *next = &mdd->mdd_child->dd_lu_dev;
423         struct dt_object *root;
424         int rc;
425
426         ENTRY;
427         rc = next->ld_ops->ldo_prepare(env, cdev, next);
428         if (rc)
429                 GOTO(out, rc);
430
431         dt_txn_callback_add(mdd->mdd_child, &mdd->mdd_txn_cb);
432         root = dt_store_open(env, mdd->mdd_child, "", mdd_root_dir_name,
433                              &mdd->mdd_root_fid);
434         if (!IS_ERR(root)) {
435                 LASSERT(root != NULL);
436                 lu_object_put(env, &root->do_lu);
437                 rc = orph_index_init(env, mdd);
438         } else
439                 rc = PTR_ERR(root);
440
441 out:
442         RETURN(rc);
443 }
444
445 const struct lu_device_operations mdd_lu_ops = {
446         .ldo_object_alloc      = mdd_object_alloc,
447         .ldo_process_config    = mdd_process_config,
448         .ldo_recovery_complete = mdd_recovery_complete,
449         .ldo_prepare           = mdd_prepare,
450 };
451
452 /*
453  * No permission check is needed.
454  */
455 static int mdd_root_get(const struct lu_env *env,
456                         struct md_device *m, struct lu_fid *f)
457 {
458         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
459
460         ENTRY;
461         *f = mdd->mdd_root_fid;
462         RETURN(0);
463 }
464
465 /*
466  * No permission check is needed.
467  */
468 static int mdd_statfs(const struct lu_env *env, struct md_device *m,
469                       struct kstatfs *sfs)
470 {
471         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
472         int rc;
473
474         ENTRY;
475
476         rc = mdd_child_ops(mdd)->dt_statfs(env, mdd->mdd_child, sfs);
477
478         RETURN(rc);
479 }
480
481 /*
482  * No permission check is needed.
483  */
484 static int mdd_maxsize_get(const struct lu_env *env, struct md_device *m,
485                            int *md_size, int *cookie_size)
486 {
487         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
488         ENTRY;
489
490         *md_size = mdd_lov_mdsize(env, mdd);
491         *cookie_size = mdd_lov_cookiesize(env, mdd);
492
493         RETURN(0);
494 }
495
496 static int mdd_init_capa_ctxt(const struct lu_env *env, struct md_device *m,
497                               int mode, unsigned long timeout, __u32 alg,
498                               struct lustre_capa_key *keys)
499 {
500         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
501         struct mds_obd    *mds = &mdd2obd_dev(mdd)->u.mds;
502         int rc;
503         ENTRY;
504
505         mds->mds_capa_keys = keys;
506         rc = mdd_child_ops(mdd)->dt_init_capa_ctxt(env, mdd->mdd_child, mode,
507                                                    timeout, alg, keys);
508         RETURN(rc);
509 }
510
511 static int mdd_update_capa_key(const struct lu_env *env,
512                                struct md_device *m,
513                                struct lustre_capa_key *key)
514 {
515         struct mdd_device *mdd = lu2mdd_dev(&m->md_lu_dev);
516         struct obd_export *lov_exp = mdd2obd_dev(mdd)->u.mds.mds_osc_exp;
517         int rc;
518         ENTRY;
519
520         rc = obd_set_info_async(lov_exp, sizeof(KEY_CAPA_KEY), KEY_CAPA_KEY,
521                                 sizeof(*key), key, NULL);
522         RETURN(rc);
523 }
524
525 static struct lu_device *mdd_device_alloc(const struct lu_env *env,
526                                           struct lu_device_type *t,
527                                           struct lustre_cfg *lcfg)
528 {
529         struct lu_device  *l;
530         struct mdd_device *m;
531
532         OBD_ALLOC_PTR(m);
533         if (m == NULL) {
534                 l = ERR_PTR(-ENOMEM);
535         } else {
536                 md_device_init(&m->mdd_md_dev, t);
537                 l = mdd2lu_dev(m);
538                 l->ld_ops = &mdd_lu_ops;
539                 m->mdd_md_dev.md_ops = &mdd_ops;
540                 md_upcall_init(&m->mdd_md_dev, NULL);
541         }
542
543         return l;
544 }
545
546 static struct lu_device *mdd_device_free(const struct lu_env *env,
547                                          struct lu_device *lu)
548 {
549         struct mdd_device *m = lu2mdd_dev(lu);
550         struct lu_device  *next = &m->mdd_child->dd_lu_dev;
551         ENTRY;
552
553         LASSERT(atomic_read(&lu->ld_ref) == 0);
554         md_device_fini(&m->mdd_md_dev);
555         OBD_FREE_PTR(m);
556         RETURN(next);
557 }
558
559 static struct obd_ops mdd_obd_device_ops = {
560         .o_owner = THIS_MODULE
561 };
562
563 /* context key constructor/destructor: mdd_ucred_key_init, mdd_ucred_key_fini */
564 LU_KEY_INIT_FINI(mdd_ucred, struct md_ucred);
565
566 static struct lu_context_key mdd_ucred_key = {
567         .lct_tags = LCT_SESSION,
568         .lct_init = mdd_ucred_key_init,
569         .lct_fini = mdd_ucred_key_fini
570 };
571
572 struct md_ucred *md_ucred(const struct lu_env *env)
573 {
574         LASSERT(env->le_ses != NULL);
575         return lu_context_key_get(env->le_ses, &mdd_ucred_key);
576 }
577 EXPORT_SYMBOL(md_ucred);
578
579 /*
580  * context key constructor/destructor:
581  * mdd_capainfo_key_init, mdd_capainfo_key_fini
582  */
583 LU_KEY_INIT_FINI(mdd_capainfo, struct md_capainfo);
584
585 struct lu_context_key mdd_capainfo_key = {
586         .lct_tags = LCT_SESSION,
587         .lct_init = mdd_capainfo_key_init,
588         .lct_fini = mdd_capainfo_key_fini
589 };
590
591 struct md_capainfo *md_capainfo(const struct lu_env *env)
592 {
593         /* NB, in mdt_init0 */
594         if (env->le_ses == NULL)
595                 return NULL;
596         return lu_context_key_get(env->le_ses, &mdd_capainfo_key);
597 }
598 EXPORT_SYMBOL(md_capainfo);
599
600 /* type constructor/destructor: mdd_type_init, mdd_type_fini */
601 LU_TYPE_INIT_FINI(mdd, &mdd_thread_key, &mdd_ucred_key, &mdd_capainfo_key);
602
603 const struct md_device_operations mdd_ops = {
604         .mdo_statfs         = mdd_statfs,
605         .mdo_root_get       = mdd_root_get,
606         .mdo_maxsize_get    = mdd_maxsize_get,
607         .mdo_init_capa_ctxt = mdd_init_capa_ctxt,
608         .mdo_update_capa_key= mdd_update_capa_key,
609 #ifdef HAVE_QUOTA_SUPPORT
610         .mdo_quota          = {
611                 .mqo_notify      = mdd_quota_notify,
612                 .mqo_setup       = mdd_quota_setup,
613                 .mqo_cleanup     = mdd_quota_cleanup,
614                 .mqo_recovery    = mdd_quota_recovery,
615                 .mqo_check       = mdd_quota_check,
616                 .mqo_on          = mdd_quota_on,
617                 .mqo_off         = mdd_quota_off,
618                 .mqo_setinfo     = mdd_quota_setinfo,
619                 .mqo_getinfo     = mdd_quota_getinfo,
620                 .mqo_setquota    = mdd_quota_setquota,
621                 .mqo_getquota    = mdd_quota_getquota,
622                 .mqo_getoinfo    = mdd_quota_getoinfo,
623                 .mqo_getoquota   = mdd_quota_getoquota,
624                 .mqo_invalidate  = mdd_quota_invalidate,
625                 .mqo_finvalidate = mdd_quota_finvalidate
626         }
627 #endif
628 };
629
630 static struct lu_device_type_operations mdd_device_type_ops = {
631         .ldto_init = mdd_type_init,
632         .ldto_fini = mdd_type_fini,
633
634         .ldto_start = mdd_type_start,
635         .ldto_stop  = mdd_type_stop,
636
637         .ldto_device_alloc = mdd_device_alloc,
638         .ldto_device_free  = mdd_device_free,
639
640         .ldto_device_init    = mdd_device_init,
641         .ldto_device_fini    = mdd_device_fini
642 };
643
644 static struct lu_device_type mdd_device_type = {
645         .ldt_tags     = LU_DEVICE_MD,
646         .ldt_name     = LUSTRE_MDD_NAME,
647         .ldt_ops      = &mdd_device_type_ops,
648         .ldt_ctx_tags = LCT_MD_THREAD
649 };
650
651 /* context key constructor: mdd_key_init */
652 LU_KEY_INIT(mdd, struct mdd_thread_info);
653
654 static void mdd_key_fini(const struct lu_context *ctx,
655                          struct lu_context_key *key, void *data)
656 {
657         struct mdd_thread_info *info = data;
658         if (info->mti_max_lmm != NULL)
659                 OBD_FREE(info->mti_max_lmm, info->mti_max_lmm_size);
660         if (info->mti_max_cookie != NULL)
661                 OBD_FREE(info->mti_max_cookie, info->mti_max_cookie_size);
662         mdd_buf_put(&info->mti_big_buf);
663
664         OBD_FREE_PTR(info);
665 }
666
667 /* context key: mdd_thread_key */
668 LU_CONTEXT_KEY_DEFINE(mdd, LCT_MD_THREAD);
669
670 static struct lu_local_obj_desc llod_capa_key = {
671         .llod_name      = CAPA_KEYS,
672         .llod_oid       = MDD_CAPA_KEYS_OID,
673         .llod_is_index  = 0,
674 };
675
676 static struct lu_local_obj_desc llod_mdd_orphan = {
677         .llod_name      = orph_index_name,
678         .llod_oid       = MDD_ORPHAN_OID,
679         .llod_is_index  = 1,
680         .llod_feat      = &dt_directory_features,
681 };
682
683 static struct lu_local_obj_desc llod_mdd_root = {
684         .llod_name      = mdd_root_dir_name,
685         .llod_oid       = MDD_ROOT_INDEX_OID,
686         .llod_is_index  = 1,
687         .llod_feat      = &dt_directory_features,
688 };
689
690 static int __init mdd_mod_init(void)
691 {
692         struct lprocfs_static_vars lvars;
693         lprocfs_mdd_init_vars(&lvars);
694
695         llo_local_obj_register(&llod_capa_key);
696         llo_local_obj_register(&llod_mdd_orphan);
697         llo_local_obj_register(&llod_mdd_root);
698
699         return class_register_type(&mdd_obd_device_ops, NULL, lvars.module_vars,
700                                    LUSTRE_MDD_NAME, &mdd_device_type);
701 }
702
703 static void __exit mdd_mod_exit(void)
704 {
705         llo_local_obj_unregister(&llod_capa_key);
706         llo_local_obj_unregister(&llod_mdd_orphan);
707         llo_local_obj_unregister(&llod_mdd_root);
708
709         class_unregister_type(LUSTRE_MDD_NAME);
710 }
711
712 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
713 MODULE_DESCRIPTION("Lustre Meta-data Device Prototype ("LUSTRE_MDD_NAME")");
714 MODULE_LICENSE("GPL");
715
716 cfs_module(mdd, "0.1.0", mdd_mod_init, mdd_mod_exit);