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