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);
73 struct lustre_mount_info *lmi;
78 lmi = server_get_mount_2(dev);
80 CERROR("Cannot get mount info for %s!\n", dev);
84 type = class_get_type(s2lsi(lmi->lmi_sb)->lsi_osd_type);
86 CERROR("Unknown type: '%s'\n",
87 s2lsi(lmi->lmi_sb)->lsi_osd_type);
93 CERROR("type: '%s'\n", s2lsi(lmi->lmi_sb)->lsi_osd_type);
94 GOTO(out_type, rc = -EINVAL);
97 ldt->ldt_obd_type = type;
98 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
100 CERROR("Cannot allocate device: '%s'\n",
101 s2lsi(lmi->lmi_sb)->lsi_osd_type);
102 GOTO(out_type, rc = -ENODEV);
105 LASSERT(ofd_lu->ld_site);
106 d->ld_site = ofd_lu->ld_site;
108 snprintf(info->fti_u.name, sizeof(info->fti_u.name),
109 "%s-osd", lustre_cfg_string(cfg, 0));
113 rc = lu_env_refill((struct lu_env *)env);
115 CERROR("Failure to refill session: '%d'\n", rc);
119 rc = ldt->ldt_ops->ldto_device_init(env, d, dev, NULL);
121 CERROR("can't init device '%s', rc = %d\n",
122 s2lsi(lmi->lmi_sb)->lsi_osd_type, rc);
126 lu_ref_add(&d->ld_reference, "lu-stack", &lu_site_init);
128 m->ofd_osd = lu2dt_dev(d);
130 /* process setup config */
131 rc = d->ld_ops->ldo_process_config(env, d, cfg);
138 ldt->ldt_ops->ldto_device_fini(env, d);
141 ldt->ldt_ops->ldto_device_free(env, d);
143 class_put_type(type);
147 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
148 struct lu_device *top)
150 struct obd_device *obd = ofd_obd(m);
151 struct lustre_cfg_bufs bufs;
152 struct lustre_cfg *lcfg;
157 lu_site_purge(env, top->ld_site, ~0);
159 /* process cleanup, pass mdt obd name to get obd umount flags */
160 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
165 lustre_cfg_bufs_set_string(&bufs, 1, flags);
166 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
168 CERROR("Cannot alloc lcfg!\n");
173 top->ld_ops->ldo_process_config(env, top, lcfg);
174 lustre_cfg_free(lcfg);
176 lu_stack_fini(env, &m->ofd_osd->dd_lu_dev);
182 /* used by MGS to process specific configurations */
183 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
184 struct lustre_cfg *cfg)
186 struct ofd_device *m = ofd_dev(d);
187 struct dt_device *dt_next = m->ofd_osd;
188 struct lu_device *next = &dt_next->dd_lu_dev;
193 switch (cfg->lcfg_command) {
195 struct lprocfs_static_vars lvars;
197 lprocfs_ofd_init_vars(&lvars);
198 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
200 if (rc > 0 || rc == -ENOSYS)
201 /* we don't understand; pass it on */
202 rc = next->ld_ops->ldo_process_config(env, next, cfg);
205 case LCFG_SPTLRPC_CONF: {
210 /* others are passed further */
211 rc = next->ld_ops->ldo_process_config(env, next, cfg);
217 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
218 const struct lu_object_conf *conf)
220 struct ofd_device *d = ofd_dev(o->lo_dev);
221 struct lu_device *under;
222 struct lu_object *below;
227 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
228 PFID(lu_object_fid(o)));
230 under = &d->ofd_osd->dd_lu_dev;
231 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
233 lu_object_add(o, below);
240 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
242 struct ofd_object *of = ofd_obj(o);
243 struct lu_object_header *h;
248 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
249 PFID(lu_object_fid(o)));
252 lu_object_header_fini(h);
253 OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
257 static int ofd_object_print(const struct lu_env *env, void *cookie,
258 lu_printer_t p, const struct lu_object *o)
260 return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
263 struct lu_object_operations ofd_obj_ops = {
264 .loo_object_init = ofd_object_init,
265 .loo_object_free = ofd_object_free,
266 .loo_object_print = ofd_object_print
269 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
270 const struct lu_object_header *hdr,
273 struct ofd_object *of;
277 OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, CFS_ALLOC_IO);
280 struct lu_object_header *h;
282 o = &of->ofo_obj.do_lu;
284 lu_object_header_init(h);
285 lu_object_init(o, h, d);
286 lu_object_add_top(h, o);
287 o->lo_ops = &ofd_obj_ops;
294 extern int ost_handle(struct ptlrpc_request *req);
296 static int ofd_start(const struct lu_env *env, struct lu_device *dev)
298 struct ofd_device *ofd = ofd_dev(dev);
299 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
304 /* initialize lower device */
305 rc = next->ld_ops->ldo_prepare(env, dev, next);
310 static int ofd_recovery_complete(const struct lu_env *env,
311 struct lu_device *dev)
313 struct ofd_device *ofd = ofd_dev(dev);
314 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
319 /* Grant space for object precreation on the self export.
320 * This initial reserved space (i.e. 20MB for zfs and 560KB for ldiskfs)
321 * is enough to create 20k objects. It is then adapted based on the
322 * precreate request size (see ofd_grant_create()
324 ofd_grant_connect(env, dev->ld_obd->obd_self_export,
325 OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace);
326 rc = next->ld_ops->ldo_recovery_complete(env, next);
330 static struct lu_device_operations ofd_lu_ops = {
331 .ldo_object_alloc = ofd_object_alloc,
332 .ldo_process_config = ofd_process_config,
333 .ldo_recovery_complete = ofd_recovery_complete,
336 static int ofd_procfs_init(struct ofd_device *ofd)
338 struct lprocfs_static_vars lvars;
339 struct obd_device *obd = ofd_obd(ofd);
340 cfs_proc_dir_entry_t *entry;
345 /* lprocfs must be setup before the ofd so state can be safely added
346 * to /proc incrementally as the ofd is setup */
347 lprocfs_ofd_init_vars(&lvars);
348 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
350 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
355 rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_LAST);
357 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
359 GOTO(obd_cleanup, rc);
362 /* Init OFD private stats here */
363 lprocfs_counter_init(obd->obd_stats, LPROC_OFD_READ_BYTES,
364 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
365 lprocfs_counter_init(obd->obd_stats, LPROC_OFD_WRITE_BYTES,
366 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
368 rc = lproc_ofd_attach_seqstat(obd);
370 CERROR("%s: create seqstat failed: %d.\n", obd->obd_name, rc);
371 GOTO(free_obd_stats, rc);
374 entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
377 CERROR("%s: error %d setting up lprocfs for %s\n",
378 obd->obd_name, rc, "exports");
379 GOTO(free_obd_stats, rc);
381 obd->obd_proc_exports_entry = entry;
383 entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
384 lprocfs_nid_stats_clear_read,
385 lprocfs_nid_stats_clear_write, obd, NULL);
388 CERROR("%s: add proc entry 'clear' failed: %d.\n",
390 GOTO(free_obd_stats, rc);
395 lprocfs_free_obd_stats(obd);
397 lprocfs_obd_cleanup(obd);
401 static int ofd_procfs_fini(struct ofd_device *ofd)
403 struct obd_device *obd = ofd_obd(ofd);
405 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
406 lprocfs_free_per_client_stats(obd);
407 lprocfs_free_obd_stats(obd);
408 lprocfs_obd_cleanup(obd);
412 extern int ost_handle(struct ptlrpc_request *req);
414 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
415 struct lu_device_type *ldt, struct lustre_cfg *cfg)
417 const char *dev = lustre_cfg_string(cfg, 0);
418 struct ofd_thread_info *info = NULL;
419 struct obd_device *obd;
420 struct obd_statfs *osfs;
425 obd = class_name2obd(dev);
427 CERROR("Cannot find obd with name %s\n", dev);
431 rc = lu_env_refill((struct lu_env *)env);
435 obd->u.obt.obt_magic = OBT_MAGIC;
437 m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
438 m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
440 cfs_spin_lock_init(&m->ofd_flags_lock);
441 m->ofd_raid_degraded = 0;
442 m->ofd_syncjournal = 0;
444 m->ofd_grant_compat_disable = 0;
447 cfs_spin_lock_init(&m->ofd_osfs_lock);
448 m->ofd_osfs_age = cfs_time_shift_64(-1000);
449 m->ofd_osfs_unstable = 0;
450 m->ofd_statfs_inflight = 0;
451 m->ofd_osfs_inflight = 0;
454 cfs_spin_lock_init(&m->ofd_grant_lock);
455 m->ofd_tot_dirty = 0;
456 m->ofd_tot_granted = 0;
457 m->ofd_tot_pending = 0;
458 m->ofd_max_group = 0;
460 cfs_rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
461 sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
463 obd->u.filter.fo_fl_oss_capa = 0;
464 CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
465 obd->u.filter.fo_capa_hash = init_capa_hash();
466 if (obd->u.filter.fo_capa_hash == NULL)
469 m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
470 m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
471 /* set this lu_device to obd, because error handling need it */
472 obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
474 rc = ofd_procfs_init(m);
476 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
480 /* No connection accepted until configurations will finish */
481 obd->obd_no_conn = 1;
482 obd->obd_replayable = 1;
483 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
484 char *str = lustre_cfg_string(cfg, 4);
486 if (strchr(str, 'n')) {
487 CWARN("%s: recovery disabled\n", obd->obd_name);
488 obd->obd_replayable = 0;
492 info = ofd_info_init(env, NULL);
496 rc = lu_site_init(&m->ofd_site, &m->ofd_dt_dev.dd_lu_dev);
498 GOTO(err_fini_proc, rc);
499 m->ofd_site.ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
501 rc = ofd_stack_init(env, m, cfg);
503 CERROR("Can't init device stack, rc %d\n", rc);
504 GOTO(err_lu_site, rc);
507 /* populate cached statfs data */
508 osfs = &ofd_info(env)->fti_u.osfs;
509 rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
511 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
512 GOTO(err_fini_stack, rc);
514 if (!IS_PO2(osfs->os_bsize)) {
515 CERROR("%s: blocksize (%d) is not a power of 2\n",
516 obd->obd_name, osfs->os_bsize);
517 GOTO(err_fini_stack, rc = -EPROTO);
519 m->ofd_blockbits = cfs_fls(osfs->os_bsize) - 1;
521 snprintf(info->fti_u.name, sizeof(info->fti_u.name), "filter-%p", m);
522 m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
523 LDLM_NAMESPACE_SERVER,
524 LDLM_NAMESPACE_GREEDY,
526 if (m->ofd_namespace == NULL)
527 GOTO(err_fini_stack, rc = -ENOMEM);
528 /* set obd_namespace for compatibility with old code */
529 obd->obd_namespace = m->ofd_namespace;
530 ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
531 m->ofd_namespace->ns_lvbo = &ofd_lvbo;
532 m->ofd_namespace->ns_lvbp = m;
534 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
535 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
537 dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
539 /* Allow at most ddp_grant_reserved% of the available filesystem space
540 * to be granted to clients, so that any errors in the grant overhead
541 * calculations do not allow granting more space to clients than can be
542 * written. Assumes that in aggregate the grant overhead calculations do
543 * not have more than ddp_grant_reserved% estimation error in them. */
545 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
547 rc = ofd_start(env, &m->ofd_dt_dev.dd_lu_dev);
549 GOTO(err_fini_stack, rc);
551 rc = lut_init(env, &m->ofd_lut, obd, m->ofd_osd);
553 GOTO(err_free_ns, rc);
555 rc = ofd_fs_setup(env, m, obd);
557 GOTO(err_fini_lut, rc);
559 target_recovery_init(&m->ofd_lut, ost_handle);
561 rc = lu_site_init_finish(&m->ofd_site);
563 GOTO(err_fs_cleanup, rc);
567 target_recovery_fini(obd);
568 ofd_fs_cleanup(env, m);
570 lut_fini(env, &m->ofd_lut);
572 ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
573 obd->obd_namespace = m->ofd_namespace = NULL;
575 ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
577 lu_site_fini(&m->ofd_site);
583 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
585 struct obd_device *obd = ofd_obd(m);
586 struct lu_device *d = &m->ofd_dt_dev.dd_lu_dev;
588 target_recovery_fini(obd);
589 obd_exports_barrier(obd);
590 obd_zombie_barrier();
592 lut_fini(env, &m->ofd_lut);
593 ofd_fs_cleanup(env, m);
595 ofd_free_capa_keys(m);
596 cleanup_capa_hash(obd->u.filter.fo_capa_hash);
598 if (m->ofd_namespace != NULL) {
599 ldlm_namespace_free(m->ofd_namespace, NULL,
600 d->ld_obd->obd_force);
601 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
604 ofd_stack_fini(env, m, m->ofd_site.ls_top_dev);
605 lu_site_fini(&m->ofd_site);
607 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
611 static struct lu_device *ofd_device_fini(const struct lu_env *env,
615 ofd_fini(env, ofd_dev(d));
619 static struct lu_device *ofd_device_free(const struct lu_env *env,
622 struct ofd_device *m = ofd_dev(d);
624 dt_device_fini(&m->ofd_dt_dev);
629 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
630 struct lu_device_type *t,
631 struct lustre_cfg *cfg)
633 struct ofd_device *m;
639 return ERR_PTR(-ENOMEM);
641 l = &m->ofd_dt_dev.dd_lu_dev;
642 dt_device_init(&m->ofd_dt_dev, t);
643 rc = ofd_init0(env, m, t, cfg);
645 ofd_device_free(env, l);
652 /* thread context key constructor/destructor */
653 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
655 static void ofd_key_exit(const struct lu_context *ctx,
656 struct lu_context_key *key, void *data)
658 struct ofd_thread_info *info = data;
660 info->fti_env = NULL;
661 info->fti_exp = NULL;
664 info->fti_transno = 0;
665 info->fti_pre_version = 0;
666 info->fti_obj = NULL;
667 info->fti_has_trans = 0;
668 info->fti_mult_trans = 0;
671 memset(&info->fti_attr, 0, sizeof info->fti_attr);
674 struct lu_context_key ofd_thread_key = {
675 .lct_tags = LCT_DT_THREAD,
676 .lct_init = ofd_key_init,
677 .lct_fini = ofd_key_fini,
678 .lct_exit = ofd_key_exit
681 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
682 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
684 static struct lu_device_type_operations ofd_device_type_ops = {
685 .ldto_init = ofd_type_init,
686 .ldto_fini = ofd_type_fini,
688 .ldto_start = ofd_type_start,
689 .ldto_stop = ofd_type_stop,
691 .ldto_device_alloc = ofd_device_alloc,
692 .ldto_device_free = ofd_device_free,
693 .ldto_device_fini = ofd_device_fini
696 static struct lu_device_type ofd_device_type = {
697 .ldt_tags = LU_DEVICE_DT,
698 .ldt_name = LUSTRE_OST_NAME,
699 .ldt_ops = &ofd_device_type_ops,
700 .ldt_ctx_tags = LCT_DT_THREAD
703 int __init ofd_init(void)
705 struct lprocfs_static_vars lvars;
708 rc = lu_kmem_init(ofd_caches);
714 lu_kmem_fini(ofd_caches);
718 lprocfs_ofd_init_vars(&lvars);
720 rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
721 LUSTRE_OST_NAME, &ofd_device_type);
725 void __exit ofd_exit(void)
728 lu_kmem_fini(ofd_caches);
729 class_unregister_type(LUSTRE_OST_NAME);
732 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
733 MODULE_DESCRIPTION("Lustre Object Filtering Device");
734 MODULE_LICENSE("GPL");
736 module_init(ofd_init);
737 module_exit(ofd_exit);