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, 2016, 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 <uapi/linux/lustre/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 if (server_fld == NULL)
159 fld_range_set_type(&range, *type);
160 rc = fld_server_lookup(env, server_fld, fid_seq(fid), &range);
164 *tgt = range.lsr_index;
165 *type = range.lsr_flags;
167 CDEBUG(D_INFO, "%s: got tgt %x for sequence: %#llx\n",
168 lod2obd(lod)->obd_name, *tgt, fid_seq(fid));
173 /* Slab for OSD object allocation */
174 struct kmem_cache *lod_object_kmem;
176 /* Slab for dt_txn_callback */
177 struct kmem_cache *lod_txn_callback_kmem;
178 static struct lu_kmem_descr lod_caches[] = {
180 .ckd_cache = &lod_object_kmem,
181 .ckd_name = "lod_obj",
182 .ckd_size = sizeof(struct lod_object)
185 .ckd_cache = &lod_txn_callback_kmem,
186 .ckd_name = "lod_txn_callback",
187 .ckd_size = sizeof(struct dt_txn_callback)
194 static struct lu_device *lod_device_fini(const struct lu_env *env,
195 struct lu_device *d);
198 * Implementation of lu_device_operations::ldo_object_alloc() for LOD
200 * Allocates and initializes LOD's slice in the given object.
202 * see include/lu_object.h for the details.
204 static struct lu_object *lod_object_alloc(const struct lu_env *env,
205 const struct lu_object_header *hdr,
206 struct lu_device *dev)
208 struct lod_object *lod_obj;
209 struct lu_object *lu_obj;
212 OBD_SLAB_ALLOC_PTR_GFP(lod_obj, lod_object_kmem, GFP_NOFS);
214 RETURN(ERR_PTR(-ENOMEM));
216 lu_obj = lod2lu_obj(lod_obj);
217 dt_object_init(&lod_obj->ldo_obj, NULL, dev);
218 lod_obj->ldo_obj.do_ops = &lod_obj_ops;
219 lu_obj->lo_ops = &lod_lu_obj_ops;
225 * Process the config log for all sub device.
227 * The function goes through all the targets in the given table
228 * and apply given configuration command on to the targets.
229 * Used to cleanup the targets at unmount.
231 * \param[in] env LU environment provided by the caller
232 * \param[in] lod lod device
233 * \param[in] ltd target's table to go through
234 * \param[in] lcfg configuration command to apply
236 * \retval 0 on success
237 * \retval negative negated errno on error
239 static int lod_sub_process_config(const struct lu_env *env,
240 struct lod_device *lod,
241 struct lod_tgt_descs *ltd,
242 struct lustre_cfg *lcfg)
244 struct lu_device *next;
249 if (ltd->ltd_tgts_size <= 0) {
250 lod_putref(lod, ltd);
253 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
254 struct lod_tgt_desc *tgt;
257 tgt = LTD_TGT(ltd, i);
258 LASSERT(tgt && tgt->ltd_tgt);
259 next = &tgt->ltd_tgt->dd_lu_dev;
260 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
262 CERROR("%s: error cleaning up LOD index %u: cmd %#x"
263 ": rc = %d\n", lod2obd(lod)->obd_name, i,
264 lcfg->lcfg_command, rc1);
268 lod_putref(lod, ltd);
272 struct lod_recovery_data {
273 struct lod_device *lrd_lod;
274 struct lod_tgt_desc *lrd_ltd;
275 struct ptlrpc_thread *lrd_thread;
281 * process update recovery record
283 * Add the update recovery recode to the update recovery list in
284 * lod_recovery_data. Then the recovery thread (target_recovery_thread)
285 * will redo these updates.
287 * \param[in]env execution environment
288 * \param[in]llh log handle of update record
289 * \param[in]rec update record to be replayed
290 * \param[in]data update recovery data which holds the necessary
291 * arguments for recovery (see struct lod_recovery_data)
293 * \retval 0 if the record is processed successfully.
294 * \retval negative errno if the record processing fails.
296 static int lod_process_recovery_updates(const struct lu_env *env,
297 struct llog_handle *llh,
298 struct llog_rec_hdr *rec,
301 struct lod_recovery_data *lrd = data;
302 struct llog_cookie *cookie = &lod_env_info(env)->lti_cookie;
303 struct lu_target *lut;
307 if (lrd->lrd_ltd == NULL) {
310 rc = lodname2mdt_index(lod2obd(lrd->lrd_lod)->obd_name, &index);
314 index = lrd->lrd_ltd->ltd_index;
318 llog_update_record_size((struct llog_update_record *)rec)) {
319 CERROR("%s broken update record! index %u "DFID".%u :"
320 " rc = %d\n", lod2obd(lrd->lrd_lod)->obd_name, index,
321 PFID(&llh->lgh_id.lgl_oi.oi_fid), rec->lrh_index, -EIO);
325 cookie->lgc_lgl = llh->lgh_id;
326 cookie->lgc_index = rec->lrh_index;
327 cookie->lgc_subsys = LLOG_UPDATELOG_ORIG_CTXT;
329 CDEBUG(D_HA, "%s: process recovery updates "DFID".%u\n",
330 lod2obd(lrd->lrd_lod)->obd_name,
331 PFID(&llh->lgh_id.lgl_oi.oi_fid), rec->lrh_index);
332 lut = lod2lu_dev(lrd->lrd_lod)->ld_site->ls_tgt;
334 if (lut->lut_obd->obd_stopping ||
335 lut->lut_obd->obd_abort_recovery)
338 return insert_update_records_to_replay_list(lut->lut_tdtd,
339 (struct llog_update_record *)rec,
344 * recovery thread for update log
346 * Start recovery thread and prepare the sub llog, then it will retrieve
347 * the update records from the correpondent MDT and do recovery.
349 * \param[in] arg pointer to the recovery data
351 * \retval 0 if recovery succeeds
352 * \retval negative errno if recovery failed.
354 static int lod_sub_recovery_thread(void *arg)
356 struct lod_recovery_data *lrd = arg;
357 struct lod_device *lod = lrd->lrd_lod;
358 struct dt_device *dt;
359 struct ptlrpc_thread *thread = lrd->lrd_thread;
360 struct llog_ctxt *ctxt = NULL;
362 struct lu_target *lut;
363 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
364 struct lod_tgt_desc *tgt = NULL;
371 thread->t_flags = SVC_RUNNING;
372 wake_up(&thread->t_ctl_waitq);
374 rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
377 CERROR("%s: can't initialize env: rc = %d\n",
378 lod2obd(lod)->obd_name, rc);
382 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
383 atomic_inc(&lut->lut_tdtd->tdtd_recovery_threads_count);
384 if (lrd->lrd_ltd == NULL)
387 dt = lrd->lrd_ltd->ltd_tgt;
389 start = ktime_get_real_seconds();
392 rc = lod_sub_prep_llog(&env, lod, dt, lrd->lrd_idx);
393 if (!rc && !lod->lod_child->dd_rdonly) {
394 /* Process the recovery record */
395 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
396 LLOG_UPDATELOG_ORIG_CTXT);
397 LASSERT(ctxt != NULL);
398 LASSERT(ctxt->loc_handle != NULL);
400 rc = llog_cat_process(&env, ctxt->loc_handle,
401 lod_process_recovery_updates, lrd, 0, 0);
405 struct lu_device *top_device;
407 top_device = lod->lod_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
408 /* Because the remote target might failover at the same time,
409 * let's retry here */
410 if ((rc == -ETIMEDOUT || rc == -EAGAIN || rc == -EIO) &&
411 dt != lod->lod_child &&
412 !top_device->ld_obd->obd_abort_recovery &&
413 !top_device->ld_obd->obd_stopping) {
415 if (ctxt->loc_handle != NULL)
421 CDEBUG(D_HA, "%s get update log failed %d, retry\n",
422 dt->dd_lu_dev.ld_obd->obd_name, rc);
426 CERROR("%s get update log failed: rc = %d\n",
427 dt->dd_lu_dev.ld_obd->obd_name, rc);
430 spin_lock(&top_device->ld_obd->obd_dev_lock);
431 if (!top_device->ld_obd->obd_abort_recovery &&
432 !top_device->ld_obd->obd_stopping)
433 top_device->ld_obd->obd_abort_recovery = 1;
434 spin_unlock(&top_device->ld_obd->obd_dev_lock);
440 CDEBUG(D_HA, "%s retrieved update log, duration %lld, retries %d\n",
441 dt->dd_lu_dev.ld_obd->obd_name, ktime_get_real_seconds() - start,
444 spin_lock(&lod->lod_lock);
445 if (lrd->lrd_ltd == NULL)
446 lod->lod_child_got_update_log = 1;
448 lrd->lrd_ltd->ltd_got_update_log = 1;
450 if (!lod->lod_child_got_update_log) {
451 spin_unlock(&lod->lod_lock);
455 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
456 tgt = LTD_TGT(ltd, i);
457 if (!tgt->ltd_got_update_log) {
458 spin_unlock(&lod->lod_lock);
462 lut->lut_tdtd->tdtd_replay_ready = 1;
463 spin_unlock(&lod->lod_lock);
465 CDEBUG(D_HA, "%s got update logs from all MDTs.\n",
466 lut->lut_obd->obd_name);
467 wake_up(&lut->lut_obd->obd_next_transno_waitq);
472 thread->t_flags = SVC_STOPPED;
473 atomic_dec(&lut->lut_tdtd->tdtd_recovery_threads_count);
474 wake_up(&lut->lut_tdtd->tdtd_recovery_threads_waitq);
475 wake_up(&thread->t_ctl_waitq);
481 * finish sub llog context
483 * Stop update recovery thread for the sub device, then cleanup the
484 * correspondent llog ctxt.
486 * \param[in] env execution environment
487 * \param[in] lod lod device to do update recovery
488 * \param[in] thread recovery thread on this sub device
490 void lod_sub_fini_llog(const struct lu_env *env,
491 struct dt_device *dt, struct ptlrpc_thread *thread)
493 struct obd_device *obd;
494 struct llog_ctxt *ctxt;
497 obd = dt->dd_lu_dev.ld_obd;
498 CDEBUG(D_INFO, "%s: finish sub llog\n", obd->obd_name);
499 /* Stop recovery thread first */
500 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
501 thread->t_flags = SVC_STOPPING;
502 wake_up(&thread->t_ctl_waitq);
503 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
506 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
510 if (ctxt->loc_handle != NULL)
511 llog_cat_close(env, ctxt->loc_handle);
513 llog_cleanup(env, ctxt);
519 * Extract MDT target index from a device name.
521 * a helper function to extract index from the given device name
522 * like "fsname-MDTxxxx-mdtlov"
524 * \param[in] lodname device name
525 * \param[out] mdt_index extracted index
527 * \retval 0 on success
528 * \retval -EINVAL if the name is invalid
530 int lodname2mdt_index(char *lodname, __u32 *mdt_index)
535 /* 1.8 configs don't have "-MDT0000" at the end */
536 ptr = strstr(lodname, "-MDT");
542 ptr = strrchr(lodname, '-');
544 CERROR("invalid MDT index in '%s'\n", lodname);
548 if (strncmp(ptr, "-mdtlov", 7) != 0) {
549 CERROR("invalid MDT index in '%s'\n", lodname);
553 if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
554 CERROR("invalid MDT index in '%s'\n", lodname);
558 if (strncmp(ptr - 8, "-MDT", 4) != 0) {
559 CERROR("invalid MDT index in '%s'\n", lodname);
563 index = simple_strtol(ptr - 4, &tmp, 16);
564 if (*tmp != '-' || index > INT_MAX) {
565 CERROR("invalid MDT index in '%s'\n", lodname);
573 * Init sub llog context
575 * Setup update llog ctxt for update recovery threads, then start the
576 * recovery thread (lod_sub_recovery_thread) to read update llog from
577 * the correspondent MDT to do update recovery.
579 * \param[in] env execution environment
580 * \param[in] lod lod device to do update recovery
581 * \param[in] dt sub dt device for which the recovery thread is
583 * \retval 0 if initialization succeeds.
584 * \retval negative errno if initialization fails.
586 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
587 struct dt_device *dt)
589 struct obd_device *obd;
590 struct lod_recovery_data *lrd = NULL;
591 struct ptlrpc_thread *thread;
592 struct task_struct *task;
593 struct l_wait_info lwi = { 0 };
594 struct lod_tgt_desc *sub_ltd = NULL;
600 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &master_index);
608 if (lod->lod_child == dt) {
609 thread = &lod->lod_child_recovery_thread;
610 index = master_index;
612 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
613 struct lod_tgt_desc *tgt = NULL;
616 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
617 tgt = LTD_TGT(ltd, i);
618 if (tgt->ltd_tgt == dt) {
619 index = tgt->ltd_index;
624 LASSERT(sub_ltd != NULL);
625 OBD_ALLOC_PTR(sub_ltd->ltd_recovery_thread);
626 if (sub_ltd->ltd_recovery_thread == NULL)
627 GOTO(free_lrd, rc = -ENOMEM);
629 thread = sub_ltd->ltd_recovery_thread;
632 CDEBUG(D_INFO, "%s init sub log %s\n", lod2obd(lod)->obd_name,
633 dt->dd_lu_dev.ld_obd->obd_name);
635 lrd->lrd_ltd = sub_ltd;
636 lrd->lrd_thread = thread;
637 lrd->lrd_idx = index;
638 init_waitqueue_head(&thread->t_ctl_waitq);
640 obd = dt->dd_lu_dev.ld_obd;
641 obd->obd_lvfs_ctxt.dt = dt;
642 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
643 NULL, &llog_common_cat_ops);
645 CERROR("%s: cannot setup updatelog llog: rc = %d\n",
647 GOTO(free_thread, rc);
650 /* Start the recovery thread */
651 task = kthread_run(lod_sub_recovery_thread, lrd, "lod%04x_rec%04x",
652 master_index, index);
655 CERROR("%s: cannot start recovery thread: rc = %d\n",
660 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING ||
661 thread->t_flags & SVC_STOPPED, &lwi);
665 lod_sub_fini_llog(env, dt, thread);
667 if (lod->lod_child != dt) {
668 OBD_FREE_PTR(sub_ltd->ltd_recovery_thread);
669 sub_ltd->ltd_recovery_thread = NULL;
677 * Stop sub recovery thread
679 * Stop sub recovery thread on all subs.
681 * \param[in] env execution environment
682 * \param[in] lod lod device to do update recovery
684 static void lod_sub_stop_recovery_threads(const struct lu_env *env,
685 struct lod_device *lod)
687 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
688 struct ptlrpc_thread *thread;
691 /* Stop the update log commit cancel threads and finish master
693 thread = &lod->lod_child_recovery_thread;
694 /* Stop recovery thread first */
695 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
696 thread->t_flags = SVC_STOPPING;
697 wake_up(&thread->t_ctl_waitq);
698 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
702 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
703 struct lod_tgt_desc *tgt;
705 tgt = LTD_TGT(ltd, i);
706 thread = tgt->ltd_recovery_thread;
707 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
708 thread->t_flags = SVC_STOPPING;
709 wake_up(&thread->t_ctl_waitq);
710 wait_event(thread->t_ctl_waitq,
711 thread->t_flags & SVC_STOPPED);
712 OBD_FREE_PTR(tgt->ltd_recovery_thread);
713 tgt->ltd_recovery_thread = NULL;
717 lod_putref(lod, ltd);
721 * finish all sub llog
723 * cleanup all of sub llog ctxt on the LOD.
725 * \param[in] env execution environment
726 * \param[in] lod lod device to do update recovery
728 static void lod_sub_fini_all_llogs(const struct lu_env *env,
729 struct lod_device *lod)
731 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
734 /* Stop the update log commit cancel threads and finish master
736 lod_sub_fini_llog(env, lod->lod_child,
737 &lod->lod_child_recovery_thread);
739 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
740 struct lod_tgt_desc *tgt;
742 tgt = LTD_TGT(ltd, i);
743 lod_sub_fini_llog(env, tgt->ltd_tgt,
744 tgt->ltd_recovery_thread);
747 lod_putref(lod, ltd);
750 static char *lod_show_update_logs_retrievers(void *data, int *size, int *count)
752 struct lod_device *lod = (struct lod_device *)data;
753 struct lu_target *lut = lod2lu_dev(lod)->ld_site->ls_tgt;
754 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
755 struct lod_tgt_desc *tgt = NULL;
761 *count = atomic_read(&lut->lut_tdtd->tdtd_recovery_threads_count);
767 *size = 5 * *count + 1;
768 OBD_ALLOC(buf, *size);
773 memset(buf, 0, *size);
775 if (!lod->lod_child_got_update_log) {
776 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &i);
777 LASSERTF(rc == 0, "Fail to parse target index: rc = %d\n", rc);
779 rc = snprintf(buf + len, *size - len, " %04x", i);
786 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
787 tgt = LTD_TGT(ltd, i);
788 if (!tgt->ltd_got_update_log) {
789 rc = snprintf(buf + len, *size - len, " %04x", i);
790 if (unlikely(rc <= 0))
802 * Prepare distribute txn
804 * Prepare distribute txn structure for LOD
806 * \param[in] env execution environment
807 * \param[in] lod_device LOD device
809 * \retval 0 if preparation succeeds.
810 * \retval negative errno if preparation fails.
812 static int lod_prepare_distribute_txn(const struct lu_env *env,
813 struct lod_device *lod)
815 struct target_distribute_txn_data *tdtd;
816 struct lu_target *lut;
820 /* Init update recovery data */
825 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
826 tdtd->tdtd_dt = &lod->lod_dt_dev;
827 rc = distribute_txn_init(env, lut, tdtd,
828 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id);
831 CERROR("%s: cannot init distribute txn: rc = %d\n",
832 lod2obd(lod)->obd_name, rc);
837 tdtd->tdtd_show_update_logs_retrievers =
838 lod_show_update_logs_retrievers;
839 tdtd->tdtd_show_retrievers_cbdata = lod;
841 lut->lut_tdtd = tdtd;
847 * Finish distribute txn
849 * Release the resource holding by distribute txn, i.e. stop distribute
852 * \param[in] env execution environment
853 * \param[in] lod lod device
855 static void lod_fini_distribute_txn(const struct lu_env *env,
856 struct lod_device *lod)
858 struct lu_target *lut;
860 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
861 target_recovery_fini(lut->lut_obd);
862 if (lut->lut_tdtd == NULL)
865 distribute_txn_fini(env, lut->lut_tdtd);
867 OBD_FREE_PTR(lut->lut_tdtd);
868 lut->lut_tdtd = NULL;
872 * Implementation of lu_device_operations::ldo_process_config() for LOD
874 * The method is called by the configuration subsystem during setup,
875 * cleanup and when the configuration changes. The method processes
876 * few specific commands like adding/removing the targets, changing
877 * the runtime parameters.
879 * \param[in] env LU environment provided by the caller
880 * \param[in] dev lod device
881 * \param[in] lcfg configuration command to apply
883 * \retval 0 on success
884 * \retval negative negated errno on error
886 * The examples are below.
888 * Add osc config log:
889 * marker 20 (flags=0x01, v2.2.49.56) lustre-OST0001 'add osc'
890 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nidxxx
891 * attach 0:lustre-OST0001-osc-MDT0001 1:osc 2:lustre-MDT0001-mdtlov_UUID
892 * setup 0:lustre-OST0001-osc-MDT0001 1:lustre-OST0001_UUID 2:nid
893 * lov_modify_tgts add 0:lustre-MDT0001-mdtlov 1:lustre-OST0001_UUID 2:1 3:1
894 * marker 20 (flags=0x02, v2.2.49.56) lustre-OST0001 'add osc'
896 * Add mdc config log:
897 * marker 10 (flags=0x01, v2.2.49.56) lustre-MDT0000 'add osp'
898 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nid
899 * attach 0:lustre-MDT0000-osp-MDT0001 1:osp 2:lustre-MDT0001-mdtlov_UUID
900 * setup 0:lustre-MDT0000-osp-MDT0001 1:lustre-MDT0000_UUID 2:nid
901 * modify_mdc_tgts add 0:lustre-MDT0001 1:lustre-MDT0000_UUID 2:0 3:1
902 * marker 10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID 'add osp'
904 static int lod_process_config(const struct lu_env *env,
905 struct lu_device *dev,
906 struct lustre_cfg *lcfg)
908 struct lod_device *lod = lu2lod_dev(dev);
909 struct lu_device *next = &lod->lod_child->dd_lu_dev;
914 switch(lcfg->lcfg_command) {
915 case LCFG_LOV_DEL_OBD:
916 case LCFG_LOV_ADD_INA:
917 case LCFG_LOV_ADD_OBD:
922 /* lov_modify_tgts add 0:lov_mdsA 1:osp 2:0 3:1
923 * modify_mdc_tgts add 0:lustre-MDT0001
924 * 1:lustre-MDT0001-mdc0002
926 arg1 = lustre_cfg_string(lcfg, 1);
928 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
929 GOTO(out, rc = -EINVAL);
930 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
931 GOTO(out, rc = -EINVAL);
933 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
936 rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
941 rc = lod_add_device(env, lod, arg1, index, gen,
942 mdt_index, LUSTRE_OSC_NAME, 1);
943 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
945 rc = lod_add_device(env, lod, arg1, index, gen,
946 mdt_index, LUSTRE_MDC_NAME, 1);
947 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
948 /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
950 rc = lod_add_device(env, lod, arg1, index, gen,
951 mdt_index, LUSTRE_OSC_NAME, 0);
953 rc = lod_del_device(env, lod,
955 arg1, index, gen, true);
962 struct obd_device *obd;
965 /* Check if it is activate/deactivate mdc
966 * lustre-MDTXXXX-osp-MDTXXXX.active=1 */
967 param = lustre_cfg_buf(lcfg, 1);
968 if (strstr(param, "osp") != NULL &&
969 strstr(param, ".active=") != NULL) {
970 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
971 struct lod_tgt_desc *sub_tgt = NULL;
976 ptr = strstr(param, ".");
978 obd = class_name2obd(param);
980 CERROR("%s: can not find %s: rc = %d\n",
981 lod2obd(lod)->obd_name, param, -EINVAL);
986 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
987 struct lod_tgt_desc *tgt;
989 tgt = LTD_TGT(ltd, i);
990 if (tgt->ltd_tgt->dd_lu_dev.ld_obd == obd) {
996 if (sub_tgt == NULL) {
997 CERROR("%s: can not find %s: rc = %d\n",
998 lod2obd(lod)->obd_name, param, -EINVAL);
1004 tmp = strstr(param, "=");
1007 struct llog_ctxt *ctxt;
1009 obd = sub_tgt->ltd_tgt->dd_lu_dev.ld_obd;
1010 ctxt = llog_get_context(obd,
1011 LLOG_UPDATELOG_ORIG_CTXT);
1013 rc = llog_setup(env, obd, &obd->obd_olg,
1014 LLOG_UPDATELOG_ORIG_CTXT,
1015 NULL, &llog_common_cat_ops);
1019 llog_ctxt_put(ctxt);
1021 rc = lod_sub_prep_llog(env, lod,
1023 sub_tgt->ltd_index);
1025 sub_tgt->ltd_active = 1;
1027 lod_sub_fini_llog(env, sub_tgt->ltd_tgt,
1029 sub_tgt->ltd_active = 0;
1035 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
1041 case LCFG_PRE_CLEANUP: {
1042 if (lod->lod_md_root != NULL) {
1043 dt_object_put(env, &lod->lod_md_root->ldo_obj);
1044 lod->lod_md_root = NULL;
1047 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1048 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1049 next = &lod->lod_child->dd_lu_dev;
1050 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1052 CDEBUG(D_HA, "%s: can't process %u: %d\n",
1053 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1055 lod_sub_stop_recovery_threads(env, lod);
1056 lod_fini_distribute_txn(env, lod);
1057 lod_sub_fini_all_llogs(env, lod);
1060 case LCFG_CLEANUP: {
1062 * do cleanup on underlying storage only when
1063 * all OSPs are cleaned up, as they use that OSD as well
1065 lu_dev_del_linkage(dev->ld_site, dev);
1066 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1067 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1068 next = &lod->lod_child->dd_lu_dev;
1069 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1071 CERROR("%s: can't process %u: %d\n",
1072 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1074 rc = obd_disconnect(lod->lod_child_exp);
1076 CERROR("error in disconnect from storage: %d\n", rc);
1080 CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
1081 lcfg->lcfg_command);
1091 * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
1093 * The method is called once the recovery is complete. This implementation
1094 * distributes the notification to all the known targets.
1096 * see include/lu_object.h for the details
1098 static int lod_recovery_complete(const struct lu_env *env,
1099 struct lu_device *dev)
1101 struct lod_device *lod = lu2lod_dev(dev);
1102 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1107 LASSERT(lod->lod_recovery_completed == 0);
1108 lod->lod_recovery_completed = 1;
1110 rc = next->ld_ops->ldo_recovery_complete(env, next);
1112 lod_getref(&lod->lod_ost_descs);
1113 if (lod->lod_osts_size > 0) {
1114 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
1115 struct lod_tgt_desc *tgt;
1116 tgt = OST_TGT(lod, i);
1117 LASSERT(tgt && tgt->ltd_tgt);
1118 next = &tgt->ltd_ost->dd_lu_dev;
1119 rc = next->ld_ops->ldo_recovery_complete(env, next);
1121 CERROR("%s: can't complete recovery on #%d:"
1122 "%d\n", lod2obd(lod)->obd_name, i, rc);
1125 lod_putref(lod, &lod->lod_ost_descs);
1130 * Init update logs on all sub device
1132 * LOD initialize update logs on all of sub devices. Because the initialization
1133 * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
1134 * lod_object_init(), this API has to be called after LOD is initialized.
1135 * \param[in] env execution environment
1136 * \param[in] lod lod device
1138 * \retval 0 if update log is initialized successfully.
1139 * \retval negative errno if initialization fails.
1141 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
1143 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1148 /* llog must be setup after LOD is initialized, because llog
1149 * initialization include FLD lookup */
1150 LASSERT(lod->lod_initialized);
1152 /* Init the llog in its own stack */
1153 rc = lod_sub_init_llog(env, lod, lod->lod_child);
1157 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
1158 struct lod_tgt_desc *tgt;
1160 tgt = LTD_TGT(ltd, i);
1161 rc = lod_sub_init_llog(env, lod, tgt->ltd_tgt);
1170 * Implementation of lu_device_operations::ldo_prepare() for LOD
1172 * see include/lu_object.h for the details.
1174 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
1175 struct lu_device *cdev)
1177 struct lod_device *lod = lu2lod_dev(cdev);
1178 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1179 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1181 struct dt_object *root;
1182 struct dt_object *dto;
1186 rc = next->ld_ops->ldo_prepare(env, pdev, next);
1188 CERROR("%s: prepare bottom error: rc = %d\n",
1189 lod2obd(lod)->obd_name, rc);
1193 lod->lod_initialized = 1;
1195 rc = dt_root_get(env, lod->lod_child, fid);
1199 root = dt_locate(env, lod->lod_child, fid);
1201 RETURN(PTR_ERR(root));
1203 /* Create update log object */
1204 index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1205 lu_update_log_fid(fid, index);
1207 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1209 lod_update_log_name,
1210 S_IFREG | S_IRUGO | S_IWUSR);
1212 GOTO(out_put, rc = PTR_ERR(dto));
1214 dt_object_put(env, dto);
1216 /* Create update log dir */
1217 lu_update_log_dir_fid(fid, index);
1218 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1220 lod_update_log_dir_name,
1221 S_IFDIR | S_IRUGO | S_IWUSR);
1223 GOTO(out_put, rc = PTR_ERR(dto));
1225 dt_object_put(env, dto);
1227 rc = lod_prepare_distribute_txn(env, lod);
1231 rc = lod_sub_init_llogs(env, lod);
1236 dt_object_put(env, root);
1241 const struct lu_device_operations lod_lu_ops = {
1242 .ldo_object_alloc = lod_object_alloc,
1243 .ldo_process_config = lod_process_config,
1244 .ldo_recovery_complete = lod_recovery_complete,
1245 .ldo_prepare = lod_prepare,
1249 * Implementation of dt_device_operations::dt_root_get() for LOD
1251 * see include/dt_object.h for the details.
1253 static int lod_root_get(const struct lu_env *env,
1254 struct dt_device *dev, struct lu_fid *f)
1256 return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
1260 * Implementation of dt_device_operations::dt_statfs() for LOD
1262 * see include/dt_object.h for the details.
1264 static int lod_statfs(const struct lu_env *env,
1265 struct dt_device *dev, struct obd_statfs *sfs)
1267 return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
1271 * Implementation of dt_device_operations::dt_trans_create() for LOD
1273 * Creates a transaction using local (to this node) OSD.
1275 * see include/dt_object.h for the details.
1277 static struct thandle *lod_trans_create(const struct lu_env *env,
1278 struct dt_device *dt)
1282 th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
1292 * Implementation of dt_device_operations::dt_trans_start() for LOD
1294 * Starts the set of local transactions using the targets involved
1295 * in declare phase. Initial support for the distributed transactions.
1297 * see include/dt_object.h for the details.
1299 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
1302 return top_trans_start(env, dt2lod_dev(dt)->lod_child, th);
1305 static int lod_trans_cb_add(struct thandle *th,
1306 struct dt_txn_commit_cb *dcb)
1308 struct top_thandle *top_th = container_of(th, struct top_thandle,
1310 return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
1314 * add noop update to the update records
1316 * Add noop updates to the update records, which is only used in
1319 * \param[in] env execution environment
1320 * \param[in] dt dt device of lod
1321 * \param[in] th thandle
1322 * \param[in] count the count of update records to be added.
1324 * \retval 0 if adding succeeds.
1325 * \retval negative errno if adding fails.
1327 static int lod_add_noop_records(const struct lu_env *env,
1328 struct dt_device *dt, struct thandle *th,
1331 struct top_thandle *top_th;
1332 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1336 top_th = container_of(th, struct top_thandle, tt_super);
1337 if (top_th->tt_multiple_thandle == NULL)
1341 for (i = 0; i < count; i++) {
1342 rc = update_record_pack(noop, th, fid);
1350 * Implementation of dt_device_operations::dt_trans_stop() for LOD
1352 * Stops the set of local transactions using the targets involved
1353 * in declare phase. Initial support for the distributed transactions.
1355 * see include/dt_object.h for the details.
1357 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
1360 if (OBD_FAIL_CHECK(OBD_FAIL_SPLIT_UPDATE_REC)) {
1363 rc = lod_add_noop_records(env, dt, th, 5000);
1367 return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
1371 * Implementation of dt_device_operations::dt_conf_get() for LOD
1373 * Currently returns the configuration provided by the local OSD.
1375 * see include/dt_object.h for the details.
1377 static void lod_conf_get(const struct lu_env *env,
1378 const struct dt_device *dev,
1379 struct dt_device_param *param)
1381 dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1385 * Implementation of dt_device_operations::dt_sync() for LOD
1387 * Syncs all known OST targets. Very very expensive and used
1388 * rarely by LFSCK now. Should not be used in general.
1390 * see include/dt_object.h for the details.
1392 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1394 struct lod_device *lod = dt2lod_dev(dev);
1395 struct lod_ost_desc *ost;
1396 struct lod_mdt_desc *mdt;
1401 lod_getref(&lod->lod_ost_descs);
1402 lod_foreach_ost(lod, i) {
1403 ost = OST_TGT(lod, i);
1404 LASSERT(ost && ost->ltd_ost);
1405 rc = dt_sync(env, ost->ltd_ost);
1407 CERROR("%s: can't sync ost %u: %d\n",
1408 lod2obd(lod)->obd_name, i, rc);
1412 lod_putref(lod, &lod->lod_ost_descs);
1417 lod_getref(&lod->lod_mdt_descs);
1418 lod_foreach_mdt(lod, i) {
1419 mdt = MDT_TGT(lod, i);
1420 LASSERT(mdt && mdt->ltd_mdt);
1421 rc = dt_sync(env, mdt->ltd_mdt);
1423 CERROR("%s: can't sync mdt %u: %d\n",
1424 lod2obd(lod)->obd_name, i, rc);
1428 lod_putref(lod, &lod->lod_mdt_descs);
1431 rc = dt_sync(env, lod->lod_child);
1437 * Implementation of dt_device_operations::dt_ro() for LOD
1439 * Turns local OSD read-only, used for the testing only.
1441 * see include/dt_object.h for the details.
1443 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1445 return dt_ro(env, dt2lod_dev(dev)->lod_child);
1449 * Implementation of dt_device_operations::dt_commit_async() for LOD
1451 * Asks local OSD to commit sooner.
1453 * see include/dt_object.h for the details.
1455 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1457 return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1460 static const struct dt_device_operations lod_dt_ops = {
1461 .dt_root_get = lod_root_get,
1462 .dt_statfs = lod_statfs,
1463 .dt_trans_create = lod_trans_create,
1464 .dt_trans_start = lod_trans_start,
1465 .dt_trans_stop = lod_trans_stop,
1466 .dt_conf_get = lod_conf_get,
1467 .dt_sync = lod_sync,
1469 .dt_commit_async = lod_commit_async,
1470 .dt_trans_cb_add = lod_trans_cb_add,
1474 * Connect to a local OSD.
1476 * Used to connect to the local OSD at mount. OSD name is taken from the
1477 * configuration command passed. This connection is used to identify LU
1478 * site and pin the OSD from early removal.
1480 * \param[in] env LU environment provided by the caller
1481 * \param[in] lod lod device
1482 * \param[in] cfg configuration command to apply
1484 * \retval 0 on success
1485 * \retval negative negated errno on error
1487 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1488 struct lustre_cfg *cfg)
1490 struct obd_connect_data *data = NULL;
1491 struct obd_device *obd;
1492 char *nextdev = NULL, *p, *s;
1497 LASSERT(lod->lod_child_exp == NULL);
1499 /* compatibility hack: we still use old config logs
1500 * which specify LOV, but we need to learn underlying
1501 * OSD device, which is supposed to be:
1502 * <fsname>-MDTxxxx-osd
1504 * 2.x MGS generates lines like the following:
1505 * #03 (176)lov_setup 0:lustre-MDT0000-mdtlov 1:(struct lov_desc)
1506 * 1.8 MGS generates lines like the following:
1507 * #03 (168)lov_setup 0:lustre-mdtlov 1:(struct lov_desc)
1509 * we use "-MDT" to differentiate 2.x from 1.8 */
1511 if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
1512 len = strlen(p) + 6;
1513 OBD_ALLOC(nextdev, len);
1514 if (nextdev == NULL)
1515 GOTO(out, rc = -ENOMEM);
1518 s = strstr(nextdev, "-mdtlov");
1519 if (unlikely(s == NULL)) {
1520 CERROR("unable to parse device name %s\n",
1521 lustre_cfg_string(cfg, 0));
1522 GOTO(out, rc = -EINVAL);
1525 if (strstr(nextdev, "-MDT")) {
1530 strcpy(s, "-MDT0000-osd");
1533 CERROR("unable to parse device name %s\n",
1534 lustre_cfg_string(cfg, 0));
1535 GOTO(out, rc = -EINVAL);
1538 OBD_ALLOC_PTR(data);
1540 GOTO(out, rc = -ENOMEM);
1542 obd = class_name2obd(nextdev);
1544 CERROR("can not locate next device: %s\n", nextdev);
1545 GOTO(out, rc = -ENOTCONN);
1548 data->ocd_connect_flags = OBD_CONNECT_VERSION;
1549 data->ocd_version = LUSTRE_VERSION_CODE;
1551 rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
1554 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1558 lod->lod_dt_dev.dd_lu_dev.ld_site =
1559 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
1560 LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
1561 lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
1567 OBD_FREE(nextdev, len);
1572 * Allocate and initialize target table.
1574 * A helper function to initialize the target table and allocate
1575 * a bitmap of the available targets.
1577 * \param[in] ltd target's table to initialize
1579 * \retval 0 on success
1580 * \retval negative negated errno on error
1582 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
1584 mutex_init(<d->ltd_mutex);
1585 init_rwsem(<d->ltd_rw_sem);
1587 /* the OST array and bitmap are allocated/grown dynamically as OSTs are
1588 * added to the LOD, see lod_add_device() */
1589 ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
1590 if (ltd->ltd_tgt_bitmap == NULL)
1593 ltd->ltd_tgts_size = 32;
1596 ltd->ltd_death_row = 0;
1597 ltd->ltd_refcount = 0;
1602 * Initialize LOD device at setup.
1604 * Initializes the given LOD device using the original configuration command.
1605 * The function initiates a connection to the local OSD and initializes few
1606 * internal structures like pools, target tables, etc.
1608 * \param[in] env LU environment provided by the caller
1609 * \param[in] lod lod device
1610 * \param[in] ldt not used
1611 * \param[in] cfg configuration command
1613 * \retval 0 on success
1614 * \retval negative negated errno on error
1616 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
1617 struct lu_device_type *ldt, struct lustre_cfg *cfg)
1619 struct dt_device_param ddp;
1620 struct obd_device *obd;
1624 obd = class_name2obd(lustre_cfg_string(cfg, 0));
1626 CERROR("Cannot find obd with name %s\n",
1627 lustre_cfg_string(cfg, 0));
1631 obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1632 lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
1633 lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
1634 lod->lod_dt_dev.dd_ops = &lod_dt_ops;
1636 rc = lod_connect_to_osd(env, lod, cfg);
1640 dt_conf_get(env, &lod->lod_dt_dev, &ddp);
1641 lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
1643 /* setup obd to be used with old lov code */
1644 rc = lod_pools_init(lod, cfg);
1646 GOTO(out_disconnect, rc);
1648 rc = lod_procfs_init(lod);
1650 GOTO(out_pools, rc);
1652 spin_lock_init(&lod->lod_lock);
1653 spin_lock_init(&lod->lod_connects_lock);
1654 lod_tgt_desc_init(&lod->lod_mdt_descs);
1655 lod_tgt_desc_init(&lod->lod_ost_descs);
1660 lod_pools_fini(lod);
1662 obd_disconnect(lod->lod_child_exp);
1667 * Implementation of lu_device_type_operations::ldto_device_free() for LOD
1669 * Releases the memory allocated for LOD device.
1671 * see include/lu_object.h for the details.
1673 static struct lu_device *lod_device_free(const struct lu_env *env,
1674 struct lu_device *lu)
1676 struct lod_device *lod = lu2lod_dev(lu);
1677 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1680 LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %p\n", lu);
1681 dt_device_fini(&lod->lod_dt_dev);
1687 * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
1689 * Allocates LOD device and calls the helpers to initialize it.
1691 * see include/lu_object.h for the details.
1693 static struct lu_device *lod_device_alloc(const struct lu_env *env,
1694 struct lu_device_type *type,
1695 struct lustre_cfg *lcfg)
1697 struct lod_device *lod;
1698 struct lu_device *lu_dev;
1702 lu_dev = ERR_PTR(-ENOMEM);
1706 lu_dev = lod2lu_dev(lod);
1707 dt_device_init(&lod->lod_dt_dev, type);
1708 rc = lod_init0(env, lod, type, lcfg);
1710 lod_device_free(env, lu_dev);
1711 lu_dev = ERR_PTR(rc);
1719 * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
1721 * Releases the internal resources used by LOD device.
1723 * see include/lu_object.h for the details.
1725 static struct lu_device *lod_device_fini(const struct lu_env *env,
1726 struct lu_device *d)
1728 struct lod_device *lod = lu2lod_dev(d);
1732 lod_pools_fini(lod);
1734 lod_procfs_fini(lod);
1736 rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
1738 CERROR("%s:can not fini ost descs %d\n",
1739 lod2obd(lod)->obd_name, rc);
1741 rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
1743 CERROR("%s:can not fini mdt descs %d\n",
1744 lod2obd(lod)->obd_name, rc);
1750 * Implementation of obd_ops::o_connect() for LOD
1752 * Used to track all the users of this specific LOD device,
1753 * so the device stays up until the last user disconnected.
1755 * \param[in] env LU environment provided by the caller
1756 * \param[out] exp export the caller will be using to access LOD
1757 * \param[in] obd OBD device representing LOD device
1758 * \param[in] cluuid unique identifier of the caller
1759 * \param[in] data not used
1760 * \param[in] localdata not used
1762 * \retval 0 on success
1763 * \retval negative negated errno on error
1765 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1766 struct obd_device *obd, struct obd_uuid *cluuid,
1767 struct obd_connect_data *data, void *localdata)
1769 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1770 struct lustre_handle conn;
1774 CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1776 rc = class_connect(&conn, obd, cluuid);
1780 *exp = class_conn2export(&conn);
1782 spin_lock(&lod->lod_connects_lock);
1783 lod->lod_connects++;
1784 /* at the moment we expect the only user */
1785 LASSERT(lod->lod_connects == 1);
1786 spin_unlock(&lod->lod_connects_lock);
1793 * Implementation of obd_ops::o_disconnect() for LOD
1795 * When the caller doesn't need to use this LOD instance, it calls
1796 * obd_disconnect() and LOD releases corresponding export/reference count.
1797 * Once all the users gone, LOD device is released.
1799 * \param[in] exp export provided to the caller in obd_connect()
1801 * \retval 0 on success
1802 * \retval negative negated errno on error
1804 static int lod_obd_disconnect(struct obd_export *exp)
1806 struct obd_device *obd = exp->exp_obd;
1807 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1808 int rc, release = 0;
1811 /* Only disconnect the underlying layers on the final disconnect. */
1812 spin_lock(&lod->lod_connects_lock);
1813 lod->lod_connects--;
1814 if (lod->lod_connects != 0) {
1815 /* why should there be more than 1 connect? */
1816 spin_unlock(&lod->lod_connects_lock);
1817 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1821 spin_unlock(&lod->lod_connects_lock);
1823 /* the last user of lod has gone, let's release the device */
1827 rc = class_disconnect(exp); /* bz 9811 */
1829 if (rc == 0 && release)
1830 class_manual_cleanup(obd);
1834 LU_KEY_INIT(lod, struct lod_thread_info);
1836 static void lod_key_fini(const struct lu_context *ctx,
1837 struct lu_context_key *key, void *data)
1839 struct lod_thread_info *info = data;
1840 struct lod_layout_component *lds =
1841 info->lti_def_striping.lds_def_comp_entries;
1842 struct ost_pool *inuse = &info->lti_inuse_osts;
1844 /* allocated in lod_get_lov_ea
1845 * XXX: this is overload, a tread may have such store but used only
1846 * once. Probably better would be pool of such stores per LOD.
1848 if (info->lti_ea_store) {
1849 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1850 info->lti_ea_store = NULL;
1851 info->lti_ea_store_size = 0;
1853 lu_buf_free(&info->lti_linkea_buf);
1856 lod_free_def_comp_entries(&info->lti_def_striping);
1859 OBD_FREE(inuse->op_array, inuse->op_size);
1864 /* context key: lod_thread_key */
1865 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1867 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1869 static struct lu_device_type_operations lod_device_type_ops = {
1870 .ldto_init = lod_type_init,
1871 .ldto_fini = lod_type_fini,
1873 .ldto_start = lod_type_start,
1874 .ldto_stop = lod_type_stop,
1876 .ldto_device_alloc = lod_device_alloc,
1877 .ldto_device_free = lod_device_free,
1879 .ldto_device_fini = lod_device_fini
1882 static struct lu_device_type lod_device_type = {
1883 .ldt_tags = LU_DEVICE_DT,
1884 .ldt_name = LUSTRE_LOD_NAME,
1885 .ldt_ops = &lod_device_type_ops,
1886 .ldt_ctx_tags = LCT_MD_THREAD,
1890 * Implementation of obd_ops::o_get_info() for LOD
1892 * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
1893 * the caller binary status whether LOD has seen connection to any OST target.
1894 * It will also check if the MDT update log context being initialized (if
1897 * \param[in] env LU environment provided by the caller
1898 * \param[in] exp export of the caller
1899 * \param[in] keylen len of the key
1900 * \param[in] key the key
1901 * \param[in] vallen not used
1902 * \param[in] val not used
1904 * \retval 0 if a connection was seen
1905 * \retval -EAGAIN if LOD isn't running yet or no
1906 * connection has been seen yet
1907 * \retval -EINVAL if not supported key is requested
1909 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1910 __u32 keylen, void *key, __u32 *vallen, void *val)
1914 if (KEY_IS(KEY_OSP_CONNECTED)) {
1915 struct obd_device *obd = exp->exp_obd;
1916 struct lod_device *d;
1917 struct lod_tgt_desc *tgt;
1921 if (!obd->obd_set_up || obd->obd_stopping)
1924 d = lu2lod_dev(obd->obd_lu_dev);
1925 lod_getref(&d->lod_ost_descs);
1926 lod_foreach_ost(d, i) {
1927 tgt = OST_TGT(d, i);
1928 LASSERT(tgt && tgt->ltd_tgt);
1929 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
1931 /* one healthy device is enough */
1935 lod_putref(d, &d->lod_ost_descs);
1937 lod_getref(&d->lod_mdt_descs);
1938 lod_foreach_mdt(d, i) {
1939 struct llog_ctxt *ctxt;
1941 tgt = MDT_TGT(d, i);
1942 LASSERT(tgt != NULL);
1943 LASSERT(tgt->ltd_tgt != NULL);
1944 if (!tgt->ltd_active)
1947 ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
1948 LLOG_UPDATELOG_ORIG_CTXT);
1950 CDEBUG(D_INFO, "%s: %s is not ready.\n",
1952 tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
1956 if (ctxt->loc_handle == NULL) {
1957 CDEBUG(D_INFO, "%s: %s is not ready.\n",
1959 tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
1961 llog_ctxt_put(ctxt);
1964 llog_ctxt_put(ctxt);
1966 lod_putref(d, &d->lod_mdt_descs);
1974 static int lod_obd_set_info_async(const struct lu_env *env,
1975 struct obd_export *exp,
1976 __u32 keylen, void *key,
1977 __u32 vallen, void *val,
1978 struct ptlrpc_request_set *set)
1980 struct obd_device *obd = class_exp2obd(exp);
1981 struct lod_device *d;
1982 struct lod_tgt_desc *tgt;
1989 set = ptlrpc_prep_set();
1994 d = lu2lod_dev(obd->obd_lu_dev);
1995 lod_getref(&d->lod_ost_descs);
1996 lod_foreach_ost(d, i) {
1997 tgt = OST_TGT(d, i);
1998 LASSERT(tgt && tgt->ltd_tgt);
1999 if (!tgt->ltd_active)
2002 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2004 if (rc2 != 0 && rc == 0)
2007 lod_putref(d, &d->lod_ost_descs);
2009 lod_getref(&d->lod_mdt_descs);
2010 lod_foreach_mdt(d, i) {
2011 tgt = MDT_TGT(d, i);
2012 LASSERT(tgt && tgt->ltd_tgt);
2013 if (!tgt->ltd_active)
2015 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2017 if (rc2 != 0 && rc == 0)
2020 lod_putref(d, &d->lod_mdt_descs);
2024 rc2 = ptlrpc_set_wait(set);
2025 if (rc2 == 0 && rc == 0)
2027 ptlrpc_set_destroy(set);
2032 static struct obd_ops lod_obd_device_ops = {
2033 .o_owner = THIS_MODULE,
2034 .o_connect = lod_obd_connect,
2035 .o_disconnect = lod_obd_disconnect,
2036 .o_get_info = lod_obd_get_info,
2037 .o_set_info_async = lod_obd_set_info_async,
2038 .o_pool_new = lod_pool_new,
2039 .o_pool_rem = lod_pool_remove,
2040 .o_pool_add = lod_pool_add,
2041 .o_pool_del = lod_pool_del,
2044 static int __init lod_init(void)
2046 struct obd_type *type;
2049 rc = lu_kmem_init(lod_caches);
2053 rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
2054 LUSTRE_LOD_NAME, &lod_device_type);
2056 lu_kmem_fini(lod_caches);
2060 /* create "lov" entry in procfs for compatibility purposes */
2061 type = class_search_type(LUSTRE_LOV_NAME);
2062 if (type != NULL && type->typ_procroot != NULL)
2065 type = class_search_type(LUSTRE_LOD_NAME);
2066 type->typ_procsym = lprocfs_register("lov", proc_lustre_root,
2068 if (IS_ERR(type->typ_procsym)) {
2069 CERROR("lod: can't create compat entry \"lov\": %d\n",
2070 (int)PTR_ERR(type->typ_procsym));
2071 type->typ_procsym = NULL;
2076 static void __exit lod_exit(void)
2078 class_unregister_type(LUSTRE_LOD_NAME);
2079 lu_kmem_fini(lod_caches);
2082 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2083 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
2084 MODULE_VERSION(LUSTRE_VERSION_STRING);
2085 MODULE_LICENSE("GPL");
2087 module_init(lod_init);
2088 module_exit(lod_exit);