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.
31 * Copyright (c) 2011, 2012 Whamcloud, Inc.
32 * Use is subject to license terms.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/osd-zfs/osd_handler.c
40 * Top-level entry points into osd module
42 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
43 * Author: Mike Pershin <tappro@whamcloud.com>
44 * Author: Johann Lombardi <johann@whamcloud.com>
48 # define EXPORT_SYMTAB
50 #define DEBUG_SUBSYSTEM S_OSD
52 #include <lustre_ver.h>
53 #include <libcfs/libcfs.h>
54 #include <obd_support.h>
55 #include <lustre_net.h>
57 #include <obd_class.h>
58 #include <lustre_disk.h>
59 #include <lustre_fid.h>
61 #include "osd_internal.h"
63 #include <sys/dnode.h>
68 #include <sys/spa_impl.h>
69 #include <sys/zfs_znode.h>
70 #include <sys/dmu_tx.h>
71 #include <sys/dmu_objset.h>
72 #include <sys/dsl_prop.h>
73 #include <sys/sa_impl.h>
76 struct lu_context_key osd_key;
78 static char *root_tag = "osd_mount, rootdb";
80 /* Slab for OSD object allocation */
81 cfs_mem_cache_t *osd_object_kmem;
83 static struct lu_kmem_descr osd_caches[] = {
85 .ckd_cache = &osd_object_kmem,
86 .ckd_name = "zfs_osd_obj",
87 .ckd_size = sizeof(struct osd_object)
94 static void arc_prune_func(int64_t bytes, void *private)
96 struct osd_device *od = private;
97 struct lu_site *site = &od->od_site;
101 rc = lu_env_init(&env, LCT_SHRINKER);
103 CERROR("%s: can't initialize shrinker env: rc = %d\n",
108 lu_site_purge(&env, site, (bytes >> 10));
114 * Concurrency: doesn't access mutable data
116 static int osd_root_get(const struct lu_env *env,
117 struct dt_device *dev, struct lu_fid *f)
119 lu_local_obj_fid(f, OSD_FS_ROOT_OID);
124 * OSD object methods.
128 * Concurrency: shouldn't matter.
130 static void osd_trans_commit_cb(void *cb_data, int error)
132 struct osd_thandle *oh = cb_data;
133 struct thandle *th = &oh->ot_super;
134 struct lu_device *lud = &th->th_dev->dd_lu_dev;
135 struct dt_txn_commit_cb *dcb, *tmp;
140 if (error == ECANCELED)
141 CWARN("%s: transaction @0x%p was aborted\n",
142 osd_dt_dev(th->th_dev)->od_svname, th);
144 CERROR("%s: transaction @0x%p commit error: rc = %d\n",
145 osd_dt_dev(th->th_dev)->od_svname, th, error);
148 dt_txn_hook_commit(th);
150 /* call per-transaction callbacks if any */
151 cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage)
152 dcb->dcb_func(NULL, th, dcb, error);
156 lu_context_exit(&th->th_ctx);
157 lu_context_fini(&th->th_ctx);
163 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
165 struct osd_thandle *oh;
167 oh = container_of0(th, struct osd_thandle, ot_super);
168 cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
174 * Concurrency: shouldn't matter.
176 static int osd_trans_start(const struct lu_env *env, struct dt_device *d,
179 struct osd_thandle *oh;
183 oh = container_of0(th, struct osd_thandle, ot_super);
187 rc = dt_txn_hook_start(env, d, th);
191 if (oh->ot_write_commit && OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
192 /* Unlike ldiskfs, ZFS checks for available space and returns
193 * -ENOSPC when assigning txg */
196 rc = -dmu_tx_assign(oh->ot_tx, TXG_WAIT);
197 if (unlikely(rc != 0)) {
198 struct osd_device *osd = osd_dt_dev(d);
199 /* dmu will call commit callback with error code during abort */
200 if (!lu_device_is_md(&d->dd_lu_dev) && rc == -ENOSPC)
201 CERROR("%s: failed to start transaction due to ENOSPC. "
202 "Metadata overhead is underestimated or "
203 "grant_ratio is too low.\n",
204 osd->od_dt_dev.dd_lu_dev.ld_obd->obd_name);
206 CERROR("%s: can't assign tx: rc = %d\n",
207 osd->od_dt_dev.dd_lu_dev.ld_obd->obd_name, rc);
209 /* add commit callback */
210 dmu_tx_callback_register(oh->ot_tx, osd_trans_commit_cb, oh);
212 lu_context_init(&th->th_ctx, th->th_tags);
213 lu_context_enter(&th->th_ctx);
214 lu_device_get(&d->dd_lu_dev);
221 * Concurrency: shouldn't matter.
223 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
225 struct osd_device *osd = osd_dt_dev(th->th_dev);
226 struct osd_thandle *oh;
231 oh = container_of0(th, struct osd_thandle, ot_super);
233 if (oh->ot_assigned == 0) {
235 dmu_tx_abort(oh->ot_tx);
236 osd_object_sa_dirty_rele(oh);
241 rc = dt_txn_hook_stop(env, th);
243 CDEBUG(D_OTHER, "%s: transaction hook failed: rc = %d\n",
247 txg = oh->ot_tx->tx_txg;
249 osd_object_sa_dirty_rele(oh);
250 dmu_tx_commit(oh->ot_tx);
253 txg_wait_synced(dmu_objset_pool(osd->od_objset.os), txg);
258 static struct thandle *osd_trans_create(const struct lu_env *env,
259 struct dt_device *dt)
261 struct osd_device *osd = osd_dt_dev(dt);
262 struct osd_thandle *oh;
267 tx = dmu_tx_create(osd->od_objset.os);
269 RETURN(ERR_PTR(-ENOMEM));
271 /* alloc callback data */
275 RETURN(ERR_PTR(-ENOMEM));
279 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
280 CFS_INIT_LIST_HEAD(&oh->ot_sa_list);
281 cfs_sema_init(&oh->ot_sa_lock, 1);
285 th->th_tags = LCT_TX_HANDLE;
290 * Concurrency: shouldn't matter.
292 int osd_statfs(const struct lu_env *env, struct dt_device *d,
293 struct obd_statfs *osfs)
295 struct osd_device *osd = osd_dt_dev(d);
299 rc = udmu_objset_statfs(&osd->od_objset, osfs);
302 osfs->os_bavail -= min_t(obd_size,
303 OSD_GRANT_FOR_LOCAL_OIDS / osfs->os_bsize,
309 * Concurrency: doesn't access mutable data.
311 static void osd_conf_get(const struct lu_env *env,
312 const struct dt_device *dev,
313 struct dt_device_param *param)
316 * XXX should be taken from not-yet-existing fs abstraction layer.
318 param->ddp_max_name_len = MAXNAMELEN;
319 param->ddp_max_nlink = 1 << 31; /* it's 8byte on a disk */
320 param->ddp_block_shift = 12; /* XXX */
321 param->ddp_mount_type = LDD_MT_ZFS;
323 param->ddp_mntopts = MNTOPT_USERXATTR | MNTOPT_ACL;
324 param->ddp_max_ea_size = DXATTR_MAX_ENTRY_SIZE;
326 /* for maxbytes, report same value as ZPL */
327 param->ddp_maxbytes = MAX_LFS_FILESIZE;
329 /* Default reserved fraction of the available space that should be kept
330 * for error margin. Unfortunately, there are many factors that can
331 * impact the overhead with zfs, so let's be very cautious for now and
332 * reserve 20% of the available space which is not given out as grant.
333 * This tunable can be changed on a live system via procfs if needed. */
334 param->ddp_grant_reserved = 20;
336 /* inodes are dynamically allocated, so we report the per-inode space
337 * consumption to upper layers. This static value is not really accurate
338 * and we should use the same logic as in udmu_objset_statfs() to
339 * estimate the real size consumed by an object */
340 param->ddp_inodespace = OSD_DNODE_EST_COUNT;
341 /* per-fragment overhead to be used by the client code */
342 param->ddp_grant_frag = udmu_blk_insert_cost();
344 param->ddp_mnt = NULL;
348 * Concurrency: shouldn't matter.
350 static int osd_sync(const struct lu_env *env, struct dt_device *d)
352 struct osd_device *osd = osd_dt_dev(d);
353 CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_ZFS_NAME);
354 txg_wait_synced(dmu_objset_pool(osd->od_objset.os), 0ULL);
358 static int osd_commit_async(const struct lu_env *env, struct dt_device *dev)
360 struct osd_device *osd = osd_dt_dev(dev);
361 tx_state_t *tx = &dmu_objset_pool(osd->od_objset.os)->dp_tx;
364 txg = tx->tx_open_txg + 1;
365 if (tx->tx_quiesce_txg_waiting < txg) {
366 tx->tx_quiesce_txg_waiting = txg;
367 cv_broadcast(&tx->tx_quiesce_more_cv);
369 mutex_exit(&tx->tx_sync_lock);
375 * Concurrency: shouldn't matter.
377 static int osd_ro(const struct lu_env *env, struct dt_device *d)
379 struct osd_device *osd = osd_dt_dev(d);
382 CERROR("%s: *** setting device %s read-only ***\n",
383 osd->od_svname, LUSTRE_OSD_ZFS_NAME);
385 spa_freeze(dmu_objset_spa(osd->od_objset.os));
391 * Concurrency: serialization provided by callers.
393 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
394 int mode, unsigned long timeout, __u32 alg,
395 struct lustre_capa_key *keys)
397 struct osd_device *dev = osd_dt_dev(d);
400 dev->od_fl_capa = mode;
401 dev->od_capa_timeout = timeout;
402 dev->od_capa_alg = alg;
403 dev->od_capa_keys = keys;
408 static struct dt_device_operations osd_dt_ops = {
409 .dt_root_get = osd_root_get,
410 .dt_statfs = osd_statfs,
411 .dt_trans_create = osd_trans_create,
412 .dt_trans_start = osd_trans_start,
413 .dt_trans_stop = osd_trans_stop,
414 .dt_trans_cb_add = osd_trans_cb_add,
415 .dt_conf_get = osd_conf_get,
417 .dt_commit_async = osd_commit_async,
419 .dt_init_capa_ctxt = osd_init_capa_ctxt,
423 * DMU OSD device type methods
425 static int osd_type_init(struct lu_device_type *t)
427 LU_CONTEXT_KEY_INIT(&osd_key);
428 return lu_context_key_register(&osd_key);
431 static void osd_type_fini(struct lu_device_type *t)
433 lu_context_key_degister(&osd_key);
436 static void *osd_key_init(const struct lu_context *ctx,
437 struct lu_context_key *key)
439 struct osd_thread_info *info;
443 info->oti_env = container_of(ctx, struct lu_env, le_ctx);
445 info = ERR_PTR(-ENOMEM);
449 static void osd_key_fini(const struct lu_context *ctx,
450 struct lu_context_key *key, void *data)
452 struct osd_thread_info *info = data;
457 static void osd_key_exit(const struct lu_context *ctx,
458 struct lu_context_key *key, void *data)
460 struct osd_thread_info *info = data;
462 memset(info, 0, sizeof(*info));
465 struct lu_context_key osd_key = {
466 .lct_tags = LCT_DT_THREAD | LCT_MD_THREAD | LCT_MG_THREAD | LCT_LOCAL,
467 .lct_init = osd_key_init,
468 .lct_fini = osd_key_fini,
469 .lct_exit = osd_key_exit
472 static int osd_shutdown(const struct lu_env *env, struct osd_device *o)
476 /* shutdown quota slave instance associated with the device */
477 if (o->od_quota_slave != NULL) {
478 qsd_fini(env, o->od_quota_slave);
479 o->od_quota_slave = NULL;
485 static int osd_mount(const struct lu_env *env,
486 struct osd_device *o, struct lustre_cfg *cfg)
488 char *dev = lustre_cfg_string(cfg, 1);
493 if (o->od_objset.os != NULL)
496 if (strlen(dev) >= sizeof(o->od_mntdev))
499 strcpy(o->od_mntdev, dev);
500 strncpy(o->od_svname, lustre_cfg_string(cfg, 4),
501 sizeof(o->od_svname) - 1);
503 rc = -udmu_objset_open(o->od_mntdev, &o->od_objset);
505 CERROR("can't open objset %s: %d\n", o->od_mntdev, rc);
509 rc = __osd_obj2dbuf(env, o->od_objset.os, o->od_objset.root,
512 CERROR("udmu_obj2dbuf() failed with error %d\n", rc);
513 udmu_objset_close(&o->od_objset);
517 o->od_root = rootdb->db_object;
518 sa_buf_rele(rootdb, root_tag);
520 /* 1. initialize oi before any file create or file open */
521 rc = osd_oi_init(env, o);
525 rc = lu_site_init(&o->od_site, osd2lu_dev(o));
529 rc = lu_site_init_finish(&o->od_site);
533 /* Use our own ZAP for inode accounting by default, this can be changed
534 * via procfs to estimate the inode usage from the block usage */
535 o->od_quota_iused_est = 0;
537 rc = osd_procfs_init(o, o->od_svname);
541 o->arc_prune_cb = arc_add_prune_callback(arc_prune_func, o);
547 static void osd_umount(const struct lu_env *env, struct osd_device *o)
551 if (cfs_atomic_read(&o->od_zerocopy_alloc))
552 CERROR("%s: lost %d allocated page(s)\n", o->od_svname,
553 cfs_atomic_read(&o->od_zerocopy_alloc));
554 if (cfs_atomic_read(&o->od_zerocopy_loan))
555 CERROR("%s: lost %d loaned abuf(s)\n", o->od_svname,
556 cfs_atomic_read(&o->od_zerocopy_loan));
557 if (cfs_atomic_read(&o->od_zerocopy_pin))
558 CERROR("%s: lost %d pinned dbuf(s)\n", o->od_svname,
559 cfs_atomic_read(&o->od_zerocopy_pin));
561 if (o->od_objset.os != NULL)
562 udmu_objset_close(&o->od_objset);
567 static int osd_device_init0(const struct lu_env *env,
568 struct osd_device *o,
569 struct lustre_cfg *cfg)
571 struct lu_device *l = osd2lu_dev(o);
574 /* if the module was re-loaded, env can loose its keys */
575 rc = lu_env_refill((struct lu_env *) env);
579 l->ld_ops = &osd_lu_ops;
580 o->od_dt_dev.dd_ops = &osd_dt_ops;
582 o->od_capa_hash = init_capa_hash();
583 if (o->od_capa_hash == NULL)
584 GOTO(out, rc = -ENOMEM);
590 static struct lu_device *osd_device_fini(const struct lu_env *env,
591 struct lu_device *dev);
593 static struct lu_device *osd_device_alloc(const struct lu_env *env,
594 struct lu_device_type *type,
595 struct lustre_cfg *cfg)
597 struct osd_device *dev;
602 return ERR_PTR(-ENOMEM);
604 rc = dt_device_init(&dev->od_dt_dev, type);
606 rc = osd_device_init0(env, dev, cfg);
608 rc = osd_mount(env, dev, cfg);
610 osd_device_fini(env, osd2lu_dev(dev));
613 dt_device_fini(&dev->od_dt_dev);
616 if (unlikely(rc != 0))
619 return rc == 0 ? osd2lu_dev(dev) : ERR_PTR(rc);
622 static struct lu_device *osd_device_free(const struct lu_env *env,
625 struct osd_device *o = osd_dev(d);
628 cleanup_capa_hash(o->od_capa_hash);
629 /* XXX: make osd top device in order to release reference */
630 d->ld_site->ls_top_dev = d;
631 lu_site_purge(env, d->ld_site, -1);
632 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
633 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
634 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
636 lu_site_fini(&o->od_site);
637 dt_device_fini(&o->od_dt_dev);
643 static struct lu_device *osd_device_fini(const struct lu_env *env,
646 struct osd_device *o = osd_dev(d);
653 if (o->od_objset.os) {
654 arc_remove_prune_callback(o->arc_prune_cb);
655 o->arc_prune_cb = NULL;
656 osd_sync(env, lu2dt_dev(d));
657 txg_wait_callbacks(spa_get_dsl(dmu_objset_spa(o->od_objset.os)));
660 rc = osd_procfs_fini(o);
662 CERROR("proc fini error %d\n", rc);
672 static int osd_device_init(const struct lu_env *env, struct lu_device *d,
673 const char *name, struct lu_device *next)
679 * To be removed, setup is performed by osd_device_{init,alloc} and
680 * cleanup is performed by osd_device_{fini,free).
682 static int osd_process_config(const struct lu_env *env,
683 struct lu_device *d, struct lustre_cfg *cfg)
685 struct osd_device *o = osd_dev(d);
689 switch(cfg->lcfg_command) {
691 err = osd_mount(env, o, cfg);
694 err = osd_shutdown(env, o);
703 static int osd_recovery_complete(const struct lu_env *env, struct lu_device *d)
710 * we use exports to track all osd users
712 static int osd_obd_connect(const struct lu_env *env, struct obd_export **exp,
713 struct obd_device *obd, struct obd_uuid *cluuid,
714 struct obd_connect_data *data, void *localdata)
716 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
717 struct lustre_handle conn;
721 CDEBUG(D_CONFIG, "connect #%d\n", osd->od_connects);
723 rc = class_connect(&conn, obd, cluuid);
727 *exp = class_conn2export(&conn);
729 cfs_spin_lock(&osd->od_objset.lock);
731 cfs_spin_unlock(&osd->od_objset.lock);
737 * once last export (we don't count self-export) disappeared
738 * osd can be released
740 static int osd_obd_disconnect(struct obd_export *exp)
742 struct obd_device *obd = exp->exp_obd;
743 struct osd_device *osd = osd_dev(obd->obd_lu_dev);
747 /* Only disconnect the underlying layers on the final disconnect. */
748 cfs_spin_lock(&osd->od_objset.lock);
750 if (osd->od_connects == 0)
752 cfs_spin_unlock(&osd->od_objset.lock);
754 rc = class_disconnect(exp); /* bz 9811 */
756 if (rc == 0 && release)
757 class_manual_cleanup(obd);
761 static int osd_prepare(const struct lu_env *env, struct lu_device *pdev,
762 struct lu_device *dev)
764 struct osd_device *osd = osd_dev(dev);
768 /* initialize quota slave instance */
769 osd->od_quota_slave = qsd_init(env, osd->od_svname, &osd->od_dt_dev,
771 if (IS_ERR(osd->od_quota_slave)) {
772 rc = PTR_ERR(osd->od_quota_slave);
773 osd->od_quota_slave = NULL;
779 struct lu_device_operations osd_lu_ops = {
780 .ldo_object_alloc = osd_object_alloc,
781 .ldo_process_config = osd_process_config,
782 .ldo_recovery_complete = osd_recovery_complete,
783 .ldo_prepare = osd_prepare,
786 static void osd_type_start(struct lu_device_type *t)
790 static void osd_type_stop(struct lu_device_type *t)
794 static struct lu_device_type_operations osd_device_type_ops = {
795 .ldto_init = osd_type_init,
796 .ldto_fini = osd_type_fini,
798 .ldto_start = osd_type_start,
799 .ldto_stop = osd_type_stop,
801 .ldto_device_alloc = osd_device_alloc,
802 .ldto_device_free = osd_device_free,
804 .ldto_device_init = osd_device_init,
805 .ldto_device_fini = osd_device_fini
808 static struct lu_device_type osd_device_type = {
809 .ldt_tags = LU_DEVICE_DT,
810 .ldt_name = LUSTRE_OSD_ZFS_NAME,
811 .ldt_ops = &osd_device_type_ops,
812 .ldt_ctx_tags = LCT_LOCAL
816 static struct obd_ops osd_obd_device_ops = {
817 .o_owner = THIS_MODULE,
818 .o_connect = osd_obd_connect,
819 .o_disconnect = osd_obd_disconnect
822 int __init osd_init(void)
826 rc = osd_options_init();
830 rc = lu_kmem_init(osd_caches);
834 rc = class_register_type(&osd_obd_device_ops, NULL,
835 lprocfs_osd_module_vars,
836 LUSTRE_OSD_ZFS_NAME, &osd_device_type);
838 lu_kmem_fini(osd_caches);
842 void __exit osd_exit(void)
844 class_unregister_type(LUSTRE_OSD_ZFS_NAME);
845 lu_kmem_fini(osd_caches);
848 extern unsigned int osd_oi_count;
849 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
850 "Number of Object Index containers to be created, "
851 "it's only valid for new filesystem.");
853 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
854 MODULE_DESCRIPTION("Lustre Object Storage Device ("LUSTRE_OSD_ZFS_NAME")");
855 MODULE_LICENSE("GPL");
857 cfs_module(osd, LUSTRE_VERSION_STRING, osd_init, osd_exit);