Whamcloud - gitweb
ab9b24c8d419bbc368ddcd940011b9b122277d8a
[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, 2013, 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 #include <lustre_fid.h>
48 #include <lustre_lfsck.h>
49
50 #include "ofd_internal.h"
51
52 /* Slab for OFD object allocation */
53 static struct kmem_cache *ofd_object_kmem;
54
55 static struct lu_kmem_descr ofd_caches[] = {
56         {
57                 .ckd_cache = &ofd_object_kmem,
58                 .ckd_name  = "ofd_obj",
59                 .ckd_size  = sizeof(struct ofd_object)
60         },
61         {
62                 .ckd_cache = NULL
63         }
64 };
65
66 static int ofd_connect_to_next(const struct lu_env *env, struct ofd_device *m,
67                                const char *next, struct obd_export **exp)
68 {
69         struct obd_connect_data *data = NULL;
70         struct obd_device       *obd;
71         int                      rc;
72         ENTRY;
73
74         OBD_ALLOC_PTR(data);
75         if (data == NULL)
76                 GOTO(out, rc = -ENOMEM);
77
78         obd = class_name2obd(next);
79         if (obd == NULL) {
80                 CERROR("%s: can't locate next device: %s\n",
81                        m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, next);
82                 GOTO(out, rc = -ENOTCONN);
83         }
84
85         data->ocd_connect_flags = OBD_CONNECT_VERSION;
86         data->ocd_version = LUSTRE_VERSION_CODE;
87
88         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
89         if (rc) {
90                 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
91                        m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, next, rc);
92                 GOTO(out, rc);
93         }
94
95         m->ofd_dt_dev.dd_lu_dev.ld_site =
96                 m->ofd_osd_exp->exp_obd->obd_lu_dev->ld_site;
97         LASSERT(m->ofd_dt_dev.dd_lu_dev.ld_site);
98         m->ofd_osd = lu2dt_dev(m->ofd_osd_exp->exp_obd->obd_lu_dev);
99         m->ofd_dt_dev.dd_lu_dev.ld_site->ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
100
101 out:
102         if (data)
103                 OBD_FREE_PTR(data);
104         RETURN(rc);
105 }
106
107 static int ofd_stack_init(const struct lu_env *env,
108                           struct ofd_device *m, struct lustre_cfg *cfg)
109 {
110         const char              *dev = lustre_cfg_string(cfg, 0);
111         struct lu_device        *d;
112         struct ofd_thread_info  *info = ofd_info(env);
113         struct lustre_mount_info *lmi;
114         int                      rc;
115         char                    *osdname;
116
117         ENTRY;
118
119         lmi = server_get_mount(dev);
120         if (lmi == NULL) {
121                 CERROR("Cannot get mount info for %s!\n", dev);
122                 RETURN(-ENODEV);
123         }
124
125         /* find bottom osd */
126         OBD_ALLOC(osdname, MTI_NAME_MAXLEN);
127         if (osdname == NULL)
128                 RETURN(-ENOMEM);
129
130         snprintf(osdname, MTI_NAME_MAXLEN, "%s-osd", dev);
131         rc = ofd_connect_to_next(env, m, osdname, &m->ofd_osd_exp);
132         OBD_FREE(osdname, MTI_NAME_MAXLEN);
133         if (rc)
134                 RETURN(rc);
135
136         d = m->ofd_osd_exp->exp_obd->obd_lu_dev;
137         LASSERT(d);
138         m->ofd_osd = lu2dt_dev(d);
139
140         snprintf(info->fti_u.name, sizeof(info->fti_u.name),
141                  "%s-osd", lustre_cfg_string(cfg, 0));
142
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_site_purge(env, top->ld_site, ~0);
176
177         LASSERT(m->ofd_osd_exp);
178         obd_disconnect(m->ofd_osd_exp);
179         m->ofd_osd = NULL;
180
181         EXIT;
182 }
183
184 /* For interoperability, see mdt_interop_param[]. */
185 static struct cfg_interop_param ofd_interop_param[] = {
186         { "ost.quota_type",     NULL },
187         { NULL }
188 };
189
190 /* used by MGS to process specific configurations */
191 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
192                               struct lustre_cfg *cfg)
193 {
194         struct ofd_device       *m = ofd_dev(d);
195         struct dt_device        *dt_next = m->ofd_osd;
196         struct lu_device        *next = &dt_next->dd_lu_dev;
197         int                      rc;
198
199         ENTRY;
200
201         switch (cfg->lcfg_command) {
202         case LCFG_PARAM: {
203                 struct lprocfs_static_vars lvars;
204
205                 /* For interoperability */
206                 struct cfg_interop_param   *ptr = NULL;
207                 struct lustre_cfg          *old_cfg = NULL;
208                 char                       *param = NULL;
209
210                 param = lustre_cfg_string(cfg, 1);
211                 if (param == NULL) {
212                         CERROR("param is empty\n");
213                         rc = -EINVAL;
214                         break;
215                 }
216
217                 ptr = class_find_old_param(param, ofd_interop_param);
218                 if (ptr != NULL) {
219                         if (ptr->new_param == NULL) {
220                                 rc = 0;
221                                 CWARN("For interoperability, skip this %s."
222                                       " It is obsolete.\n", ptr->old_param);
223                                 break;
224                         }
225
226                         CWARN("Found old param %s, changed it to %s.\n",
227                               ptr->old_param, ptr->new_param);
228
229                         old_cfg = cfg;
230                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
231                         if (IS_ERR(cfg)) {
232                                 rc = PTR_ERR(cfg);
233                                 break;
234                         }
235                 }
236
237                 lprocfs_ofd_init_vars(&lvars);
238                 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
239                                               d->ld_obd);
240                 if (rc > 0 || rc == -ENOSYS)
241                         /* we don't understand; pass it on */
242                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
243                 break;
244         }
245         case LCFG_SPTLRPC_CONF: {
246                 rc = -ENOTSUPP;
247                 break;
248         }
249         default:
250                 /* others are passed further */
251                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
252                 break;
253         }
254         RETURN(rc);
255 }
256
257 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
258                            const struct lu_object_conf *conf)
259 {
260         struct ofd_device       *d = ofd_dev(o->lo_dev);
261         struct lu_device        *under;
262         struct lu_object        *below;
263         int                      rc = 0;
264
265         ENTRY;
266
267         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
268                PFID(lu_object_fid(o)));
269
270         under = &d->ofd_osd->dd_lu_dev;
271         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
272         if (below != NULL)
273                 lu_object_add(o, below);
274         else
275                 rc = -ENOMEM;
276
277         RETURN(rc);
278 }
279
280 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
281 {
282         struct ofd_object       *of = ofd_obj(o);
283         struct lu_object_header *h;
284
285         ENTRY;
286
287         h = o->lo_header;
288         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
289                PFID(lu_object_fid(o)));
290
291         lu_object_fini(o);
292         lu_object_header_fini(h);
293         OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
294         EXIT;
295 }
296
297 static int ofd_object_print(const struct lu_env *env, void *cookie,
298                             lu_printer_t p, const struct lu_object *o)
299 {
300         return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
301 }
302
303 struct lu_object_operations ofd_obj_ops = {
304         .loo_object_init        = ofd_object_init,
305         .loo_object_free        = ofd_object_free,
306         .loo_object_print       = ofd_object_print
307 };
308
309 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
310                                           const struct lu_object_header *hdr,
311                                           struct lu_device *d)
312 {
313         struct ofd_object *of;
314
315         ENTRY;
316
317         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, __GFP_IO);
318         if (of != NULL) {
319                 struct lu_object        *o;
320                 struct lu_object_header *h;
321
322                 o = &of->ofo_obj.do_lu;
323                 h = &of->ofo_header;
324                 lu_object_header_init(h);
325                 lu_object_init(o, h, d);
326                 lu_object_add_top(h, o);
327                 o->lo_ops = &ofd_obj_ops;
328                 RETURN(o);
329         } else {
330                 RETURN(NULL);
331         }
332 }
333
334 extern int ost_handle(struct ptlrpc_request *req);
335
336 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
337                        struct lu_device *dev)
338 {
339         struct ofd_thread_info          *info;
340         struct ofd_device               *ofd = ofd_dev(dev);
341         struct obd_device               *obd = ofd_obd(ofd);
342         struct lu_device                *next = &ofd->ofd_osd->dd_lu_dev;
343         struct lfsck_start_param         lsp;
344         int                              rc;
345
346         ENTRY;
347
348         rc = lu_env_refill((struct lu_env *)env);
349         if (rc != 0) {
350                 CERROR("Failure to refill session: '%d'\n", rc);
351                 RETURN(rc);
352         }
353
354         info = ofd_info_init(env, NULL);
355         if (info == NULL)
356                 RETURN(-EFAULT);
357
358         /* initialize lower device */
359         rc = next->ld_ops->ldo_prepare(env, dev, next);
360         if (rc != 0)
361                 RETURN(rc);
362
363         rc = lfsck_register(env, ofd->ofd_osd, &ofd->ofd_dt_dev, false);
364         if (rc != 0) {
365                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
366                        obd->obd_name, rc);
367                 RETURN(rc);
368         }
369
370         lsp.lsp_start = NULL;
371         lsp.lsp_namespace = ofd->ofd_namespace;
372         rc = lfsck_start(env, ofd->ofd_osd, &lsp);
373         if (rc != 0) {
374                 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
375                       obd->obd_name, rc);
376                 rc = 0;
377         }
378
379         target_recovery_init(&ofd->ofd_lut, ost_handle);
380         LASSERT(obd->obd_no_conn);
381         spin_lock(&obd->obd_dev_lock);
382         obd->obd_no_conn = 0;
383         spin_unlock(&obd->obd_dev_lock);
384
385         if (obd->obd_recovering == 0)
386                 ofd_postrecov(env, ofd);
387
388         RETURN(rc);
389 }
390
391 static int ofd_recovery_complete(const struct lu_env *env,
392                                  struct lu_device *dev)
393 {
394         struct ofd_device       *ofd = ofd_dev(dev);
395         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
396         int                      rc = 0, max_precreate;
397
398         ENTRY;
399
400         /* Grant space for object precreation on the self export.
401          * This initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
402          * is enough to create 10k objects. More space is then acquired for
403          * precreation in ofd_grant_create().
404          */
405         max_precreate = OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace / 2;
406         ofd_grant_connect(env, dev->ld_obd->obd_self_export, max_precreate,
407                           false);
408         rc = next->ld_ops->ldo_recovery_complete(env, next);
409         RETURN(rc);
410 }
411
412 static struct lu_device_operations ofd_lu_ops = {
413         .ldo_object_alloc       = ofd_object_alloc,
414         .ldo_process_config     = ofd_process_config,
415         .ldo_recovery_complete  = ofd_recovery_complete,
416         .ldo_prepare            = ofd_prepare,
417 };
418
419 static int ofd_procfs_init(struct ofd_device *ofd)
420 {
421         struct lprocfs_static_vars       lvars;
422         struct obd_device               *obd = ofd_obd(ofd);
423         cfs_proc_dir_entry_t            *entry;
424         int                              rc = 0;
425
426         ENTRY;
427
428         /* lprocfs must be setup before the ofd so state can be safely added
429          * to /proc incrementally as the ofd is setup */
430         lprocfs_ofd_init_vars(&lvars);
431         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
432         if (rc) {
433                 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
434                        obd->obd_name, rc);
435                 RETURN(rc);
436         }
437
438         rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_LAST);
439         if (rc) {
440                 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
441                        obd->obd_name, rc);
442                 GOTO(obd_cleanup, rc);
443         }
444
445         /* Init OFD private stats here */
446         lprocfs_counter_init(obd->obd_stats, LPROC_OFD_READ_BYTES,
447                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
448         lprocfs_counter_init(obd->obd_stats, LPROC_OFD_WRITE_BYTES,
449                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
450
451         entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
452         if (IS_ERR(entry)) {
453                 rc = PTR_ERR(entry);
454                 CERROR("%s: error %d setting up lprocfs for %s\n",
455                        obd->obd_name, rc, "exports");
456                 GOTO(obd_cleanup, rc);
457         }
458         obd->obd_proc_exports_entry = entry;
459
460         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
461                                    lprocfs_nid_stats_clear_read,
462                                    lprocfs_nid_stats_clear_write, obd, NULL);
463         if (IS_ERR(entry)) {
464                 rc = PTR_ERR(entry);
465                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
466                        obd->obd_name, rc);
467                 GOTO(obd_cleanup, rc);
468         }
469
470         rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
471                                     ofd_stats_counter_init);
472         if (rc)
473                 GOTO(remove_entry_clear, rc);
474         RETURN(0);
475 remove_entry_clear:
476         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
477 obd_cleanup:
478         lprocfs_obd_cleanup(obd);
479         lprocfs_free_obd_stats(obd);
480
481         return rc;
482 }
483
484 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
485 {
486         struct obd_device       *obd = ofd_obd(ofd);
487         struct obd_device       *osd_obd = ofd->ofd_osd_exp->exp_obd;
488         cfs_proc_dir_entry_t    *osd_root = osd_obd->obd_type->typ_procroot;
489         cfs_proc_dir_entry_t    *osd_dir;
490
491         osd_dir = lprocfs_srch(osd_root, obd->obd_name);
492         if (osd_dir == NULL)
493                 return;
494
495         if (lprocfs_srch(osd_dir, "brw_stats") != NULL)
496                 lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
497                                     "../../%s/%s/brw_stats",
498                                     osd_root->name, osd_dir->name);
499
500         if (lprocfs_srch(osd_dir, "read_cache_enable") != NULL)
501                 lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
502                                     "../../%s/%s/read_cache_enable",
503                                     osd_root->name, osd_dir->name);
504
505         if (lprocfs_srch(osd_dir, "readcache_max_filesize") != NULL)
506                 lprocfs_add_symlink("readcache_max_filesize",
507                                     obd->obd_proc_entry,
508                                     "../../%s/%s/readcache_max_filesize",
509                                     osd_root->name, osd_dir->name);
510
511         if (lprocfs_srch(osd_dir, "writethrough_cache_enable") != NULL)
512                 lprocfs_add_symlink("writethrough_cache_enable",
513                                     obd->obd_proc_entry,
514                                     "../../%s/%s/writethrough_cache_enable",
515                                     osd_root->name, osd_dir->name);
516 }
517
518 static void ofd_procfs_fini(struct ofd_device *ofd)
519 {
520         struct obd_device *obd = ofd_obd(ofd);
521
522         lprocfs_remove_proc_entry("writethrough_cache_enable",
523                                   obd->obd_proc_entry);
524         lprocfs_remove_proc_entry("readcache_max_filesize",
525                                   obd->obd_proc_entry);
526         lprocfs_remove_proc_entry("read_cache_enable", obd->obd_proc_entry);
527         lprocfs_remove_proc_entry("brw_stats", obd->obd_proc_entry);
528         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
529         lprocfs_free_per_client_stats(obd);
530         lprocfs_obd_cleanup(obd);
531         lprocfs_free_obd_stats(obd);
532         lprocfs_job_stats_fini(obd);
533 }
534
535 extern int ost_handle(struct ptlrpc_request *req);
536
537 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
538 {
539         return seq_site_fini(env, &ofd->ofd_seq_site);
540 }
541
542 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
543 {
544         struct seq_server_site  *ss = &ofd->ofd_seq_site;
545         struct lu_device        *lu = &ofd->ofd_dt_dev.dd_lu_dev;
546         char                    *obd_name = ofd_name(ofd);
547         char                    *name = NULL;
548         int                     rc = 0;
549
550         ss = &ofd->ofd_seq_site;
551         lu->ld_site->ld_seq_site = ss;
552         ss->ss_lu = lu->ld_site;
553         ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
554
555         OBD_ALLOC_PTR(ss->ss_server_seq);
556         if (ss->ss_server_seq == NULL)
557                 GOTO(out_free, rc = -ENOMEM);
558
559         OBD_ALLOC(name, strlen(obd_name) + 10);
560         if (!name) {
561                 OBD_FREE_PTR(ss->ss_server_seq);
562                 ss->ss_server_seq = NULL;
563                 GOTO(out_free, rc = -ENOMEM);
564         }
565
566         rc = seq_server_init(ss->ss_server_seq, ofd->ofd_osd, obd_name,
567                              LUSTRE_SEQ_SERVER, ss, env);
568         if (rc) {
569                 CERROR("%s : seq server init error %d\n", obd_name, rc);
570                 GOTO(out_free, rc);
571         }
572         ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
573
574         OBD_ALLOC_PTR(ss->ss_client_seq);
575         if (ss->ss_client_seq == NULL)
576                 GOTO(out_free, rc = -ENOMEM);
577
578         snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
579         rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
580                              name, NULL);
581         if (rc) {
582                 CERROR("%s : seq client init error %d\n", obd_name, rc);
583                 GOTO(out_free, rc);
584         }
585         OBD_FREE(name, strlen(obd_name) + 10);
586         name = NULL;
587
588         rc = seq_server_set_cli(ss->ss_server_seq, ss->ss_client_seq, env);
589
590 out_free:
591         if (rc) {
592                 if (ss->ss_server_seq) {
593                         seq_server_fini(ss->ss_server_seq, env);
594                         OBD_FREE_PTR(ss->ss_server_seq);
595                         ss->ss_server_seq = NULL;
596                 }
597
598                 if (ss->ss_client_seq) {
599                         seq_client_fini(ss->ss_client_seq);
600                         OBD_FREE_PTR(ss->ss_client_seq);
601                         ss->ss_client_seq = NULL;
602                 }
603
604                 if (name) {
605                         OBD_FREE(name, strlen(obd_name) + 10);
606                         name = NULL;
607                 }
608         }
609
610         return rc;
611 }
612
613 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
614                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
615 {
616         const char              *dev = lustre_cfg_string(cfg, 0);
617         struct ofd_thread_info  *info = NULL;
618         struct obd_device       *obd;
619         struct obd_statfs       *osfs;
620         int                      rc;
621
622         ENTRY;
623
624         obd = class_name2obd(dev);
625         if (obd == NULL) {
626                 CERROR("Cannot find obd with name %s\n", dev);
627                 RETURN(-ENODEV);
628         }
629
630         rc = lu_env_refill((struct lu_env *)env);
631         if (rc != 0)
632                 RETURN(rc);
633
634         obd->u.obt.obt_magic = OBT_MAGIC;
635
636         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
637         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
638
639         spin_lock_init(&m->ofd_flags_lock);
640         m->ofd_raid_degraded = 0;
641         m->ofd_syncjournal = 0;
642         ofd_slc_set(m);
643         m->ofd_grant_compat_disable = 0;
644
645         /* statfs data */
646         spin_lock_init(&m->ofd_osfs_lock);
647         m->ofd_osfs_age = cfs_time_shift_64(-1000);
648         m->ofd_osfs_unstable = 0;
649         m->ofd_statfs_inflight = 0;
650         m->ofd_osfs_inflight = 0;
651
652         /* grant data */
653         spin_lock_init(&m->ofd_grant_lock);
654         m->ofd_tot_dirty = 0;
655         m->ofd_tot_granted = 0;
656         m->ofd_tot_pending = 0;
657         m->ofd_seq_count = 0;
658
659         spin_lock_init(&m->ofd_batch_lock);
660         rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
661         sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
662
663         obd->u.filter.fo_fl_oss_capa = 0;
664         CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
665         obd->u.filter.fo_capa_hash = init_capa_hash();
666         if (obd->u.filter.fo_capa_hash == NULL)
667                 RETURN(-ENOMEM);
668
669         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
670         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
671         /* set this lu_device to obd, because error handling need it */
672         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
673
674         rc = ofd_procfs_init(m);
675         if (rc) {
676                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
677                 RETURN(rc);
678         }
679
680         /* No connection accepted until configurations will finish */
681         spin_lock(&obd->obd_dev_lock);
682         obd->obd_no_conn = 1;
683         spin_unlock(&obd->obd_dev_lock);
684         obd->obd_replayable = 1;
685         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
686                 char *str = lustre_cfg_string(cfg, 4);
687
688                 if (strchr(str, 'n')) {
689                         CWARN("%s: recovery disabled\n", obd->obd_name);
690                         obd->obd_replayable = 0;
691                 }
692         }
693
694         info = ofd_info_init(env, NULL);
695         if (info == NULL)
696                 RETURN(-EFAULT);
697
698         rc = ofd_stack_init(env, m, cfg);
699         if (rc) {
700                 CERROR("Can't init device stack, rc %d\n", rc);
701                 GOTO(err_fini_proc, rc);
702         }
703
704         ofd_procfs_add_brw_stats_symlink(m);
705
706         /* populate cached statfs data */
707         osfs = &ofd_info(env)->fti_u.osfs;
708         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
709         if (rc != 0) {
710                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
711                 GOTO(err_fini_stack, rc);
712         }
713         if (!IS_PO2(osfs->os_bsize)) {
714                 CERROR("%s: blocksize (%d) is not a power of 2\n",
715                                 obd->obd_name, osfs->os_bsize);
716                 GOTO(err_fini_stack, rc = -EPROTO);
717         }
718         m->ofd_blockbits = fls(osfs->os_bsize) - 1;
719
720         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
721         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
722                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
723
724         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
725                  "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
726         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
727                                               LDLM_NAMESPACE_SERVER,
728                                               LDLM_NAMESPACE_GREEDY,
729                                               LDLM_NS_TYPE_OST);
730         if (m->ofd_namespace == NULL)
731                 GOTO(err_fini_stack, rc = -ENOMEM);
732         /* set obd_namespace for compatibility with old code */
733         obd->obd_namespace = m->ofd_namespace;
734         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
735         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
736         m->ofd_namespace->ns_lvbp = m;
737
738         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
739                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
740
741         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
742
743         /* Allow at most ddp_grant_reserved% of the available filesystem space
744          * to be granted to clients, so that any errors in the grant overhead
745          * calculations do not allow granting more space to clients than can be
746          * written. Assumes that in aggregate the grant overhead calculations do
747          * not have more than ddp_grant_reserved% estimation error in them. */
748         m->ofd_grant_ratio =
749                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
750
751         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, NULL,
752                       OBD_FAIL_OST_ALL_REQUEST_NET,
753                       OBD_FAIL_OST_ALL_REPLY_NET);
754         if (rc)
755                 GOTO(err_free_ns, rc);
756
757         rc = ofd_fs_setup(env, m, obd);
758         if (rc)
759                 GOTO(err_fini_lut, rc);
760
761         RETURN(0);
762 err_fini_lut:
763         tgt_fini(env, &m->ofd_lut);
764 err_free_ns:
765         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
766         obd->obd_namespace = m->ofd_namespace = NULL;
767 err_fini_stack:
768         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
769 err_fini_proc:
770         ofd_procfs_fini(m);
771         return rc;
772 }
773
774 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
775 {
776         struct obd_device *obd = ofd_obd(m);
777         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
778
779         lfsck_stop(env, m->ofd_osd, true);
780         lfsck_degister(env, m->ofd_osd);
781         target_recovery_fini(obd);
782         obd_exports_barrier(obd);
783         obd_zombie_barrier();
784
785         tgt_fini(env, &m->ofd_lut);
786         ofd_fs_cleanup(env, m);
787
788         ofd_free_capa_keys(m);
789         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
790
791         if (m->ofd_namespace != NULL) {
792                 ldlm_namespace_free(m->ofd_namespace, NULL,
793                                     d->ld_obd->obd_force);
794                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
795         }
796
797         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
798         ofd_procfs_fini(m);
799         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
800         server_put_mount(obd->obd_name, NULL);
801         EXIT;
802 }
803
804 static struct lu_device *ofd_device_fini(const struct lu_env *env,
805                                          struct lu_device *d)
806 {
807         ENTRY;
808         ofd_fini(env, ofd_dev(d));
809         RETURN(NULL);
810 }
811
812 static struct lu_device *ofd_device_free(const struct lu_env *env,
813                                          struct lu_device *d)
814 {
815         struct ofd_device *m = ofd_dev(d);
816
817         dt_device_fini(&m->ofd_dt_dev);
818         OBD_FREE_PTR(m);
819         RETURN(NULL);
820 }
821
822 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
823                                           struct lu_device_type *t,
824                                           struct lustre_cfg *cfg)
825 {
826         struct ofd_device *m;
827         struct lu_device  *l;
828         int                rc;
829
830         OBD_ALLOC_PTR(m);
831         if (m == NULL)
832                 return ERR_PTR(-ENOMEM);
833
834         l = &m->ofd_dt_dev.dd_lu_dev;
835         dt_device_init(&m->ofd_dt_dev, t);
836         rc = ofd_init0(env, m, t, cfg);
837         if (rc != 0) {
838                 ofd_device_free(env, l);
839                 l = ERR_PTR(rc);
840         }
841
842         return l;
843 }
844
845 /* thread context key constructor/destructor */
846 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
847
848 static void ofd_key_exit(const struct lu_context *ctx,
849                          struct lu_context_key *key, void *data)
850 {
851         struct ofd_thread_info *info = data;
852
853         info->fti_env = NULL;
854         info->fti_exp = NULL;
855
856         info->fti_xid = 0;
857         info->fti_transno = 0;
858         info->fti_pre_version = 0;
859         info->fti_obj = NULL;
860         info->fti_has_trans = 0;
861         info->fti_mult_trans = 0;
862         info->fti_used = 0;
863
864         memset(&info->fti_attr, 0, sizeof info->fti_attr);
865 }
866
867 struct lu_context_key ofd_thread_key = {
868         .lct_tags = LCT_DT_THREAD,
869         .lct_init = ofd_key_init,
870         .lct_fini = ofd_key_fini,
871         .lct_exit = ofd_key_exit
872 };
873
874 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
875 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
876
877 static struct lu_device_type_operations ofd_device_type_ops = {
878         .ldto_init              = ofd_type_init,
879         .ldto_fini              = ofd_type_fini,
880
881         .ldto_start             = ofd_type_start,
882         .ldto_stop              = ofd_type_stop,
883
884         .ldto_device_alloc      = ofd_device_alloc,
885         .ldto_device_free       = ofd_device_free,
886         .ldto_device_fini       = ofd_device_fini
887 };
888
889 static struct lu_device_type ofd_device_type = {
890         .ldt_tags       = LU_DEVICE_DT,
891         .ldt_name       = LUSTRE_OST_NAME,
892         .ldt_ops        = &ofd_device_type_ops,
893         .ldt_ctx_tags   = LCT_DT_THREAD
894 };
895
896 int __init ofd_init(void)
897 {
898         struct lprocfs_static_vars      lvars;
899         int                             rc;
900
901         rc = lu_kmem_init(ofd_caches);
902         if (rc)
903                 return rc;
904
905         rc = ofd_fmd_init();
906         if (rc) {
907                 lu_kmem_fini(ofd_caches);
908                 return(rc);
909         }
910
911         lprocfs_ofd_init_vars(&lvars);
912
913         rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
914                                  LUSTRE_OST_NAME, &ofd_device_type);
915         return rc;
916 }
917
918 void __exit ofd_exit(void)
919 {
920         ofd_fmd_exit();
921         lu_kmem_fini(ofd_caches);
922         class_unregister_type(LUSTRE_OST_NAME);
923 }
924
925 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
926 MODULE_DESCRIPTION("Lustre Object Filtering Device");
927 MODULE_LICENSE("GPL");
928
929 module_init(ofd_init);
930 module_exit(ofd_exit);