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_connect_to_next(const struct lu_env *env, struct ofd_device *m,
65 const char *next, struct obd_export **exp)
67 struct obd_connect_data *data = NULL;
68 struct obd_device *obd;
74 GOTO(out, rc = -ENOMEM);
76 obd = class_name2obd(next);
78 CERROR("%s: can't locate next device: %s\n",
79 m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, next);
80 GOTO(out, rc = -ENOTCONN);
83 data->ocd_connect_flags = OBD_CONNECT_VERSION;
84 data->ocd_version = LUSTRE_VERSION_CODE;
86 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
88 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
89 m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, next, rc);
99 static int ofd_stack_init(const struct lu_env *env,
100 struct ofd_device *m, struct lustre_cfg *cfg)
102 struct lu_device *ofd_lu = &m->ofd_dt_dev.dd_lu_dev;
103 const char *dev = lustre_cfg_string(cfg, 0);
105 struct ofd_thread_info *info = ofd_info(env);
106 struct lustre_mount_info *lmi;
112 lmi = server_get_mount(dev);
114 CERROR("Cannot get mount info for %s!\n", dev);
118 /* find bottom osd */
119 OBD_ALLOC(osdname, MTI_NAME_MAXLEN);
123 snprintf(osdname, MTI_NAME_MAXLEN, "%s-osd", dev);
124 rc = ofd_connect_to_next(env, m, osdname, &m->ofd_osd_exp);
125 OBD_FREE(osdname, MTI_NAME_MAXLEN);
129 d = m->ofd_osd_exp->exp_obd->obd_lu_dev;
131 m->ofd_osd = lu2dt_dev(d);
133 LASSERT(ofd_lu->ld_site);
134 d->ld_site = ofd_lu->ld_site;
136 snprintf(info->fti_u.name, sizeof(info->fti_u.name),
137 "%s-osd", lustre_cfg_string(cfg, 0));
142 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
143 struct lu_device *top)
145 struct obd_device *obd = ofd_obd(m);
146 struct lustre_cfg_bufs bufs;
147 struct lustre_cfg *lcfg;
152 lu_site_purge(env, top->ld_site, ~0);
154 /* process cleanup, pass mdt obd name to get obd umount flags */
155 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
160 lustre_cfg_bufs_set_string(&bufs, 1, flags);
161 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
163 CERROR("Cannot alloc lcfg!\n");
168 top->ld_ops->ldo_process_config(env, top, lcfg);
169 lustre_cfg_free(lcfg);
171 lu_site_purge(env, top->ld_site, ~0);
173 LASSERT(m->ofd_osd_exp);
174 obd_disconnect(m->ofd_osd_exp);
180 /* used by MGS to process specific configurations */
181 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
182 struct lustre_cfg *cfg)
184 struct ofd_device *m = ofd_dev(d);
185 struct dt_device *dt_next = m->ofd_osd;
186 struct lu_device *next = &dt_next->dd_lu_dev;
191 switch (cfg->lcfg_command) {
193 struct lprocfs_static_vars lvars;
195 lprocfs_ofd_init_vars(&lvars);
196 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
198 if (rc > 0 || rc == -ENOSYS)
199 /* we don't understand; pass it on */
200 rc = next->ld_ops->ldo_process_config(env, next, cfg);
203 case LCFG_SPTLRPC_CONF: {
208 /* others are passed further */
209 rc = next->ld_ops->ldo_process_config(env, next, cfg);
215 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
216 const struct lu_object_conf *conf)
218 struct ofd_device *d = ofd_dev(o->lo_dev);
219 struct lu_device *under;
220 struct lu_object *below;
225 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
226 PFID(lu_object_fid(o)));
228 under = &d->ofd_osd->dd_lu_dev;
229 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
231 lu_object_add(o, below);
238 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
240 struct ofd_object *of = ofd_obj(o);
241 struct lu_object_header *h;
246 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
247 PFID(lu_object_fid(o)));
250 lu_object_header_fini(h);
251 OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
255 static int ofd_object_print(const struct lu_env *env, void *cookie,
256 lu_printer_t p, const struct lu_object *o)
258 return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
261 struct lu_object_operations ofd_obj_ops = {
262 .loo_object_init = ofd_object_init,
263 .loo_object_free = ofd_object_free,
264 .loo_object_print = ofd_object_print
267 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
268 const struct lu_object_header *hdr,
271 struct ofd_object *of;
275 OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, CFS_ALLOC_IO);
278 struct lu_object_header *h;
280 o = &of->ofo_obj.do_lu;
282 lu_object_header_init(h);
283 lu_object_init(o, h, d);
284 lu_object_add_top(h, o);
285 o->lo_ops = &ofd_obj_ops;
292 extern int ost_handle(struct ptlrpc_request *req);
294 static int ofd_start(const struct lu_env *env, struct lu_device *dev)
296 struct ofd_device *ofd = ofd_dev(dev);
297 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
302 /* initialize lower device */
303 rc = next->ld_ops->ldo_prepare(env, dev, next);
308 static int ofd_recovery_complete(const struct lu_env *env,
309 struct lu_device *dev)
311 struct ofd_device *ofd = ofd_dev(dev);
312 struct lu_device *next = &ofd->ofd_osd->dd_lu_dev;
317 /* Grant space for object precreation on the self export.
318 * This initial reserved space (i.e. 20MB for zfs and 560KB for ldiskfs)
319 * is enough to create 20k objects. It is then adapted based on the
320 * precreate request size (see ofd_grant_create()
322 ofd_grant_connect(env, dev->ld_obd->obd_self_export,
323 OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace);
324 rc = next->ld_ops->ldo_recovery_complete(env, next);
328 static struct lu_device_operations ofd_lu_ops = {
329 .ldo_object_alloc = ofd_object_alloc,
330 .ldo_process_config = ofd_process_config,
331 .ldo_recovery_complete = ofd_recovery_complete,
334 static int ofd_procfs_init(struct ofd_device *ofd)
336 struct lprocfs_static_vars lvars;
337 struct obd_device *obd = ofd_obd(ofd);
338 cfs_proc_dir_entry_t *entry;
343 /* lprocfs must be setup before the ofd so state can be safely added
344 * to /proc incrementally as the ofd is setup */
345 lprocfs_ofd_init_vars(&lvars);
346 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
348 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
353 rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_LAST);
355 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
357 GOTO(obd_cleanup, rc);
360 /* Init OFD private stats here */
361 lprocfs_counter_init(obd->obd_stats, LPROC_OFD_READ_BYTES,
362 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
363 lprocfs_counter_init(obd->obd_stats, LPROC_OFD_WRITE_BYTES,
364 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
366 rc = lproc_ofd_attach_seqstat(obd);
368 CERROR("%s: create seqstat failed: %d.\n", obd->obd_name, rc);
369 GOTO(free_obd_stats, rc);
372 entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
375 CERROR("%s: error %d setting up lprocfs for %s\n",
376 obd->obd_name, rc, "exports");
377 GOTO(free_obd_stats, rc);
379 obd->obd_proc_exports_entry = entry;
381 entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
382 lprocfs_nid_stats_clear_read,
383 lprocfs_nid_stats_clear_write, obd, NULL);
386 CERROR("%s: add proc entry 'clear' failed: %d.\n",
388 GOTO(free_obd_stats, rc);
393 lprocfs_free_obd_stats(obd);
395 lprocfs_obd_cleanup(obd);
399 static int ofd_procfs_fini(struct ofd_device *ofd)
401 struct obd_device *obd = ofd_obd(ofd);
403 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
404 lprocfs_free_per_client_stats(obd);
405 lprocfs_free_obd_stats(obd);
406 lprocfs_obd_cleanup(obd);
410 extern int ost_handle(struct ptlrpc_request *req);
412 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
413 struct lu_device_type *ldt, struct lustre_cfg *cfg)
415 const char *dev = lustre_cfg_string(cfg, 0);
416 struct ofd_thread_info *info = NULL;
417 struct obd_device *obd;
418 struct obd_statfs *osfs;
423 obd = class_name2obd(dev);
425 CERROR("Cannot find obd with name %s\n", dev);
429 rc = lu_env_refill((struct lu_env *)env);
433 obd->u.obt.obt_magic = OBT_MAGIC;
435 m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
436 m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
438 cfs_spin_lock_init(&m->ofd_flags_lock);
439 m->ofd_raid_degraded = 0;
440 m->ofd_syncjournal = 0;
442 m->ofd_grant_compat_disable = 0;
445 cfs_spin_lock_init(&m->ofd_osfs_lock);
446 m->ofd_osfs_age = cfs_time_shift_64(-1000);
447 m->ofd_osfs_unstable = 0;
448 m->ofd_statfs_inflight = 0;
449 m->ofd_osfs_inflight = 0;
452 cfs_spin_lock_init(&m->ofd_grant_lock);
453 m->ofd_tot_dirty = 0;
454 m->ofd_tot_granted = 0;
455 m->ofd_tot_pending = 0;
456 m->ofd_max_group = 0;
458 cfs_rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
459 sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
461 obd->u.filter.fo_fl_oss_capa = 0;
462 CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
463 obd->u.filter.fo_capa_hash = init_capa_hash();
464 if (obd->u.filter.fo_capa_hash == NULL)
467 m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
468 m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
469 /* set this lu_device to obd, because error handling need it */
470 obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
472 rc = ofd_procfs_init(m);
474 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
478 /* No connection accepted until configurations will finish */
479 obd->obd_no_conn = 1;
480 obd->obd_replayable = 1;
481 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
482 char *str = lustre_cfg_string(cfg, 4);
484 if (strchr(str, 'n')) {
485 CWARN("%s: recovery disabled\n", obd->obd_name);
486 obd->obd_replayable = 0;
490 info = ofd_info_init(env, NULL);
494 rc = lu_site_init(&m->ofd_site, &m->ofd_dt_dev.dd_lu_dev);
496 GOTO(err_fini_proc, rc);
497 m->ofd_site.ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
499 rc = ofd_stack_init(env, m, cfg);
501 CERROR("Can't init device stack, rc %d\n", rc);
502 GOTO(err_lu_site, rc);
505 /* populate cached statfs data */
506 osfs = &ofd_info(env)->fti_u.osfs;
507 rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
509 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
510 GOTO(err_fini_stack, rc);
512 if (!IS_PO2(osfs->os_bsize)) {
513 CERROR("%s: blocksize (%d) is not a power of 2\n",
514 obd->obd_name, osfs->os_bsize);
515 GOTO(err_fini_stack, rc = -EPROTO);
517 m->ofd_blockbits = cfs_fls(osfs->os_bsize) - 1;
519 snprintf(info->fti_u.name, sizeof(info->fti_u.name), "filter-%p", m);
520 m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
521 LDLM_NAMESPACE_SERVER,
522 LDLM_NAMESPACE_GREEDY,
524 if (m->ofd_namespace == NULL)
525 GOTO(err_fini_stack, rc = -ENOMEM);
526 /* set obd_namespace for compatibility with old code */
527 obd->obd_namespace = m->ofd_namespace;
528 ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
529 m->ofd_namespace->ns_lvbo = &ofd_lvbo;
530 m->ofd_namespace->ns_lvbp = m;
532 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
533 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
535 dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
537 /* Allow at most ddp_grant_reserved% of the available filesystem space
538 * to be granted to clients, so that any errors in the grant overhead
539 * calculations do not allow granting more space to clients than can be
540 * written. Assumes that in aggregate the grant overhead calculations do
541 * not have more than ddp_grant_reserved% estimation error in them. */
543 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
545 rc = ofd_start(env, &m->ofd_dt_dev.dd_lu_dev);
547 GOTO(err_fini_stack, rc);
549 rc = lut_init(env, &m->ofd_lut, obd, m->ofd_osd);
551 GOTO(err_free_ns, rc);
553 rc = ofd_fs_setup(env, m, obd);
555 GOTO(err_fini_lut, rc);
557 target_recovery_init(&m->ofd_lut, ost_handle);
559 rc = lu_site_init_finish(&m->ofd_site);
561 GOTO(err_fs_cleanup, rc);
565 target_recovery_fini(obd);
566 ofd_fs_cleanup(env, m);
568 lut_fini(env, &m->ofd_lut);
570 ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
571 obd->obd_namespace = m->ofd_namespace = NULL;
573 ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
575 lu_site_fini(&m->ofd_site);
581 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
583 struct obd_device *obd = ofd_obd(m);
584 struct lu_device *d = &m->ofd_dt_dev.dd_lu_dev;
586 target_recovery_fini(obd);
587 obd_exports_barrier(obd);
588 obd_zombie_barrier();
590 lut_fini(env, &m->ofd_lut);
591 ofd_fs_cleanup(env, m);
593 ofd_free_capa_keys(m);
594 cleanup_capa_hash(obd->u.filter.fo_capa_hash);
596 if (m->ofd_namespace != NULL) {
597 ldlm_namespace_free(m->ofd_namespace, NULL,
598 d->ld_obd->obd_force);
599 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
602 ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
603 lu_site_fini(&m->ofd_site);
605 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
606 server_put_mount(obd->obd_name, NULL);
610 static struct lu_device *ofd_device_fini(const struct lu_env *env,
614 ofd_fini(env, ofd_dev(d));
618 static struct lu_device *ofd_device_free(const struct lu_env *env,
621 struct ofd_device *m = ofd_dev(d);
623 dt_device_fini(&m->ofd_dt_dev);
628 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
629 struct lu_device_type *t,
630 struct lustre_cfg *cfg)
632 struct ofd_device *m;
638 return ERR_PTR(-ENOMEM);
640 l = &m->ofd_dt_dev.dd_lu_dev;
641 dt_device_init(&m->ofd_dt_dev, t);
642 rc = ofd_init0(env, m, t, cfg);
644 ofd_device_free(env, l);
651 /* thread context key constructor/destructor */
652 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
654 static void ofd_key_exit(const struct lu_context *ctx,
655 struct lu_context_key *key, void *data)
657 struct ofd_thread_info *info = data;
659 info->fti_env = NULL;
660 info->fti_exp = NULL;
663 info->fti_transno = 0;
664 info->fti_pre_version = 0;
665 info->fti_obj = NULL;
666 info->fti_has_trans = 0;
667 info->fti_mult_trans = 0;
670 memset(&info->fti_attr, 0, sizeof info->fti_attr);
673 struct lu_context_key ofd_thread_key = {
674 .lct_tags = LCT_DT_THREAD,
675 .lct_init = ofd_key_init,
676 .lct_fini = ofd_key_fini,
677 .lct_exit = ofd_key_exit
680 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
681 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
683 static struct lu_device_type_operations ofd_device_type_ops = {
684 .ldto_init = ofd_type_init,
685 .ldto_fini = ofd_type_fini,
687 .ldto_start = ofd_type_start,
688 .ldto_stop = ofd_type_stop,
690 .ldto_device_alloc = ofd_device_alloc,
691 .ldto_device_free = ofd_device_free,
692 .ldto_device_fini = ofd_device_fini
695 static struct lu_device_type ofd_device_type = {
696 .ldt_tags = LU_DEVICE_DT,
697 .ldt_name = LUSTRE_OST_NAME,
698 .ldt_ops = &ofd_device_type_ops,
699 .ldt_ctx_tags = LCT_DT_THREAD
702 int __init ofd_init(void)
704 struct lprocfs_static_vars lvars;
707 rc = lu_kmem_init(ofd_caches);
713 lu_kmem_fini(ofd_caches);
717 lprocfs_ofd_init_vars(&lvars);
719 rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
720 LUSTRE_OST_NAME, &ofd_device_type);
724 void __exit ofd_exit(void)
727 lu_kmem_fini(ofd_caches);
728 class_unregister_type(LUSTRE_OST_NAME);
731 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
732 MODULE_DESCRIPTION("Lustre Object Filtering Device");
733 MODULE_LICENSE("GPL");
735 module_init(ofd_init);
736 module_exit(ofd_exit);