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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/lod/lod_dev.c
34 * Lustre Logical Object Device
36 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
37 * Author: Mikhail Pershin <mike.pershin@intel.com>
40 * The Logical Object Device (LOD) layer manages access to striped
41 * objects (both regular files and directories). It implements the DT
42 * device and object APIs and is responsible for creating, storing,
43 * and loading striping information as an extended attribute of the
44 * underlying OSD object. LOD is the server side analog of the LOV and
45 * LMV layers on the client side.
47 * Metadata LU object stack (layers of the same compound LU object,
48 * all have the same FID):
58 * During LOD object initialization the localness or remoteness of the
59 * object FID dictates the choice between OSD and OSP.
61 * An LOD object (file or directory) with N stripes (each has a
68 * S0 S1 S2 S3 S(N-1) OS[DP] objects, seen as DT objects by LOD
70 * When upper layers must access an object's stripes (which are
71 * themselves OST or MDT LU objects) LOD finds these objects by their
72 * FIDs and stores them as an array of DT object pointers on the
73 * object. Declarations and operations on LOD objects are received by
74 * LOD (as DT object operations) and performed on the underlying
75 * OS[DP] object and (as needed) on the stripes. From the perspective
76 * of LOD, a stripe-less file (created by mknod() or open with
77 * O_LOV_DELAY_CREATE) is an object which does not yet have stripes,
78 * while a non-striped directory (created by mkdir()) is an object
79 * which will never have stripes.
81 * The LOD layer also implements a small subset of the OBD device API
82 * to support MDT stack initialization and finalization (an MDD device
83 * connects and disconnects itself to and from the underlying LOD
84 * device), and pool management. In turn LOD uses the OBD device API
85 * to connect it self to the underlying OSD, and to connect itself to
86 * OSP devices representing the MDTs and OSTs that bear the stripes of
90 #define DEBUG_SUBSYSTEM S_MDS
92 #include <linux/kthread.h>
93 #include <obd_class.h>
94 #include <md_object.h>
95 #include <lustre_fid.h>
96 #include <lustre_param.h>
97 #include <lustre_update.h>
98 #include <lustre_log.h>
100 #include "lod_internal.h"
102 static const char lod_update_log_name[] = "update_log";
103 static const char lod_update_log_dir_name[] = "update_log_dir";
106 * Lookup target by FID.
108 * Lookup MDT/OST target index by FID. Type of the target can be
111 * \param[in] env LU environment provided by the caller
112 * \param[in] lod lod device
114 * \param[out] tgt result target index
115 * \param[in] type expected type of the target:
116 * LU_SEQ_RANGE_{MDT,OST,ANY}
118 * \retval 0 on success
119 * \retval negative negated errno on error
121 int lod_fld_lookup(const struct lu_env *env, struct lod_device *lod,
122 const struct lu_fid *fid, __u32 *tgt, int *type)
124 struct lu_seq_range range = { 0 };
125 struct lu_server_fld *server_fld;
129 if (!fid_is_sane(fid)) {
130 CERROR("%s: invalid FID "DFID"\n", lod2obd(lod)->obd_name,
135 if (fid_is_idif(fid)) {
136 *tgt = fid_idif_ost_idx(fid);
137 *type = LU_SEQ_RANGE_OST;
141 if (fid_is_update_log(fid) || fid_is_update_log_dir(fid)) {
143 *type = LU_SEQ_RANGE_MDT;
147 if (!lod->lod_initialized || (!fid_seq_in_fldb(fid_seq(fid)))) {
148 LASSERT(lu_site2seq(lod2lu_dev(lod)->ld_site) != NULL);
150 *tgt = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
151 *type = LU_SEQ_RANGE_MDT;
155 server_fld = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_server_fld;
156 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 "DOSTID":%u :"
320 " rc = %d\n", lod2obd(lrd->lrd_lod)->obd_name, index,
321 POSTID(&llh->lgh_id.lgl_oi), 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 "DOSTID":%u\n",
330 lod2obd(lrd->lrd_lod)->obd_name,
331 POSTID(&llh->lgh_id.lgl_oi), 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;
368 thread->t_flags = SVC_RUNNING;
369 wake_up(&thread->t_ctl_waitq);
371 rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
374 CERROR("%s: can't initialize env: rc = %d\n",
375 lod2obd(lod)->obd_name, rc);
379 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
380 atomic_inc(&lut->lut_tdtd->tdtd_recovery_threads_count);
381 if (lrd->lrd_ltd == NULL)
384 dt = lrd->lrd_ltd->ltd_tgt;
387 rc = lod_sub_prep_llog(&env, lod, dt, lrd->lrd_idx);
389 /* Process the recovery record */
390 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
391 LLOG_UPDATELOG_ORIG_CTXT);
392 LASSERT(ctxt != NULL);
393 LASSERT(ctxt->loc_handle != NULL);
395 rc = llog_cat_process(&env, ctxt->loc_handle,
396 lod_process_recovery_updates, lrd, 0, 0);
400 struct lu_device *top_device;
402 top_device = lod->lod_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
403 /* Because the remote target might failover at the same time,
404 * let's retry here */
405 if ((rc == -ETIMEDOUT || rc == -EAGAIN || rc == -EIO) &&
406 dt != lod->lod_child &&
407 !top_device->ld_obd->obd_abort_recovery &&
408 !top_device->ld_obd->obd_stopping) {
410 if (ctxt->loc_handle != NULL)
418 CERROR("%s getting update log failed: rc = %d\n",
419 dt->dd_lu_dev.ld_obd->obd_name, rc);
422 spin_lock(&top_device->ld_obd->obd_dev_lock);
423 if (!top_device->ld_obd->obd_abort_recovery &&
424 !top_device->ld_obd->obd_stopping)
425 top_device->ld_obd->obd_abort_recovery = 1;
426 spin_unlock(&top_device->ld_obd->obd_dev_lock);
432 CDEBUG(D_HA, "%s retrieve update log: rc = %d\n",
433 dt->dd_lu_dev.ld_obd->obd_name, rc);
435 if (lrd->lrd_ltd == NULL)
436 lod->lod_child_got_update_log = 1;
438 lrd->lrd_ltd->ltd_got_update_log = 1;
440 if (lod->lod_child_got_update_log) {
441 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
442 struct lod_tgt_desc *tgt = NULL;
443 bool all_got_log = true;
446 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
447 tgt = LTD_TGT(ltd, i);
448 if (!tgt->ltd_got_update_log) {
455 CDEBUG(D_HA, "%s got update logs from all MDTs.\n",
456 lut->lut_obd->obd_name);
457 lut->lut_tdtd->tdtd_replay_ready = 1;
458 wake_up(&lut->lut_obd->obd_next_transno_waitq);
464 thread->t_flags = SVC_STOPPED;
465 atomic_dec(&lut->lut_tdtd->tdtd_recovery_threads_count);
466 wake_up(&lut->lut_tdtd->tdtd_recovery_threads_waitq);
467 wake_up(&thread->t_ctl_waitq);
473 * finish sub llog context
475 * Stop update recovery thread for the sub device, then cleanup the
476 * correspondent llog ctxt.
478 * \param[in] env execution environment
479 * \param[in] lod lod device to do update recovery
480 * \param[in] thread recovery thread on this sub device
482 void lod_sub_fini_llog(const struct lu_env *env,
483 struct dt_device *dt, struct ptlrpc_thread *thread)
485 struct obd_device *obd;
486 struct llog_ctxt *ctxt;
489 obd = dt->dd_lu_dev.ld_obd;
490 CDEBUG(D_INFO, "%s: finish sub llog\n", obd->obd_name);
491 /* Stop recovery thread first */
492 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
493 thread->t_flags = SVC_STOPPING;
494 wake_up(&thread->t_ctl_waitq);
495 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
498 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
502 if (ctxt->loc_handle != NULL)
503 llog_cat_close(env, ctxt->loc_handle);
505 llog_cleanup(env, ctxt);
511 * Extract MDT target index from a device name.
513 * a helper function to extract index from the given device name
514 * like "fsname-MDTxxxx-mdtlov"
516 * \param[in] lodname device name
517 * \param[out] mdt_index extracted index
519 * \retval 0 on success
520 * \retval -EINVAL if the name is invalid
522 int lodname2mdt_index(char *lodname, __u32 *mdt_index)
527 /* 1.8 configs don't have "-MDT0000" at the end */
528 ptr = strstr(lodname, "-MDT");
534 ptr = strrchr(lodname, '-');
536 CERROR("invalid MDT index in '%s'\n", lodname);
540 if (strncmp(ptr, "-mdtlov", 7) != 0) {
541 CERROR("invalid MDT index in '%s'\n", lodname);
545 if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
546 CERROR("invalid MDT index in '%s'\n", lodname);
550 if (strncmp(ptr - 8, "-MDT", 4) != 0) {
551 CERROR("invalid MDT index in '%s'\n", lodname);
555 index = simple_strtol(ptr - 4, &tmp, 16);
556 if (*tmp != '-' || index > INT_MAX) {
557 CERROR("invalid MDT index in '%s'\n", lodname);
565 * Init sub llog context
567 * Setup update llog ctxt for update recovery threads, then start the
568 * recovery thread (lod_sub_recovery_thread) to read update llog from
569 * the correspondent MDT to do update recovery.
571 * \param[in] env execution environment
572 * \param[in] lod lod device to do update recovery
573 * \param[in] dt sub dt device for which the recovery thread is
575 * \retval 0 if initialization succeeds.
576 * \retval negative errno if initialization fails.
578 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
579 struct dt_device *dt)
581 struct obd_device *obd;
582 struct lod_recovery_data *lrd = NULL;
583 struct ptlrpc_thread *thread;
584 struct task_struct *task;
585 struct l_wait_info lwi = { 0 };
586 struct lod_tgt_desc *sub_ltd = NULL;
592 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &master_index);
600 if (lod->lod_child == dt) {
601 thread = &lod->lod_child_recovery_thread;
602 index = master_index;
604 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
605 struct lod_tgt_desc *tgt = NULL;
608 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
609 tgt = LTD_TGT(ltd, i);
610 if (tgt->ltd_tgt == dt) {
611 index = tgt->ltd_index;
616 LASSERT(sub_ltd != NULL);
617 OBD_ALLOC_PTR(sub_ltd->ltd_recovery_thread);
618 if (sub_ltd->ltd_recovery_thread == NULL)
619 GOTO(free_lrd, rc = -ENOMEM);
621 thread = sub_ltd->ltd_recovery_thread;
624 CDEBUG(D_INFO, "%s init sub log %s\n", lod2obd(lod)->obd_name,
625 dt->dd_lu_dev.ld_obd->obd_name);
627 lrd->lrd_ltd = sub_ltd;
628 lrd->lrd_thread = thread;
629 lrd->lrd_idx = index;
630 init_waitqueue_head(&thread->t_ctl_waitq);
632 obd = dt->dd_lu_dev.ld_obd;
633 obd->obd_lvfs_ctxt.dt = dt;
634 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
635 NULL, &llog_common_cat_ops);
637 CERROR("%s: cannot setup updatelog llog: rc = %d\n",
639 GOTO(free_thread, rc);
642 /* Start the recovery thread */
643 task = kthread_run(lod_sub_recovery_thread, lrd, "lod%04x_rec%04x",
644 master_index, index);
647 CERROR("%s: cannot start recovery thread: rc = %d\n",
652 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING ||
653 thread->t_flags & SVC_STOPPED, &lwi);
657 lod_sub_fini_llog(env, dt, thread);
659 if (lod->lod_child != dt) {
660 OBD_FREE_PTR(sub_ltd->ltd_recovery_thread);
661 sub_ltd->ltd_recovery_thread = NULL;
669 * Stop sub recovery thread
671 * Stop sub recovery thread on all subs.
673 * \param[in] env execution environment
674 * \param[in] lod lod device to do update recovery
676 static void lod_sub_stop_recovery_threads(const struct lu_env *env,
677 struct lod_device *lod)
679 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
680 struct ptlrpc_thread *thread;
683 /* Stop the update log commit cancel threads and finish master
685 thread = &lod->lod_child_recovery_thread;
686 /* Stop recovery thread first */
687 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
688 thread->t_flags = SVC_STOPPING;
689 wake_up(&thread->t_ctl_waitq);
690 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
694 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
695 struct lod_tgt_desc *tgt;
697 tgt = LTD_TGT(ltd, i);
698 thread = tgt->ltd_recovery_thread;
699 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
700 thread->t_flags = SVC_STOPPING;
701 wake_up(&thread->t_ctl_waitq);
702 wait_event(thread->t_ctl_waitq,
703 thread->t_flags & SVC_STOPPED);
704 OBD_FREE_PTR(tgt->ltd_recovery_thread);
705 tgt->ltd_recovery_thread = NULL;
709 lod_putref(lod, ltd);
713 * finish all sub llog
715 * cleanup all of sub llog ctxt on the LOD.
717 * \param[in] env execution environment
718 * \param[in] lod lod device to do update recovery
720 static void lod_sub_fini_all_llogs(const struct lu_env *env,
721 struct lod_device *lod)
723 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
726 /* Stop the update log commit cancel threads and finish master
728 lod_sub_fini_llog(env, lod->lod_child,
729 &lod->lod_child_recovery_thread);
731 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
732 struct lod_tgt_desc *tgt;
734 tgt = LTD_TGT(ltd, i);
735 lod_sub_fini_llog(env, tgt->ltd_tgt,
736 tgt->ltd_recovery_thread);
739 lod_putref(lod, ltd);
742 static char *lod_show_update_logs_retrievers(void *data, int *size, int *count)
744 struct lod_device *lod = (struct lod_device *)data;
745 struct lu_target *lut = lod2lu_dev(lod)->ld_site->ls_tgt;
746 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
747 struct lod_tgt_desc *tgt = NULL;
753 *count = atomic_read(&lut->lut_tdtd->tdtd_recovery_threads_count);
759 *size = 5 * *count + 1;
760 OBD_ALLOC(buf, *size);
765 memset(buf, 0, *size);
767 if (!lod->lod_child_got_update_log) {
768 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &i);
769 LASSERTF(rc == 0, "Fail to parse target index: rc = %d\n", rc);
771 rc = snprintf(buf + len, *size - len, " %04x", i);
778 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
779 tgt = LTD_TGT(ltd, i);
780 if (!tgt->ltd_got_update_log) {
781 rc = snprintf(buf + len, *size - len, " %04x", i);
782 if (unlikely(rc <= 0))
794 * Prepare distribute txn
796 * Prepare distribute txn structure for LOD
798 * \param[in] env execution environment
799 * \param[in] lod_device LOD device
801 * \retval 0 if preparation succeeds.
802 * \retval negative errno if preparation fails.
804 static int lod_prepare_distribute_txn(const struct lu_env *env,
805 struct lod_device *lod)
807 struct target_distribute_txn_data *tdtd;
808 struct lu_target *lut;
812 /* Init update recovery data */
817 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
818 tdtd->tdtd_dt = &lod->lod_dt_dev;
819 rc = distribute_txn_init(env, lut, tdtd,
820 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id);
823 CERROR("%s: cannot init distribute txn: rc = %d\n",
824 lod2obd(lod)->obd_name, rc);
829 tdtd->tdtd_show_update_logs_retrievers =
830 lod_show_update_logs_retrievers;
831 tdtd->tdtd_show_retrievers_cbdata = lod;
833 lut->lut_tdtd = tdtd;
839 * Finish distribute txn
841 * Release the resource holding by distribute txn, i.e. stop distribute
844 * \param[in] env execution environment
845 * \param[in] lod lod device
847 static void lod_fini_distribute_txn(const struct lu_env *env,
848 struct lod_device *lod)
850 struct lu_target *lut;
852 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
853 target_recovery_fini(lut->lut_obd);
854 if (lut->lut_tdtd == NULL)
857 distribute_txn_fini(env, lut->lut_tdtd);
859 OBD_FREE_PTR(lut->lut_tdtd);
860 lut->lut_tdtd = NULL;
864 * Implementation of lu_device_operations::ldo_process_config() for LOD
866 * The method is called by the configuration subsystem during setup,
867 * cleanup and when the configuration changes. The method processes
868 * few specific commands like adding/removing the targets, changing
869 * the runtime parameters.
871 * \param[in] env LU environment provided by the caller
872 * \param[in] dev lod device
873 * \param[in] lcfg configuration command to apply
875 * \retval 0 on success
876 * \retval negative negated errno on error
878 * The examples are below.
880 * Add osc config log:
881 * marker 20 (flags=0x01, v2.2.49.56) lustre-OST0001 'add osc'
882 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nidxxx
883 * attach 0:lustre-OST0001-osc-MDT0001 1:osc 2:lustre-MDT0001-mdtlov_UUID
884 * setup 0:lustre-OST0001-osc-MDT0001 1:lustre-OST0001_UUID 2:nid
885 * lov_modify_tgts add 0:lustre-MDT0001-mdtlov 1:lustre-OST0001_UUID 2:1 3:1
886 * marker 20 (flags=0x02, v2.2.49.56) lustre-OST0001 'add osc'
888 * Add mdc config log:
889 * marker 10 (flags=0x01, v2.2.49.56) lustre-MDT0000 'add osp'
890 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nid
891 * attach 0:lustre-MDT0000-osp-MDT0001 1:osp 2:lustre-MDT0001-mdtlov_UUID
892 * setup 0:lustre-MDT0000-osp-MDT0001 1:lustre-MDT0000_UUID 2:nid
893 * modify_mdc_tgts add 0:lustre-MDT0001 1:lustre-MDT0000_UUID 2:0 3:1
894 * marker 10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID 'add osp'
896 static int lod_process_config(const struct lu_env *env,
897 struct lu_device *dev,
898 struct lustre_cfg *lcfg)
900 struct lod_device *lod = lu2lod_dev(dev);
901 struct lu_device *next = &lod->lod_child->dd_lu_dev;
906 switch(lcfg->lcfg_command) {
907 case LCFG_LOV_DEL_OBD:
908 case LCFG_LOV_ADD_INA:
909 case LCFG_LOV_ADD_OBD:
914 /* lov_modify_tgts add 0:lov_mdsA 1:osp 2:0 3:1
915 * modify_mdc_tgts add 0:lustre-MDT0001
916 * 1:lustre-MDT0001-mdc0002
918 arg1 = lustre_cfg_string(lcfg, 1);
920 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
921 GOTO(out, rc = -EINVAL);
922 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
923 GOTO(out, rc = -EINVAL);
925 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
928 rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
933 rc = lod_add_device(env, lod, arg1, index, gen,
934 mdt_index, LUSTRE_OSC_NAME, 1);
935 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
937 rc = lod_add_device(env, lod, arg1, index, gen,
938 mdt_index, LUSTRE_MDC_NAME, 1);
939 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
940 /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
942 rc = lod_add_device(env, lod, arg1, index, gen,
943 mdt_index, LUSTRE_OSC_NAME, 0);
945 rc = lod_del_device(env, lod,
947 arg1, index, gen, true);
954 struct obd_device *obd;
957 /* Check if it is activate/deactivate mdc
958 * lustre-MDTXXXX-osp-MDTXXXX.active=1 */
959 param = lustre_cfg_buf(lcfg, 1);
960 if (strstr(param, "osp") != NULL &&
961 strstr(param, ".active=") != NULL) {
962 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
963 struct lod_tgt_desc *sub_tgt = NULL;
968 ptr = strstr(param, ".");
970 obd = class_name2obd(param);
972 CERROR("%s: can not find %s: rc = %d\n",
973 lod2obd(lod)->obd_name, param, -EINVAL);
978 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
979 struct lod_tgt_desc *tgt;
981 tgt = LTD_TGT(ltd, i);
982 if (tgt->ltd_tgt->dd_lu_dev.ld_obd == obd) {
988 if (sub_tgt == NULL) {
989 CERROR("%s: can not find %s: rc = %d\n",
990 lod2obd(lod)->obd_name, param, -EINVAL);
996 tmp = strstr(param, "=");
999 struct llog_ctxt *ctxt;
1001 obd = sub_tgt->ltd_tgt->dd_lu_dev.ld_obd;
1002 ctxt = llog_get_context(obd,
1003 LLOG_UPDATELOG_ORIG_CTXT);
1005 rc = llog_setup(env, obd, &obd->obd_olg,
1006 LLOG_UPDATELOG_ORIG_CTXT,
1007 NULL, &llog_common_cat_ops);
1011 llog_ctxt_put(ctxt);
1013 rc = lod_sub_prep_llog(env, lod,
1015 sub_tgt->ltd_index);
1017 sub_tgt->ltd_active = 1;
1019 lod_sub_fini_llog(env, sub_tgt->ltd_tgt,
1021 sub_tgt->ltd_active = 0;
1027 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
1033 case LCFG_PRE_CLEANUP: {
1034 if (lod->lod_md_root != NULL) {
1035 lu_object_put(env, &lod->lod_md_root->ldo_obj.do_lu);
1036 lod->lod_md_root = NULL;
1039 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1040 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1041 next = &lod->lod_child->dd_lu_dev;
1042 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1044 CDEBUG(D_HA, "%s: can't process %u: %d\n",
1045 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1047 lod_sub_stop_recovery_threads(env, lod);
1048 lod_fini_distribute_txn(env, lod);
1049 lod_sub_fini_all_llogs(env, lod);
1052 case LCFG_CLEANUP: {
1054 * do cleanup on underlying storage only when
1055 * all OSPs are cleaned up, as they use that OSD as well
1057 lu_dev_del_linkage(dev->ld_site, dev);
1058 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1059 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1060 next = &lod->lod_child->dd_lu_dev;
1061 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1063 CERROR("%s: can't process %u: %d\n",
1064 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1066 rc = obd_disconnect(lod->lod_child_exp);
1068 CERROR("error in disconnect from storage: %d\n", rc);
1072 CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
1073 lcfg->lcfg_command);
1083 * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
1085 * The method is called once the recovery is complete. This implementation
1086 * distributes the notification to all the known targets.
1088 * see include/lu_object.h for the details
1090 static int lod_recovery_complete(const struct lu_env *env,
1091 struct lu_device *dev)
1093 struct lod_device *lod = lu2lod_dev(dev);
1094 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1099 LASSERT(lod->lod_recovery_completed == 0);
1100 lod->lod_recovery_completed = 1;
1102 rc = next->ld_ops->ldo_recovery_complete(env, next);
1104 lod_getref(&lod->lod_ost_descs);
1105 if (lod->lod_osts_size > 0) {
1106 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
1107 struct lod_tgt_desc *tgt;
1108 tgt = OST_TGT(lod, i);
1109 LASSERT(tgt && tgt->ltd_tgt);
1110 next = &tgt->ltd_ost->dd_lu_dev;
1111 rc = next->ld_ops->ldo_recovery_complete(env, next);
1113 CERROR("%s: can't complete recovery on #%d:"
1114 "%d\n", lod2obd(lod)->obd_name, i, rc);
1117 lod_putref(lod, &lod->lod_ost_descs);
1122 * Init update logs on all sub device
1124 * LOD initialize update logs on all of sub devices. Because the initialization
1125 * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
1126 * lod_object_init(), this API has to be called after LOD is initialized.
1127 * \param[in] env execution environment
1128 * \param[in] lod lod device
1130 * \retval 0 if update log is initialized successfully.
1131 * \retval negative errno if initialization fails.
1133 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
1135 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1140 /* llog must be setup after LOD is initialized, because llog
1141 * initialization include FLD lookup */
1142 LASSERT(lod->lod_initialized);
1144 /* Init the llog in its own stack */
1145 rc = lod_sub_init_llog(env, lod, lod->lod_child);
1149 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
1150 struct lod_tgt_desc *tgt;
1152 tgt = LTD_TGT(ltd, i);
1153 rc = lod_sub_init_llog(env, lod, tgt->ltd_tgt);
1162 * Implementation of lu_device_operations::ldo_prepare() for LOD
1164 * see include/lu_object.h for the details.
1166 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
1167 struct lu_device *cdev)
1169 struct lod_device *lod = lu2lod_dev(cdev);
1170 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1171 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1173 struct dt_object *root;
1174 struct dt_object *dto;
1178 rc = next->ld_ops->ldo_prepare(env, pdev, next);
1180 CERROR("%s: prepare bottom error: rc = %d\n",
1181 lod2obd(lod)->obd_name, rc);
1185 lod->lod_initialized = 1;
1187 rc = dt_root_get(env, lod->lod_child, fid);
1191 root = dt_locate(env, lod->lod_child, fid);
1193 RETURN(PTR_ERR(root));
1195 /* Create update log object */
1196 index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1197 lu_update_log_fid(fid, index);
1199 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1201 lod_update_log_name,
1202 S_IFREG | S_IRUGO | S_IWUSR);
1204 GOTO(out_put, rc = PTR_ERR(dto));
1206 lu_object_put(env, &dto->do_lu);
1208 /* Create update log dir */
1209 lu_update_log_dir_fid(fid, index);
1210 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1212 lod_update_log_dir_name,
1213 S_IFDIR | S_IRUGO | S_IWUSR);
1215 GOTO(out_put, rc = PTR_ERR(dto));
1217 lu_object_put(env, &dto->do_lu);
1219 rc = lod_prepare_distribute_txn(env, lod);
1223 rc = lod_sub_init_llogs(env, lod);
1228 lu_object_put(env, &root->do_lu);
1233 const struct lu_device_operations lod_lu_ops = {
1234 .ldo_object_alloc = lod_object_alloc,
1235 .ldo_process_config = lod_process_config,
1236 .ldo_recovery_complete = lod_recovery_complete,
1237 .ldo_prepare = lod_prepare,
1241 * Implementation of dt_device_operations::dt_root_get() for LOD
1243 * see include/dt_object.h for the details.
1245 static int lod_root_get(const struct lu_env *env,
1246 struct dt_device *dev, struct lu_fid *f)
1248 return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
1252 * Implementation of dt_device_operations::dt_statfs() for LOD
1254 * see include/dt_object.h for the details.
1256 static int lod_statfs(const struct lu_env *env,
1257 struct dt_device *dev, struct obd_statfs *sfs)
1259 return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
1263 * Implementation of dt_device_operations::dt_trans_create() for LOD
1265 * Creates a transaction using local (to this node) OSD.
1267 * see include/dt_object.h for the details.
1269 static struct thandle *lod_trans_create(const struct lu_env *env,
1270 struct dt_device *dt)
1274 th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
1284 * Implementation of dt_device_operations::dt_trans_start() for LOD
1286 * Starts the set of local transactions using the targets involved
1287 * in declare phase. Initial support for the distributed transactions.
1289 * see include/dt_object.h for the details.
1291 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
1294 return top_trans_start(env, dt2lod_dev(dt)->lod_child, th);
1297 static int lod_trans_cb_add(struct thandle *th,
1298 struct dt_txn_commit_cb *dcb)
1300 struct top_thandle *top_th = container_of(th, struct top_thandle,
1302 return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
1306 * add noop update to the update records
1308 * Add noop updates to the update records, which is only used in
1311 * \param[in] env execution environment
1312 * \param[in] dt dt device of lod
1313 * \param[in] th thandle
1314 * \param[in] count the count of update records to be added.
1316 * \retval 0 if adding succeeds.
1317 * \retval negative errno if adding fails.
1319 static int lod_add_noop_records(const struct lu_env *env,
1320 struct dt_device *dt, struct thandle *th,
1323 struct top_thandle *top_th;
1324 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1328 top_th = container_of(th, struct top_thandle, tt_super);
1329 if (top_th->tt_multiple_thandle == NULL)
1333 for (i = 0; i < count; i++) {
1334 rc = update_record_pack(noop, th, fid);
1342 * Implementation of dt_device_operations::dt_trans_stop() for LOD
1344 * Stops the set of local transactions using the targets involved
1345 * in declare phase. Initial support for the distributed transactions.
1347 * see include/dt_object.h for the details.
1349 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
1352 if (OBD_FAIL_CHECK(OBD_FAIL_SPLIT_UPDATE_REC)) {
1355 rc = lod_add_noop_records(env, dt, th, 5000);
1359 return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
1363 * Implementation of dt_device_operations::dt_conf_get() for LOD
1365 * Currently returns the configuration provided by the local OSD.
1367 * see include/dt_object.h for the details.
1369 static void lod_conf_get(const struct lu_env *env,
1370 const struct dt_device *dev,
1371 struct dt_device_param *param)
1373 dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1377 * Implementation of dt_device_operations::dt_sync() for LOD
1379 * Syncs all known OST targets. Very very expensive and used
1380 * rarely by LFSCK now. Should not be used in general.
1382 * see include/dt_object.h for the details.
1384 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1386 struct lod_device *lod = dt2lod_dev(dev);
1387 struct lod_ost_desc *ost;
1392 lod_getref(&lod->lod_ost_descs);
1393 lod_foreach_ost(lod, i) {
1394 ost = OST_TGT(lod, i);
1395 LASSERT(ost && ost->ltd_ost);
1396 rc = dt_sync(env, ost->ltd_ost);
1398 CERROR("%s: can't sync %u: %d\n",
1399 lod2obd(lod)->obd_name, i, rc);
1403 lod_putref(lod, &lod->lod_ost_descs);
1405 rc = dt_sync(env, lod->lod_child);
1411 * Implementation of dt_device_operations::dt_ro() for LOD
1413 * Turns local OSD read-only, used for the testing only.
1415 * see include/dt_object.h for the details.
1417 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1419 return dt_ro(env, dt2lod_dev(dev)->lod_child);
1423 * Implementation of dt_device_operations::dt_commit_async() for LOD
1425 * Asks local OSD to commit sooner.
1427 * see include/dt_object.h for the details.
1429 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1431 return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1434 static const struct dt_device_operations lod_dt_ops = {
1435 .dt_root_get = lod_root_get,
1436 .dt_statfs = lod_statfs,
1437 .dt_trans_create = lod_trans_create,
1438 .dt_trans_start = lod_trans_start,
1439 .dt_trans_stop = lod_trans_stop,
1440 .dt_conf_get = lod_conf_get,
1441 .dt_sync = lod_sync,
1443 .dt_commit_async = lod_commit_async,
1444 .dt_trans_cb_add = lod_trans_cb_add,
1448 * Connect to a local OSD.
1450 * Used to connect to the local OSD at mount. OSD name is taken from the
1451 * configuration command passed. This connection is used to identify LU
1452 * site and pin the OSD from early removal.
1454 * \param[in] env LU environment provided by the caller
1455 * \param[in] lod lod device
1456 * \param[in] cfg configuration command to apply
1458 * \retval 0 on success
1459 * \retval negative negated errno on error
1461 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1462 struct lustre_cfg *cfg)
1464 struct obd_connect_data *data = NULL;
1465 struct obd_device *obd;
1466 char *nextdev = NULL, *p, *s;
1471 LASSERT(lod->lod_child_exp == NULL);
1473 /* compatibility hack: we still use old config logs
1474 * which specify LOV, but we need to learn underlying
1475 * OSD device, which is supposed to be:
1476 * <fsname>-MDTxxxx-osd
1478 * 2.x MGS generates lines like the following:
1479 * #03 (176)lov_setup 0:lustre-MDT0000-mdtlov 1:(struct lov_desc)
1480 * 1.8 MGS generates lines like the following:
1481 * #03 (168)lov_setup 0:lustre-mdtlov 1:(struct lov_desc)
1483 * we use "-MDT" to differentiate 2.x from 1.8 */
1485 if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
1486 len = strlen(p) + 6;
1487 OBD_ALLOC(nextdev, len);
1488 if (nextdev == NULL)
1489 GOTO(out, rc = -ENOMEM);
1492 s = strstr(nextdev, "-mdtlov");
1493 if (unlikely(s == NULL)) {
1494 CERROR("unable to parse device name %s\n",
1495 lustre_cfg_string(cfg, 0));
1496 GOTO(out, rc = -EINVAL);
1499 if (strstr(nextdev, "-MDT")) {
1504 strcpy(s, "-MDT0000-osd");
1507 CERROR("unable to parse device name %s\n",
1508 lustre_cfg_string(cfg, 0));
1509 GOTO(out, rc = -EINVAL);
1512 OBD_ALLOC_PTR(data);
1514 GOTO(out, rc = -ENOMEM);
1516 obd = class_name2obd(nextdev);
1518 CERROR("can not locate next device: %s\n", nextdev);
1519 GOTO(out, rc = -ENOTCONN);
1522 data->ocd_connect_flags = OBD_CONNECT_VERSION;
1523 data->ocd_version = LUSTRE_VERSION_CODE;
1525 rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
1528 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1532 lod->lod_dt_dev.dd_lu_dev.ld_site =
1533 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
1534 LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
1535 lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
1541 OBD_FREE(nextdev, len);
1546 * Allocate and initialize target table.
1548 * A helper function to initialize the target table and allocate
1549 * a bitmap of the available targets.
1551 * \param[in] ltd target's table to initialize
1553 * \retval 0 on success
1554 * \retval negative negated errno on error
1556 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
1558 mutex_init(<d->ltd_mutex);
1559 init_rwsem(<d->ltd_rw_sem);
1561 /* the OST array and bitmap are allocated/grown dynamically as OSTs are
1562 * added to the LOD, see lod_add_device() */
1563 ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
1564 if (ltd->ltd_tgt_bitmap == NULL)
1567 ltd->ltd_tgts_size = 32;
1570 ltd->ltd_death_row = 0;
1571 ltd->ltd_refcount = 0;
1576 * Initialize LOD device at setup.
1578 * Initializes the given LOD device using the original configuration command.
1579 * The function initiates a connection to the local OSD and initializes few
1580 * internal structures like pools, target tables, etc.
1582 * \param[in] env LU environment provided by the caller
1583 * \param[in] lod lod device
1584 * \param[in] ldt not used
1585 * \param[in] cfg configuration command
1587 * \retval 0 on success
1588 * \retval negative negated errno on error
1590 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
1591 struct lu_device_type *ldt, struct lustre_cfg *cfg)
1593 struct dt_device_param ddp;
1594 struct obd_device *obd;
1598 obd = class_name2obd(lustre_cfg_string(cfg, 0));
1600 CERROR("Cannot find obd with name %s\n",
1601 lustre_cfg_string(cfg, 0));
1605 obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1606 lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
1607 lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
1608 lod->lod_dt_dev.dd_ops = &lod_dt_ops;
1610 rc = lod_connect_to_osd(env, lod, cfg);
1614 dt_conf_get(env, &lod->lod_dt_dev, &ddp);
1615 lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
1617 /* setup obd to be used with old lov code */
1618 rc = lod_pools_init(lod, cfg);
1620 GOTO(out_disconnect, rc);
1622 rc = lod_procfs_init(lod);
1624 GOTO(out_pools, rc);
1626 spin_lock_init(&lod->lod_lock);
1627 spin_lock_init(&lod->lod_connects_lock);
1628 lod_tgt_desc_init(&lod->lod_mdt_descs);
1629 lod_tgt_desc_init(&lod->lod_ost_descs);
1634 lod_pools_fini(lod);
1636 obd_disconnect(lod->lod_child_exp);
1641 * Implementation of lu_device_type_operations::ldto_device_free() for LOD
1643 * Releases the memory allocated for LOD device.
1645 * see include/lu_object.h for the details.
1647 static struct lu_device *lod_device_free(const struct lu_env *env,
1648 struct lu_device *lu)
1650 struct lod_device *lod = lu2lod_dev(lu);
1651 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1654 LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %p\n", lu);
1655 dt_device_fini(&lod->lod_dt_dev);
1661 * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
1663 * Allocates LOD device and calls the helpers to initialize it.
1665 * see include/lu_object.h for the details.
1667 static struct lu_device *lod_device_alloc(const struct lu_env *env,
1668 struct lu_device_type *type,
1669 struct lustre_cfg *lcfg)
1671 struct lod_device *lod;
1672 struct lu_device *lu_dev;
1676 lu_dev = ERR_PTR(-ENOMEM);
1680 lu_dev = lod2lu_dev(lod);
1681 dt_device_init(&lod->lod_dt_dev, type);
1682 rc = lod_init0(env, lod, type, lcfg);
1684 lod_device_free(env, lu_dev);
1685 lu_dev = ERR_PTR(rc);
1693 * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
1695 * Releases the internal resources used by LOD device.
1697 * see include/lu_object.h for the details.
1699 static struct lu_device *lod_device_fini(const struct lu_env *env,
1700 struct lu_device *d)
1702 struct lod_device *lod = lu2lod_dev(d);
1706 lod_pools_fini(lod);
1708 lod_procfs_fini(lod);
1710 rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
1712 CERROR("%s:can not fini ost descs %d\n",
1713 lod2obd(lod)->obd_name, rc);
1715 rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
1717 CERROR("%s:can not fini mdt descs %d\n",
1718 lod2obd(lod)->obd_name, rc);
1724 * Implementation of obd_ops::o_connect() for LOD
1726 * Used to track all the users of this specific LOD device,
1727 * so the device stays up until the last user disconnected.
1729 * \param[in] env LU environment provided by the caller
1730 * \param[out] exp export the caller will be using to access LOD
1731 * \param[in] obd OBD device representing LOD device
1732 * \param[in] cluuid unique identifier of the caller
1733 * \param[in] data not used
1734 * \param[in] localdata not used
1736 * \retval 0 on success
1737 * \retval negative negated errno on error
1739 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1740 struct obd_device *obd, struct obd_uuid *cluuid,
1741 struct obd_connect_data *data, void *localdata)
1743 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1744 struct lustre_handle conn;
1748 CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1750 rc = class_connect(&conn, obd, cluuid);
1754 *exp = class_conn2export(&conn);
1756 spin_lock(&lod->lod_connects_lock);
1757 lod->lod_connects++;
1758 /* at the moment we expect the only user */
1759 LASSERT(lod->lod_connects == 1);
1760 spin_unlock(&lod->lod_connects_lock);
1767 * Implementation of obd_ops::o_disconnect() for LOD
1769 * When the caller doesn't need to use this LOD instance, it calls
1770 * obd_disconnect() and LOD releases corresponding export/reference count.
1771 * Once all the users gone, LOD device is released.
1773 * \param[in] exp export provided to the caller in obd_connect()
1775 * \retval 0 on success
1776 * \retval negative negated errno on error
1778 static int lod_obd_disconnect(struct obd_export *exp)
1780 struct obd_device *obd = exp->exp_obd;
1781 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1782 int rc, release = 0;
1785 /* Only disconnect the underlying layers on the final disconnect. */
1786 spin_lock(&lod->lod_connects_lock);
1787 lod->lod_connects--;
1788 if (lod->lod_connects != 0) {
1789 /* why should there be more than 1 connect? */
1790 spin_unlock(&lod->lod_connects_lock);
1791 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1795 spin_unlock(&lod->lod_connects_lock);
1797 /* the last user of lod has gone, let's release the device */
1801 rc = class_disconnect(exp); /* bz 9811 */
1803 if (rc == 0 && release)
1804 class_manual_cleanup(obd);
1808 LU_KEY_INIT(lod, struct lod_thread_info);
1810 static void lod_key_fini(const struct lu_context *ctx,
1811 struct lu_context_key *key, void *data)
1813 struct lod_thread_info *info = data;
1814 /* allocated in lod_get_lov_ea
1815 * XXX: this is overload, a tread may have such store but used only
1816 * once. Probably better would be pool of such stores per LOD.
1818 if (info->lti_ea_store) {
1819 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1820 info->lti_ea_store = NULL;
1821 info->lti_ea_store_size = 0;
1823 lu_buf_free(&info->lti_linkea_buf);
1827 /* context key: lod_thread_key */
1828 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1830 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1832 static struct lu_device_type_operations lod_device_type_ops = {
1833 .ldto_init = lod_type_init,
1834 .ldto_fini = lod_type_fini,
1836 .ldto_start = lod_type_start,
1837 .ldto_stop = lod_type_stop,
1839 .ldto_device_alloc = lod_device_alloc,
1840 .ldto_device_free = lod_device_free,
1842 .ldto_device_fini = lod_device_fini
1845 static struct lu_device_type lod_device_type = {
1846 .ldt_tags = LU_DEVICE_DT,
1847 .ldt_name = LUSTRE_LOD_NAME,
1848 .ldt_ops = &lod_device_type_ops,
1849 .ldt_ctx_tags = LCT_MD_THREAD,
1853 * Implementation of obd_ops::o_get_info() for LOD
1855 * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
1856 * the caller binary status whether LOD has seen connection to any OST target.
1857 * It will also check if the MDT update log context being initialized (if
1860 * \param[in] env LU environment provided by the caller
1861 * \param[in] exp export of the caller
1862 * \param[in] keylen len of the key
1863 * \param[in] key the key
1864 * \param[in] vallen not used
1865 * \param[in] val not used
1867 * \retval 0 if a connection was seen
1868 * \retval -EAGAIN if LOD isn't running yet or no
1869 * connection has been seen yet
1870 * \retval -EINVAL if not supported key is requested
1872 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1873 __u32 keylen, void *key, __u32 *vallen, void *val)
1877 if (KEY_IS(KEY_OSP_CONNECTED)) {
1878 struct obd_device *obd = exp->exp_obd;
1879 struct lod_device *d;
1880 struct lod_tgt_desc *tgt;
1884 if (!obd->obd_set_up || obd->obd_stopping)
1887 d = lu2lod_dev(obd->obd_lu_dev);
1888 lod_getref(&d->lod_ost_descs);
1889 lod_foreach_ost(d, i) {
1890 tgt = OST_TGT(d, i);
1891 LASSERT(tgt && tgt->ltd_tgt);
1892 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
1894 /* one healthy device is enough */
1898 lod_putref(d, &d->lod_ost_descs);
1900 lod_getref(&d->lod_mdt_descs);
1901 lod_foreach_mdt(d, i) {
1902 struct llog_ctxt *ctxt;
1904 tgt = MDT_TGT(d, i);
1905 LASSERT(tgt != NULL);
1906 LASSERT(tgt->ltd_tgt != NULL);
1907 if (!tgt->ltd_active)
1910 ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
1911 LLOG_UPDATELOG_ORIG_CTXT);
1913 CDEBUG(D_INFO, "%s: %s is not ready.\n",
1915 tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
1919 if (ctxt->loc_handle == NULL) {
1920 CDEBUG(D_INFO, "%s: %s is not ready.\n",
1922 tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
1924 llog_ctxt_put(ctxt);
1927 llog_ctxt_put(ctxt);
1929 lod_putref(d, &d->lod_mdt_descs);
1937 static int lod_obd_set_info_async(const struct lu_env *env,
1938 struct obd_export *exp,
1939 __u32 keylen, void *key,
1940 __u32 vallen, void *val,
1941 struct ptlrpc_request_set *set)
1943 struct obd_device *obd = class_exp2obd(exp);
1944 struct lod_device *d;
1945 struct lod_tgt_desc *tgt;
1952 set = ptlrpc_prep_set();
1957 d = lu2lod_dev(obd->obd_lu_dev);
1958 lod_getref(&d->lod_ost_descs);
1959 lod_foreach_ost(d, i) {
1960 tgt = OST_TGT(d, i);
1961 LASSERT(tgt && tgt->ltd_tgt);
1962 if (!tgt->ltd_active)
1965 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
1967 if (rc2 != 0 && rc == 0)
1970 lod_putref(d, &d->lod_ost_descs);
1972 lod_getref(&d->lod_mdt_descs);
1973 lod_foreach_mdt(d, i) {
1974 tgt = MDT_TGT(d, i);
1975 LASSERT(tgt && tgt->ltd_tgt);
1976 if (!tgt->ltd_active)
1978 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
1980 if (rc2 != 0 && rc == 0)
1983 lod_putref(d, &d->lod_mdt_descs);
1987 rc2 = ptlrpc_set_wait(set);
1988 if (rc2 == 0 && rc == 0)
1990 ptlrpc_set_destroy(set);
1995 static struct obd_ops lod_obd_device_ops = {
1996 .o_owner = THIS_MODULE,
1997 .o_connect = lod_obd_connect,
1998 .o_disconnect = lod_obd_disconnect,
1999 .o_get_info = lod_obd_get_info,
2000 .o_set_info_async = lod_obd_set_info_async,
2001 .o_pool_new = lod_pool_new,
2002 .o_pool_rem = lod_pool_remove,
2003 .o_pool_add = lod_pool_add,
2004 .o_pool_del = lod_pool_del,
2007 static int __init lod_init(void)
2009 struct obd_type *type;
2012 rc = lu_kmem_init(lod_caches);
2016 rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
2017 LUSTRE_LOD_NAME, &lod_device_type);
2019 lu_kmem_fini(lod_caches);
2023 /* create "lov" entry in procfs for compatibility purposes */
2024 type = class_search_type(LUSTRE_LOV_NAME);
2025 if (type != NULL && type->typ_procroot != NULL)
2028 type = class_search_type(LUSTRE_LOD_NAME);
2029 type->typ_procsym = lprocfs_register("lov", proc_lustre_root,
2031 if (IS_ERR(type->typ_procsym)) {
2032 CERROR("lod: can't create compat entry \"lov\": %d\n",
2033 (int)PTR_ERR(type->typ_procsym));
2034 type->typ_procsym = NULL;
2039 static void __exit lod_exit(void)
2041 class_unregister_type(LUSTRE_LOD_NAME);
2042 lu_kmem_fini(lod_caches);
2045 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2046 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
2047 MODULE_VERSION(LUSTRE_VERSION_STRING);
2048 MODULE_LICENSE("GPL");
2050 module_init(lod_init);
2051 module_exit(lod_exit);