Whamcloud - gitweb
LU-2484 obd: add md_stats to MDC and LMV devices
[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         obd->obd_uses_nid_stats = 1;
452
453         entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
454         if (IS_ERR(entry)) {
455                 rc = PTR_ERR(entry);
456                 CERROR("%s: error %d setting up lprocfs for %s\n",
457                        obd->obd_name, rc, "exports");
458                 GOTO(obd_cleanup, rc);
459         }
460         obd->obd_proc_exports_entry = entry;
461
462         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
463                                    lprocfs_nid_stats_clear_read,
464                                    lprocfs_nid_stats_clear_write, obd, NULL);
465         if (IS_ERR(entry)) {
466                 rc = PTR_ERR(entry);
467                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
468                        obd->obd_name, rc);
469                 GOTO(obd_cleanup, rc);
470         }
471
472         rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
473                                     ofd_stats_counter_init);
474         if (rc)
475                 GOTO(remove_entry_clear, rc);
476         RETURN(0);
477 remove_entry_clear:
478         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
479 obd_cleanup:
480         lprocfs_obd_cleanup(obd);
481         lprocfs_free_obd_stats(obd);
482
483         return rc;
484 }
485
486 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
487 {
488         struct obd_device       *obd = ofd_obd(ofd);
489         struct obd_device       *osd_obd = ofd->ofd_osd_exp->exp_obd;
490         cfs_proc_dir_entry_t    *osd_root = osd_obd->obd_type->typ_procroot;
491         cfs_proc_dir_entry_t    *osd_dir;
492
493         osd_dir = lprocfs_srch(osd_root, obd->obd_name);
494         if (osd_dir == NULL)
495                 return;
496
497         if (lprocfs_srch(osd_dir, "brw_stats") != NULL)
498                 lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
499                                     "../../%s/%s/brw_stats",
500                                     osd_root->name, osd_dir->name);
501
502         if (lprocfs_srch(osd_dir, "read_cache_enable") != NULL)
503                 lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
504                                     "../../%s/%s/read_cache_enable",
505                                     osd_root->name, osd_dir->name);
506
507         if (lprocfs_srch(osd_dir, "readcache_max_filesize") != NULL)
508                 lprocfs_add_symlink("readcache_max_filesize",
509                                     obd->obd_proc_entry,
510                                     "../../%s/%s/readcache_max_filesize",
511                                     osd_root->name, osd_dir->name);
512
513         if (lprocfs_srch(osd_dir, "writethrough_cache_enable") != NULL)
514                 lprocfs_add_symlink("writethrough_cache_enable",
515                                     obd->obd_proc_entry,
516                                     "../../%s/%s/writethrough_cache_enable",
517                                     osd_root->name, osd_dir->name);
518 }
519
520 static void ofd_procfs_fini(struct ofd_device *ofd)
521 {
522         struct obd_device *obd = ofd_obd(ofd);
523
524         lprocfs_remove_proc_entry("writethrough_cache_enable",
525                                   obd->obd_proc_entry);
526         lprocfs_remove_proc_entry("readcache_max_filesize",
527                                   obd->obd_proc_entry);
528         lprocfs_remove_proc_entry("read_cache_enable", obd->obd_proc_entry);
529         lprocfs_remove_proc_entry("brw_stats", obd->obd_proc_entry);
530         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
531         lprocfs_free_per_client_stats(obd);
532         lprocfs_obd_cleanup(obd);
533         lprocfs_free_obd_stats(obd);
534         lprocfs_job_stats_fini(obd);
535 }
536
537 extern int ost_handle(struct ptlrpc_request *req);
538
539 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
540 {
541         return seq_site_fini(env, &ofd->ofd_seq_site);
542 }
543
544 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
545 {
546         struct seq_server_site  *ss = &ofd->ofd_seq_site;
547         struct lu_device        *lu = &ofd->ofd_dt_dev.dd_lu_dev;
548         char                    *obd_name = ofd_name(ofd);
549         char                    *name = NULL;
550         int                     rc = 0;
551
552         ss = &ofd->ofd_seq_site;
553         lu->ld_site->ld_seq_site = ss;
554         ss->ss_lu = lu->ld_site;
555         ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
556
557         OBD_ALLOC_PTR(ss->ss_server_seq);
558         if (ss->ss_server_seq == NULL)
559                 GOTO(out_free, rc = -ENOMEM);
560
561         OBD_ALLOC(name, strlen(obd_name) + 10);
562         if (!name) {
563                 OBD_FREE_PTR(ss->ss_server_seq);
564                 ss->ss_server_seq = NULL;
565                 GOTO(out_free, rc = -ENOMEM);
566         }
567
568         rc = seq_server_init(ss->ss_server_seq, ofd->ofd_osd, obd_name,
569                              LUSTRE_SEQ_SERVER, ss, env);
570         if (rc) {
571                 CERROR("%s : seq server init error %d\n", obd_name, rc);
572                 GOTO(out_free, rc);
573         }
574         ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
575
576         OBD_ALLOC_PTR(ss->ss_client_seq);
577         if (ss->ss_client_seq == NULL)
578                 GOTO(out_free, rc = -ENOMEM);
579
580         snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
581         rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
582                              name, NULL);
583         if (rc) {
584                 CERROR("%s : seq client init error %d\n", obd_name, rc);
585                 GOTO(out_free, rc);
586         }
587         OBD_FREE(name, strlen(obd_name) + 10);
588         name = NULL;
589
590         rc = seq_server_set_cli(ss->ss_server_seq, ss->ss_client_seq, env);
591
592 out_free:
593         if (rc) {
594                 if (ss->ss_server_seq) {
595                         seq_server_fini(ss->ss_server_seq, env);
596                         OBD_FREE_PTR(ss->ss_server_seq);
597                         ss->ss_server_seq = NULL;
598                 }
599
600                 if (ss->ss_client_seq) {
601                         seq_client_fini(ss->ss_client_seq);
602                         OBD_FREE_PTR(ss->ss_client_seq);
603                         ss->ss_client_seq = NULL;
604                 }
605
606                 if (name) {
607                         OBD_FREE(name, strlen(obd_name) + 10);
608                         name = NULL;
609                 }
610         }
611
612         return rc;
613 }
614
615 static struct tgt_opc_slice ofd_common_slice[] = {
616         {
617                 .tos_opc_start = UPDATE_OBJ,
618                 .tos_opc_end   = UPDATE_LAST_OPC,
619                 .tos_hs        = tgt_out_handlers
620         },
621         {
622                 .tos_opc_start  = SEQ_FIRST_OPC,
623                 .tos_opc_end    = SEQ_LAST_OPC,
624                 .tos_hs         = seq_handlers
625         },
626         {
627                 .tos_hs         = NULL
628         }
629 };
630
631 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
632                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
633 {
634         const char              *dev = lustre_cfg_string(cfg, 0);
635         struct ofd_thread_info  *info = NULL;
636         struct obd_device       *obd;
637         struct obd_statfs       *osfs;
638         int                      rc;
639
640         ENTRY;
641
642         obd = class_name2obd(dev);
643         if (obd == NULL) {
644                 CERROR("Cannot find obd with name %s\n", dev);
645                 RETURN(-ENODEV);
646         }
647
648         rc = lu_env_refill((struct lu_env *)env);
649         if (rc != 0)
650                 RETURN(rc);
651
652         obd->u.obt.obt_magic = OBT_MAGIC;
653
654         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
655         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
656
657         spin_lock_init(&m->ofd_flags_lock);
658         m->ofd_raid_degraded = 0;
659         m->ofd_syncjournal = 0;
660         ofd_slc_set(m);
661         m->ofd_grant_compat_disable = 0;
662
663         /* statfs data */
664         spin_lock_init(&m->ofd_osfs_lock);
665         m->ofd_osfs_age = cfs_time_shift_64(-1000);
666         m->ofd_osfs_unstable = 0;
667         m->ofd_statfs_inflight = 0;
668         m->ofd_osfs_inflight = 0;
669
670         /* grant data */
671         spin_lock_init(&m->ofd_grant_lock);
672         m->ofd_tot_dirty = 0;
673         m->ofd_tot_granted = 0;
674         m->ofd_tot_pending = 0;
675         m->ofd_seq_count = 0;
676
677         spin_lock_init(&m->ofd_batch_lock);
678         rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
679         sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
680
681         obd->u.filter.fo_fl_oss_capa = 0;
682         CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
683         obd->u.filter.fo_capa_hash = init_capa_hash();
684         if (obd->u.filter.fo_capa_hash == NULL)
685                 RETURN(-ENOMEM);
686
687         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
688         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
689         /* set this lu_device to obd, because error handling need it */
690         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
691
692         rc = ofd_procfs_init(m);
693         if (rc) {
694                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
695                 RETURN(rc);
696         }
697
698         /* No connection accepted until configurations will finish */
699         spin_lock(&obd->obd_dev_lock);
700         obd->obd_no_conn = 1;
701         spin_unlock(&obd->obd_dev_lock);
702         obd->obd_replayable = 1;
703         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
704                 char *str = lustre_cfg_string(cfg, 4);
705
706                 if (strchr(str, 'n')) {
707                         CWARN("%s: recovery disabled\n", obd->obd_name);
708                         obd->obd_replayable = 0;
709                 }
710         }
711
712         info = ofd_info_init(env, NULL);
713         if (info == NULL)
714                 RETURN(-EFAULT);
715
716         rc = ofd_stack_init(env, m, cfg);
717         if (rc) {
718                 CERROR("Can't init device stack, rc %d\n", rc);
719                 GOTO(err_fini_proc, rc);
720         }
721
722         ofd_procfs_add_brw_stats_symlink(m);
723
724         /* populate cached statfs data */
725         osfs = &ofd_info(env)->fti_u.osfs;
726         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
727         if (rc != 0) {
728                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
729                 GOTO(err_fini_stack, rc);
730         }
731         if (!IS_PO2(osfs->os_bsize)) {
732                 CERROR("%s: blocksize (%d) is not a power of 2\n",
733                                 obd->obd_name, osfs->os_bsize);
734                 GOTO(err_fini_stack, rc = -EPROTO);
735         }
736         m->ofd_blockbits = fls(osfs->os_bsize) - 1;
737
738         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
739         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
740                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
741
742         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
743                  "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
744         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
745                                               LDLM_NAMESPACE_SERVER,
746                                               LDLM_NAMESPACE_GREEDY,
747                                               LDLM_NS_TYPE_OST);
748         if (m->ofd_namespace == NULL)
749                 GOTO(err_fini_stack, rc = -ENOMEM);
750         /* set obd_namespace for compatibility with old code */
751         obd->obd_namespace = m->ofd_namespace;
752         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
753         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
754         m->ofd_namespace->ns_lvbp = m;
755
756         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
757                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
758
759         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
760
761         /* Allow at most ddp_grant_reserved% of the available filesystem space
762          * to be granted to clients, so that any errors in the grant overhead
763          * calculations do not allow granting more space to clients than can be
764          * written. Assumes that in aggregate the grant overhead calculations do
765          * not have more than ddp_grant_reserved% estimation error in them. */
766         m->ofd_grant_ratio =
767                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
768
769         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
770                       OBD_FAIL_OST_ALL_REQUEST_NET,
771                       OBD_FAIL_OST_ALL_REPLY_NET);
772         if (rc)
773                 GOTO(err_free_ns, rc);
774
775         rc = ofd_fs_setup(env, m, obd);
776         if (rc)
777                 GOTO(err_fini_lut, rc);
778
779         RETURN(0);
780 err_fini_lut:
781         tgt_fini(env, &m->ofd_lut);
782 err_free_ns:
783         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
784         obd->obd_namespace = m->ofd_namespace = NULL;
785 err_fini_stack:
786         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
787 err_fini_proc:
788         ofd_procfs_fini(m);
789         return rc;
790 }
791
792 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
793 {
794         struct obd_device *obd = ofd_obd(m);
795         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
796
797         lfsck_stop(env, m->ofd_osd, true);
798         lfsck_degister(env, m->ofd_osd);
799         target_recovery_fini(obd);
800         obd_exports_barrier(obd);
801         obd_zombie_barrier();
802
803         tgt_fini(env, &m->ofd_lut);
804         ofd_fs_cleanup(env, m);
805
806         ofd_free_capa_keys(m);
807         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
808
809         if (m->ofd_namespace != NULL) {
810                 ldlm_namespace_free(m->ofd_namespace, NULL,
811                                     d->ld_obd->obd_force);
812                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
813         }
814
815         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
816         ofd_procfs_fini(m);
817         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
818         server_put_mount(obd->obd_name, NULL);
819         EXIT;
820 }
821
822 static struct lu_device *ofd_device_fini(const struct lu_env *env,
823                                          struct lu_device *d)
824 {
825         ENTRY;
826         ofd_fini(env, ofd_dev(d));
827         RETURN(NULL);
828 }
829
830 static struct lu_device *ofd_device_free(const struct lu_env *env,
831                                          struct lu_device *d)
832 {
833         struct ofd_device *m = ofd_dev(d);
834
835         dt_device_fini(&m->ofd_dt_dev);
836         OBD_FREE_PTR(m);
837         RETURN(NULL);
838 }
839
840 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
841                                           struct lu_device_type *t,
842                                           struct lustre_cfg *cfg)
843 {
844         struct ofd_device *m;
845         struct lu_device  *l;
846         int                rc;
847
848         OBD_ALLOC_PTR(m);
849         if (m == NULL)
850                 return ERR_PTR(-ENOMEM);
851
852         l = &m->ofd_dt_dev.dd_lu_dev;
853         dt_device_init(&m->ofd_dt_dev, t);
854         rc = ofd_init0(env, m, t, cfg);
855         if (rc != 0) {
856                 ofd_device_free(env, l);
857                 l = ERR_PTR(rc);
858         }
859
860         return l;
861 }
862
863 /* thread context key constructor/destructor */
864 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
865
866 static void ofd_key_exit(const struct lu_context *ctx,
867                          struct lu_context_key *key, void *data)
868 {
869         struct ofd_thread_info *info = data;
870
871         info->fti_env = NULL;
872         info->fti_exp = NULL;
873
874         info->fti_xid = 0;
875         info->fti_transno = 0;
876         info->fti_pre_version = 0;
877         info->fti_obj = NULL;
878         info->fti_has_trans = 0;
879         info->fti_mult_trans = 0;
880         info->fti_used = 0;
881
882         memset(&info->fti_attr, 0, sizeof info->fti_attr);
883 }
884
885 struct lu_context_key ofd_thread_key = {
886         .lct_tags = LCT_DT_THREAD,
887         .lct_init = ofd_key_init,
888         .lct_fini = ofd_key_fini,
889         .lct_exit = ofd_key_exit
890 };
891
892 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
893 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
894
895 static struct lu_device_type_operations ofd_device_type_ops = {
896         .ldto_init              = ofd_type_init,
897         .ldto_fini              = ofd_type_fini,
898
899         .ldto_start             = ofd_type_start,
900         .ldto_stop              = ofd_type_stop,
901
902         .ldto_device_alloc      = ofd_device_alloc,
903         .ldto_device_free       = ofd_device_free,
904         .ldto_device_fini       = ofd_device_fini
905 };
906
907 static struct lu_device_type ofd_device_type = {
908         .ldt_tags       = LU_DEVICE_DT,
909         .ldt_name       = LUSTRE_OST_NAME,
910         .ldt_ops        = &ofd_device_type_ops,
911         .ldt_ctx_tags   = LCT_DT_THREAD
912 };
913
914 int __init ofd_init(void)
915 {
916         struct lprocfs_static_vars      lvars;
917         int                             rc;
918
919         rc = lu_kmem_init(ofd_caches);
920         if (rc)
921                 return rc;
922
923         rc = ofd_fmd_init();
924         if (rc) {
925                 lu_kmem_fini(ofd_caches);
926                 return(rc);
927         }
928
929         lprocfs_ofd_init_vars(&lvars);
930
931         rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
932                                  LUSTRE_OST_NAME, &ofd_device_type);
933         return rc;
934 }
935
936 void __exit ofd_exit(void)
937 {
938         ofd_fmd_exit();
939         lu_kmem_fini(ofd_caches);
940         class_unregister_type(LUSTRE_OST_NAME);
941 }
942
943 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
944 MODULE_DESCRIPTION("Lustre Object Filtering Device");
945 MODULE_LICENSE("GPL");
946
947 module_init(ofd_init);
948 module_exit(ofd_exit);