Whamcloud - gitweb
LU-3467 target: move OUT to the unified target code
[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 struct tgt_opc_slice ofd_common_slice[] = {
614         {
615                 .tos_opc_start = UPDATE_OBJ,
616                 .tos_opc_end   = UPDATE_LAST_OPC,
617                 .tos_hs        = tgt_out_handlers
618         },
619         {
620                 .tos_hs         = NULL
621         }
622 };
623
624 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
625                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
626 {
627         const char              *dev = lustre_cfg_string(cfg, 0);
628         struct ofd_thread_info  *info = NULL;
629         struct obd_device       *obd;
630         struct obd_statfs       *osfs;
631         int                      rc;
632
633         ENTRY;
634
635         obd = class_name2obd(dev);
636         if (obd == NULL) {
637                 CERROR("Cannot find obd with name %s\n", dev);
638                 RETURN(-ENODEV);
639         }
640
641         rc = lu_env_refill((struct lu_env *)env);
642         if (rc != 0)
643                 RETURN(rc);
644
645         obd->u.obt.obt_magic = OBT_MAGIC;
646
647         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
648         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
649
650         spin_lock_init(&m->ofd_flags_lock);
651         m->ofd_raid_degraded = 0;
652         m->ofd_syncjournal = 0;
653         ofd_slc_set(m);
654         m->ofd_grant_compat_disable = 0;
655
656         /* statfs data */
657         spin_lock_init(&m->ofd_osfs_lock);
658         m->ofd_osfs_age = cfs_time_shift_64(-1000);
659         m->ofd_osfs_unstable = 0;
660         m->ofd_statfs_inflight = 0;
661         m->ofd_osfs_inflight = 0;
662
663         /* grant data */
664         spin_lock_init(&m->ofd_grant_lock);
665         m->ofd_tot_dirty = 0;
666         m->ofd_tot_granted = 0;
667         m->ofd_tot_pending = 0;
668         m->ofd_seq_count = 0;
669
670         spin_lock_init(&m->ofd_batch_lock);
671         rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
672         sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
673
674         obd->u.filter.fo_fl_oss_capa = 0;
675         CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
676         obd->u.filter.fo_capa_hash = init_capa_hash();
677         if (obd->u.filter.fo_capa_hash == NULL)
678                 RETURN(-ENOMEM);
679
680         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
681         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
682         /* set this lu_device to obd, because error handling need it */
683         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
684
685         rc = ofd_procfs_init(m);
686         if (rc) {
687                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
688                 RETURN(rc);
689         }
690
691         /* No connection accepted until configurations will finish */
692         spin_lock(&obd->obd_dev_lock);
693         obd->obd_no_conn = 1;
694         spin_unlock(&obd->obd_dev_lock);
695         obd->obd_replayable = 1;
696         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
697                 char *str = lustre_cfg_string(cfg, 4);
698
699                 if (strchr(str, 'n')) {
700                         CWARN("%s: recovery disabled\n", obd->obd_name);
701                         obd->obd_replayable = 0;
702                 }
703         }
704
705         info = ofd_info_init(env, NULL);
706         if (info == NULL)
707                 RETURN(-EFAULT);
708
709         rc = ofd_stack_init(env, m, cfg);
710         if (rc) {
711                 CERROR("Can't init device stack, rc %d\n", rc);
712                 GOTO(err_fini_proc, rc);
713         }
714
715         ofd_procfs_add_brw_stats_symlink(m);
716
717         /* populate cached statfs data */
718         osfs = &ofd_info(env)->fti_u.osfs;
719         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
720         if (rc != 0) {
721                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
722                 GOTO(err_fini_stack, rc);
723         }
724         if (!IS_PO2(osfs->os_bsize)) {
725                 CERROR("%s: blocksize (%d) is not a power of 2\n",
726                                 obd->obd_name, osfs->os_bsize);
727                 GOTO(err_fini_stack, rc = -EPROTO);
728         }
729         m->ofd_blockbits = fls(osfs->os_bsize) - 1;
730
731         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
732         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
733                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
734
735         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
736                  "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
737         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
738                                               LDLM_NAMESPACE_SERVER,
739                                               LDLM_NAMESPACE_GREEDY,
740                                               LDLM_NS_TYPE_OST);
741         if (m->ofd_namespace == NULL)
742                 GOTO(err_fini_stack, rc = -ENOMEM);
743         /* set obd_namespace for compatibility with old code */
744         obd->obd_namespace = m->ofd_namespace;
745         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
746         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
747         m->ofd_namespace->ns_lvbp = m;
748
749         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
750                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
751
752         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
753
754         /* Allow at most ddp_grant_reserved% of the available filesystem space
755          * to be granted to clients, so that any errors in the grant overhead
756          * calculations do not allow granting more space to clients than can be
757          * written. Assumes that in aggregate the grant overhead calculations do
758          * not have more than ddp_grant_reserved% estimation error in them. */
759         m->ofd_grant_ratio =
760                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
761
762         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
763                       OBD_FAIL_OST_ALL_REQUEST_NET,
764                       OBD_FAIL_OST_ALL_REPLY_NET);
765         if (rc)
766                 GOTO(err_free_ns, rc);
767
768         rc = ofd_fs_setup(env, m, obd);
769         if (rc)
770                 GOTO(err_fini_lut, rc);
771
772         RETURN(0);
773 err_fini_lut:
774         tgt_fini(env, &m->ofd_lut);
775 err_free_ns:
776         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
777         obd->obd_namespace = m->ofd_namespace = NULL;
778 err_fini_stack:
779         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
780 err_fini_proc:
781         ofd_procfs_fini(m);
782         return rc;
783 }
784
785 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
786 {
787         struct obd_device *obd = ofd_obd(m);
788         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
789
790         lfsck_stop(env, m->ofd_osd, true);
791         lfsck_degister(env, m->ofd_osd);
792         target_recovery_fini(obd);
793         obd_exports_barrier(obd);
794         obd_zombie_barrier();
795
796         tgt_fini(env, &m->ofd_lut);
797         ofd_fs_cleanup(env, m);
798
799         ofd_free_capa_keys(m);
800         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
801
802         if (m->ofd_namespace != NULL) {
803                 ldlm_namespace_free(m->ofd_namespace, NULL,
804                                     d->ld_obd->obd_force);
805                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
806         }
807
808         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
809         ofd_procfs_fini(m);
810         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
811         server_put_mount(obd->obd_name, NULL);
812         EXIT;
813 }
814
815 static struct lu_device *ofd_device_fini(const struct lu_env *env,
816                                          struct lu_device *d)
817 {
818         ENTRY;
819         ofd_fini(env, ofd_dev(d));
820         RETURN(NULL);
821 }
822
823 static struct lu_device *ofd_device_free(const struct lu_env *env,
824                                          struct lu_device *d)
825 {
826         struct ofd_device *m = ofd_dev(d);
827
828         dt_device_fini(&m->ofd_dt_dev);
829         OBD_FREE_PTR(m);
830         RETURN(NULL);
831 }
832
833 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
834                                           struct lu_device_type *t,
835                                           struct lustre_cfg *cfg)
836 {
837         struct ofd_device *m;
838         struct lu_device  *l;
839         int                rc;
840
841         OBD_ALLOC_PTR(m);
842         if (m == NULL)
843                 return ERR_PTR(-ENOMEM);
844
845         l = &m->ofd_dt_dev.dd_lu_dev;
846         dt_device_init(&m->ofd_dt_dev, t);
847         rc = ofd_init0(env, m, t, cfg);
848         if (rc != 0) {
849                 ofd_device_free(env, l);
850                 l = ERR_PTR(rc);
851         }
852
853         return l;
854 }
855
856 /* thread context key constructor/destructor */
857 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
858
859 static void ofd_key_exit(const struct lu_context *ctx,
860                          struct lu_context_key *key, void *data)
861 {
862         struct ofd_thread_info *info = data;
863
864         info->fti_env = NULL;
865         info->fti_exp = NULL;
866
867         info->fti_xid = 0;
868         info->fti_transno = 0;
869         info->fti_pre_version = 0;
870         info->fti_obj = NULL;
871         info->fti_has_trans = 0;
872         info->fti_mult_trans = 0;
873         info->fti_used = 0;
874
875         memset(&info->fti_attr, 0, sizeof info->fti_attr);
876 }
877
878 struct lu_context_key ofd_thread_key = {
879         .lct_tags = LCT_DT_THREAD,
880         .lct_init = ofd_key_init,
881         .lct_fini = ofd_key_fini,
882         .lct_exit = ofd_key_exit
883 };
884
885 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
886 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
887
888 static struct lu_device_type_operations ofd_device_type_ops = {
889         .ldto_init              = ofd_type_init,
890         .ldto_fini              = ofd_type_fini,
891
892         .ldto_start             = ofd_type_start,
893         .ldto_stop              = ofd_type_stop,
894
895         .ldto_device_alloc      = ofd_device_alloc,
896         .ldto_device_free       = ofd_device_free,
897         .ldto_device_fini       = ofd_device_fini
898 };
899
900 static struct lu_device_type ofd_device_type = {
901         .ldt_tags       = LU_DEVICE_DT,
902         .ldt_name       = LUSTRE_OST_NAME,
903         .ldt_ops        = &ofd_device_type_ops,
904         .ldt_ctx_tags   = LCT_DT_THREAD
905 };
906
907 int __init ofd_init(void)
908 {
909         struct lprocfs_static_vars      lvars;
910         int                             rc;
911
912         rc = lu_kmem_init(ofd_caches);
913         if (rc)
914                 return rc;
915
916         rc = ofd_fmd_init();
917         if (rc) {
918                 lu_kmem_fini(ofd_caches);
919                 return(rc);
920         }
921
922         lprocfs_ofd_init_vars(&lvars);
923
924         rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
925                                  LUSTRE_OST_NAME, &ofd_device_type);
926         return rc;
927 }
928
929 void __exit ofd_exit(void)
930 {
931         ofd_fmd_exit();
932         lu_kmem_fini(ofd_caches);
933         class_unregister_type(LUSTRE_OST_NAME);
934 }
935
936 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
937 MODULE_DESCRIPTION("Lustre Object Filtering Device");
938 MODULE_LICENSE("GPL");
939
940 module_init(ofd_init);
941 module_exit(ofd_exit);