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