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;
1036 if (strstr(param, PARAM_LOD) != NULL)
1037 rc = class_process_proc_param(PARAM_LOD, obd->obd_vars,
1040 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
1047 case LCFG_PRE_CLEANUP: {
1048 if (lod->lod_md_root != NULL) {
1049 dt_object_put(env, &lod->lod_md_root->ldo_obj);
1050 lod->lod_md_root = NULL;
1053 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1054 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1055 next = &lod->lod_child->dd_lu_dev;
1056 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1058 CDEBUG(D_HA, "%s: can't process %u: %d\n",
1059 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1061 lod_sub_stop_recovery_threads(env, lod);
1062 lod_fini_distribute_txn(env, lod);
1063 lod_sub_fini_all_llogs(env, lod);
1066 case LCFG_CLEANUP: {
1068 * do cleanup on underlying storage only when
1069 * all OSPs are cleaned up, as they use that OSD as well
1071 lu_dev_del_linkage(dev->ld_site, dev);
1072 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1073 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1074 next = &lod->lod_child->dd_lu_dev;
1075 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1077 CERROR("%s: can't process %u: %d\n",
1078 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1080 rc = obd_disconnect(lod->lod_child_exp);
1082 CERROR("error in disconnect from storage: %d\n", rc);
1086 CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
1087 lcfg->lcfg_command);
1097 * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
1099 * The method is called once the recovery is complete. This implementation
1100 * distributes the notification to all the known targets.
1102 * see include/lu_object.h for the details
1104 static int lod_recovery_complete(const struct lu_env *env,
1105 struct lu_device *dev)
1107 struct lod_device *lod = lu2lod_dev(dev);
1108 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1113 LASSERT(lod->lod_recovery_completed == 0);
1114 lod->lod_recovery_completed = 1;
1116 rc = next->ld_ops->ldo_recovery_complete(env, next);
1118 lod_getref(&lod->lod_ost_descs);
1119 if (lod->lod_osts_size > 0) {
1120 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
1121 struct lod_tgt_desc *tgt;
1122 tgt = OST_TGT(lod, i);
1123 LASSERT(tgt && tgt->ltd_tgt);
1124 next = &tgt->ltd_ost->dd_lu_dev;
1125 rc = next->ld_ops->ldo_recovery_complete(env, next);
1127 CERROR("%s: can't complete recovery on #%d:"
1128 "%d\n", lod2obd(lod)->obd_name, i, rc);
1131 lod_putref(lod, &lod->lod_ost_descs);
1136 * Init update logs on all sub device
1138 * LOD initialize update logs on all of sub devices. Because the initialization
1139 * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
1140 * lod_object_init(), this API has to be called after LOD is initialized.
1141 * \param[in] env execution environment
1142 * \param[in] lod lod device
1144 * \retval 0 if update log is initialized successfully.
1145 * \retval negative errno if initialization fails.
1147 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
1149 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1154 /* llog must be setup after LOD is initialized, because llog
1155 * initialization include FLD lookup */
1156 LASSERT(lod->lod_initialized);
1158 /* Init the llog in its own stack */
1159 rc = lod_sub_init_llog(env, lod, lod->lod_child);
1163 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
1164 struct lod_tgt_desc *tgt;
1166 tgt = LTD_TGT(ltd, i);
1167 rc = lod_sub_init_llog(env, lod, tgt->ltd_tgt);
1176 * Implementation of lu_device_operations::ldo_prepare() for LOD
1178 * see include/lu_object.h for the details.
1180 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
1181 struct lu_device *cdev)
1183 struct lod_device *lod = lu2lod_dev(cdev);
1184 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1185 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1187 struct dt_object *root;
1188 struct dt_object *dto;
1192 rc = next->ld_ops->ldo_prepare(env, pdev, next);
1194 CERROR("%s: prepare bottom error: rc = %d\n",
1195 lod2obd(lod)->obd_name, rc);
1199 lod->lod_initialized = 1;
1201 rc = dt_root_get(env, lod->lod_child, fid);
1205 root = dt_locate(env, lod->lod_child, fid);
1207 RETURN(PTR_ERR(root));
1209 /* Create update log object */
1210 index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1211 lu_update_log_fid(fid, index);
1213 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1215 lod_update_log_name,
1216 S_IFREG | S_IRUGO | S_IWUSR);
1218 GOTO(out_put, rc = PTR_ERR(dto));
1220 dt_object_put(env, dto);
1222 /* Create update log dir */
1223 lu_update_log_dir_fid(fid, index);
1224 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1226 lod_update_log_dir_name,
1227 S_IFDIR | S_IRUGO | S_IWUSR);
1229 GOTO(out_put, rc = PTR_ERR(dto));
1231 dt_object_put(env, dto);
1233 rc = lod_prepare_distribute_txn(env, lod);
1237 rc = lod_sub_init_llogs(env, lod);
1242 dt_object_put(env, root);
1247 const struct lu_device_operations lod_lu_ops = {
1248 .ldo_object_alloc = lod_object_alloc,
1249 .ldo_process_config = lod_process_config,
1250 .ldo_recovery_complete = lod_recovery_complete,
1251 .ldo_prepare = lod_prepare,
1255 * Implementation of dt_device_operations::dt_root_get() for LOD
1257 * see include/dt_object.h for the details.
1259 static int lod_root_get(const struct lu_env *env,
1260 struct dt_device *dev, struct lu_fid *f)
1262 return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
1266 * Implementation of dt_device_operations::dt_statfs() for LOD
1268 * see include/dt_object.h for the details.
1270 static int lod_statfs(const struct lu_env *env,
1271 struct dt_device *dev, struct obd_statfs *sfs)
1273 return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
1277 * Implementation of dt_device_operations::dt_trans_create() for LOD
1279 * Creates a transaction using local (to this node) OSD.
1281 * see include/dt_object.h for the details.
1283 static struct thandle *lod_trans_create(const struct lu_env *env,
1284 struct dt_device *dt)
1288 th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
1298 * Implementation of dt_device_operations::dt_trans_start() for LOD
1300 * Starts the set of local transactions using the targets involved
1301 * in declare phase. Initial support for the distributed transactions.
1303 * see include/dt_object.h for the details.
1305 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
1308 return top_trans_start(env, dt2lod_dev(dt)->lod_child, th);
1311 static int lod_trans_cb_add(struct thandle *th,
1312 struct dt_txn_commit_cb *dcb)
1314 struct top_thandle *top_th = container_of(th, struct top_thandle,
1316 return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
1320 * add noop update to the update records
1322 * Add noop updates to the update records, which is only used in
1325 * \param[in] env execution environment
1326 * \param[in] dt dt device of lod
1327 * \param[in] th thandle
1328 * \param[in] count the count of update records to be added.
1330 * \retval 0 if adding succeeds.
1331 * \retval negative errno if adding fails.
1333 static int lod_add_noop_records(const struct lu_env *env,
1334 struct dt_device *dt, struct thandle *th,
1337 struct top_thandle *top_th;
1338 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1342 top_th = container_of(th, struct top_thandle, tt_super);
1343 if (top_th->tt_multiple_thandle == NULL)
1347 for (i = 0; i < count; i++) {
1348 rc = update_record_pack(noop, th, fid);
1356 * Implementation of dt_device_operations::dt_trans_stop() for LOD
1358 * Stops the set of local transactions using the targets involved
1359 * in declare phase. Initial support for the distributed transactions.
1361 * see include/dt_object.h for the details.
1363 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
1366 if (OBD_FAIL_CHECK(OBD_FAIL_SPLIT_UPDATE_REC)) {
1369 rc = lod_add_noop_records(env, dt, th, 5000);
1373 return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
1377 * Implementation of dt_device_operations::dt_conf_get() for LOD
1379 * Currently returns the configuration provided by the local OSD.
1381 * see include/dt_object.h for the details.
1383 static void lod_conf_get(const struct lu_env *env,
1384 const struct dt_device *dev,
1385 struct dt_device_param *param)
1387 dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1391 * Implementation of dt_device_operations::dt_sync() for LOD
1393 * Syncs all known OST targets. Very very expensive and used
1394 * rarely by LFSCK now. Should not be used in general.
1396 * see include/dt_object.h for the details.
1398 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1400 struct lod_device *lod = dt2lod_dev(dev);
1401 struct lod_ost_desc *ost;
1402 struct lod_mdt_desc *mdt;
1407 lod_getref(&lod->lod_ost_descs);
1408 lod_foreach_ost(lod, i) {
1409 ost = OST_TGT(lod, i);
1410 LASSERT(ost && ost->ltd_ost);
1411 rc = dt_sync(env, ost->ltd_ost);
1413 CERROR("%s: can't sync ost %u: %d\n",
1414 lod2obd(lod)->obd_name, i, rc);
1418 lod_putref(lod, &lod->lod_ost_descs);
1423 lod_getref(&lod->lod_mdt_descs);
1424 lod_foreach_mdt(lod, i) {
1425 mdt = MDT_TGT(lod, i);
1426 LASSERT(mdt && mdt->ltd_mdt);
1427 rc = dt_sync(env, mdt->ltd_mdt);
1429 CERROR("%s: can't sync mdt %u: %d\n",
1430 lod2obd(lod)->obd_name, i, rc);
1434 lod_putref(lod, &lod->lod_mdt_descs);
1437 rc = dt_sync(env, lod->lod_child);
1443 * Implementation of dt_device_operations::dt_ro() for LOD
1445 * Turns local OSD read-only, used for the testing only.
1447 * see include/dt_object.h for the details.
1449 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1451 return dt_ro(env, dt2lod_dev(dev)->lod_child);
1455 * Implementation of dt_device_operations::dt_commit_async() for LOD
1457 * Asks local OSD to commit sooner.
1459 * see include/dt_object.h for the details.
1461 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1463 return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1466 static const struct dt_device_operations lod_dt_ops = {
1467 .dt_root_get = lod_root_get,
1468 .dt_statfs = lod_statfs,
1469 .dt_trans_create = lod_trans_create,
1470 .dt_trans_start = lod_trans_start,
1471 .dt_trans_stop = lod_trans_stop,
1472 .dt_conf_get = lod_conf_get,
1473 .dt_sync = lod_sync,
1475 .dt_commit_async = lod_commit_async,
1476 .dt_trans_cb_add = lod_trans_cb_add,
1480 * Connect to a local OSD.
1482 * Used to connect to the local OSD at mount. OSD name is taken from the
1483 * configuration command passed. This connection is used to identify LU
1484 * site and pin the OSD from early removal.
1486 * \param[in] env LU environment provided by the caller
1487 * \param[in] lod lod device
1488 * \param[in] cfg configuration command to apply
1490 * \retval 0 on success
1491 * \retval negative negated errno on error
1493 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1494 struct lustre_cfg *cfg)
1496 struct obd_connect_data *data = NULL;
1497 struct obd_device *obd;
1498 char *nextdev = NULL, *p, *s;
1503 LASSERT(lod->lod_child_exp == NULL);
1505 /* compatibility hack: we still use old config logs
1506 * which specify LOV, but we need to learn underlying
1507 * OSD device, which is supposed to be:
1508 * <fsname>-MDTxxxx-osd
1510 * 2.x MGS generates lines like the following:
1511 * #03 (176)lov_setup 0:lustre-MDT0000-mdtlov 1:(struct lov_desc)
1512 * 1.8 MGS generates lines like the following:
1513 * #03 (168)lov_setup 0:lustre-mdtlov 1:(struct lov_desc)
1515 * we use "-MDT" to differentiate 2.x from 1.8 */
1517 if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
1518 len = strlen(p) + 6;
1519 OBD_ALLOC(nextdev, len);
1520 if (nextdev == NULL)
1521 GOTO(out, rc = -ENOMEM);
1524 s = strstr(nextdev, "-mdtlov");
1525 if (unlikely(s == NULL)) {
1526 CERROR("unable to parse device name %s\n",
1527 lustre_cfg_string(cfg, 0));
1528 GOTO(out, rc = -EINVAL);
1531 if (strstr(nextdev, "-MDT")) {
1536 strcpy(s, "-MDT0000-osd");
1539 CERROR("unable to parse device name %s\n",
1540 lustre_cfg_string(cfg, 0));
1541 GOTO(out, rc = -EINVAL);
1544 OBD_ALLOC_PTR(data);
1546 GOTO(out, rc = -ENOMEM);
1548 obd = class_name2obd(nextdev);
1550 CERROR("can not locate next device: %s\n", nextdev);
1551 GOTO(out, rc = -ENOTCONN);
1554 data->ocd_connect_flags = OBD_CONNECT_VERSION;
1555 data->ocd_version = LUSTRE_VERSION_CODE;
1557 rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
1560 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1564 lod->lod_dt_dev.dd_lu_dev.ld_site =
1565 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
1566 LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
1567 lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
1573 OBD_FREE(nextdev, len);
1578 * Allocate and initialize target table.
1580 * A helper function to initialize the target table and allocate
1581 * a bitmap of the available targets.
1583 * \param[in] ltd target's table to initialize
1585 * \retval 0 on success
1586 * \retval negative negated errno on error
1588 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
1590 mutex_init(<d->ltd_mutex);
1591 init_rwsem(<d->ltd_rw_sem);
1593 /* the OST array and bitmap are allocated/grown dynamically as OSTs are
1594 * added to the LOD, see lod_add_device() */
1595 ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
1596 if (ltd->ltd_tgt_bitmap == NULL)
1599 ltd->ltd_tgts_size = 32;
1602 ltd->ltd_death_row = 0;
1603 ltd->ltd_refcount = 0;
1608 * Initialize LOD device at setup.
1610 * Initializes the given LOD device using the original configuration command.
1611 * The function initiates a connection to the local OSD and initializes few
1612 * internal structures like pools, target tables, etc.
1614 * \param[in] env LU environment provided by the caller
1615 * \param[in] lod lod device
1616 * \param[in] ldt not used
1617 * \param[in] cfg configuration command
1619 * \retval 0 on success
1620 * \retval negative negated errno on error
1622 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
1623 struct lu_device_type *ldt, struct lustre_cfg *cfg)
1625 struct dt_device_param ddp;
1626 struct obd_device *obd;
1630 obd = class_name2obd(lustre_cfg_string(cfg, 0));
1632 CERROR("Cannot find obd with name %s\n",
1633 lustre_cfg_string(cfg, 0));
1637 obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1638 lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
1639 lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
1640 lod->lod_dt_dev.dd_ops = &lod_dt_ops;
1642 rc = lod_connect_to_osd(env, lod, cfg);
1646 dt_conf_get(env, &lod->lod_dt_dev, &ddp);
1647 lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
1648 lod->lod_dom_max_stripesize = (1ULL << 20); /* 1Mb as default value */
1650 /* setup obd to be used with old lov code */
1651 rc = lod_pools_init(lod, cfg);
1653 GOTO(out_disconnect, rc);
1655 rc = lod_procfs_init(lod);
1657 GOTO(out_pools, rc);
1659 spin_lock_init(&lod->lod_lock);
1660 spin_lock_init(&lod->lod_connects_lock);
1661 lod_tgt_desc_init(&lod->lod_mdt_descs);
1662 lod_tgt_desc_init(&lod->lod_ost_descs);
1667 lod_pools_fini(lod);
1669 obd_disconnect(lod->lod_child_exp);
1674 * Implementation of lu_device_type_operations::ldto_device_free() for LOD
1676 * Releases the memory allocated for LOD device.
1678 * see include/lu_object.h for the details.
1680 static struct lu_device *lod_device_free(const struct lu_env *env,
1681 struct lu_device *lu)
1683 struct lod_device *lod = lu2lod_dev(lu);
1684 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1687 LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %p\n", lu);
1688 dt_device_fini(&lod->lod_dt_dev);
1694 * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
1696 * Allocates LOD device and calls the helpers to initialize it.
1698 * see include/lu_object.h for the details.
1700 static struct lu_device *lod_device_alloc(const struct lu_env *env,
1701 struct lu_device_type *type,
1702 struct lustre_cfg *lcfg)
1704 struct lod_device *lod;
1705 struct lu_device *lu_dev;
1709 lu_dev = ERR_PTR(-ENOMEM);
1713 lu_dev = lod2lu_dev(lod);
1714 dt_device_init(&lod->lod_dt_dev, type);
1715 rc = lod_init0(env, lod, type, lcfg);
1717 lod_device_free(env, lu_dev);
1718 lu_dev = ERR_PTR(rc);
1726 * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
1728 * Releases the internal resources used by LOD device.
1730 * see include/lu_object.h for the details.
1732 static struct lu_device *lod_device_fini(const struct lu_env *env,
1733 struct lu_device *d)
1735 struct lod_device *lod = lu2lod_dev(d);
1739 lod_pools_fini(lod);
1741 lod_procfs_fini(lod);
1743 rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
1745 CERROR("%s:can not fini ost descs %d\n",
1746 lod2obd(lod)->obd_name, rc);
1748 rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
1750 CERROR("%s:can not fini mdt descs %d\n",
1751 lod2obd(lod)->obd_name, rc);
1757 * Implementation of obd_ops::o_connect() for LOD
1759 * Used to track all the users of this specific LOD device,
1760 * so the device stays up until the last user disconnected.
1762 * \param[in] env LU environment provided by the caller
1763 * \param[out] exp export the caller will be using to access LOD
1764 * \param[in] obd OBD device representing LOD device
1765 * \param[in] cluuid unique identifier of the caller
1766 * \param[in] data not used
1767 * \param[in] localdata not used
1769 * \retval 0 on success
1770 * \retval negative negated errno on error
1772 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1773 struct obd_device *obd, struct obd_uuid *cluuid,
1774 struct obd_connect_data *data, void *localdata)
1776 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1777 struct lustre_handle conn;
1781 CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1783 rc = class_connect(&conn, obd, cluuid);
1787 *exp = class_conn2export(&conn);
1789 spin_lock(&lod->lod_connects_lock);
1790 lod->lod_connects++;
1791 /* at the moment we expect the only user */
1792 LASSERT(lod->lod_connects == 1);
1793 spin_unlock(&lod->lod_connects_lock);
1800 * Implementation of obd_ops::o_disconnect() for LOD
1802 * When the caller doesn't need to use this LOD instance, it calls
1803 * obd_disconnect() and LOD releases corresponding export/reference count.
1804 * Once all the users gone, LOD device is released.
1806 * \param[in] exp export provided to the caller in obd_connect()
1808 * \retval 0 on success
1809 * \retval negative negated errno on error
1811 static int lod_obd_disconnect(struct obd_export *exp)
1813 struct obd_device *obd = exp->exp_obd;
1814 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1815 int rc, release = 0;
1818 /* Only disconnect the underlying layers on the final disconnect. */
1819 spin_lock(&lod->lod_connects_lock);
1820 lod->lod_connects--;
1821 if (lod->lod_connects != 0) {
1822 /* why should there be more than 1 connect? */
1823 spin_unlock(&lod->lod_connects_lock);
1824 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1828 spin_unlock(&lod->lod_connects_lock);
1830 /* the last user of lod has gone, let's release the device */
1834 rc = class_disconnect(exp); /* bz 9811 */
1836 if (rc == 0 && release)
1837 class_manual_cleanup(obd);
1841 LU_KEY_INIT(lod, struct lod_thread_info);
1843 static void lod_key_fini(const struct lu_context *ctx,
1844 struct lu_context_key *key, void *data)
1846 struct lod_thread_info *info = data;
1847 struct lod_layout_component *lds =
1848 info->lti_def_striping.lds_def_comp_entries;
1849 struct ost_pool *inuse = &info->lti_inuse_osts;
1851 /* allocated in lod_get_lov_ea
1852 * XXX: this is overload, a tread may have such store but used only
1853 * once. Probably better would be pool of such stores per LOD.
1855 if (info->lti_ea_store) {
1856 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1857 info->lti_ea_store = NULL;
1858 info->lti_ea_store_size = 0;
1860 lu_buf_free(&info->lti_linkea_buf);
1863 lod_free_def_comp_entries(&info->lti_def_striping);
1866 OBD_FREE(inuse->op_array, inuse->op_size);
1868 if (info->lti_comp_size > 0)
1869 OBD_FREE(info->lti_comp_idx,
1870 info->lti_comp_size * sizeof(__u32));
1875 /* context key: lod_thread_key */
1876 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1878 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1880 static struct lu_device_type_operations lod_device_type_ops = {
1881 .ldto_init = lod_type_init,
1882 .ldto_fini = lod_type_fini,
1884 .ldto_start = lod_type_start,
1885 .ldto_stop = lod_type_stop,
1887 .ldto_device_alloc = lod_device_alloc,
1888 .ldto_device_free = lod_device_free,
1890 .ldto_device_fini = lod_device_fini
1893 static struct lu_device_type lod_device_type = {
1894 .ldt_tags = LU_DEVICE_DT,
1895 .ldt_name = LUSTRE_LOD_NAME,
1896 .ldt_ops = &lod_device_type_ops,
1897 .ldt_ctx_tags = LCT_MD_THREAD,
1901 * Implementation of obd_ops::o_get_info() for LOD
1903 * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
1904 * the caller binary status whether LOD has seen connection to any OST target.
1905 * It will also check if the MDT update log context being initialized (if
1908 * \param[in] env LU environment provided by the caller
1909 * \param[in] exp export of the caller
1910 * \param[in] keylen len of the key
1911 * \param[in] key the key
1912 * \param[in] vallen not used
1913 * \param[in] val not used
1915 * \retval 0 if a connection was seen
1916 * \retval -EAGAIN if LOD isn't running yet or no
1917 * connection has been seen yet
1918 * \retval -EINVAL if not supported key is requested
1920 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1921 __u32 keylen, void *key, __u32 *vallen, void *val)
1925 if (KEY_IS(KEY_OSP_CONNECTED)) {
1926 struct obd_device *obd = exp->exp_obd;
1927 struct lod_device *d;
1928 struct lod_tgt_desc *tgt;
1932 if (!obd->obd_set_up || obd->obd_stopping)
1935 d = lu2lod_dev(obd->obd_lu_dev);
1936 lod_getref(&d->lod_ost_descs);
1937 lod_foreach_ost(d, i) {
1938 tgt = OST_TGT(d, i);
1939 LASSERT(tgt && tgt->ltd_tgt);
1940 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
1942 /* one healthy device is enough */
1946 lod_putref(d, &d->lod_ost_descs);
1948 lod_getref(&d->lod_mdt_descs);
1949 lod_foreach_mdt(d, i) {
1950 struct llog_ctxt *ctxt;
1952 tgt = MDT_TGT(d, i);
1953 LASSERT(tgt != NULL);
1954 LASSERT(tgt->ltd_tgt != NULL);
1955 if (!tgt->ltd_active)
1958 ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
1959 LLOG_UPDATELOG_ORIG_CTXT);
1961 CDEBUG(D_INFO, "%s: %s is not ready.\n",
1963 tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
1967 if (ctxt->loc_handle == NULL) {
1968 CDEBUG(D_INFO, "%s: %s is not ready.\n",
1970 tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
1972 llog_ctxt_put(ctxt);
1975 llog_ctxt_put(ctxt);
1977 lod_putref(d, &d->lod_mdt_descs);
1985 static int lod_obd_set_info_async(const struct lu_env *env,
1986 struct obd_export *exp,
1987 __u32 keylen, void *key,
1988 __u32 vallen, void *val,
1989 struct ptlrpc_request_set *set)
1991 struct obd_device *obd = class_exp2obd(exp);
1992 struct lod_device *d;
1993 struct lod_tgt_desc *tgt;
2000 set = ptlrpc_prep_set();
2005 d = lu2lod_dev(obd->obd_lu_dev);
2006 lod_getref(&d->lod_ost_descs);
2007 lod_foreach_ost(d, i) {
2008 tgt = OST_TGT(d, i);
2009 LASSERT(tgt && tgt->ltd_tgt);
2010 if (!tgt->ltd_active)
2013 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2015 if (rc2 != 0 && rc == 0)
2018 lod_putref(d, &d->lod_ost_descs);
2020 lod_getref(&d->lod_mdt_descs);
2021 lod_foreach_mdt(d, i) {
2022 tgt = MDT_TGT(d, i);
2023 LASSERT(tgt && tgt->ltd_tgt);
2024 if (!tgt->ltd_active)
2026 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2028 if (rc2 != 0 && rc == 0)
2031 lod_putref(d, &d->lod_mdt_descs);
2035 rc2 = ptlrpc_set_wait(set);
2036 if (rc2 == 0 && rc == 0)
2038 ptlrpc_set_destroy(set);
2043 static struct obd_ops lod_obd_device_ops = {
2044 .o_owner = THIS_MODULE,
2045 .o_connect = lod_obd_connect,
2046 .o_disconnect = lod_obd_disconnect,
2047 .o_get_info = lod_obd_get_info,
2048 .o_set_info_async = lod_obd_set_info_async,
2049 .o_pool_new = lod_pool_new,
2050 .o_pool_rem = lod_pool_remove,
2051 .o_pool_add = lod_pool_add,
2052 .o_pool_del = lod_pool_del,
2055 static int __init lod_init(void)
2057 struct obd_type *type;
2060 rc = lu_kmem_init(lod_caches);
2064 rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
2065 LUSTRE_LOD_NAME, &lod_device_type);
2067 lu_kmem_fini(lod_caches);
2071 /* create "lov" entry in procfs for compatibility purposes */
2072 type = class_search_type(LUSTRE_LOV_NAME);
2073 if (type != NULL && type->typ_procroot != NULL)
2076 type = class_search_type(LUSTRE_LOD_NAME);
2077 type->typ_procsym = lprocfs_register("lov", proc_lustre_root,
2079 if (IS_ERR(type->typ_procsym)) {
2080 CERROR("lod: can't create compat entry \"lov\": %d\n",
2081 (int)PTR_ERR(type->typ_procsym));
2082 type->typ_procsym = NULL;
2087 static void __exit lod_exit(void)
2089 class_unregister_type(LUSTRE_LOD_NAME);
2090 lu_kmem_fini(lod_caches);
2093 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2094 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
2095 MODULE_VERSION(LUSTRE_VERSION_STRING);
2096 MODULE_LICENSE("GPL");
2098 module_init(lod_init);
2099 module_exit(lod_exit);