Whamcloud - gitweb
8711a3004028617d41873b2e5179251fe5fcdf6c
[fs/lustre-release.git] / lustre / ofd / ofd_dev.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) 2009, 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/ofd/ofd.c
37  *
38  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39  * Author: Mike Pershin <tappro@whamcloud.com>
40  * Author: Johann Lombardi <johann@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #include <obd_class.h>
46 #include <lustre_param.h>
47
48 #include "ofd_internal.h"
49
50 /* Slab for OFD object allocation */
51 static cfs_mem_cache_t *ofd_object_kmem;
52
53 static struct lu_kmem_descr ofd_caches[] = {
54         {
55                 .ckd_cache = &ofd_object_kmem,
56                 .ckd_name  = "ofd_obj",
57                 .ckd_size  = sizeof(struct ofd_object)
58         },
59         {
60                 .ckd_cache = NULL
61         }
62 };
63
64 static int ofd_stack_init(const struct lu_env *env,
65                           struct ofd_device *m, struct lustre_cfg *cfg)
66 {
67         struct lu_device        *ofd_lu = &m->ofd_dt_dev.dd_lu_dev;
68         const char              *dev = lustre_cfg_string(cfg, 0);
69         struct obd_type         *type;
70         struct lu_device_type   *ldt;
71         struct lu_device        *d;
72         struct ofd_thread_info  *info = ofd_info(env);
73         struct lustre_mount_info *lmi;
74         int                      rc;
75
76         ENTRY;
77
78         lmi = server_get_mount_2(dev);
79         if (lmi == NULL) {
80                 CERROR("Cannot get mount info for %s!\n", dev);
81                 RETURN(-EFAULT);
82         }
83
84         type = class_get_type(s2lsi(lmi->lmi_sb)->lsi_osd_type);
85         if (!type) {
86                 CERROR("Unknown type: '%s'\n",
87                        s2lsi(lmi->lmi_sb)->lsi_osd_type);
88                 RETURN(-ENODEV);
89         }
90
91         rc = lu_env_refill((struct lu_env *)env);
92         if (rc != 0) {
93                 CERROR("Failure to refill session: '%d'\n", rc);
94                 GOTO(out_type, rc);
95         }
96
97         ldt = type->typ_lu;
98         if (ldt == NULL) {
99                 CERROR("type: '%s'\n", s2lsi(lmi->lmi_sb)->lsi_osd_type);
100                 GOTO(out_type, rc = -EINVAL);
101         }
102
103         ldt->ldt_obd_type = type;
104         d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
105         if (IS_ERR(d)) {
106                 CERROR("Cannot allocate device: '%s'\n",
107                        s2lsi(lmi->lmi_sb)->lsi_osd_type);
108                 GOTO(out_type, rc = -ENODEV);
109         }
110
111         LASSERT(ofd_lu->ld_site);
112         d->ld_site = ofd_lu->ld_site;
113
114         snprintf(info->fti_u.name, sizeof(info->fti_u.name),
115                  "%s-osd", lustre_cfg_string(cfg, 0));
116
117         type->typ_refcnt++;
118         rc = ldt->ldt_ops->ldto_device_init(env, d, dev, NULL);
119         if (rc) {
120                 CERROR("can't init device '%s', rc = %d\n",
121                        s2lsi(lmi->lmi_sb)->lsi_osd_type, rc);
122                 GOTO(out_free, rc);
123         }
124         lu_device_get(d);
125         lu_ref_add(&d->ld_reference, "lu-stack", &lu_site_init);
126
127         m->ofd_osd = lu2dt_dev(d);
128
129         /* process setup config */
130         rc = d->ld_ops->ldo_process_config(env, d, cfg);
131         if (rc)
132                 GOTO(out_fini, rc);
133
134         RETURN(rc);
135
136 out_fini:
137         ldt->ldt_ops->ldto_device_fini(env, d);
138 out_free:
139         type->typ_refcnt--;
140         ldt->ldt_ops->ldto_device_free(env, d);
141 out_type:
142         class_put_type(type);
143         RETURN(rc);
144 }
145
146 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
147                            struct lu_device *top)
148 {
149         struct obd_device       *obd = ofd_obd(m);
150         struct lustre_cfg_bufs   bufs;
151         struct lustre_cfg       *lcfg;
152         char                     flags[3] = "";
153
154         ENTRY;
155
156         lu_site_purge(env, top->ld_site, ~0);
157
158         /* process cleanup, pass mdt obd name to get obd umount flags */
159         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
160         if (obd->obd_force)
161                 strcat(flags, "F");
162         if (obd->obd_fail)
163                 strcat(flags, "A");
164         lustre_cfg_bufs_set_string(&bufs, 1, flags);
165         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
166         if (!lcfg) {
167                 CERROR("Cannot alloc lcfg!\n");
168                 RETURN_EXIT;
169         }
170
171         LASSERT(top);
172         top->ld_ops->ldo_process_config(env, top, lcfg);
173         lustre_cfg_free(lcfg);
174
175         lu_stack_fini(env, &m->ofd_osd->dd_lu_dev);
176         m->ofd_osd = NULL;
177
178         EXIT;
179 }
180
181 /* used by MGS to process specific configurations */
182 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
183                               struct lustre_cfg *cfg)
184 {
185         struct ofd_device       *m = ofd_dev(d);
186         struct dt_device        *dt_next = m->ofd_osd;
187         struct lu_device        *next = &dt_next->dd_lu_dev;
188         int                      rc;
189
190         ENTRY;
191
192         switch (cfg->lcfg_command) {
193         case LCFG_PARAM: {
194                 struct lprocfs_static_vars lvars;
195
196                 lprocfs_ofd_init_vars(&lvars);
197                 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
198                                               d->ld_obd);
199                 if (rc > 0 || rc == -ENOSYS)
200                         /* we don't understand; pass it on */
201                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
202                 break;
203         }
204         case LCFG_SPTLRPC_CONF: {
205                 rc = -ENOTSUPP;
206                 break;
207         }
208         default:
209                 /* others are passed further */
210                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
211                 break;
212         }
213         RETURN(rc);
214 }
215
216 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
217                            const struct lu_object_conf *conf)
218 {
219         struct ofd_device       *d = ofd_dev(o->lo_dev);
220         struct lu_device        *under;
221         struct lu_object        *below;
222         int                      rc = 0;
223
224         ENTRY;
225
226         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
227                PFID(lu_object_fid(o)));
228
229         under = &d->ofd_osd->dd_lu_dev;
230         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
231         if (below != NULL)
232                 lu_object_add(o, below);
233         else
234                 rc = -ENOMEM;
235
236         RETURN(rc);
237 }
238
239 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
240 {
241         struct ofd_object       *of = ofd_obj(o);
242         struct lu_object_header *h;
243
244         ENTRY;
245
246         h = o->lo_header;
247         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
248                PFID(lu_object_fid(o)));
249
250         lu_object_fini(o);
251         lu_object_header_fini(h);
252         OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
253         EXIT;
254 }
255
256 static int ofd_object_print(const struct lu_env *env, void *cookie,
257                             lu_printer_t p, const struct lu_object *o)
258 {
259         return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
260 }
261
262 struct lu_object_operations ofd_obj_ops = {
263         .loo_object_init        = ofd_object_init,
264         .loo_object_free        = ofd_object_free,
265         .loo_object_print       = ofd_object_print
266 };
267
268 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
269                                           const struct lu_object_header *hdr,
270                                           struct lu_device *d)
271 {
272         struct ofd_object *of;
273
274         ENTRY;
275
276         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, CFS_ALLOC_IO);
277         if (of != NULL) {
278                 struct lu_object        *o;
279                 struct lu_object_header *h;
280
281                 o = &of->ofo_obj.do_lu;
282                 h = &of->ofo_header;
283                 lu_object_header_init(h);
284                 lu_object_init(o, h, d);
285                 lu_object_add_top(h, o);
286                 o->lo_ops = &ofd_obj_ops;
287                 RETURN(o);
288         } else {
289                 RETURN(NULL);
290         }
291 }
292
293 extern int ost_handle(struct ptlrpc_request *req);
294
295 static int ofd_start(const struct lu_env *env, struct lu_device *dev)
296 {
297         struct ofd_device       *ofd = ofd_dev(dev);
298         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
299         int                      rc;
300
301         ENTRY;
302
303         /* initialize lower device */
304         rc = next->ld_ops->ldo_prepare(env, dev, next);
305
306         RETURN(rc);
307 }
308
309 static int ofd_recovery_complete(const struct lu_env *env,
310                                  struct lu_device *dev)
311 {
312         struct ofd_device       *ofd = ofd_dev(dev);
313         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
314         int                      rc = 0;
315
316         ENTRY;
317
318         /* Grant space for object precreation on the self export.
319          * This initial reserved space (i.e. 20MB for zfs and 560KB for ldiskfs)
320          * is enough to create 20k objects. It is then adapted based on the
321          * precreate request size (see ofd_grant_create()
322          */
323         ofd_grant_connect(env, dev->ld_obd->obd_self_export,
324                           OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace);
325         rc = next->ld_ops->ldo_recovery_complete(env, next);
326         RETURN(rc);
327 }
328
329 static struct lu_device_operations ofd_lu_ops = {
330         .ldo_object_alloc       = ofd_object_alloc,
331         .ldo_process_config     = ofd_process_config,
332         .ldo_recovery_complete  = ofd_recovery_complete,
333 };
334
335 static int ofd_procfs_init(struct ofd_device *ofd)
336 {
337         struct lprocfs_static_vars       lvars;
338         struct obd_device               *obd = ofd_obd(ofd);
339         cfs_proc_dir_entry_t            *entry;
340         int                              rc = 0;
341
342         ENTRY;
343
344         /* lprocfs must be setup before the ofd so state can be safely added
345          * to /proc incrementally as the ofd is setup */
346         lprocfs_ofd_init_vars(&lvars);
347         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
348         if (rc) {
349                 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
350                        obd->obd_name, rc);
351                 RETURN(rc);
352         }
353
354         rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_LAST);
355         if (rc) {
356                 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
357                        obd->obd_name, rc);
358                 GOTO(obd_cleanup, rc);
359         }
360
361         /* Init OFD private stats here */
362         lprocfs_counter_init(obd->obd_stats, LPROC_OFD_READ_BYTES,
363                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
364         lprocfs_counter_init(obd->obd_stats, LPROC_OFD_WRITE_BYTES,
365                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
366
367         rc = lproc_ofd_attach_seqstat(obd);
368         if (rc) {
369                 CERROR("%s: create seqstat failed: %d.\n", obd->obd_name, rc);
370                 GOTO(free_obd_stats, rc);
371         }
372
373         entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
374         if (IS_ERR(entry)) {
375                 rc = PTR_ERR(entry);
376                 CERROR("%s: error %d setting up lprocfs for %s\n",
377                        obd->obd_name, rc, "exports");
378                 GOTO(free_obd_stats, rc);
379         }
380         obd->obd_proc_exports_entry = entry;
381
382         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
383                                    lprocfs_nid_stats_clear_read,
384                                    lprocfs_nid_stats_clear_write, obd, NULL);
385         if (IS_ERR(entry)) {
386                 rc = PTR_ERR(entry);
387                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
388                        obd->obd_name, rc);
389                 GOTO(free_obd_stats, rc);
390         }
391         RETURN(0);
392
393 free_obd_stats:
394         lprocfs_free_obd_stats(obd);
395 obd_cleanup:
396         lprocfs_obd_cleanup(obd);
397         return rc;
398 }
399
400 static int ofd_procfs_fini(struct ofd_device *ofd)
401 {
402         struct obd_device *obd = ofd_obd(ofd);
403
404         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
405         lprocfs_free_per_client_stats(obd);
406         lprocfs_free_obd_stats(obd);
407         lprocfs_obd_cleanup(obd);
408         return 0;
409 }
410
411 extern int ost_handle(struct ptlrpc_request *req);
412
413 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
414                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
415 {
416         const char              *dev = lustre_cfg_string(cfg, 0);
417         struct ofd_thread_info  *info = NULL;
418         struct obd_device       *obd;
419         struct obd_statfs       *osfs;
420         int                      rc;
421
422         ENTRY;
423
424         obd = class_name2obd(dev);
425         if (obd == NULL) {
426                 CERROR("Cannot find obd with name %s\n", dev);
427                 RETURN(-ENODEV);
428         }
429
430         rc = lu_env_refill((struct lu_env *)env);
431         if (rc != 0)
432                 RETURN(rc);
433
434         obd->u.obt.obt_magic = OBT_MAGIC;
435
436         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
437         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
438
439         cfs_spin_lock_init(&m->ofd_flags_lock);
440         m->ofd_raid_degraded = 0;
441         m->ofd_syncjournal = 0;
442         ofd_slc_set(m);
443         m->ofd_grant_compat_disable = 0;
444
445         /* statfs data */
446         cfs_spin_lock_init(&m->ofd_osfs_lock);
447         m->ofd_osfs_age = cfs_time_shift_64(-1000);
448         m->ofd_osfs_unstable = 0;
449         m->ofd_statfs_inflight = 0;
450         m->ofd_osfs_inflight = 0;
451
452         /* grant data */
453         cfs_spin_lock_init(&m->ofd_grant_lock);
454         m->ofd_tot_dirty = 0;
455         m->ofd_tot_granted = 0;
456         m->ofd_tot_pending = 0;
457         m->ofd_max_group = 0;
458
459         cfs_rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
460         sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
461
462         obd->u.filter.fo_fl_oss_capa = 0;
463         CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
464         obd->u.filter.fo_capa_hash = init_capa_hash();
465         if (obd->u.filter.fo_capa_hash == NULL)
466                 RETURN(-ENOMEM);
467
468         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
469         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
470         /* set this lu_device to obd, because error handling need it */
471         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
472
473         rc = ofd_procfs_init(m);
474         if (rc) {
475                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
476                 RETURN(rc);
477         }
478
479         /* No connection accepted until configurations will finish */
480         obd->obd_no_conn = 1;
481         obd->obd_replayable = 1;
482         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
483                 char *str = lustre_cfg_string(cfg, 4);
484
485                 if (strchr(str, 'n')) {
486                         CWARN("%s: recovery disabled\n", obd->obd_name);
487                         obd->obd_replayable = 0;
488                 }
489         }
490
491         info = ofd_info_init(env, NULL);
492         if (info == NULL)
493                 RETURN(-EFAULT);
494
495         rc = lu_site_init(&m->ofd_site, &m->ofd_dt_dev.dd_lu_dev);
496         if (rc)
497                 GOTO(err_fini_proc, rc);
498         m->ofd_site.ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
499
500         rc = ofd_stack_init(env, m, cfg);
501         if (rc) {
502                 CERROR("Can't init device stack, rc %d\n", rc);
503                 GOTO(err_lu_site, rc);
504         }
505
506         /* populate cached statfs data */
507         osfs = &ofd_info(env)->fti_u.osfs;
508         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
509         if (rc != 0) {
510                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
511                 GOTO(err_fini_stack, rc);
512         }
513         if (!IS_PO2(osfs->os_bsize)) {
514                 CERROR("%s: blocksize (%d) is not a power of 2\n",
515                                 obd->obd_name, osfs->os_bsize);
516                 GOTO(err_fini_stack, rc = -EPROTO);
517         }
518         m->ofd_blockbits = cfs_fls(osfs->os_bsize) - 1;
519
520         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "filter-%p", m);
521         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
522                                               LDLM_NAMESPACE_SERVER,
523                                               LDLM_NAMESPACE_GREEDY,
524                                               LDLM_NS_TYPE_OST);
525         if (m->ofd_namespace == NULL)
526                 GOTO(err_fini_stack, rc = -ENOMEM);
527         /* set obd_namespace for compatibility with old code */
528         obd->obd_namespace = m->ofd_namespace;
529         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
530         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
531         m->ofd_namespace->ns_lvbp = m;
532
533         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
534                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
535
536         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
537
538         /* Allow at most ddp_grant_reserved% of the available filesystem space
539          * to be granted to clients, so that any errors in the grant overhead
540          * calculations do not allow granting more space to clients than can be
541          * written. Assumes that in aggregate the grant overhead calculations do
542          * not have more than ddp_grant_reserved% estimation error in them. */
543         m->ofd_grant_ratio =
544                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
545
546         rc = ofd_start(env, &m->ofd_dt_dev.dd_lu_dev);
547         if (rc)
548                 GOTO(err_fini_stack, rc);
549
550         rc = lut_init(env, &m->ofd_lut, obd, m->ofd_osd);
551         if (rc)
552                 GOTO(err_free_ns, rc);
553
554         rc = ofd_fs_setup(env, m, obd);
555         if (rc)
556                 GOTO(err_fini_lut, rc);
557
558         target_recovery_init(&m->ofd_lut, ost_handle);
559
560         rc = lu_site_init_finish(&m->ofd_site);
561         if (rc)
562                 GOTO(err_fs_cleanup, rc);
563
564         RETURN(0);
565 err_fs_cleanup:
566         target_recovery_fini(obd);
567         ofd_fs_cleanup(env, m);
568 err_fini_lut:
569         lut_fini(env, &m->ofd_lut);
570 err_free_ns:
571         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
572         obd->obd_namespace = m->ofd_namespace = NULL;
573 err_fini_stack:
574         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
575 err_lu_site:
576         lu_site_fini(&m->ofd_site);
577 err_fini_proc:
578         ofd_procfs_fini(m);
579         return rc;
580 }
581
582 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
583 {
584         struct obd_device *obd = ofd_obd(m);
585         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
586
587         target_recovery_fini(obd);
588         obd_exports_barrier(obd);
589         obd_zombie_barrier();
590
591         lut_fini(env, &m->ofd_lut);
592         ofd_fs_cleanup(env, m);
593
594         ofd_free_capa_keys(m);
595         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
596
597         if (m->ofd_namespace != NULL) {
598                 ldlm_namespace_free(m->ofd_namespace, NULL,
599                                     d->ld_obd->obd_force);
600                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
601         }
602
603         ofd_stack_fini(env, m, m->ofd_site.ls_top_dev);
604         lu_site_fini(&m->ofd_site);
605         ofd_procfs_fini(m);
606         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
607         EXIT;
608 }
609
610 static struct lu_device *ofd_device_fini(const struct lu_env *env,
611                                          struct lu_device *d)
612 {
613         ENTRY;
614         ofd_fini(env, ofd_dev(d));
615         RETURN(NULL);
616 }
617
618 static struct lu_device *ofd_device_free(const struct lu_env *env,
619                                          struct lu_device *d)
620 {
621         struct ofd_device *m = ofd_dev(d);
622
623         dt_device_fini(&m->ofd_dt_dev);
624         OBD_FREE_PTR(m);
625         RETURN(NULL);
626 }
627
628 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
629                                           struct lu_device_type *t,
630                                           struct lustre_cfg *cfg)
631 {
632         struct ofd_device *m;
633         struct lu_device  *l;
634         int                rc;
635
636         OBD_ALLOC_PTR(m);
637         if (m == NULL)
638                 return ERR_PTR(-ENOMEM);
639
640         l = &m->ofd_dt_dev.dd_lu_dev;
641         dt_device_init(&m->ofd_dt_dev, t);
642         rc = ofd_init0(env, m, t, cfg);
643         if (rc != 0) {
644                 ofd_device_free(env, l);
645                 l = ERR_PTR(rc);
646         }
647
648         return l;
649 }
650
651 /* thread context key constructor/destructor */
652 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
653
654 static void ofd_key_exit(const struct lu_context *ctx,
655                          struct lu_context_key *key, void *data)
656 {
657         struct ofd_thread_info *info = data;
658
659         info->fti_env = NULL;
660         info->fti_exp = NULL;
661
662         info->fti_xid = 0;
663         info->fti_transno = 0;
664         info->fti_pre_version = 0;
665         info->fti_obj = NULL;
666         info->fti_has_trans = 0;
667         info->fti_mult_trans = 0;
668         info->fti_used = 0;
669
670         memset(&info->fti_attr, 0, sizeof info->fti_attr);
671 }
672
673 struct lu_context_key ofd_thread_key = {
674         .lct_tags = LCT_DT_THREAD,
675         .lct_init = ofd_key_init,
676         .lct_fini = ofd_key_fini,
677         .lct_exit = ofd_key_exit
678 };
679
680 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
681 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
682
683 static struct lu_device_type_operations ofd_device_type_ops = {
684         .ldto_init              = ofd_type_init,
685         .ldto_fini              = ofd_type_fini,
686
687         .ldto_start             = ofd_type_start,
688         .ldto_stop              = ofd_type_stop,
689
690         .ldto_device_alloc      = ofd_device_alloc,
691         .ldto_device_free       = ofd_device_free,
692         .ldto_device_fini       = ofd_device_fini
693 };
694
695 static struct lu_device_type ofd_device_type = {
696         .ldt_tags       = LU_DEVICE_DT,
697         .ldt_name       = LUSTRE_OST_NAME,
698         .ldt_ops        = &ofd_device_type_ops,
699         .ldt_ctx_tags   = LCT_DT_THREAD
700 };
701
702 int __init ofd_init(void)
703 {
704         struct lprocfs_static_vars      lvars;
705         int                             rc;
706
707         rc = lu_kmem_init(ofd_caches);
708         if (rc)
709                 return rc;
710
711         rc = ofd_fmd_init();
712         if (rc) {
713                 lu_kmem_fini(ofd_caches);
714                 return(rc);
715         }
716
717         lprocfs_ofd_init_vars(&lvars);
718
719         rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
720                                  LUSTRE_OST_NAME, &ofd_device_type);
721         return rc;
722 }
723
724 void __exit ofd_exit(void)
725 {
726         ofd_fmd_exit();
727         lu_kmem_fini(ofd_caches);
728         class_unregister_type(LUSTRE_OST_NAME);
729 }
730
731 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
732 MODULE_DESCRIPTION("Lustre Object Filtering Device");
733 MODULE_LICENSE("GPL");
734
735 module_init(ofd_init);
736 module_exit(ofd_exit);