Whamcloud - gitweb
LU-2446 build: Update Whamcloud copyright messages for Intel
[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) 2012, Intel Corporation.
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 /* For interoperability, see mdt_interop_param[]. */
183 static struct cfg_interop_param ofd_interop_param[] = {
184         { "ost.quota_type",     NULL },
185         { NULL }
186 };
187
188 /* used by MGS to process specific configurations */
189 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
190                               struct lustre_cfg *cfg)
191 {
192         struct ofd_device       *m = ofd_dev(d);
193         struct dt_device        *dt_next = m->ofd_osd;
194         struct lu_device        *next = &dt_next->dd_lu_dev;
195         int                      rc;
196
197         ENTRY;
198
199         switch (cfg->lcfg_command) {
200         case LCFG_PARAM: {
201                 struct lprocfs_static_vars lvars;
202
203                 /* For interoperability */
204                 struct cfg_interop_param   *ptr = NULL;
205                 struct lustre_cfg          *old_cfg = NULL;
206                 char                       *param = NULL;
207
208                 param = lustre_cfg_string(cfg, 1);
209                 if (param == NULL) {
210                         CERROR("param is empty\n");
211                         rc = -EINVAL;
212                         break;
213                 }
214
215                 ptr = class_find_old_param(param, ofd_interop_param);
216                 if (ptr != NULL) {
217                         if (ptr->new_param == NULL) {
218                                 rc = 0;
219                                 CWARN("For interoperability, skip this %s."
220                                       " It is obsolete.\n", ptr->old_param);
221                                 break;
222                         }
223
224                         CWARN("Found old param %s, changed it to %s.\n",
225                               ptr->old_param, ptr->new_param);
226
227                         old_cfg = cfg;
228                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
229                         if (IS_ERR(cfg)) {
230                                 rc = PTR_ERR(cfg);
231                                 break;
232                         }
233                 }
234
235                 lprocfs_ofd_init_vars(&lvars);
236                 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
237                                               d->ld_obd);
238                 if (rc > 0 || rc == -ENOSYS)
239                         /* we don't understand; pass it on */
240                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
241                 break;
242         }
243         case LCFG_SPTLRPC_CONF: {
244                 rc = -ENOTSUPP;
245                 break;
246         }
247         default:
248                 /* others are passed further */
249                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
250                 break;
251         }
252         RETURN(rc);
253 }
254
255 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
256                            const struct lu_object_conf *conf)
257 {
258         struct ofd_device       *d = ofd_dev(o->lo_dev);
259         struct lu_device        *under;
260         struct lu_object        *below;
261         int                      rc = 0;
262
263         ENTRY;
264
265         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
266                PFID(lu_object_fid(o)));
267
268         under = &d->ofd_osd->dd_lu_dev;
269         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
270         if (below != NULL)
271                 lu_object_add(o, below);
272         else
273                 rc = -ENOMEM;
274
275         RETURN(rc);
276 }
277
278 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
279 {
280         struct ofd_object       *of = ofd_obj(o);
281         struct lu_object_header *h;
282
283         ENTRY;
284
285         h = o->lo_header;
286         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
287                PFID(lu_object_fid(o)));
288
289         lu_object_fini(o);
290         lu_object_header_fini(h);
291         OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
292         EXIT;
293 }
294
295 static int ofd_object_print(const struct lu_env *env, void *cookie,
296                             lu_printer_t p, const struct lu_object *o)
297 {
298         return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
299 }
300
301 struct lu_object_operations ofd_obj_ops = {
302         .loo_object_init        = ofd_object_init,
303         .loo_object_free        = ofd_object_free,
304         .loo_object_print       = ofd_object_print
305 };
306
307 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
308                                           const struct lu_object_header *hdr,
309                                           struct lu_device *d)
310 {
311         struct ofd_object *of;
312
313         ENTRY;
314
315         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, CFS_ALLOC_IO);
316         if (of != NULL) {
317                 struct lu_object        *o;
318                 struct lu_object_header *h;
319
320                 o = &of->ofo_obj.do_lu;
321                 h = &of->ofo_header;
322                 lu_object_header_init(h);
323                 lu_object_init(o, h, d);
324                 lu_object_add_top(h, o);
325                 o->lo_ops = &ofd_obj_ops;
326                 RETURN(o);
327         } else {
328                 RETURN(NULL);
329         }
330 }
331
332 extern int ost_handle(struct ptlrpc_request *req);
333
334 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
335                        struct lu_device *dev)
336 {
337         struct ofd_thread_info *info;
338         struct ofd_device       *ofd = ofd_dev(dev);
339         struct obd_device       *obd = ofd_obd(ofd);
340         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
341         int                      rc;
342
343         ENTRY;
344
345         rc = lu_env_refill((struct lu_env *)env);
346         if (rc != 0) {
347                 CERROR("Failure to refill session: '%d'\n", rc);
348                 RETURN(rc);
349         }
350
351         info = ofd_info_init(env, NULL);
352         if (info == NULL)
353                 RETURN(-EFAULT);
354
355         /* initialize lower device */
356         rc = next->ld_ops->ldo_prepare(env, dev, next);
357
358         target_recovery_init(&ofd->ofd_lut, ost_handle);
359         LASSERT(obd->obd_no_conn);
360         spin_lock(&obd->obd_dev_lock);
361         obd->obd_no_conn = 0;
362         spin_unlock(&obd->obd_dev_lock);
363
364         if (obd->obd_recovering == 0)
365                 ofd_postrecov(env, ofd);
366
367         RETURN(rc);
368 }
369
370 static int ofd_recovery_complete(const struct lu_env *env,
371                                  struct lu_device *dev)
372 {
373         struct ofd_device       *ofd = ofd_dev(dev);
374         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
375         int                      rc = 0;
376
377         ENTRY;
378
379         /* Grant space for object precreation on the self export.
380          * This initial reserved space (i.e. 20MB for zfs and 560KB for ldiskfs)
381          * is enough to create 20k objects. It is then adapted based on the
382          * precreate request size (see ofd_grant_create()
383          */
384         ofd_grant_connect(env, dev->ld_obd->obd_self_export,
385                           OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace);
386         rc = next->ld_ops->ldo_recovery_complete(env, next);
387         RETURN(rc);
388 }
389
390 static struct lu_device_operations ofd_lu_ops = {
391         .ldo_object_alloc       = ofd_object_alloc,
392         .ldo_process_config     = ofd_process_config,
393         .ldo_recovery_complete  = ofd_recovery_complete,
394         .ldo_prepare            = ofd_prepare,
395 };
396
397 static int ofd_procfs_init(struct ofd_device *ofd)
398 {
399         struct lprocfs_static_vars       lvars;
400         struct obd_device               *obd = ofd_obd(ofd);
401         cfs_proc_dir_entry_t            *entry;
402         int                              rc = 0;
403
404         ENTRY;
405
406         /* lprocfs must be setup before the ofd so state can be safely added
407          * to /proc incrementally as the ofd is setup */
408         lprocfs_ofd_init_vars(&lvars);
409         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
410         if (rc) {
411                 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
412                        obd->obd_name, rc);
413                 RETURN(rc);
414         }
415
416         rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_LAST);
417         if (rc) {
418                 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
419                        obd->obd_name, rc);
420                 GOTO(obd_cleanup, rc);
421         }
422
423         /* Init OFD private stats here */
424         lprocfs_counter_init(obd->obd_stats, LPROC_OFD_READ_BYTES,
425                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
426         lprocfs_counter_init(obd->obd_stats, LPROC_OFD_WRITE_BYTES,
427                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
428
429         rc = lproc_ofd_attach_seqstat(obd);
430         if (rc) {
431                 CERROR("%s: create seqstat failed: %d.\n", obd->obd_name, rc);
432                 GOTO(free_obd_stats, rc);
433         }
434
435         entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
436         if (IS_ERR(entry)) {
437                 rc = PTR_ERR(entry);
438                 CERROR("%s: error %d setting up lprocfs for %s\n",
439                        obd->obd_name, rc, "exports");
440                 GOTO(free_obd_stats, rc);
441         }
442         obd->obd_proc_exports_entry = entry;
443
444         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
445                                    lprocfs_nid_stats_clear_read,
446                                    lprocfs_nid_stats_clear_write, obd, NULL);
447         if (IS_ERR(entry)) {
448                 rc = PTR_ERR(entry);
449                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
450                        obd->obd_name, rc);
451                 GOTO(free_obd_stats, rc);
452         }
453
454         rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
455                                     ofd_stats_counter_init);
456         if (rc)
457                 GOTO(remove_entry_clear, rc);
458         RETURN(0);
459 remove_entry_clear:
460         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
461 free_obd_stats:
462         lprocfs_free_obd_stats(obd);
463 obd_cleanup:
464         lprocfs_obd_cleanup(obd);
465         return rc;
466 }
467
468 static int ofd_procfs_fini(struct ofd_device *ofd)
469 {
470         struct obd_device *obd = ofd_obd(ofd);
471
472         lprocfs_job_stats_fini(obd);
473         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
474         lprocfs_free_per_client_stats(obd);
475         lprocfs_free_obd_stats(obd);
476         lprocfs_obd_cleanup(obd);
477         return 0;
478 }
479
480 extern int ost_handle(struct ptlrpc_request *req);
481
482 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
483                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
484 {
485         const char              *dev = lustre_cfg_string(cfg, 0);
486         struct ofd_thread_info  *info = NULL;
487         struct obd_device       *obd;
488         struct obd_statfs       *osfs;
489         int                      rc;
490
491         ENTRY;
492
493         obd = class_name2obd(dev);
494         if (obd == NULL) {
495                 CERROR("Cannot find obd with name %s\n", dev);
496                 RETURN(-ENODEV);
497         }
498
499         rc = lu_env_refill((struct lu_env *)env);
500         if (rc != 0)
501                 RETURN(rc);
502
503         obd->u.obt.obt_magic = OBT_MAGIC;
504
505         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
506         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
507
508         spin_lock_init(&m->ofd_flags_lock);
509         m->ofd_raid_degraded = 0;
510         m->ofd_syncjournal = 0;
511         ofd_slc_set(m);
512         m->ofd_grant_compat_disable = 0;
513
514         /* statfs data */
515         spin_lock_init(&m->ofd_osfs_lock);
516         m->ofd_osfs_age = cfs_time_shift_64(-1000);
517         m->ofd_osfs_unstable = 0;
518         m->ofd_statfs_inflight = 0;
519         m->ofd_osfs_inflight = 0;
520
521         /* grant data */
522         spin_lock_init(&m->ofd_grant_lock);
523         m->ofd_tot_dirty = 0;
524         m->ofd_tot_granted = 0;
525         m->ofd_tot_pending = 0;
526         m->ofd_max_group = 0;
527
528         rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
529         sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
530
531         obd->u.filter.fo_fl_oss_capa = 0;
532         CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
533         obd->u.filter.fo_capa_hash = init_capa_hash();
534         if (obd->u.filter.fo_capa_hash == NULL)
535                 RETURN(-ENOMEM);
536
537         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
538         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
539         /* set this lu_device to obd, because error handling need it */
540         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
541
542         rc = ofd_procfs_init(m);
543         if (rc) {
544                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
545                 RETURN(rc);
546         }
547
548         /* No connection accepted until configurations will finish */
549         obd->obd_no_conn = 1;
550         obd->obd_replayable = 1;
551         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
552                 char *str = lustre_cfg_string(cfg, 4);
553
554                 if (strchr(str, 'n')) {
555                         CWARN("%s: recovery disabled\n", obd->obd_name);
556                         obd->obd_replayable = 0;
557                 }
558         }
559
560         info = ofd_info_init(env, NULL);
561         if (info == NULL)
562                 RETURN(-EFAULT);
563
564         rc = ofd_stack_init(env, m, cfg);
565         if (rc) {
566                 CERROR("Can't init device stack, rc %d\n", rc);
567                 GOTO(err_fini_proc, rc);
568         }
569
570         /* populate cached statfs data */
571         osfs = &ofd_info(env)->fti_u.osfs;
572         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
573         if (rc != 0) {
574                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
575                 GOTO(err_fini_stack, rc);
576         }
577         if (!IS_PO2(osfs->os_bsize)) {
578                 CERROR("%s: blocksize (%d) is not a power of 2\n",
579                                 obd->obd_name, osfs->os_bsize);
580                 GOTO(err_fini_stack, rc = -EPROTO);
581         }
582         m->ofd_blockbits = fls(osfs->os_bsize) - 1;
583
584         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
585         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
586                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
587
588         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "filter-%p", m);
589         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
590                                               LDLM_NAMESPACE_SERVER,
591                                               LDLM_NAMESPACE_GREEDY,
592                                               LDLM_NS_TYPE_OST);
593         if (m->ofd_namespace == NULL)
594                 GOTO(err_fini_stack, rc = -ENOMEM);
595         /* set obd_namespace for compatibility with old code */
596         obd->obd_namespace = m->ofd_namespace;
597         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
598         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
599         m->ofd_namespace->ns_lvbp = m;
600
601         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
602                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
603
604         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
605
606         /* Allow at most ddp_grant_reserved% of the available filesystem space
607          * to be granted to clients, so that any errors in the grant overhead
608          * calculations do not allow granting more space to clients than can be
609          * written. Assumes that in aggregate the grant overhead calculations do
610          * not have more than ddp_grant_reserved% estimation error in them. */
611         m->ofd_grant_ratio =
612                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
613
614         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd);
615         if (rc)
616                 GOTO(err_free_ns, rc);
617
618         rc = ofd_fs_setup(env, m, obd);
619         if (rc)
620                 GOTO(err_fini_lut, rc);
621
622         RETURN(0);
623 err_fini_lut:
624         tgt_fini(env, &m->ofd_lut);
625 err_free_ns:
626         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
627         obd->obd_namespace = m->ofd_namespace = NULL;
628 err_fini_stack:
629         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
630 err_fini_proc:
631         ofd_procfs_fini(m);
632         return rc;
633 }
634
635 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
636 {
637         struct obd_device *obd = ofd_obd(m);
638         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
639
640         target_recovery_fini(obd);
641         obd_exports_barrier(obd);
642         obd_zombie_barrier();
643
644         tgt_fini(env, &m->ofd_lut);
645         ofd_fs_cleanup(env, m);
646
647         ofd_free_capa_keys(m);
648         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
649
650         if (m->ofd_namespace != NULL) {
651                 ldlm_namespace_free(m->ofd_namespace, NULL,
652                                     d->ld_obd->obd_force);
653                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
654         }
655
656         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
657         ofd_procfs_fini(m);
658         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
659         server_put_mount(obd->obd_name, NULL);
660         EXIT;
661 }
662
663 static struct lu_device *ofd_device_fini(const struct lu_env *env,
664                                          struct lu_device *d)
665 {
666         ENTRY;
667         ofd_fini(env, ofd_dev(d));
668         RETURN(NULL);
669 }
670
671 static struct lu_device *ofd_device_free(const struct lu_env *env,
672                                          struct lu_device *d)
673 {
674         struct ofd_device *m = ofd_dev(d);
675
676         dt_device_fini(&m->ofd_dt_dev);
677         OBD_FREE_PTR(m);
678         RETURN(NULL);
679 }
680
681 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
682                                           struct lu_device_type *t,
683                                           struct lustre_cfg *cfg)
684 {
685         struct ofd_device *m;
686         struct lu_device  *l;
687         int                rc;
688
689         OBD_ALLOC_PTR(m);
690         if (m == NULL)
691                 return ERR_PTR(-ENOMEM);
692
693         l = &m->ofd_dt_dev.dd_lu_dev;
694         dt_device_init(&m->ofd_dt_dev, t);
695         rc = ofd_init0(env, m, t, cfg);
696         if (rc != 0) {
697                 ofd_device_free(env, l);
698                 l = ERR_PTR(rc);
699         }
700
701         return l;
702 }
703
704 /* thread context key constructor/destructor */
705 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
706
707 static void ofd_key_exit(const struct lu_context *ctx,
708                          struct lu_context_key *key, void *data)
709 {
710         struct ofd_thread_info *info = data;
711
712         info->fti_env = NULL;
713         info->fti_exp = NULL;
714
715         info->fti_xid = 0;
716         info->fti_transno = 0;
717         info->fti_pre_version = 0;
718         info->fti_obj = NULL;
719         info->fti_has_trans = 0;
720         info->fti_mult_trans = 0;
721         info->fti_used = 0;
722
723         memset(&info->fti_attr, 0, sizeof info->fti_attr);
724 }
725
726 struct lu_context_key ofd_thread_key = {
727         .lct_tags = LCT_DT_THREAD,
728         .lct_init = ofd_key_init,
729         .lct_fini = ofd_key_fini,
730         .lct_exit = ofd_key_exit
731 };
732
733 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
734 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
735
736 static struct lu_device_type_operations ofd_device_type_ops = {
737         .ldto_init              = ofd_type_init,
738         .ldto_fini              = ofd_type_fini,
739
740         .ldto_start             = ofd_type_start,
741         .ldto_stop              = ofd_type_stop,
742
743         .ldto_device_alloc      = ofd_device_alloc,
744         .ldto_device_free       = ofd_device_free,
745         .ldto_device_fini       = ofd_device_fini
746 };
747
748 static struct lu_device_type ofd_device_type = {
749         .ldt_tags       = LU_DEVICE_DT,
750         .ldt_name       = LUSTRE_OST_NAME,
751         .ldt_ops        = &ofd_device_type_ops,
752         .ldt_ctx_tags   = LCT_DT_THREAD
753 };
754
755 int __init ofd_init(void)
756 {
757         struct lprocfs_static_vars      lvars;
758         int                             rc;
759
760         rc = lu_kmem_init(ofd_caches);
761         if (rc)
762                 return rc;
763
764         rc = ofd_fmd_init();
765         if (rc) {
766                 lu_kmem_fini(ofd_caches);
767                 return(rc);
768         }
769
770         lprocfs_ofd_init_vars(&lvars);
771
772         rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
773                                  LUSTRE_OST_NAME, &ofd_device_type);
774         return rc;
775 }
776
777 void __exit ofd_exit(void)
778 {
779         ofd_fmd_exit();
780         lu_kmem_fini(ofd_caches);
781         class_unregister_type(LUSTRE_OST_NAME);
782 }
783
784 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
785 MODULE_DESCRIPTION("Lustre Object Filtering Device");
786 MODULE_LICENSE("GPL");
787
788 module_init(ofd_init);
789 module_exit(ofd_exit);