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, 2017, 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 mutex_init(&lod_obj->ldo_layout_mutex);
217 lu_obj = lod2lu_obj(lod_obj);
218 dt_object_init(&lod_obj->ldo_obj, NULL, dev);
219 lod_obj->ldo_obj.do_ops = &lod_obj_ops;
220 lu_obj->lo_ops = &lod_lu_obj_ops;
226 * Process the config log for all sub device.
228 * The function goes through all the targets in the given table
229 * and apply given configuration command on to the targets.
230 * Used to cleanup the targets at unmount.
232 * \param[in] env LU environment provided by the caller
233 * \param[in] lod lod device
234 * \param[in] ltd target's table to go through
235 * \param[in] lcfg configuration command to apply
237 * \retval 0 on success
238 * \retval negative negated errno on error
240 static int lod_sub_process_config(const struct lu_env *env,
241 struct lod_device *lod,
242 struct lod_tgt_descs *ltd,
243 struct lustre_cfg *lcfg)
245 struct lu_device *next;
250 if (ltd->ltd_tgts_size <= 0) {
251 lod_putref(lod, ltd);
254 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
255 struct lod_tgt_desc *tgt;
258 tgt = LTD_TGT(ltd, i);
259 LASSERT(tgt && tgt->ltd_tgt);
260 next = &tgt->ltd_tgt->dd_lu_dev;
261 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
263 CERROR("%s: error cleaning up LOD index %u: cmd %#x"
264 ": rc = %d\n", lod2obd(lod)->obd_name, i,
265 lcfg->lcfg_command, rc1);
269 lod_putref(lod, ltd);
273 struct lod_recovery_data {
274 struct lod_device *lrd_lod;
275 struct lod_tgt_desc *lrd_ltd;
276 struct ptlrpc_thread *lrd_thread;
282 * process update recovery record
284 * Add the update recovery recode to the update recovery list in
285 * lod_recovery_data. Then the recovery thread (target_recovery_thread)
286 * will redo these updates.
288 * \param[in]env execution environment
289 * \param[in]llh log handle of update record
290 * \param[in]rec update record to be replayed
291 * \param[in]data update recovery data which holds the necessary
292 * arguments for recovery (see struct lod_recovery_data)
294 * \retval 0 if the record is processed successfully.
295 * \retval negative errno if the record processing fails.
297 static int lod_process_recovery_updates(const struct lu_env *env,
298 struct llog_handle *llh,
299 struct llog_rec_hdr *rec,
302 struct lod_recovery_data *lrd = data;
303 struct llog_cookie *cookie = &lod_env_info(env)->lti_cookie;
304 struct lu_target *lut;
308 if (lrd->lrd_ltd == NULL) {
311 rc = lodname2mdt_index(lod2obd(lrd->lrd_lod)->obd_name, &index);
315 index = lrd->lrd_ltd->ltd_index;
319 llog_update_record_size((struct llog_update_record *)rec)) {
320 CERROR("%s broken update record! index %u "DFID".%u :"
321 " rc = %d\n", lod2obd(lrd->lrd_lod)->obd_name, index,
322 PFID(&llh->lgh_id.lgl_oi.oi_fid), rec->lrh_index, -EIO);
326 cookie->lgc_lgl = llh->lgh_id;
327 cookie->lgc_index = rec->lrh_index;
328 cookie->lgc_subsys = LLOG_UPDATELOG_ORIG_CTXT;
330 CDEBUG(D_HA, "%s: process recovery updates "DFID".%u\n",
331 lod2obd(lrd->lrd_lod)->obd_name,
332 PFID(&llh->lgh_id.lgl_oi.oi_fid), rec->lrh_index);
333 lut = lod2lu_dev(lrd->lrd_lod)->ld_site->ls_tgt;
335 if (lut->lut_obd->obd_stopping ||
336 lut->lut_obd->obd_abort_recovery)
339 return insert_update_records_to_replay_list(lut->lut_tdtd,
340 (struct llog_update_record *)rec,
345 * recovery thread for update log
347 * Start recovery thread and prepare the sub llog, then it will retrieve
348 * the update records from the correpondent MDT and do recovery.
350 * \param[in] arg pointer to the recovery data
352 * \retval 0 if recovery succeeds
353 * \retval negative errno if recovery failed.
355 static int lod_sub_recovery_thread(void *arg)
357 struct lod_recovery_data *lrd = arg;
358 struct lod_device *lod = lrd->lrd_lod;
359 struct dt_device *dt;
360 struct ptlrpc_thread *thread = lrd->lrd_thread;
361 struct llog_ctxt *ctxt = NULL;
363 struct lu_target *lut;
364 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
365 struct lod_tgt_desc *tgt = NULL;
372 thread->t_flags = SVC_RUNNING;
373 wake_up(&thread->t_ctl_waitq);
375 rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
378 CERROR("%s: can't initialize env: rc = %d\n",
379 lod2obd(lod)->obd_name, rc);
383 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
384 atomic_inc(&lut->lut_tdtd->tdtd_recovery_threads_count);
385 if (lrd->lrd_ltd == NULL)
388 dt = lrd->lrd_ltd->ltd_tgt;
390 start = ktime_get_real_seconds();
393 rc = lod_sub_prep_llog(&env, lod, dt, lrd->lrd_idx);
394 if (!rc && !lod->lod_child->dd_rdonly) {
395 /* Process the recovery record */
396 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
397 LLOG_UPDATELOG_ORIG_CTXT);
398 LASSERT(ctxt != NULL);
399 LASSERT(ctxt->loc_handle != NULL);
401 rc = llog_cat_process(&env, ctxt->loc_handle,
402 lod_process_recovery_updates, lrd, 0, 0);
406 struct lu_device *top_device;
408 top_device = lod->lod_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
409 /* Because the remote target might failover at the same time,
410 * let's retry here */
411 if ((rc == -ETIMEDOUT || rc == -EAGAIN || rc == -EIO) &&
412 dt != lod->lod_child &&
413 !top_device->ld_obd->obd_abort_recovery &&
414 !top_device->ld_obd->obd_stopping) {
416 if (ctxt->loc_handle != NULL)
422 CDEBUG(D_HA, "%s get update log failed %d, retry\n",
423 dt->dd_lu_dev.ld_obd->obd_name, rc);
427 CERROR("%s get update log failed: rc = %d\n",
428 dt->dd_lu_dev.ld_obd->obd_name, rc);
431 spin_lock(&top_device->ld_obd->obd_dev_lock);
432 if (!top_device->ld_obd->obd_abort_recovery &&
433 !top_device->ld_obd->obd_stopping)
434 top_device->ld_obd->obd_abort_recovery = 1;
435 spin_unlock(&top_device->ld_obd->obd_dev_lock);
441 CDEBUG(D_HA, "%s retrieved update log, duration %lld, retries %d\n",
442 dt->dd_lu_dev.ld_obd->obd_name, ktime_get_real_seconds() - start,
445 spin_lock(&lod->lod_lock);
446 if (lrd->lrd_ltd == NULL)
447 lod->lod_child_got_update_log = 1;
449 lrd->lrd_ltd->ltd_got_update_log = 1;
451 if (!lod->lod_child_got_update_log) {
452 spin_unlock(&lod->lod_lock);
456 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
457 tgt = LTD_TGT(ltd, i);
458 if (!tgt->ltd_got_update_log) {
459 spin_unlock(&lod->lod_lock);
463 lut->lut_tdtd->tdtd_replay_ready = 1;
464 spin_unlock(&lod->lod_lock);
466 CDEBUG(D_HA, "%s got update logs from all MDTs.\n",
467 lut->lut_obd->obd_name);
468 wake_up(&lut->lut_obd->obd_next_transno_waitq);
473 thread->t_flags = SVC_STOPPED;
474 atomic_dec(&lut->lut_tdtd->tdtd_recovery_threads_count);
475 wake_up(&lut->lut_tdtd->tdtd_recovery_threads_waitq);
476 wake_up(&thread->t_ctl_waitq);
482 * finish sub llog context
484 * Stop update recovery thread for the sub device, then cleanup the
485 * correspondent llog ctxt.
487 * \param[in] env execution environment
488 * \param[in] lod lod device to do update recovery
489 * \param[in] thread recovery thread on this sub device
491 void lod_sub_fini_llog(const struct lu_env *env,
492 struct dt_device *dt, struct ptlrpc_thread *thread)
494 struct obd_device *obd;
495 struct llog_ctxt *ctxt;
498 obd = dt->dd_lu_dev.ld_obd;
499 CDEBUG(D_INFO, "%s: finish sub llog\n", obd->obd_name);
500 /* Stop recovery thread first */
501 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
502 thread->t_flags = SVC_STOPPING;
503 wake_up(&thread->t_ctl_waitq);
504 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
507 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
511 if (ctxt->loc_handle != NULL)
512 llog_cat_close(env, ctxt->loc_handle);
514 llog_cleanup(env, ctxt);
520 * Extract MDT target index from a device name.
522 * a helper function to extract index from the given device name
523 * like "fsname-MDTxxxx-mdtlov"
525 * \param[in] lodname device name
526 * \param[out] mdt_index extracted index
528 * \retval 0 on success
529 * \retval -EINVAL if the name is invalid
531 int lodname2mdt_index(char *lodname, __u32 *mdt_index)
536 /* 1.8 configs don't have "-MDT0000" at the end */
537 ptr = strstr(lodname, "-MDT");
543 ptr = strrchr(lodname, '-');
545 CERROR("invalid MDT index in '%s'\n", lodname);
549 if (strncmp(ptr, "-mdtlov", 7) != 0) {
550 CERROR("invalid MDT index in '%s'\n", lodname);
554 if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
555 CERROR("invalid MDT index in '%s'\n", lodname);
559 if (strncmp(ptr - 8, "-MDT", 4) != 0) {
560 CERROR("invalid MDT index in '%s'\n", lodname);
564 index = simple_strtol(ptr - 4, &tmp, 16);
565 if (*tmp != '-' || index > INT_MAX) {
566 CERROR("invalid MDT index in '%s'\n", lodname);
574 * Init sub llog context
576 * Setup update llog ctxt for update recovery threads, then start the
577 * recovery thread (lod_sub_recovery_thread) to read update llog from
578 * the correspondent MDT to do update recovery.
580 * \param[in] env execution environment
581 * \param[in] lod lod device to do update recovery
582 * \param[in] dt sub dt device for which the recovery thread is
584 * \retval 0 if initialization succeeds.
585 * \retval negative errno if initialization fails.
587 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
588 struct dt_device *dt)
590 struct obd_device *obd;
591 struct lod_recovery_data *lrd = NULL;
592 struct ptlrpc_thread *thread;
593 struct task_struct *task;
594 struct l_wait_info lwi = { 0 };
595 struct lod_tgt_desc *sub_ltd = NULL;
601 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &master_index);
609 if (lod->lod_child == dt) {
610 thread = &lod->lod_child_recovery_thread;
611 index = master_index;
613 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
614 struct lod_tgt_desc *tgt = NULL;
617 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
618 tgt = LTD_TGT(ltd, i);
619 if (tgt->ltd_tgt == dt) {
620 index = tgt->ltd_index;
625 LASSERT(sub_ltd != NULL);
626 OBD_ALLOC_PTR(sub_ltd->ltd_recovery_thread);
627 if (sub_ltd->ltd_recovery_thread == NULL)
628 GOTO(free_lrd, rc = -ENOMEM);
630 thread = sub_ltd->ltd_recovery_thread;
633 CDEBUG(D_INFO, "%s init sub log %s\n", lod2obd(lod)->obd_name,
634 dt->dd_lu_dev.ld_obd->obd_name);
636 lrd->lrd_ltd = sub_ltd;
637 lrd->lrd_thread = thread;
638 lrd->lrd_idx = index;
639 init_waitqueue_head(&thread->t_ctl_waitq);
641 obd = dt->dd_lu_dev.ld_obd;
642 obd->obd_lvfs_ctxt.dt = dt;
643 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
644 NULL, &llog_common_cat_ops);
646 CERROR("%s: cannot setup updatelog llog: rc = %d\n",
648 GOTO(free_thread, rc);
651 /* Start the recovery thread */
652 task = kthread_run(lod_sub_recovery_thread, lrd, "lod%04x_rec%04x",
653 master_index, index);
656 CERROR("%s: cannot start recovery thread: rc = %d\n",
661 l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING ||
662 thread->t_flags & SVC_STOPPED, &lwi);
666 lod_sub_fini_llog(env, dt, thread);
668 if (lod->lod_child != dt) {
669 OBD_FREE_PTR(sub_ltd->ltd_recovery_thread);
670 sub_ltd->ltd_recovery_thread = NULL;
678 * Stop sub recovery thread
680 * Stop sub recovery thread on all subs.
682 * \param[in] env execution environment
683 * \param[in] lod lod device to do update recovery
685 static void lod_sub_stop_recovery_threads(const struct lu_env *env,
686 struct lod_device *lod)
688 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
689 struct ptlrpc_thread *thread;
692 /* Stop the update log commit cancel threads and finish master
694 thread = &lod->lod_child_recovery_thread;
695 /* Stop recovery thread first */
696 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
697 thread->t_flags = SVC_STOPPING;
698 wake_up(&thread->t_ctl_waitq);
699 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
703 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
704 struct lod_tgt_desc *tgt;
706 tgt = LTD_TGT(ltd, i);
707 thread = tgt->ltd_recovery_thread;
708 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
709 thread->t_flags = SVC_STOPPING;
710 wake_up(&thread->t_ctl_waitq);
711 wait_event(thread->t_ctl_waitq,
712 thread->t_flags & SVC_STOPPED);
713 OBD_FREE_PTR(tgt->ltd_recovery_thread);
714 tgt->ltd_recovery_thread = NULL;
718 lod_putref(lod, ltd);
722 * finish all sub llog
724 * cleanup all of sub llog ctxt on the LOD.
726 * \param[in] env execution environment
727 * \param[in] lod lod device to do update recovery
729 static void lod_sub_fini_all_llogs(const struct lu_env *env,
730 struct lod_device *lod)
732 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
735 /* Stop the update log commit cancel threads and finish master
737 lod_sub_fini_llog(env, lod->lod_child,
738 &lod->lod_child_recovery_thread);
740 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
741 struct lod_tgt_desc *tgt;
743 tgt = LTD_TGT(ltd, i);
744 lod_sub_fini_llog(env, tgt->ltd_tgt,
745 tgt->ltd_recovery_thread);
748 lod_putref(lod, ltd);
751 static char *lod_show_update_logs_retrievers(void *data, int *size, int *count)
753 struct lod_device *lod = (struct lod_device *)data;
754 struct lu_target *lut = lod2lu_dev(lod)->ld_site->ls_tgt;
755 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
756 struct lod_tgt_desc *tgt = NULL;
762 *count = atomic_read(&lut->lut_tdtd->tdtd_recovery_threads_count);
768 *size = 5 * *count + 1;
769 OBD_ALLOC(buf, *size);
774 memset(buf, 0, *size);
776 if (!lod->lod_child_got_update_log) {
777 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &i);
778 LASSERTF(rc == 0, "Fail to parse target index: rc = %d\n", rc);
780 rc = snprintf(buf + len, *size - len, " %04x", i);
787 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
788 tgt = LTD_TGT(ltd, i);
789 if (!tgt->ltd_got_update_log) {
790 rc = snprintf(buf + len, *size - len, " %04x", i);
791 if (unlikely(rc <= 0))
803 * Prepare distribute txn
805 * Prepare distribute txn structure for LOD
807 * \param[in] env execution environment
808 * \param[in] lod_device LOD device
810 * \retval 0 if preparation succeeds.
811 * \retval negative errno if preparation fails.
813 static int lod_prepare_distribute_txn(const struct lu_env *env,
814 struct lod_device *lod)
816 struct target_distribute_txn_data *tdtd;
817 struct lu_target *lut;
821 /* Init update recovery data */
826 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
827 tdtd->tdtd_dt = &lod->lod_dt_dev;
828 rc = distribute_txn_init(env, lut, tdtd,
829 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id);
832 CERROR("%s: cannot init distribute txn: rc = %d\n",
833 lod2obd(lod)->obd_name, rc);
838 tdtd->tdtd_show_update_logs_retrievers =
839 lod_show_update_logs_retrievers;
840 tdtd->tdtd_show_retrievers_cbdata = lod;
842 lut->lut_tdtd = tdtd;
848 * Finish distribute txn
850 * Release the resource holding by distribute txn, i.e. stop distribute
853 * \param[in] env execution environment
854 * \param[in] lod lod device
856 static void lod_fini_distribute_txn(const struct lu_env *env,
857 struct lod_device *lod)
859 struct lu_target *lut;
861 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
862 target_recovery_fini(lut->lut_obd);
863 if (lut->lut_tdtd == NULL)
866 distribute_txn_fini(env, lut->lut_tdtd);
868 OBD_FREE_PTR(lut->lut_tdtd);
869 lut->lut_tdtd = NULL;
873 * Implementation of lu_device_operations::ldo_process_config() for LOD
875 * The method is called by the configuration subsystem during setup,
876 * cleanup and when the configuration changes. The method processes
877 * few specific commands like adding/removing the targets, changing
878 * the runtime parameters.
880 * \param[in] env LU environment provided by the caller
881 * \param[in] dev lod device
882 * \param[in] lcfg configuration command to apply
884 * \retval 0 on success
885 * \retval negative negated errno on error
887 * The examples are below.
889 * Add osc config log:
890 * marker 20 (flags=0x01, v2.2.49.56) lustre-OST0001 'add osc'
891 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nidxxx
892 * attach 0:lustre-OST0001-osc-MDT0001 1:osc 2:lustre-MDT0001-mdtlov_UUID
893 * setup 0:lustre-OST0001-osc-MDT0001 1:lustre-OST0001_UUID 2:nid
894 * lov_modify_tgts add 0:lustre-MDT0001-mdtlov 1:lustre-OST0001_UUID 2:1 3:1
895 * marker 20 (flags=0x02, v2.2.49.56) lustre-OST0001 'add osc'
897 * Add mdc config log:
898 * marker 10 (flags=0x01, v2.2.49.56) lustre-MDT0000 'add osp'
899 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nid
900 * attach 0:lustre-MDT0000-osp-MDT0001 1:osp 2:lustre-MDT0001-mdtlov_UUID
901 * setup 0:lustre-MDT0000-osp-MDT0001 1:lustre-MDT0000_UUID 2:nid
902 * modify_mdc_tgts add 0:lustre-MDT0001 1:lustre-MDT0000_UUID 2:0 3:1
903 * marker 10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID 'add osp'
905 static int lod_process_config(const struct lu_env *env,
906 struct lu_device *dev,
907 struct lustre_cfg *lcfg)
909 struct lod_device *lod = lu2lod_dev(dev);
910 struct lu_device *next = &lod->lod_child->dd_lu_dev;
915 switch(lcfg->lcfg_command) {
916 case LCFG_LOV_DEL_OBD:
917 case LCFG_LOV_ADD_INA:
918 case LCFG_LOV_ADD_OBD:
923 /* lov_modify_tgts add 0:lov_mdsA 1:osp 2:0 3:1
924 * modify_mdc_tgts add 0:lustre-MDT0001
925 * 1:lustre-MDT0001-mdc0002
927 arg1 = lustre_cfg_string(lcfg, 1);
929 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
930 GOTO(out, rc = -EINVAL);
931 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
932 GOTO(out, rc = -EINVAL);
934 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
937 rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
942 rc = lod_add_device(env, lod, arg1, index, gen,
943 mdt_index, LUSTRE_OSC_NAME, 1);
944 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
946 rc = lod_add_device(env, lod, arg1, index, gen,
947 mdt_index, LUSTRE_MDC_NAME, 1);
948 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
949 /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
951 rc = lod_add_device(env, lod, arg1, index, gen,
952 mdt_index, LUSTRE_OSC_NAME, 0);
954 rc = lod_del_device(env, lod,
956 arg1, index, gen, true);
963 struct obd_device *obd;
967 /* Check if it is activate/deactivate mdc
968 * lustre-MDTXXXX-osp-MDTXXXX.active=1 */
969 param = lustre_cfg_buf(lcfg, 1);
970 if (strstr(param, "osp") != NULL &&
971 strstr(param, ".active=") != NULL) {
972 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
973 struct lod_tgt_desc *sub_tgt = NULL;
978 ptr = strstr(param, ".");
980 obd = class_name2obd(param);
982 CERROR("%s: can not find %s: rc = %d\n",
983 lod2obd(lod)->obd_name, param, -EINVAL);
988 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
989 struct lod_tgt_desc *tgt;
991 tgt = LTD_TGT(ltd, i);
992 if (tgt->ltd_tgt->dd_lu_dev.ld_obd == obd) {
998 if (sub_tgt == NULL) {
999 CERROR("%s: can not find %s: rc = %d\n",
1000 lod2obd(lod)->obd_name, param, -EINVAL);
1006 tmp = strstr(param, "=");
1009 struct llog_ctxt *ctxt;
1011 obd = sub_tgt->ltd_tgt->dd_lu_dev.ld_obd;
1012 ctxt = llog_get_context(obd,
1013 LLOG_UPDATELOG_ORIG_CTXT);
1015 rc = llog_setup(env, obd, &obd->obd_olg,
1016 LLOG_UPDATELOG_ORIG_CTXT,
1017 NULL, &llog_common_cat_ops);
1021 llog_ctxt_put(ctxt);
1023 rc = lod_sub_prep_llog(env, lod,
1025 sub_tgt->ltd_index);
1027 sub_tgt->ltd_active = 1;
1029 lod_sub_fini_llog(env, sub_tgt->ltd_tgt,
1031 sub_tgt->ltd_active = 0;
1037 if (strstr(param, PARAM_LOD) != NULL)
1038 count = class_modify_config(lcfg, PARAM_LOD,
1039 &lod->lod_dt_dev.dd_kobj);
1041 count = class_modify_config(lcfg, PARAM_LOV,
1042 &lod->lod_dt_dev.dd_kobj);
1043 rc = count > 0 ? 0 : count;
1046 case LCFG_PRE_CLEANUP: {
1047 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1048 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1049 next = &lod->lod_child->dd_lu_dev;
1050 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1052 CDEBUG(D_HA, "%s: can't process %u: %d\n",
1053 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1055 lod_sub_stop_recovery_threads(env, lod);
1056 lod_fini_distribute_txn(env, lod);
1057 lod_sub_fini_all_llogs(env, lod);
1060 case LCFG_CLEANUP: {
1061 if (lod->lod_md_root != NULL) {
1062 dt_object_put(env, &lod->lod_md_root->ldo_obj);
1063 lod->lod_md_root = NULL;
1067 * do cleanup on underlying storage only when
1068 * all OSPs are cleaned up, as they use that OSD as well
1070 lu_dev_del_linkage(dev->ld_site, dev);
1071 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1072 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1073 next = &lod->lod_child->dd_lu_dev;
1074 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1076 CERROR("%s: can't process %u: %d\n",
1077 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1079 rc = obd_disconnect(lod->lod_child_exp);
1081 CERROR("error in disconnect from storage: %d\n", rc);
1085 CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
1086 lcfg->lcfg_command);
1096 * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
1098 * The method is called once the recovery is complete. This implementation
1099 * distributes the notification to all the known targets.
1101 * see include/lu_object.h for the details
1103 static int lod_recovery_complete(const struct lu_env *env,
1104 struct lu_device *dev)
1106 struct lod_device *lod = lu2lod_dev(dev);
1107 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1112 LASSERT(lod->lod_recovery_completed == 0);
1113 lod->lod_recovery_completed = 1;
1115 rc = next->ld_ops->ldo_recovery_complete(env, next);
1117 lod_getref(&lod->lod_ost_descs);
1118 if (lod->lod_osts_size > 0) {
1119 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
1120 struct lod_tgt_desc *tgt;
1121 tgt = OST_TGT(lod, i);
1122 LASSERT(tgt && tgt->ltd_tgt);
1123 next = &tgt->ltd_ost->dd_lu_dev;
1124 rc = next->ld_ops->ldo_recovery_complete(env, next);
1126 CERROR("%s: can't complete recovery on #%d:"
1127 "%d\n", lod2obd(lod)->obd_name, i, rc);
1130 lod_putref(lod, &lod->lod_ost_descs);
1135 * Init update logs on all sub device
1137 * LOD initialize update logs on all of sub devices. Because the initialization
1138 * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
1139 * lod_object_init(), this API has to be called after LOD is initialized.
1140 * \param[in] env execution environment
1141 * \param[in] lod lod device
1143 * \retval 0 if update log is initialized successfully.
1144 * \retval negative errno if initialization fails.
1146 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
1148 struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
1153 /* llog must be setup after LOD is initialized, because llog
1154 * initialization include FLD lookup */
1155 LASSERT(lod->lod_initialized);
1157 /* Init the llog in its own stack */
1158 rc = lod_sub_init_llog(env, lod, lod->lod_child);
1162 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
1163 struct lod_tgt_desc *tgt;
1165 tgt = LTD_TGT(ltd, i);
1166 rc = lod_sub_init_llog(env, lod, tgt->ltd_tgt);
1175 * Implementation of lu_device_operations::ldo_prepare() for LOD
1177 * see include/lu_object.h for the details.
1179 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
1180 struct lu_device *cdev)
1182 struct lod_device *lod = lu2lod_dev(cdev);
1183 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1184 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1186 struct dt_object *root;
1187 struct dt_object *dto;
1191 rc = next->ld_ops->ldo_prepare(env, pdev, next);
1193 CERROR("%s: prepare bottom error: rc = %d\n",
1194 lod2obd(lod)->obd_name, rc);
1198 lod->lod_initialized = 1;
1200 rc = dt_root_get(env, lod->lod_child, fid);
1204 root = dt_locate(env, lod->lod_child, fid);
1206 RETURN(PTR_ERR(root));
1208 /* Create update log object */
1209 index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1210 lu_update_log_fid(fid, index);
1212 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1214 lod_update_log_name,
1215 S_IFREG | S_IRUGO | S_IWUSR);
1217 GOTO(out_put, rc = PTR_ERR(dto));
1219 dt_object_put(env, dto);
1221 /* Create update log dir */
1222 lu_update_log_dir_fid(fid, index);
1223 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1225 lod_update_log_dir_name,
1226 S_IFDIR | S_IRUGO | S_IWUSR);
1228 GOTO(out_put, rc = PTR_ERR(dto));
1230 dt_object_put(env, dto);
1232 rc = lod_prepare_distribute_txn(env, lod);
1236 rc = lod_sub_init_llogs(env, lod);
1241 dt_object_put(env, root);
1246 const struct lu_device_operations lod_lu_ops = {
1247 .ldo_object_alloc = lod_object_alloc,
1248 .ldo_process_config = lod_process_config,
1249 .ldo_recovery_complete = lod_recovery_complete,
1250 .ldo_prepare = lod_prepare,
1254 * Implementation of dt_device_operations::dt_root_get() for LOD
1256 * see include/dt_object.h for the details.
1258 static int lod_root_get(const struct lu_env *env,
1259 struct dt_device *dev, struct lu_fid *f)
1261 return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
1264 static void lod_statfs_sum(struct obd_statfs *sfs,
1265 struct obd_statfs *ost_sfs, int *bs)
1267 while (ost_sfs->os_bsize < *bs) {
1269 sfs->os_bsize >>= 1;
1270 sfs->os_bavail <<= 1;
1271 sfs->os_blocks <<= 1;
1272 sfs->os_bfree <<= 1;
1273 sfs->os_granted <<= 1;
1275 while (ost_sfs->os_bsize > *bs) {
1276 ost_sfs->os_bsize >>= 1;
1277 ost_sfs->os_bavail <<= 1;
1278 ost_sfs->os_blocks <<= 1;
1279 ost_sfs->os_bfree <<= 1;
1280 ost_sfs->os_granted <<= 1;
1282 sfs->os_bavail += ost_sfs->os_bavail;
1283 sfs->os_blocks += ost_sfs->os_blocks;
1284 sfs->os_bfree += ost_sfs->os_bfree;
1285 sfs->os_granted += ost_sfs->os_granted;
1289 * Implementation of dt_device_operations::dt_statfs() for LOD
1291 * see include/dt_object.h for the details.
1293 static int lod_statfs(const struct lu_env *env,
1294 struct dt_device *dev, struct obd_statfs *sfs)
1296 struct lod_device *lod = dt2lod_dev(dev);
1297 struct lod_ost_desc *ost;
1298 struct lod_mdt_desc *mdt;
1299 struct obd_statfs ost_sfs;
1303 rc = dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
1312 sfs->os_granted = 0;
1314 lod_getref(&lod->lod_mdt_descs);
1315 lod_foreach_mdt(lod, i) {
1316 mdt = MDT_TGT(lod, i);
1317 LASSERT(mdt && mdt->ltd_mdt);
1318 rc = dt_statfs(env, mdt->ltd_mdt, &ost_sfs);
1322 sfs->os_files += ost_sfs.os_files;
1323 sfs->os_ffree += ost_sfs.os_ffree;
1324 lod_statfs_sum(sfs, &ost_sfs, &bs);
1326 lod_putref(lod, &lod->lod_mdt_descs);
1328 /* at some point we can check whether DoM is enabled and
1329 * decide how to account MDT space. for simplicity let's
1330 * just fallback to pre-DoM policy if any OST is alive */
1333 lod_getref(&lod->lod_ost_descs);
1334 lod_foreach_ost(lod, i) {
1335 ost = OST_TGT(lod, i);
1336 LASSERT(ost && ost->ltd_ost);
1337 rc = dt_statfs(env, ost->ltd_ost, &ost_sfs);
1339 if (rc || ost_sfs.os_bsize == 0)
1342 /* if only MDTs and DoM report MDT space,
1343 * otherwise only OST space */
1347 sfs->os_granted = 0;
1350 ost_sfs.os_bavail += ost_sfs.os_granted;
1351 lod_statfs_sum(sfs, &ost_sfs, &bs);
1352 LASSERTF(bs == ost_sfs.os_bsize, "%d != %d\n",
1353 (int)sfs->os_bsize, (int)ost_sfs.os_bsize);
1355 lod_putref(lod, &lod->lod_ost_descs);
1356 sfs->os_state |= OS_STATE_SUM;
1358 /* a single successful statfs should be enough */
1366 * Implementation of dt_device_operations::dt_trans_create() for LOD
1368 * Creates a transaction using local (to this node) OSD.
1370 * see include/dt_object.h for the details.
1372 static struct thandle *lod_trans_create(const struct lu_env *env,
1373 struct dt_device *dt)
1377 th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
1387 * Implementation of dt_device_operations::dt_trans_start() for LOD
1389 * Starts the set of local transactions using the targets involved
1390 * in declare phase. Initial support for the distributed transactions.
1392 * see include/dt_object.h for the details.
1394 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
1397 return top_trans_start(env, dt2lod_dev(dt)->lod_child, th);
1400 static int lod_trans_cb_add(struct thandle *th,
1401 struct dt_txn_commit_cb *dcb)
1403 struct top_thandle *top_th = container_of(th, struct top_thandle,
1405 return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
1409 * add noop update to the update records
1411 * Add noop updates to the update records, which is only used in
1414 * \param[in] env execution environment
1415 * \param[in] dt dt device of lod
1416 * \param[in] th thandle
1417 * \param[in] count the count of update records to be added.
1419 * \retval 0 if adding succeeds.
1420 * \retval negative errno if adding fails.
1422 static int lod_add_noop_records(const struct lu_env *env,
1423 struct dt_device *dt, struct thandle *th,
1426 struct top_thandle *top_th;
1427 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1431 top_th = container_of(th, struct top_thandle, tt_super);
1432 if (top_th->tt_multiple_thandle == NULL)
1436 for (i = 0; i < count; i++) {
1437 rc = update_record_pack(noop, th, fid);
1445 * Implementation of dt_device_operations::dt_trans_stop() for LOD
1447 * Stops the set of local transactions using the targets involved
1448 * in declare phase. Initial support for the distributed transactions.
1450 * see include/dt_object.h for the details.
1452 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
1455 if (OBD_FAIL_CHECK(OBD_FAIL_SPLIT_UPDATE_REC)) {
1458 rc = lod_add_noop_records(env, dt, th, 5000);
1462 return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
1466 * Implementation of dt_device_operations::dt_conf_get() for LOD
1468 * Currently returns the configuration provided by the local OSD.
1470 * see include/dt_object.h for the details.
1472 static void lod_conf_get(const struct lu_env *env,
1473 const struct dt_device *dev,
1474 struct dt_device_param *param)
1476 dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1480 * Implementation of dt_device_operations::dt_sync() for LOD
1482 * Syncs all known OST targets. Very very expensive and used
1483 * rarely by LFSCK now. Should not be used in general.
1485 * see include/dt_object.h for the details.
1487 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1489 struct lod_device *lod = dt2lod_dev(dev);
1490 struct lod_ost_desc *ost;
1491 struct lod_mdt_desc *mdt;
1496 lod_getref(&lod->lod_ost_descs);
1497 lod_foreach_ost(lod, i) {
1498 ost = OST_TGT(lod, i);
1499 LASSERT(ost && ost->ltd_ost);
1500 if (!ost->ltd_active)
1502 rc = dt_sync(env, ost->ltd_ost);
1504 if (rc != -ENOTCONN) {
1505 CERROR("%s: can't sync ost %u: %d\n",
1506 lod2obd(lod)->obd_name, i, rc);
1512 lod_putref(lod, &lod->lod_ost_descs);
1517 lod_getref(&lod->lod_mdt_descs);
1518 lod_foreach_mdt(lod, i) {
1519 mdt = MDT_TGT(lod, i);
1520 LASSERT(mdt && mdt->ltd_mdt);
1521 if (!mdt->ltd_active)
1523 rc = dt_sync(env, mdt->ltd_mdt);
1525 if (rc != -ENOTCONN) {
1526 CERROR("%s: can't sync mdt %u: %d\n",
1527 lod2obd(lod)->obd_name, i, rc);
1533 lod_putref(lod, &lod->lod_mdt_descs);
1536 rc = dt_sync(env, lod->lod_child);
1542 * Implementation of dt_device_operations::dt_ro() for LOD
1544 * Turns local OSD read-only, used for the testing only.
1546 * see include/dt_object.h for the details.
1548 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1550 return dt_ro(env, dt2lod_dev(dev)->lod_child);
1554 * Implementation of dt_device_operations::dt_commit_async() for LOD
1556 * Asks local OSD to commit sooner.
1558 * see include/dt_object.h for the details.
1560 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1562 return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1565 static const struct dt_device_operations lod_dt_ops = {
1566 .dt_root_get = lod_root_get,
1567 .dt_statfs = lod_statfs,
1568 .dt_trans_create = lod_trans_create,
1569 .dt_trans_start = lod_trans_start,
1570 .dt_trans_stop = lod_trans_stop,
1571 .dt_conf_get = lod_conf_get,
1572 .dt_sync = lod_sync,
1574 .dt_commit_async = lod_commit_async,
1575 .dt_trans_cb_add = lod_trans_cb_add,
1579 * Connect to a local OSD.
1581 * Used to connect to the local OSD at mount. OSD name is taken from the
1582 * configuration command passed. This connection is used to identify LU
1583 * site and pin the OSD from early removal.
1585 * \param[in] env LU environment provided by the caller
1586 * \param[in] lod lod device
1587 * \param[in] cfg configuration command to apply
1589 * \retval 0 on success
1590 * \retval negative negated errno on error
1592 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1593 struct lustre_cfg *cfg)
1595 struct obd_connect_data *data = NULL;
1596 struct obd_device *obd;
1597 char *nextdev = NULL, *p, *s;
1602 LASSERT(lod->lod_child_exp == NULL);
1604 /* compatibility hack: we still use old config logs
1605 * which specify LOV, but we need to learn underlying
1606 * OSD device, which is supposed to be:
1607 * <fsname>-MDTxxxx-osd
1609 * 2.x MGS generates lines like the following:
1610 * #03 (176)lov_setup 0:lustre-MDT0000-mdtlov 1:(struct lov_desc)
1611 * 1.8 MGS generates lines like the following:
1612 * #03 (168)lov_setup 0:lustre-mdtlov 1:(struct lov_desc)
1614 * we use "-MDT" to differentiate 2.x from 1.8 */
1616 if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
1617 len = strlen(p) + 6;
1618 OBD_ALLOC(nextdev, len);
1619 if (nextdev == NULL)
1620 GOTO(out, rc = -ENOMEM);
1623 s = strstr(nextdev, "-mdtlov");
1624 if (unlikely(s == NULL)) {
1625 CERROR("unable to parse device name %s\n",
1626 lustre_cfg_string(cfg, 0));
1627 GOTO(out, rc = -EINVAL);
1630 if (strstr(nextdev, "-MDT")) {
1635 strcpy(s, "-MDT0000-osd");
1638 CERROR("unable to parse device name %s\n",
1639 lustre_cfg_string(cfg, 0));
1640 GOTO(out, rc = -EINVAL);
1643 OBD_ALLOC_PTR(data);
1645 GOTO(out, rc = -ENOMEM);
1647 obd = class_name2obd(nextdev);
1649 CERROR("can not locate next device: %s\n", nextdev);
1650 GOTO(out, rc = -ENOTCONN);
1653 data->ocd_connect_flags = OBD_CONNECT_VERSION;
1654 data->ocd_version = LUSTRE_VERSION_CODE;
1656 rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
1659 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1663 lod->lod_dt_dev.dd_lu_dev.ld_site =
1664 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
1665 LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
1666 lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
1672 OBD_FREE(nextdev, len);
1677 * Allocate and initialize target table.
1679 * A helper function to initialize the target table and allocate
1680 * a bitmap of the available targets.
1682 * \param[in] ltd target's table to initialize
1684 * \retval 0 on success
1685 * \retval negative negated errno on error
1687 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
1689 mutex_init(<d->ltd_mutex);
1690 init_rwsem(<d->ltd_rw_sem);
1692 /* the OST array and bitmap are allocated/grown dynamically as OSTs are
1693 * added to the LOD, see lod_add_device() */
1694 ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
1695 if (ltd->ltd_tgt_bitmap == NULL)
1698 ltd->ltd_tgts_size = 32;
1701 ltd->ltd_death_row = 0;
1702 ltd->ltd_refcount = 0;
1707 * Initialize LOD device at setup.
1709 * Initializes the given LOD device using the original configuration command.
1710 * The function initiates a connection to the local OSD and initializes few
1711 * internal structures like pools, target tables, etc.
1713 * \param[in] env LU environment provided by the caller
1714 * \param[in] lod lod device
1715 * \param[in] ldt not used
1716 * \param[in] cfg configuration command
1718 * \retval 0 on success
1719 * \retval negative negated errno on error
1721 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
1722 struct lu_device_type *ldt, struct lustre_cfg *cfg)
1724 struct dt_device_param ddp;
1725 struct obd_device *obd;
1729 obd = class_name2obd(lustre_cfg_string(cfg, 0));
1731 CERROR("Cannot find obd with name %s\n",
1732 lustre_cfg_string(cfg, 0));
1736 obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1737 lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
1738 lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
1739 lod->lod_dt_dev.dd_ops = &lod_dt_ops;
1741 rc = lod_connect_to_osd(env, lod, cfg);
1745 dt_conf_get(env, &lod->lod_dt_dev, &ddp);
1746 lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
1747 lod->lod_dom_max_stripesize = (1ULL << 20); /* 1Mb as default value */
1749 /* setup obd to be used with old lov code */
1750 rc = lod_pools_init(lod, cfg);
1752 GOTO(out_disconnect, rc);
1754 rc = lod_procfs_init(lod);
1756 GOTO(out_pools, rc);
1758 spin_lock_init(&lod->lod_lock);
1759 spin_lock_init(&lod->lod_connects_lock);
1760 lod_tgt_desc_init(&lod->lod_mdt_descs);
1761 lod_tgt_desc_init(&lod->lod_ost_descs);
1766 lod_pools_fini(lod);
1768 obd_disconnect(lod->lod_child_exp);
1773 * Implementation of lu_device_type_operations::ldto_device_free() for LOD
1775 * Releases the memory allocated for LOD device.
1777 * see include/lu_object.h for the details.
1779 static struct lu_device *lod_device_free(const struct lu_env *env,
1780 struct lu_device *lu)
1782 struct lod_device *lod = lu2lod_dev(lu);
1783 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1786 if (atomic_read(&lu->ld_ref) > 0 &&
1787 !cfs_hash_is_empty(lu->ld_site->ls_obj_hash)) {
1788 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1789 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
1791 LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %p\n", lu);
1792 dt_device_fini(&lod->lod_dt_dev);
1798 * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
1800 * Allocates LOD device and calls the helpers to initialize it.
1802 * see include/lu_object.h for the details.
1804 static struct lu_device *lod_device_alloc(const struct lu_env *env,
1805 struct lu_device_type *type,
1806 struct lustre_cfg *lcfg)
1808 struct lod_device *lod;
1809 struct lu_device *lu_dev;
1813 lu_dev = ERR_PTR(-ENOMEM);
1817 lu_dev = lod2lu_dev(lod);
1818 dt_device_init(&lod->lod_dt_dev, type);
1819 rc = lod_init0(env, lod, type, lcfg);
1821 lod_device_free(env, lu_dev);
1822 lu_dev = ERR_PTR(rc);
1829 static void lod_avoid_guide_fini(struct lod_avoid_guide *lag)
1831 if (lag->lag_oss_avoid_array)
1832 OBD_FREE(lag->lag_oss_avoid_array,
1833 sizeof(__u32) * lag->lag_oaa_size);
1834 if (lag->lag_ost_avoid_bitmap)
1835 CFS_FREE_BITMAP(lag->lag_ost_avoid_bitmap);
1839 * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
1841 * Releases the internal resources used by LOD device.
1843 * see include/lu_object.h for the details.
1845 static struct lu_device *lod_device_fini(const struct lu_env *env,
1846 struct lu_device *d)
1848 struct lod_device *lod = lu2lod_dev(d);
1852 lod_pools_fini(lod);
1854 lod_procfs_fini(lod);
1856 rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
1858 CERROR("%s:can not fini ost descs %d\n",
1859 lod2obd(lod)->obd_name, rc);
1861 rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
1863 CERROR("%s:can not fini mdt descs %d\n",
1864 lod2obd(lod)->obd_name, rc);
1870 * Implementation of obd_ops::o_connect() for LOD
1872 * Used to track all the users of this specific LOD device,
1873 * so the device stays up until the last user disconnected.
1875 * \param[in] env LU environment provided by the caller
1876 * \param[out] exp export the caller will be using to access LOD
1877 * \param[in] obd OBD device representing LOD device
1878 * \param[in] cluuid unique identifier of the caller
1879 * \param[in] data not used
1880 * \param[in] localdata not used
1882 * \retval 0 on success
1883 * \retval negative negated errno on error
1885 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1886 struct obd_device *obd, struct obd_uuid *cluuid,
1887 struct obd_connect_data *data, void *localdata)
1889 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1890 struct lustre_handle conn;
1894 CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1896 rc = class_connect(&conn, obd, cluuid);
1900 *exp = class_conn2export(&conn);
1902 spin_lock(&lod->lod_connects_lock);
1903 lod->lod_connects++;
1904 /* at the moment we expect the only user */
1905 LASSERT(lod->lod_connects == 1);
1906 spin_unlock(&lod->lod_connects_lock);
1913 * Implementation of obd_ops::o_disconnect() for LOD
1915 * When the caller doesn't need to use this LOD instance, it calls
1916 * obd_disconnect() and LOD releases corresponding export/reference count.
1917 * Once all the users gone, LOD device is released.
1919 * \param[in] exp export provided to the caller in obd_connect()
1921 * \retval 0 on success
1922 * \retval negative negated errno on error
1924 static int lod_obd_disconnect(struct obd_export *exp)
1926 struct obd_device *obd = exp->exp_obd;
1927 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1928 int rc, release = 0;
1931 /* Only disconnect the underlying layers on the final disconnect. */
1932 spin_lock(&lod->lod_connects_lock);
1933 lod->lod_connects--;
1934 if (lod->lod_connects != 0) {
1935 /* why should there be more than 1 connect? */
1936 spin_unlock(&lod->lod_connects_lock);
1937 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1941 spin_unlock(&lod->lod_connects_lock);
1943 /* the last user of lod has gone, let's release the device */
1947 rc = class_disconnect(exp); /* bz 9811 */
1949 if (rc == 0 && release)
1950 class_manual_cleanup(obd);
1954 LU_KEY_INIT(lod, struct lod_thread_info);
1956 static void lod_key_fini(const struct lu_context *ctx,
1957 struct lu_context_key *key, void *data)
1959 struct lod_thread_info *info = data;
1960 struct lod_layout_component *lds =
1961 info->lti_def_striping.lds_def_comp_entries;
1963 /* allocated in lod_get_lov_ea
1964 * XXX: this is overload, a tread may have such store but used only
1965 * once. Probably better would be pool of such stores per LOD.
1967 if (info->lti_ea_store) {
1968 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1969 info->lti_ea_store = NULL;
1970 info->lti_ea_store_size = 0;
1972 lu_buf_free(&info->lti_linkea_buf);
1975 lod_free_def_comp_entries(&info->lti_def_striping);
1977 if (info->lti_comp_size > 0)
1978 OBD_FREE(info->lti_comp_idx,
1979 info->lti_comp_size * sizeof(__u32));
1981 lod_avoid_guide_fini(&info->lti_avoid);
1986 /* context key: lod_thread_key */
1987 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1989 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1991 static struct lu_device_type_operations lod_device_type_ops = {
1992 .ldto_init = lod_type_init,
1993 .ldto_fini = lod_type_fini,
1995 .ldto_start = lod_type_start,
1996 .ldto_stop = lod_type_stop,
1998 .ldto_device_alloc = lod_device_alloc,
1999 .ldto_device_free = lod_device_free,
2001 .ldto_device_fini = lod_device_fini
2004 static struct lu_device_type lod_device_type = {
2005 .ldt_tags = LU_DEVICE_DT,
2006 .ldt_name = LUSTRE_LOD_NAME,
2007 .ldt_ops = &lod_device_type_ops,
2008 .ldt_ctx_tags = LCT_MD_THREAD,
2012 * Implementation of obd_ops::o_get_info() for LOD
2014 * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
2015 * the caller binary status whether LOD has seen connection to any OST target.
2016 * It will also check if the MDT update log context being initialized (if
2019 * \param[in] env LU environment provided by the caller
2020 * \param[in] exp export of the caller
2021 * \param[in] keylen len of the key
2022 * \param[in] key the key
2023 * \param[in] vallen not used
2024 * \param[in] val not used
2026 * \retval 0 if a connection was seen
2027 * \retval -EAGAIN if LOD isn't running yet or no
2028 * connection has been seen yet
2029 * \retval -EINVAL if not supported key is requested
2031 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
2032 __u32 keylen, void *key, __u32 *vallen, void *val)
2036 if (KEY_IS(KEY_OSP_CONNECTED)) {
2037 struct obd_device *obd = exp->exp_obd;
2038 struct lod_device *d;
2039 struct lod_tgt_desc *tgt;
2043 if (!obd->obd_set_up || obd->obd_stopping)
2046 d = lu2lod_dev(obd->obd_lu_dev);
2047 lod_getref(&d->lod_ost_descs);
2048 lod_foreach_ost(d, i) {
2049 tgt = OST_TGT(d, i);
2050 LASSERT(tgt && tgt->ltd_tgt);
2051 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
2053 /* one healthy device is enough */
2057 lod_putref(d, &d->lod_ost_descs);
2059 lod_getref(&d->lod_mdt_descs);
2060 lod_foreach_mdt(d, i) {
2061 struct llog_ctxt *ctxt;
2063 tgt = MDT_TGT(d, i);
2064 LASSERT(tgt != NULL);
2065 LASSERT(tgt->ltd_tgt != NULL);
2066 if (!tgt->ltd_active)
2069 ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
2070 LLOG_UPDATELOG_ORIG_CTXT);
2072 CDEBUG(D_INFO, "%s: %s is not ready.\n",
2074 tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
2078 if (ctxt->loc_handle == NULL) {
2079 CDEBUG(D_INFO, "%s: %s is not ready.\n",
2081 tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
2083 llog_ctxt_put(ctxt);
2086 llog_ctxt_put(ctxt);
2088 lod_putref(d, &d->lod_mdt_descs);
2096 static int lod_obd_set_info_async(const struct lu_env *env,
2097 struct obd_export *exp,
2098 __u32 keylen, void *key,
2099 __u32 vallen, void *val,
2100 struct ptlrpc_request_set *set)
2102 struct obd_device *obd = class_exp2obd(exp);
2103 struct lod_device *d;
2104 struct lod_tgt_desc *tgt;
2111 set = ptlrpc_prep_set();
2116 d = lu2lod_dev(obd->obd_lu_dev);
2117 lod_getref(&d->lod_ost_descs);
2118 lod_foreach_ost(d, i) {
2119 tgt = OST_TGT(d, i);
2120 LASSERT(tgt && tgt->ltd_tgt);
2121 if (!tgt->ltd_active)
2124 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2126 if (rc2 != 0 && rc == 0)
2129 lod_putref(d, &d->lod_ost_descs);
2131 lod_getref(&d->lod_mdt_descs);
2132 lod_foreach_mdt(d, i) {
2133 tgt = MDT_TGT(d, i);
2134 LASSERT(tgt && tgt->ltd_tgt);
2135 if (!tgt->ltd_active)
2137 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2139 if (rc2 != 0 && rc == 0)
2142 lod_putref(d, &d->lod_mdt_descs);
2146 rc2 = ptlrpc_set_wait(env, set);
2147 if (rc2 == 0 && rc == 0)
2149 ptlrpc_set_destroy(set);
2154 static struct obd_ops lod_obd_device_ops = {
2155 .o_owner = THIS_MODULE,
2156 .o_connect = lod_obd_connect,
2157 .o_disconnect = lod_obd_disconnect,
2158 .o_get_info = lod_obd_get_info,
2159 .o_set_info_async = lod_obd_set_info_async,
2160 .o_pool_new = lod_pool_new,
2161 .o_pool_rem = lod_pool_remove,
2162 .o_pool_add = lod_pool_add,
2163 .o_pool_del = lod_pool_del,
2166 static struct obd_type sym;
2168 static int __init lod_init(void)
2170 struct dentry *symlink;
2171 struct obd_type *type;
2172 struct kobject *kobj;
2176 rc = lu_kmem_init(lod_caches);
2180 rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
2181 LUSTRE_LOD_NAME, &lod_device_type);
2183 lu_kmem_fini(lod_caches);
2187 /* create "lov" entry for compatibility purposes */
2189 dname.len = strlen(dname.name);
2190 dname.hash = ll_full_name_hash(debugfs_lustre_root, dname.name,
2192 symlink = d_lookup(debugfs_lustre_root, &dname);
2194 symlink = debugfs_create_dir(dname.name, debugfs_lustre_root);
2195 if (IS_ERR_OR_NULL(symlink)) {
2196 rc = symlink ? PTR_ERR(symlink) : -ENOMEM;
2199 sym.typ_debugfs_entry = symlink;
2204 kobj = kset_find_obj(lustre_kset, dname.name);
2210 kobj = class_setup_tunables(dname.name);
2213 if (sym.typ_debugfs_entry)
2214 ldebugfs_remove(&sym.typ_debugfs_entry);
2217 sym.typ_kobj = kobj;
2220 type = class_search_type(LUSTRE_LOV_NAME);
2221 if (type != NULL && type->typ_procroot != NULL)
2224 type = class_search_type(LUSTRE_LOD_NAME);
2225 type->typ_procsym = lprocfs_register("lov", proc_lustre_root,
2227 if (IS_ERR(type->typ_procsym)) {
2228 CERROR("lod: can't create compat entry \"lov\": %d\n",
2229 (int)PTR_ERR(type->typ_procsym));
2230 type->typ_procsym = NULL;
2236 static void __exit lod_exit(void)
2238 ldebugfs_remove(&sym.typ_debugfs_entry);
2239 kobject_put(sym.typ_kobj);
2240 class_unregister_type(LUSTRE_LOD_NAME);
2241 lu_kmem_fini(lod_caches);
2244 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2245 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
2246 MODULE_VERSION(LUSTRE_VERSION_STRING);
2247 MODULE_LICENSE("GPL");
2249 module_init(lod_init);
2250 module_exit(lod_exit);