4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39 * Author: Mike Pershin <tappro@whamcloud.com>
40 * Author: Johann Lombardi <johann@whamcloud.com>
43 #define DEBUG_SUBSYSTEM S_FILTER
45 #include <obd_class.h>
46 #include <lustre_param.h>
48 #include "ofd_internal.h"
50 /* Slab for OFD object allocation */
51 static cfs_mem_cache_t *ofd_object_kmem;
53 static struct lu_kmem_descr ofd_caches[] = {
55 .ckd_cache = &ofd_object_kmem,
56 .ckd_name = "ofd_obj",
57 .ckd_size = sizeof(struct ofd_object)
64 static int ofd_stack_init(const struct lu_env *env,
65 struct ofd_device *m, struct lustre_cfg *cfg)
67 struct lu_device *ofd_lu = &m->ofd_dt_dev.dd_lu_dev;
68 const char *dev = lustre_cfg_string(cfg, 0);
69 struct obd_type *type;
70 struct lu_device_type *ldt;
72 struct ofd_thread_info *info = ofd_info(env);
77 /* XXX: we should be able to use different OSDs here */
78 type = class_get_type(LUSTRE_OSD_NAME);
80 CERROR("Unknown type: '%s'\n", LUSTRE_OSD_NAME);
84 rc = lu_env_refill((struct lu_env *)env);
86 CERROR("Failure to refill session: '%d'\n", rc);
92 CERROR("type: '%s'\n", LUSTRE_OSD_NAME);
93 GOTO(out_type, rc = -EINVAL);
96 ldt->ldt_obd_type = type;
97 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
99 CERROR("Cannot allocate device: '%s'\n", LUSTRE_OSD_NAME);
100 GOTO(out_type, rc = -ENODEV);
103 LASSERT(ofd_lu->ld_site);
104 d->ld_site = ofd_lu->ld_site;
106 snprintf(info->fti_u.name, sizeof(info->fti_u.name),
107 "%s-osd", lustre_cfg_string(cfg, 0));
110 rc = ldt->ldt_ops->ldto_device_init(env, d, dev, NULL);
112 CERROR("can't init device '%s', rc %d\n", LUSTRE_OSD_NAME, rc);
116 lu_ref_add(&d->ld_reference, "lu-stack", &lu_site_init);
118 m->ofd_osd = lu2dt_dev(d);
120 /* process setup config */
121 rc = d->ld_ops->ldo_process_config(env, d, cfg);
128 ldt->ldt_ops->ldto_device_fini(env, d);
131 ldt->ldt_ops->ldto_device_free(env, d);
133 class_put_type(type);
137 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
138 struct lu_device *top)
140 struct obd_device *obd = ofd_obd(m);
141 struct lustre_cfg_bufs bufs;
142 struct lustre_cfg *lcfg;
147 lu_site_purge(env, top->ld_site, ~0);
149 /* process cleanup, pass mdt obd name to get obd umount flags */
150 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
155 lustre_cfg_bufs_set_string(&bufs, 1, flags);
156 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
158 CERROR("Cannot alloc lcfg!\n");
163 top->ld_ops->ldo_process_config(env, top, lcfg);
164 lustre_cfg_free(lcfg);
166 lu_stack_fini(env, &m->ofd_osd->dd_lu_dev);
172 /* used by MGS to process specific configurations */
173 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
174 struct lustre_cfg *cfg)
176 struct ofd_device *m = ofd_dev(d);
177 struct dt_device *dt_next = m->ofd_osd;
178 struct lu_device *next = &dt_next->dd_lu_dev;
183 switch (cfg->lcfg_command) {
185 struct lprocfs_static_vars lvars;
187 lprocfs_ofd_init_vars(&lvars);
188 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
190 if (rc > 0 || rc == -ENOSYS)
191 /* we don't understand; pass it on */
192 rc = next->ld_ops->ldo_process_config(env, next, cfg);
195 case LCFG_SPTLRPC_CONF: {
200 /* others are passed further */
201 rc = next->ld_ops->ldo_process_config(env, next, cfg);
207 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
208 const struct lu_object_conf *conf)
210 struct ofd_device *d = ofd_dev(o->lo_dev);
211 struct lu_device *under;
212 struct lu_object *below;
217 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
218 PFID(lu_object_fid(o)));
220 under = &d->ofd_osd->dd_lu_dev;
221 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
223 lu_object_add(o, below);
230 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
232 struct ofd_object *of = ofd_obj(o);
233 struct lu_object_header *h;
238 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
239 PFID(lu_object_fid(o)));
242 lu_object_header_fini(h);
243 OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
247 static int ofd_object_print(const struct lu_env *env, void *cookie,
248 lu_printer_t p, const struct lu_object *o)
250 return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
253 struct lu_object_operations ofd_obj_ops = {
254 .loo_object_init = ofd_object_init,
255 .loo_object_free = ofd_object_free,
256 .loo_object_print = ofd_object_print
259 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
260 const struct lu_object_header *hdr,
263 struct ofd_object *of;
267 OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, CFS_ALLOC_IO);
270 struct lu_object_header *h;
272 o = &of->ofo_obj.do_lu;
274 lu_object_header_init(h);
275 lu_object_init(o, h, d);
276 lu_object_add_top(h, o);
277 o->lo_ops = &ofd_obj_ops;
284 extern int ost_handle(struct ptlrpc_request *req);
286 static int ofd_start(const struct lu_env *env, struct lu_device *dev)
288 struct ofd_device *ofd = ofd_dev(dev);
289 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
294 /* initialize lower device */
295 rc = next->ld_ops->ldo_prepare(env, dev, next);
300 static int ofd_recovery_complete(const struct lu_env *env,
301 struct lu_device *dev)
303 struct ofd_device *ofd = ofd_dev(dev);
304 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
309 rc = next->ld_ops->ldo_recovery_complete(env, next);
313 static struct lu_device_operations ofd_lu_ops = {
314 .ldo_object_alloc = ofd_object_alloc,
315 .ldo_process_config = ofd_process_config,
316 .ldo_recovery_complete = ofd_recovery_complete,
319 static int ofd_procfs_init(struct ofd_device *ofd)
321 struct lprocfs_static_vars lvars;
322 struct obd_device *obd = ofd_obd(ofd);
323 cfs_proc_dir_entry_t *entry;
328 /* lprocfs must be setup before the ofd so state can be safely added
329 * to /proc incrementally as the ofd is setup */
330 lprocfs_ofd_init_vars(&lvars);
331 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
333 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
338 rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_LAST);
340 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
342 GOTO(obd_cleanup, rc);
345 /* Init OFD private stats here */
346 lprocfs_counter_init(obd->obd_stats, LPROC_OFD_READ_BYTES,
347 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
348 lprocfs_counter_init(obd->obd_stats, LPROC_OFD_WRITE_BYTES,
349 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
351 rc = lproc_ofd_attach_seqstat(obd);
353 CERROR("%s: create seqstat failed: %d.\n", obd->obd_name, rc);
354 GOTO(free_obd_stats, rc);
357 entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
360 CERROR("%s: error %d setting up lprocfs for %s\n",
361 obd->obd_name, rc, "exports");
362 GOTO(free_obd_stats, rc);
364 obd->obd_proc_exports_entry = entry;
366 entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
367 lprocfs_nid_stats_clear_read,
368 lprocfs_nid_stats_clear_write, obd, NULL);
371 CERROR("%s: add proc entry 'clear' failed: %d.\n",
373 GOTO(free_obd_stats, rc);
378 lprocfs_free_obd_stats(obd);
380 lprocfs_obd_cleanup(obd);
384 static int ofd_procfs_fini(struct ofd_device *ofd)
386 struct obd_device *obd = ofd_obd(ofd);
388 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
389 lprocfs_free_per_client_stats(obd);
390 lprocfs_free_obd_stats(obd);
391 lprocfs_obd_cleanup(obd);
395 extern int ost_handle(struct ptlrpc_request *req);
397 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
398 struct lu_device_type *ldt, struct lustre_cfg *cfg)
400 const char *dev = lustre_cfg_string(cfg, 0);
401 struct ofd_thread_info *info = NULL;
402 struct obd_device *obd;
407 obd = class_name2obd(dev);
409 CERROR("Cannot find obd with name %s\n", dev);
413 rc = lu_env_refill((struct lu_env *)env);
417 obd->u.obt.obt_magic = OBT_MAGIC;
419 m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
420 m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
422 cfs_spin_lock_init(&m->ofd_flags_lock);
423 m->ofd_raid_degraded = 0;
424 m->ofd_syncjournal = 0;
427 m->ofd_max_group = 0;
429 cfs_rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
430 sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
432 obd->u.filter.fo_fl_oss_capa = 0;
433 CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
434 obd->u.filter.fo_capa_hash = init_capa_hash();
435 if (obd->u.filter.fo_capa_hash == NULL)
438 m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
439 m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
440 /* set this lu_device to obd, because error handling need it */
441 obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
443 rc = ofd_procfs_init(m);
445 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
449 /* No connection accepted until configurations will finish */
450 obd->obd_no_conn = 1;
451 obd->obd_replayable = 1;
452 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
453 char *str = lustre_cfg_string(cfg, 4);
455 if (strchr(str, 'n')) {
456 CWARN("%s: recovery disabled\n", obd->obd_name);
457 obd->obd_replayable = 0;
461 info = ofd_info_init(env, NULL);
465 rc = lu_site_init(&m->ofd_site, &m->ofd_dt_dev.dd_lu_dev);
467 GOTO(err_fini_proc, rc);
468 m->ofd_site.ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
470 rc = ofd_stack_init(env, m, cfg);
472 CERROR("Can't init device stack, rc %d\n", rc);
473 GOTO(err_lu_site, rc);
476 m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
477 LDLM_NAMESPACE_SERVER,
478 LDLM_NAMESPACE_GREEDY,
480 if (m->ofd_namespace == NULL)
481 GOTO(err_fini_stack, rc = -ENOMEM);
482 /* set obd_namespace for compatibility with old code */
483 obd->obd_namespace = m->ofd_namespace;
485 dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
487 rc = ofd_start(env, &m->ofd_dt_dev.dd_lu_dev);
489 GOTO(err_fini_stack, rc);
491 rc = lut_init(env, &m->ofd_lut, obd, m->ofd_osd);
493 GOTO(err_free_ns, rc);
495 rc = ofd_fs_setup(env, m, obd);
497 GOTO(err_fini_lut, rc);
499 target_recovery_init(&m->ofd_lut, ost_handle);
501 rc = lu_site_init_finish(&m->ofd_site);
503 GOTO(err_fs_cleanup, rc);
507 target_recovery_fini(obd);
508 ofd_fs_cleanup(env, m);
510 lut_fini(env, &m->ofd_lut);
512 ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
513 obd->obd_namespace = m->ofd_namespace = NULL;
515 ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
517 lu_site_fini(&m->ofd_site);
523 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
525 struct obd_device *obd = ofd_obd(m);
526 struct lu_device *d = &m->ofd_dt_dev.dd_lu_dev;
528 target_recovery_fini(obd);
529 obd_exports_barrier(obd);
530 obd_zombie_barrier();
532 lut_fini(env, &m->ofd_lut);
533 ofd_fs_cleanup(env, m);
535 ofd_free_capa_keys(m);
536 cleanup_capa_hash(obd->u.filter.fo_capa_hash);
538 if (m->ofd_namespace != NULL) {
539 ldlm_namespace_free(m->ofd_namespace, NULL,
540 d->ld_obd->obd_force);
541 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
544 ofd_stack_fini(env, m, m->ofd_site.ls_top_dev);
545 lu_site_fini(&m->ofd_site);
547 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
551 static struct lu_device *ofd_device_fini(const struct lu_env *env,
555 ofd_fini(env, ofd_dev(d));
559 static struct lu_device *ofd_device_free(const struct lu_env *env,
562 struct ofd_device *m = ofd_dev(d);
564 dt_device_fini(&m->ofd_dt_dev);
569 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
570 struct lu_device_type *t,
571 struct lustre_cfg *cfg)
573 struct ofd_device *m;
579 return ERR_PTR(-ENOMEM);
581 l = &m->ofd_dt_dev.dd_lu_dev;
582 dt_device_init(&m->ofd_dt_dev, t);
583 rc = ofd_init0(env, m, t, cfg);
585 ofd_device_free(env, l);
592 /* thread context key constructor/destructor */
593 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
595 static void ofd_key_exit(const struct lu_context *ctx,
596 struct lu_context_key *key, void *data)
598 struct ofd_thread_info *info = data;
600 info->fti_env = NULL;
601 info->fti_exp = NULL;
604 struct lu_context_key ofd_thread_key = {
605 .lct_tags = LCT_DT_THREAD,
606 .lct_init = ofd_key_init,
607 .lct_fini = ofd_key_fini,
608 .lct_exit = ofd_key_exit
611 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
612 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
614 static struct lu_device_type_operations ofd_device_type_ops = {
615 .ldto_init = ofd_type_init,
616 .ldto_fini = ofd_type_fini,
618 .ldto_start = ofd_type_start,
619 .ldto_stop = ofd_type_stop,
621 .ldto_device_alloc = ofd_device_alloc,
622 .ldto_device_free = ofd_device_free,
623 .ldto_device_fini = ofd_device_fini
626 static struct lu_device_type ofd_device_type = {
627 .ldt_tags = LU_DEVICE_DT,
628 .ldt_name = LUSTRE_OST_NAME,
629 .ldt_ops = &ofd_device_type_ops,
630 .ldt_ctx_tags = LCT_DT_THREAD
633 int __init ofd_init(void)
635 struct lprocfs_static_vars lvars;
638 rc = lu_kmem_init(ofd_caches);
644 lu_kmem_fini(ofd_caches);
648 lprocfs_ofd_init_vars(&lvars);
650 rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
651 LUSTRE_OST_NAME, &ofd_device_type);
655 void __exit ofd_exit(void)
658 lu_kmem_fini(ofd_caches);
659 class_unregister_type(LUSTRE_OST_NAME);
662 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
663 MODULE_DESCRIPTION("Lustre Object Filtering Device");
664 MODULE_LICENSE("GPL");
666 module_init(ofd_init);
667 module_exit(ofd_exit);