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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/lod/lod_dev.c
34 * Lustre Logical Object Device
36 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
37 * Author: Mikhail Pershin <mike.pershin@intel.com>
40 * The Logical Object Device (LOD) layer manages access to striped
41 * objects (both regular files and directories). It implements the DT
42 * device and object APIs and is responsible for creating, storing,
43 * and loading striping information as an extended attribute of the
44 * underlying OSD object. LOD is the server side analog of the LOV and
45 * LMV layers on the client side.
47 * Metadata LU object stack (layers of the same compound LU object,
48 * all have the same FID):
58 * During LOD object initialization the localness or remoteness of the
59 * object FID dictates the choice between OSD and OSP.
61 * An LOD object (file or directory) with N stripes (each has a
68 * S0 S1 S2 S3 S(N-1) OS[DP] objects, seen as DT objects by LOD
70 * When upper layers must access an object's stripes (which are
71 * themselves OST or MDT LU objects) LOD finds these objects by their
72 * FIDs and stores them as an array of DT object pointers on the
73 * object. Declarations and operations on LOD objects are received by
74 * LOD (as DT object operations) and performed on the underlying
75 * OS[DP] object and (as needed) on the stripes. From the perspective
76 * of LOD, a stripe-less file (created by mknod() or open with
77 * O_LOV_DELAY_CREATE) is an object which does not yet have stripes,
78 * while a non-striped directory (created by mkdir()) is an object
79 * which will never have stripes.
81 * The LOD layer also implements a small subset of the OBD device API
82 * to support MDT stack initialization and finalization (an MDD device
83 * connects and disconnects itself to and from the underlying LOD
84 * device), and pool management. In turn LOD uses the OBD device API
85 * to connect it self to the underlying OSD, and to connect itself to
86 * OSP devices representing the MDTs and OSTs that bear the stripes of
90 #define DEBUG_SUBSYSTEM S_MDS
92 #include <linux/kthread.h>
93 #include <obd_class.h>
94 #include <md_object.h>
95 #include <lustre_fid.h>
96 #include <lustre_param.h>
97 #include <lustre_update.h>
98 #include <lustre_log.h>
100 #include "lod_internal.h"
102 static const char lod_update_log_name[] = "update_log";
103 static const char lod_update_log_dir_name[] = "update_log_dir";
106 * Lookup target by FID.
108 * Lookup MDT/OST target index by FID. Type of the target can be
111 * \param[in] env LU environment provided by the caller
112 * \param[in] lod lod device
114 * \param[out] tgt result target index
115 * \param[in] type expected type of the target:
116 * LU_SEQ_RANGE_{MDT,OST,ANY}
118 * \retval 0 on success
119 * \retval negative negated errno on error
121 int lod_fld_lookup(const struct lu_env *env, struct lod_device *lod,
122 const struct lu_fid *fid, __u32 *tgt, int *type)
124 struct lu_seq_range range = { 0 };
125 struct lu_server_fld *server_fld;
129 if (!fid_is_sane(fid)) {
130 CERROR("%s: invalid FID "DFID"\n", lod2obd(lod)->obd_name,
135 if (fid_is_idif(fid)) {
136 *tgt = fid_idif_ost_idx(fid);
137 *type = LU_SEQ_RANGE_OST;
141 if (fid_is_update_log(fid) || fid_is_update_log_dir(fid)) {
143 *type = LU_SEQ_RANGE_MDT;
147 if (!lod->lod_initialized || (!fid_seq_in_fldb(fid_seq(fid)))) {
148 LASSERT(lu_site2seq(lod2lu_dev(lod)->ld_site) != NULL);
150 *tgt = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
151 *type = LU_SEQ_RANGE_MDT;
155 server_fld = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_server_fld;
156 fld_range_set_type(&range, *type);
157 rc = fld_server_lookup(env, server_fld, fid_seq(fid), &range);
161 *tgt = range.lsr_index;
162 *type = range.lsr_flags;
164 CDEBUG(D_INFO, "%s: got tgt %x for sequence: "LPX64"\n",
165 lod2obd(lod)->obd_name, *tgt, fid_seq(fid));
170 /* Slab for OSD object allocation */
171 struct kmem_cache *lod_object_kmem;
173 static struct lu_kmem_descr lod_caches[] = {
175 .ckd_cache = &lod_object_kmem,
176 .ckd_name = "lod_obj",
177 .ckd_size = sizeof(struct lod_object)
184 static struct lu_device *lod_device_fini(const struct lu_env *env,
185 struct lu_device *d);
188 * Implementation of lu_device_operations::ldo_object_alloc() for LOD
190 * Allocates and initializes LOD's slice in the given object.
192 * see include/lu_object.h for the details.
194 static struct lu_object *lod_object_alloc(const struct lu_env *env,
195 const struct lu_object_header *hdr,
196 struct lu_device *dev)
198 struct lod_object *lod_obj;
199 struct lu_object *lu_obj;
202 OBD_SLAB_ALLOC_PTR_GFP(lod_obj, lod_object_kmem, GFP_NOFS);
204 RETURN(ERR_PTR(-ENOMEM));
206 lu_obj = lod2lu_obj(lod_obj);
207 dt_object_init(&lod_obj->ldo_obj, NULL, dev);
208 lod_obj->ldo_obj.do_ops = &lod_obj_ops;
209 lu_obj->lo_ops = &lod_lu_obj_ops;
215 * Cleanup table of target's descriptors.
217 * The function goes through all the targets in the given table
218 * and apply given configuration command on to the targets.
219 * Used to cleanup the targets at unmount.
221 * \param[in] env LU environment provided by the caller
222 * \param[in] lod lod device
223 * \param[in] ltd target's table to go through
224 * \param[in] lcfg configuration command to apply
226 * \retval 0 on success
227 * \retval negative negated errno on error
229 static int lod_sub_process_config(const struct lu_env *env,
230 struct lod_device *lod,
231 struct lod_tgt_descs *ltd,
232 struct lustre_cfg *lcfg)
234 struct lu_device *next;
239 if (ltd->ltd_tgts_size <= 0) {
240 lod_putref(lod, ltd);
243 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
244 struct lod_tgt_desc *tgt;
247 tgt = LTD_TGT(ltd, i);
248 LASSERT(tgt && tgt->ltd_tgt);
249 next = &tgt->ltd_tgt->dd_lu_dev;
250 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
252 CERROR("%s: error cleaning up LOD index %u: cmd %#x"
253 ": rc = %d\n", lod2obd(lod)->obd_name, i,
254 lcfg->lcfg_command, rc1);
258 lod_putref(lod, ltd);
262 struct lod_recovery_data {
263 struct lod_device *lrd_lod;
264 struct lod_tgt_desc *lrd_ltd;
265 struct ptlrpc_thread *lrd_thread;
270 * recovery thread for update log
272 * Start recovery thread and prepare the sub llog, then it will retrieve
273 * the update records from the correpondent MDT and do recovery.
275 * \param[in] arg pointer to the recovery data
277 * \retval 0 if recovery succeeds
278 * \retval negative errno if recovery failed.
280 static int lod_sub_recovery_thread(void *arg)
282 struct lod_recovery_data *lrd = arg;
283 struct lod_device *lod = lrd->lrd_lod;
284 struct dt_device *dt;
285 struct ptlrpc_thread *thread = lrd->lrd_thread;
290 thread->t_flags = SVC_RUNNING;
291 wake_up(&thread->t_ctl_waitq);
293 rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
296 CERROR("%s: can't initialize env: rc = %d\n",
297 lod2obd(lod)->obd_name, rc);
301 if (lrd->lrd_ltd == NULL)
304 dt = lrd->lrd_ltd->ltd_tgt;
306 rc = lod_sub_prep_llog(&env, lod, dt, lrd->lrd_idx);
310 /* XXX do recovery in the following patches */
314 thread->t_flags = SVC_STOPPED;
315 wake_up(&thread->t_ctl_waitq);
321 * Extract MDT target index from a device name.
323 * a helper function to extract index from the given device name
324 * like "fsname-MDTxxxx-mdtlov"
326 * \param[in] lodname device name
327 * \param[out] mdt_index extracted index
329 * \retval 0 on success
330 * \retval -EINVAL if the name is invalid
332 int lodname2mdt_index(char *lodname, __u32 *mdt_index)
337 /* 1.8 configs don't have "-MDT0000" at the end */
338 ptr = strstr(lodname, "-MDT");
344 ptr = strrchr(lodname, '-');
346 CERROR("invalid MDT index in '%s'\n", lodname);
350 if (strncmp(ptr, "-mdtlov", 7) != 0) {
351 CERROR("invalid MDT index in '%s'\n", lodname);
355 if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
356 CERROR("invalid MDT index in '%s'\n", lodname);
360 if (strncmp(ptr - 8, "-MDT", 4) != 0) {
361 CERROR("invalid MDT index in '%s'\n", lodname);
365 index = simple_strtol(ptr - 4, &tmp, 16);
366 if (*tmp != '-' || index > INT_MAX) {
367 CERROR("invalid MDT index in '%s'\n", lodname);
375 * Init sub llog context
377 * Setup update llog ctxt for update recovery threads, then start the
378 * recovery thread (lod_sub_recovery_thread) to read update llog from
379 * the correspondent MDT to do update recovery.
381 * \param[in] env execution environment
382 * \param[in] lod lod device to do update recovery
383 * \param[in] dt sub dt device for which the recovery thread is
385 * \retval 0 if initialization succeeds.
386 * \retval negative errno if initialization fails.
388 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
389 struct dt_device *dt)
391 struct obd_device *obd;
392 struct lod_recovery_data *lrd = NULL;
393 struct ptlrpc_thread *thread;
394 struct task_struct *task;
395 struct l_wait_info lwi = { 0 };
396 struct lod_tgt_desc *sub_ltd = NULL;
405 if (lod->lod_child == dt) {
406 thread = &lod->lod_child_recovery_thread;
407 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &index);
413 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
414 struct lod_tgt_desc *tgt = NULL;
417 mutex_lock(<d->ltd_mutex);
418 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
419 tgt = LTD_TGT(ltd, i);
420 if (tgt->ltd_tgt == dt) {
421 index = tgt->ltd_index;
426 mutex_unlock(<d->ltd_mutex);
427 OBD_ALLOC_PTR(tgt->ltd_recovery_thread);
428 if (tgt->ltd_recovery_thread == NULL) {
432 thread = tgt->ltd_recovery_thread;
436 lrd->lrd_ltd = sub_ltd;
437 lrd->lrd_thread = thread;
438 lrd->lrd_idx = index;
439 init_waitqueue_head(&thread->t_ctl_waitq);
441 obd = dt->dd_lu_dev.ld_obd;
442 obd->obd_lvfs_ctxt.dt = dt;
443 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
444 NULL, &llog_common_cat_ops);
446 CERROR("%s: cannot setup updatelog llog: rc = %d\n",
452 /* Start the recovery thread */
453 task = kthread_run(lod_sub_recovery_thread, lrd, "lod_recov_%04x",
458 CERROR("%s: cannot start recovery thread: rc = %d\n",
463 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING ||
464 thread->t_flags & SVC_STOPPED, &lwi);
467 lod_sub_fini_llog(env, dt, thread);
473 * finish sub llog context
475 * Stop update recovery thread for the sub device, then cleanup the
476 * correspondent llog ctxt.
478 * \param[in] env execution environment
479 * \param[in] dt dt device(lod) to do update recovery
480 * \param[in] thread recovery thread on this sub device
482 void lod_sub_fini_llog(const struct lu_env *env,
483 struct dt_device *dt, struct ptlrpc_thread *thread)
485 struct obd_device *obd;
486 struct llog_ctxt *ctxt;
488 CDEBUG(D_INFO, "%s: finish sub llog\n", dt->dd_lu_dev.ld_obd->obd_name);
489 /* Stop recovery thread first */
490 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
491 thread->t_flags = SVC_STOPPING;
492 wake_up(&thread->t_ctl_waitq);
493 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
496 obd = dt->dd_lu_dev.ld_obd;
497 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
501 if (ctxt->loc_handle != NULL)
502 llog_cat_close(env, ctxt->loc_handle);
504 llog_cleanup(env, ctxt);
508 * finish all sub llog
510 * cleanup all of sub llog ctxt on the LOD.
512 * \param[in] env execution environment
513 * \param[in] lod lod device to do update recovery
515 void lod_sub_fini_all_llogs(const struct lu_env *env, struct lod_device *lod)
517 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
520 /* Stop the update log commit cancel threads and finish master
522 lod_sub_fini_llog(env, lod->lod_child, &lod->lod_child_recovery_thread);
525 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
526 struct lod_tgt_desc *tgt;
528 tgt = LTD_TGT(ltd, i);
529 if (tgt->ltd_recovery_thread != NULL) {
530 lod_sub_fini_llog(env, tgt->ltd_tgt,
531 tgt->ltd_recovery_thread);
532 OBD_FREE_PTR(tgt->ltd_recovery_thread);
533 tgt->ltd_recovery_thread = NULL;
537 lod_putref(lod, ltd);
541 * Implementation of lu_device_operations::ldo_process_config() for LOD
543 * The method is called by the configuration subsystem during setup,
544 * cleanup and when the configuration changes. The method processes
545 * few specific commands like adding/removing the targets, changing
546 * the runtime parameters.
548 * \param[in] env LU environment provided by the caller
549 * \param[in] dev lod device
550 * \param[in] lcfg configuration command to apply
552 * \retval 0 on success
553 * \retval negative negated errno on error
555 * The examples are below.
557 * Add osc config log:
558 * marker 20 (flags=0x01, v2.2.49.56) lustre-OST0001 'add osc'
559 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nidxxx
560 * attach 0:lustre-OST0001-osc-MDT0001 1:osc 2:lustre-MDT0001-mdtlov_UUID
561 * setup 0:lustre-OST0001-osc-MDT0001 1:lustre-OST0001_UUID 2:nid
562 * lov_modify_tgts add 0:lustre-MDT0001-mdtlov 1:lustre-OST0001_UUID 2:1 3:1
563 * marker 20 (flags=0x02, v2.2.49.56) lustre-OST0001 'add osc'
565 * Add mdc config log:
566 * marker 10 (flags=0x01, v2.2.49.56) lustre-MDT0000 'add osp'
567 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nid
568 * attach 0:lustre-MDT0000-osp-MDT0001 1:osp 2:lustre-MDT0001-mdtlov_UUID
569 * setup 0:lustre-MDT0000-osp-MDT0001 1:lustre-MDT0000_UUID 2:nid
570 * modify_mdc_tgts add 0:lustre-MDT0001 1:lustre-MDT0000_UUID 2:0 3:1
571 * marker 10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID 'add osp'
573 static int lod_process_config(const struct lu_env *env,
574 struct lu_device *dev,
575 struct lustre_cfg *lcfg)
577 struct lod_device *lod = lu2lod_dev(dev);
578 struct lu_device *next = &lod->lod_child->dd_lu_dev;
583 switch(lcfg->lcfg_command) {
584 case LCFG_LOV_DEL_OBD:
585 case LCFG_LOV_ADD_INA:
586 case LCFG_LOV_ADD_OBD:
591 /* lov_modify_tgts add 0:lov_mdsA 1:osp 2:0 3:1
592 * modify_mdc_tgts add 0:lustre-MDT0001
593 * 1:lustre-MDT0001-mdc0002
595 arg1 = lustre_cfg_string(lcfg, 1);
597 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
598 GOTO(out, rc = -EINVAL);
599 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
600 GOTO(out, rc = -EINVAL);
602 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
605 rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
610 rc = lod_add_device(env, lod, arg1, index, gen,
611 mdt_index, LUSTRE_OSC_NAME, 1);
612 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
614 rc = lod_add_device(env, lod, arg1, index, gen,
615 mdt_index, LUSTRE_MDC_NAME, 1);
616 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
617 /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
619 rc = lod_add_device(env, lod, arg1, index, gen,
620 mdt_index, LUSTRE_OSC_NAME, 0);
622 rc = lod_del_device(env, lod,
624 arg1, index, gen, true);
631 struct obd_device *obd = lod2obd(lod);
633 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
639 case LCFG_PRE_CLEANUP: {
640 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
641 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
642 next = &lod->lod_child->dd_lu_dev;
643 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
645 CDEBUG(D_HA, "%s: can't process %u: %d\n",
646 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
648 lod_sub_fini_all_llogs(env, lod);
653 * do cleanup on underlying storage only when
654 * all OSPs are cleaned up, as they use that OSD as well
656 lu_dev_del_linkage(dev->ld_site, dev);
657 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
658 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
659 next = &lod->lod_child->dd_lu_dev;
660 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
662 CERROR("%s: can't process %u: %d\n",
663 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
665 rc = obd_disconnect(lod->lod_child_exp);
667 CERROR("error in disconnect from storage: %d\n", rc);
671 CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
682 * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
684 * The method is called once the recovery is complete. This implementation
685 * distributes the notification to all the known targets.
687 * see include/lu_object.h for the details
689 static int lod_recovery_complete(const struct lu_env *env,
690 struct lu_device *dev)
692 struct lod_device *lod = lu2lod_dev(dev);
693 struct lu_device *next = &lod->lod_child->dd_lu_dev;
698 LASSERT(lod->lod_recovery_completed == 0);
699 lod->lod_recovery_completed = 1;
701 rc = next->ld_ops->ldo_recovery_complete(env, next);
703 lod_getref(&lod->lod_ost_descs);
704 if (lod->lod_osts_size > 0) {
705 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
706 struct lod_tgt_desc *tgt;
707 tgt = OST_TGT(lod, i);
708 LASSERT(tgt && tgt->ltd_tgt);
709 next = &tgt->ltd_ost->dd_lu_dev;
710 rc = next->ld_ops->ldo_recovery_complete(env, next);
712 CERROR("%s: can't complete recovery on #%d:"
713 "%d\n", lod2obd(lod)->obd_name, i, rc);
716 lod_putref(lod, &lod->lod_ost_descs);
721 * Init update logs on all sub device
723 * LOD initialize update logs on all of sub devices. Because the initialization
724 * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
725 * lod_object_init(), this API has to be called after LOD is initialized.
726 * \param[in] env execution environment
727 * \param[in] lod lod device
729 * \retval 0 if update log is initialized successfully.
730 * \retval negative errno if initialization fails.
732 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
734 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
739 /* llog must be setup after LOD is initialized, because llog
740 * initialization include FLD lookup */
741 LASSERT(lod->lod_initialized);
743 /* Init the llog in its own stack */
744 rc = lod_sub_init_llog(env, lod, lod->lod_child);
748 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
749 struct lod_tgt_desc *tgt;
751 tgt = LTD_TGT(ltd, i);
752 rc = lod_sub_init_llog(env, lod, tgt->ltd_tgt);
761 * Implementation of lu_device_operations::ldo_prepare() for LOD
763 * see include/lu_object.h for the details.
765 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
766 struct lu_device *cdev)
768 struct lod_device *lod = lu2lod_dev(cdev);
769 struct lu_device *next = &lod->lod_child->dd_lu_dev;
770 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
772 struct dt_object *root;
773 struct dt_object *dto;
777 rc = next->ld_ops->ldo_prepare(env, pdev, next);
779 CERROR("%s: prepare bottom error: rc = %d\n",
780 lod2obd(lod)->obd_name, rc);
784 lod->lod_initialized = 1;
786 rc = dt_root_get(env, lod->lod_child, fid);
790 root = dt_locate(env, lod->lod_child, fid);
792 RETURN(PTR_ERR(root));
794 index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
795 lu_update_log_fid(fid, index);
797 /* Create update log object */
798 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
801 S_IFREG | S_IRUGO | S_IWUSR);
803 GOTO(out_put, rc = PTR_ERR(dto));
805 lu_object_put(env, &dto->do_lu);
807 /* Create update log dir */
808 lu_update_log_dir_fid(fid, index);
809 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
811 lod_update_log_dir_name,
812 S_IFDIR | S_IRUGO | S_IWUSR);
814 GOTO(out_put, rc = PTR_ERR(dto));
816 lu_object_put(env, &dto->do_lu);
818 rc = lod_sub_init_llogs(env, lod);
823 lu_object_put(env, &root->do_lu);
828 const struct lu_device_operations lod_lu_ops = {
829 .ldo_object_alloc = lod_object_alloc,
830 .ldo_process_config = lod_process_config,
831 .ldo_recovery_complete = lod_recovery_complete,
832 .ldo_prepare = lod_prepare,
836 * Implementation of dt_device_operations::dt_root_get() for LOD
838 * see include/dt_object.h for the details.
840 static int lod_root_get(const struct lu_env *env,
841 struct dt_device *dev, struct lu_fid *f)
843 return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
847 * Implementation of dt_device_operations::dt_statfs() for LOD
849 * see include/dt_object.h for the details.
851 static int lod_statfs(const struct lu_env *env,
852 struct dt_device *dev, struct obd_statfs *sfs)
854 return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
858 * Implementation of dt_device_operations::dt_trans_create() for LOD
860 * Creates a transaction using local (to this node) OSD.
862 * see include/dt_object.h for the details.
864 static struct thandle *lod_trans_create(const struct lu_env *env,
865 struct dt_device *dt)
869 th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
879 * Implementation of dt_device_operations::dt_trans_start() for LOD
881 * Starts the set of local transactions using the targets involved
882 * in declare phase. Initial support for the distributed transactions.
884 * see include/dt_object.h for the details.
886 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
889 return top_trans_start(env, dt2lod_dev(dt)->lod_child, th);
892 static int lod_trans_cb_add(struct thandle *th,
893 struct dt_txn_commit_cb *dcb)
895 struct top_thandle *top_th = container_of(th, struct top_thandle,
897 return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
901 * Implementation of dt_device_operations::dt_trans_stop() for LOD
903 * Stops the set of local transactions using the targets involved
904 * in declare phase. Initial support for the distributed transactions.
906 * see include/dt_object.h for the details.
908 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
911 return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
915 * Implementation of dt_device_operations::dt_conf_get() for LOD
917 * Currently returns the configuration provided by the local OSD.
919 * see include/dt_object.h for the details.
921 static void lod_conf_get(const struct lu_env *env,
922 const struct dt_device *dev,
923 struct dt_device_param *param)
925 dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
929 * Implementation of dt_device_operations::dt_sync() for LOD
931 * Syncs all known OST targets. Very very expensive and used
932 * rarely by LFSCK now. Should not be used in general.
934 * see include/dt_object.h for the details.
936 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
938 struct lod_device *lod = dt2lod_dev(dev);
939 struct lod_ost_desc *ost;
944 lod_getref(&lod->lod_ost_descs);
945 lod_foreach_ost(lod, i) {
946 ost = OST_TGT(lod, i);
947 LASSERT(ost && ost->ltd_ost);
948 rc = dt_sync(env, ost->ltd_ost);
950 CERROR("%s: can't sync %u: %d\n",
951 lod2obd(lod)->obd_name, i, rc);
955 lod_putref(lod, &lod->lod_ost_descs);
957 rc = dt_sync(env, lod->lod_child);
963 * Implementation of dt_device_operations::dt_ro() for LOD
965 * Turns local OSD read-only, used for the testing only.
967 * see include/dt_object.h for the details.
969 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
971 return dt_ro(env, dt2lod_dev(dev)->lod_child);
975 * Implementation of dt_device_operations::dt_commit_async() for LOD
977 * Asks local OSD to commit sooner.
979 * see include/dt_object.h for the details.
981 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
983 return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
986 static const struct dt_device_operations lod_dt_ops = {
987 .dt_root_get = lod_root_get,
988 .dt_statfs = lod_statfs,
989 .dt_trans_create = lod_trans_create,
990 .dt_trans_start = lod_trans_start,
991 .dt_trans_stop = lod_trans_stop,
992 .dt_conf_get = lod_conf_get,
995 .dt_commit_async = lod_commit_async,
996 .dt_trans_cb_add = lod_trans_cb_add,
1000 * Connect to a local OSD.
1002 * Used to connect to the local OSD at mount. OSD name is taken from the
1003 * configuration command passed. This connection is used to identify LU
1004 * site and pin the OSD from early removal.
1006 * \param[in] env LU environment provided by the caller
1007 * \param[in] lod lod device
1008 * \param[in] cfg configuration command to apply
1010 * \retval 0 on success
1011 * \retval negative negated errno on error
1013 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1014 struct lustre_cfg *cfg)
1016 struct obd_connect_data *data = NULL;
1017 struct obd_device *obd;
1018 char *nextdev = NULL, *p, *s;
1023 LASSERT(lod->lod_child_exp == NULL);
1025 /* compatibility hack: we still use old config logs
1026 * which specify LOV, but we need to learn underlying
1027 * OSD device, which is supposed to be:
1028 * <fsname>-MDTxxxx-osd
1030 * 2.x MGS generates lines like the following:
1031 * #03 (176)lov_setup 0:lustre-MDT0000-mdtlov 1:(struct lov_desc)
1032 * 1.8 MGS generates lines like the following:
1033 * #03 (168)lov_setup 0:lustre-mdtlov 1:(struct lov_desc)
1035 * we use "-MDT" to differentiate 2.x from 1.8 */
1037 if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
1038 len = strlen(p) + 1;
1039 OBD_ALLOC(nextdev, len);
1040 if (nextdev == NULL)
1041 GOTO(out, rc = -ENOMEM);
1044 s = strstr(nextdev, "-mdtlov");
1045 if (unlikely(s == NULL)) {
1046 CERROR("unable to parse device name %s\n",
1047 lustre_cfg_string(cfg, 0));
1048 GOTO(out, rc = -EINVAL);
1051 if (strstr(nextdev, "-MDT")) {
1056 strcpy(s, "-MDT0000-osd");
1059 CERROR("unable to parse device name %s\n",
1060 lustre_cfg_string(cfg, 0));
1061 GOTO(out, rc = -EINVAL);
1064 OBD_ALLOC_PTR(data);
1066 GOTO(out, rc = -ENOMEM);
1068 obd = class_name2obd(nextdev);
1070 CERROR("can not locate next device: %s\n", nextdev);
1071 GOTO(out, rc = -ENOTCONN);
1074 data->ocd_connect_flags = OBD_CONNECT_VERSION;
1075 data->ocd_version = LUSTRE_VERSION_CODE;
1077 rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
1080 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1084 lod->lod_dt_dev.dd_lu_dev.ld_site =
1085 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
1086 LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
1087 lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
1093 OBD_FREE(nextdev, len);
1098 * Allocate and initialize target table.
1100 * A helper function to initialize the target table and allocate
1101 * a bitmap of the available targets.
1103 * \param[in] ltd target's table to initialize
1105 * \retval 0 on success
1106 * \retval negative negated errno on error
1108 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
1110 mutex_init(<d->ltd_mutex);
1111 init_rwsem(<d->ltd_rw_sem);
1113 /* the OST array and bitmap are allocated/grown dynamically as OSTs are
1114 * added to the LOD, see lod_add_device() */
1115 ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
1116 if (ltd->ltd_tgt_bitmap == NULL)
1119 ltd->ltd_tgts_size = 32;
1122 ltd->ltd_death_row = 0;
1123 ltd->ltd_refcount = 0;
1128 * Initialize LOD device at setup.
1130 * Initializes the given LOD device using the original configuration command.
1131 * The function initiates a connection to the local OSD and initializes few
1132 * internal structures like pools, target tables, etc.
1134 * \param[in] env LU environment provided by the caller
1135 * \param[in] lod lod device
1136 * \param[in] ldt not used
1137 * \param[in] cfg configuration command
1139 * \retval 0 on success
1140 * \retval negative negated errno on error
1142 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
1143 struct lu_device_type *ldt, struct lustre_cfg *cfg)
1145 struct dt_device_param ddp;
1146 struct obd_device *obd;
1150 obd = class_name2obd(lustre_cfg_string(cfg, 0));
1152 CERROR("Cannot find obd with name %s\n",
1153 lustre_cfg_string(cfg, 0));
1157 obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1158 lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
1159 lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
1160 lod->lod_dt_dev.dd_ops = &lod_dt_ops;
1162 rc = lod_connect_to_osd(env, lod, cfg);
1166 dt_conf_get(env, &lod->lod_dt_dev, &ddp);
1167 lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
1169 /* setup obd to be used with old lov code */
1170 rc = lod_pools_init(lod, cfg);
1172 GOTO(out_disconnect, rc);
1174 rc = lod_procfs_init(lod);
1176 GOTO(out_pools, rc);
1178 spin_lock_init(&lod->lod_desc_lock);
1179 spin_lock_init(&lod->lod_connects_lock);
1180 lod_tgt_desc_init(&lod->lod_mdt_descs);
1181 lod_tgt_desc_init(&lod->lod_ost_descs);
1186 lod_pools_fini(lod);
1188 obd_disconnect(lod->lod_child_exp);
1193 * Implementation of lu_device_type_operations::ldto_device_free() for LOD
1195 * Releases the memory allocated for LOD device.
1197 * see include/lu_object.h for the details.
1199 static struct lu_device *lod_device_free(const struct lu_env *env,
1200 struct lu_device *lu)
1202 struct lod_device *lod = lu2lod_dev(lu);
1203 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1206 LASSERT(atomic_read(&lu->ld_ref) == 0);
1207 dt_device_fini(&lod->lod_dt_dev);
1213 * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
1215 * Allocates LOD device and calls the helpers to initialize it.
1217 * see include/lu_object.h for the details.
1219 static struct lu_device *lod_device_alloc(const struct lu_env *env,
1220 struct lu_device_type *type,
1221 struct lustre_cfg *lcfg)
1223 struct lod_device *lod;
1224 struct lu_device *lu_dev;
1228 lu_dev = ERR_PTR(-ENOMEM);
1232 lu_dev = lod2lu_dev(lod);
1233 dt_device_init(&lod->lod_dt_dev, type);
1234 rc = lod_init0(env, lod, type, lcfg);
1236 lod_device_free(env, lu_dev);
1237 lu_dev = ERR_PTR(rc);
1245 * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
1247 * Releases the internal resources used by LOD device.
1249 * see include/lu_object.h for the details.
1251 static struct lu_device *lod_device_fini(const struct lu_env *env,
1252 struct lu_device *d)
1254 struct lod_device *lod = lu2lod_dev(d);
1258 lod_pools_fini(lod);
1260 lod_procfs_fini(lod);
1262 rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
1264 CERROR("%s:can not fini ost descs %d\n",
1265 lod2obd(lod)->obd_name, rc);
1267 rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
1269 CERROR("%s:can not fini mdt descs %d\n",
1270 lod2obd(lod)->obd_name, rc);
1276 * Implementation of obd_ops::o_connect() for LOD
1278 * Used to track all the users of this specific LOD device,
1279 * so the device stays up until the last user disconnected.
1281 * \param[in] env LU environment provided by the caller
1282 * \param[out] exp export the caller will be using to access LOD
1283 * \param[in] obd OBD device representing LOD device
1284 * \param[in] cluuid unique identifier of the caller
1285 * \param[in] data not used
1286 * \param[in] localdata not used
1288 * \retval 0 on success
1289 * \retval negative negated errno on error
1291 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1292 struct obd_device *obd, struct obd_uuid *cluuid,
1293 struct obd_connect_data *data, void *localdata)
1295 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1296 struct lustre_handle conn;
1300 CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1302 rc = class_connect(&conn, obd, cluuid);
1306 *exp = class_conn2export(&conn);
1308 spin_lock(&lod->lod_connects_lock);
1309 lod->lod_connects++;
1310 /* at the moment we expect the only user */
1311 LASSERT(lod->lod_connects == 1);
1312 spin_unlock(&lod->lod_connects_lock);
1319 * Implementation of obd_ops::o_disconnect() for LOD
1321 * When the caller doesn't need to use this LOD instance, it calls
1322 * obd_disconnect() and LOD releases corresponding export/reference count.
1323 * Once all the users gone, LOD device is released.
1325 * \param[in] exp export provided to the caller in obd_connect()
1327 * \retval 0 on success
1328 * \retval negative negated errno on error
1330 static int lod_obd_disconnect(struct obd_export *exp)
1332 struct obd_device *obd = exp->exp_obd;
1333 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1334 int rc, release = 0;
1337 /* Only disconnect the underlying layers on the final disconnect. */
1338 spin_lock(&lod->lod_connects_lock);
1339 lod->lod_connects--;
1340 if (lod->lod_connects != 0) {
1341 /* why should there be more than 1 connect? */
1342 spin_unlock(&lod->lod_connects_lock);
1343 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1347 spin_unlock(&lod->lod_connects_lock);
1349 /* the last user of lod has gone, let's release the device */
1353 rc = class_disconnect(exp); /* bz 9811 */
1355 if (rc == 0 && release)
1356 class_manual_cleanup(obd);
1360 LU_KEY_INIT(lod, struct lod_thread_info);
1362 static void lod_key_fini(const struct lu_context *ctx,
1363 struct lu_context_key *key, void *data)
1365 struct lod_thread_info *info = data;
1366 /* allocated in lod_get_lov_ea
1367 * XXX: this is overload, a tread may have such store but used only
1368 * once. Probably better would be pool of such stores per LOD.
1370 if (info->lti_ea_store) {
1371 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1372 info->lti_ea_store = NULL;
1373 info->lti_ea_store_size = 0;
1375 lu_buf_free(&info->lti_linkea_buf);
1379 /* context key: lod_thread_key */
1380 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1382 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1384 static struct lu_device_type_operations lod_device_type_ops = {
1385 .ldto_init = lod_type_init,
1386 .ldto_fini = lod_type_fini,
1388 .ldto_start = lod_type_start,
1389 .ldto_stop = lod_type_stop,
1391 .ldto_device_alloc = lod_device_alloc,
1392 .ldto_device_free = lod_device_free,
1394 .ldto_device_fini = lod_device_fini
1397 static struct lu_device_type lod_device_type = {
1398 .ldt_tags = LU_DEVICE_DT,
1399 .ldt_name = LUSTRE_LOD_NAME,
1400 .ldt_ops = &lod_device_type_ops,
1401 .ldt_ctx_tags = LCT_MD_THREAD,
1405 * Implementation of obd_ops::o_get_info() for LOD
1407 * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
1408 * the caller binary status whether LOD has seen connection to any OST target.
1409 * It will also check if the MDT update log context being initialized (if
1412 * \param[in] env LU environment provided by the caller
1413 * \param[in] exp export of the caller
1414 * \param[in] keylen len of the key
1415 * \param[in] key the key
1416 * \param[in] vallen not used
1417 * \param[in] val not used
1419 * \retval 0 if a connection was seen
1420 * \retval -EAGAIN if LOD isn't running yet or no
1421 * connection has been seen yet
1422 * \retval -EINVAL if not supported key is requested
1424 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1425 __u32 keylen, void *key, __u32 *vallen, void *val)
1429 if (KEY_IS(KEY_OSP_CONNECTED)) {
1430 struct obd_device *obd = exp->exp_obd;
1431 struct lod_device *d;
1432 struct lod_tgt_desc *tgt;
1436 if (!obd->obd_set_up || obd->obd_stopping)
1439 d = lu2lod_dev(obd->obd_lu_dev);
1440 lod_getref(&d->lod_ost_descs);
1441 lod_foreach_ost(d, i) {
1442 tgt = OST_TGT(d, i);
1443 LASSERT(tgt && tgt->ltd_tgt);
1444 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
1446 /* one healthy device is enough */
1450 lod_putref(d, &d->lod_ost_descs);
1452 lod_getref(&d->lod_mdt_descs);
1453 lod_foreach_mdt(d, i) {
1454 struct llog_ctxt *ctxt;
1456 tgt = MDT_TGT(d, i);
1457 LASSERT(tgt != NULL);
1458 LASSERT(tgt->ltd_tgt != NULL);
1459 ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
1460 LLOG_UPDATELOG_ORIG_CTXT);
1465 if (ctxt->loc_handle == NULL) {
1467 llog_ctxt_put(ctxt);
1470 llog_ctxt_put(ctxt);
1472 lod_putref(d, &d->lod_mdt_descs);
1480 static struct obd_ops lod_obd_device_ops = {
1481 .o_owner = THIS_MODULE,
1482 .o_connect = lod_obd_connect,
1483 .o_disconnect = lod_obd_disconnect,
1484 .o_get_info = lod_obd_get_info,
1485 .o_pool_new = lod_pool_new,
1486 .o_pool_rem = lod_pool_remove,
1487 .o_pool_add = lod_pool_add,
1488 .o_pool_del = lod_pool_del,
1491 static int __init lod_mod_init(void)
1493 struct obd_type *type;
1496 rc = lu_kmem_init(lod_caches);
1500 rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
1501 LUSTRE_LOD_NAME, &lod_device_type);
1503 lu_kmem_fini(lod_caches);
1507 /* create "lov" entry in procfs for compatibility purposes */
1508 type = class_search_type(LUSTRE_LOV_NAME);
1509 if (type != NULL && type->typ_procroot != NULL)
1512 type = class_search_type(LUSTRE_LOD_NAME);
1513 type->typ_procsym = lprocfs_register("lov", proc_lustre_root,
1515 if (IS_ERR(type->typ_procsym)) {
1516 CERROR("lod: can't create compat entry \"lov\": %d\n",
1517 (int)PTR_ERR(type->typ_procsym));
1518 type->typ_procsym = NULL;
1523 static void __exit lod_mod_exit(void)
1525 class_unregister_type(LUSTRE_LOD_NAME);
1526 lu_kmem_fini(lod_caches);
1529 MODULE_AUTHOR("Intel Corporation. <https://wiki.hpdd.intel.com/>");
1530 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
1531 MODULE_LICENSE("GPL");
1533 module_init(lod_mod_init);
1534 module_exit(lod_mod_exit);