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/
31 * lustre/lod/lod_dev.c
33 * Lustre Logical Object Device
35 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
36 * Author: Mikhail Pershin <mike.pershin@intel.com>
39 * The Logical Object Device (LOD) layer manages access to striped
40 * objects (both regular files and directories). It implements the DT
41 * device and object APIs and is responsible for creating, storing,
42 * and loading striping information as an extended attribute of the
43 * underlying OSD object. LOD is the server side analog of the LOV and
44 * LMV layers on the client side.
46 * Metadata LU object stack (layers of the same compound LU object,
47 * all have the same FID):
57 * During LOD object initialization the localness or remoteness of the
58 * object FID dictates the choice between OSD and OSP.
60 * An LOD object (file or directory) with N stripes (each has a
67 * S0 S1 S2 S3 S(N-1) OS[DP] objects, seen as DT objects by LOD
69 * When upper layers must access an object's stripes (which are
70 * themselves OST or MDT LU objects) LOD finds these objects by their
71 * FIDs and stores them as an array of DT object pointers on the
72 * object. Declarations and operations on LOD objects are received by
73 * LOD (as DT object operations) and performed on the underlying
74 * OS[DP] object and (as needed) on the stripes. From the perspective
75 * of LOD, a stripe-less file (created by mknod() or open with
76 * O_LOV_DELAY_CREATE) is an object which does not yet have stripes,
77 * while a non-striped directory (created by mkdir()) is an object
78 * which will never have stripes.
80 * The LOD layer also implements a small subset of the OBD device API
81 * to support MDT stack initialization and finalization (an MDD device
82 * connects and disconnects itself to and from the underlying LOD
83 * device), and pool management. In turn LOD uses the OBD device API
84 * to connect it self to the underlying OSD, and to connect itself to
85 * OSP devices representing the MDTs and OSTs that bear the stripes of
89 #define DEBUG_SUBSYSTEM S_MDS
91 #include <linux/kthread.h>
92 #include <obd_class.h>
93 #include <md_object.h>
94 #include <lustre_fid.h>
95 #include <uapi/linux/lustre/lustre_param.h>
96 #include <uapi/linux/lustre/lustre_ioctl.h>
97 #include <lustre_update.h>
98 #include <lustre_log.h>
99 #include <lustre_lmv.h>
100 #include <llog_swab.h>
102 #include "lod_internal.h"
104 static const char lod_update_log_name[] = "update_log";
105 static const char lod_update_log_dir_name[] = "update_log_dir";
108 * Lookup target by FID.
110 * Lookup MDT/OST target index by FID. Type of the target can be
113 * \param[in] env LU environment provided by the caller
114 * \param[in] lod lod device
116 * \param[out] tgt result target index
117 * \param[in] type expected type of the target:
118 * LU_SEQ_RANGE_{MDT,OST,ANY}
120 * \retval 0 on success
121 * \retval negative negated errno on error
123 int lod_fld_lookup(const struct lu_env *env, struct lod_device *lod,
124 const struct lu_fid *fid, u32 *tgt, int *type)
126 struct lu_seq_range range = { 0 };
127 struct lu_server_fld *server_fld;
132 if (!fid_is_sane(fid)) {
133 CERROR("%s: invalid FID "DFID"\n", lod2obd(lod)->obd_name,
138 if (fid_is_idif(fid)) {
139 *tgt = fid_idif_ost_idx(fid);
140 *type = LU_SEQ_RANGE_OST;
144 if (fid_is_update_log(fid) || fid_is_update_log_dir(fid)) {
146 *type = LU_SEQ_RANGE_MDT;
150 if (!lod->lod_initialized || (!fid_seq_in_fldb(fid_seq(fid)))) {
151 LASSERT(lu_site2seq(lod2lu_dev(lod)->ld_site) != NULL);
153 *tgt = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
154 *type = LU_SEQ_RANGE_MDT;
158 server_fld = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_server_fld;
162 fld_range_set_type(&range, *type);
163 rc = fld_server_lookup(env, server_fld, fid_seq(fid), &range);
167 *tgt = range.lsr_index;
168 *type = range.lsr_flags;
170 CDEBUG(D_INFO, "%s: got tgt %x for sequence: %#llx\n",
171 lod2obd(lod)->obd_name, *tgt, fid_seq(fid));
176 /* Slab for OSD object allocation */
177 struct kmem_cache *lod_object_kmem;
179 /* Slab for dt_txn_callback */
180 struct kmem_cache *lod_txn_callback_kmem;
181 static struct lu_kmem_descr lod_caches[] = {
183 .ckd_cache = &lod_object_kmem,
184 .ckd_name = "lod_obj",
185 .ckd_size = sizeof(struct lod_object)
188 .ckd_cache = &lod_txn_callback_kmem,
189 .ckd_name = "lod_txn_callback",
190 .ckd_size = sizeof(struct dt_txn_callback)
197 static struct lu_device *lod_device_fini(const struct lu_env *env,
198 struct lu_device *d);
201 * Implementation of lu_device_operations::ldo_object_alloc() for LOD
203 * Allocates and initializes LOD's slice in the given object.
205 * see include/lu_object.h for the details.
207 static struct lu_object *lod_object_alloc(const struct lu_env *env,
208 const struct lu_object_header *hdr,
209 struct lu_device *dev)
211 struct lod_object *lod_obj;
212 struct lu_object *lu_obj;
216 OBD_SLAB_ALLOC_PTR_GFP(lod_obj, lod_object_kmem, GFP_NOFS);
218 RETURN(ERR_PTR(-ENOMEM));
220 mutex_init(&lod_obj->ldo_layout_mutex);
221 lu_obj = lod2lu_obj(lod_obj);
222 dt_object_init(&lod_obj->ldo_obj, NULL, dev);
223 lod_obj->ldo_obj.do_ops = &lod_obj_ops;
224 lu_obj->lo_ops = &lod_lu_obj_ops;
230 * Process the config log for all sub device.
232 * The function goes through all the targets in the given table
233 * and apply given configuration command on to the targets.
234 * Used to cleanup the targets at unmount.
236 * \param[in] env LU environment provided by the caller
237 * \param[in] lod lod device
238 * \param[in] ltd target's table to go through
239 * \param[in] lcfg configuration command to apply
241 * \retval 0 on success
242 * \retval negative negated errno on error
244 static int lod_sub_process_config(const struct lu_env *env,
245 struct lod_device *lod,
246 struct lod_tgt_descs *ltd,
247 struct lustre_cfg *lcfg)
249 struct lu_device *next;
250 struct lu_tgt_desc *tgt;
254 ltd_foreach_tgt(ltd, tgt) {
257 LASSERT(tgt && tgt->ltd_tgt);
258 next = &tgt->ltd_tgt->dd_lu_dev;
259 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
261 CERROR("%s: error cleaning up LOD index %u: cmd %#x : rc = %d\n",
262 lod2obd(lod)->obd_name, tgt->ltd_index,
263 lcfg->lcfg_command, rc1);
267 lod_putref(lod, ltd);
271 struct lod_recovery_data {
272 struct lod_device *lrd_lod;
273 struct lod_tgt_desc *lrd_ltd;
274 struct task_struct **lrd_task;
276 struct lu_env lrd_env;
277 struct completion *lrd_started;
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;
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: rc = %d\n",
321 lod2obd(lrd->lrd_lod)->obd_name, index,
322 PLOGID(&llh->lgh_id), 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 PLOGID(&llh->lgh_id), rec->lrh_index);
333 lut = lod2lu_dev(lrd->lrd_lod)->ld_site->ls_tgt;
335 if (obd_mdt_recovery_abort(lut->lut_obd))
338 return insert_update_records_to_replay_list(lut->lut_tdtd,
339 (struct llog_update_record *)rec,
343 /* retain old catalog, create new catalog and update catlist */
344 static int lod_sub_recreate_llog(const struct lu_env *env,
345 struct lod_device *lod, struct dt_device *dt,
348 struct lod_thread_info *lti = lod_env_info(env);
349 struct llog_ctxt *ctxt;
350 struct llog_handle *lgh;
351 struct llog_catid *cid = <i->lti_cid;
352 struct lu_fid *fid = <i->lti_fid;
353 struct obd_device *obd;
357 lu_update_log_fid(fid, index);
358 rc = lodname2mdt_index(lod2obd(lod)->obd_name, (__u32 *)&index);
362 rc = llog_osd_get_cat_list(env, dt, index, 1, NULL, fid);
364 CERROR("%s: can't access update_log: rc = %d\n",
365 lod2obd(lod)->obd_name, rc);
369 obd = dt->dd_lu_dev.ld_obd;
370 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
371 LASSERT(ctxt != NULL);
372 if (ctxt->loc_handle) {
373 /* retain old catalog */
374 llog_retain(env, ctxt->loc_handle);
375 llog_cat_close(env, ctxt->loc_handle);
376 LASSERT(!ctxt->loc_handle);
379 ctxt->loc_flags |= LLOG_CTXT_FLAG_NORMAL_FID;
380 ctxt->loc_chunk_size = LLOG_MIN_CHUNK_SIZE * 4;
381 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
385 LASSERT(lgh != NULL);
386 rc = llog_init_handle(env, lgh, LLOG_F_IS_CAT, NULL);
390 cid->lci_logid = lgh->lgh_id;
391 rc = llog_osd_put_cat_list(env, dt, index, 1, cid, fid);
395 ctxt->loc_handle = lgh;
397 CDEBUG(D_INFO, "%s: recreate catalog "DFID"\n",
398 obd->obd_name, PLOGID(&cid->lci_logid));
401 llog_cat_close(env, lgh);
407 /* retain update catalog and llogs, and create a new catalog */
408 static int lod_sub_cancel_llog(const struct lu_env *env,
409 struct lod_device *lod, struct dt_device *dt,
412 struct llog_ctxt *ctxt;
415 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
416 LLOG_UPDATELOG_ORIG_CTXT);
420 if (ctxt->loc_handle) {
421 LCONSOLE(D_INFO, "%s: cancel update llog "DFID"\n",
422 dt->dd_lu_dev.ld_obd->obd_name,
423 PLOGID(&ctxt->loc_handle->lgh_id));
424 /* set retention on logs to simplify reclamation */
425 llog_process_or_fork(env, ctxt->loc_handle, llog_cat_retain_cb,
428 /* retain old catalog and create a new one */
429 lod_sub_recreate_llog(env, lod, dt, index);
435 * recovery thread for update log
437 * Start recovery thread and prepare the sub llog, then it will retrieve
438 * the update records from the correpondent MDT and do recovery.
440 * \param[in] arg pointer to the recovery data
442 * \retval 0 if recovery succeeds
443 * \retval negative errno if recovery failed.
445 static int lod_sub_recovery_thread(void *arg)
447 struct lod_recovery_data *lrd = arg;
448 struct lod_device *lod = lrd->lrd_lod;
449 struct dt_device *dt;
450 struct llog_ctxt *ctxt = NULL;
451 struct lu_env *env = &lrd->lrd_env;
452 struct lu_target *lut;
453 struct lu_tgt_desc *mdt = NULL;
454 struct lu_device *top_device;
461 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
462 atomic_inc(&lut->lut_tdtd->tdtd_recovery_threads_count);
466 dt = lrd->lrd_ltd->ltd_tgt;
468 start = ktime_get_real_seconds();
469 complete(lrd->lrd_started);
473 if (unlikely(CFS_FAIL_PRECHECK(OBD_FAIL_TGT_RECOVERY_CONNECT)) &&
475 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_RECOVERY_CONNECT, cfs_fail_val);
478 rc = lod_sub_prep_llog(env, lod, dt, lrd->lrd_idx);
481 if (!rc && !lod->lod_child->dd_rdonly) {
482 /* Process the recovery record */
483 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
484 LLOG_UPDATELOG_ORIG_CTXT);
485 LASSERT(ctxt != NULL);
486 LASSERT(ctxt->loc_handle != NULL);
488 rc = llog_cat_process(env, ctxt->loc_handle,
489 lod_process_recovery_updates, lrd, 0, 0);
492 top_device = lod->lod_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
493 if (rc < 0 && dt != lod->lod_child &&
494 !obd_mdt_recovery_abort(top_device->ld_obd)) {
496 /* remote update llog is shorter than expected from
497 * local header. Cached copy could be de-synced during
498 * recovery, trust remote llog data
500 CDEBUG(D_HA, "%s update log data de-sync\n",
501 dt->dd_lu_dev.ld_obd->obd_name);
503 } else if (rc == -ETIMEDOUT || rc == -EAGAIN || rc == -EIO) {
505 * the remote target might failover at the same time,
509 if (ctxt->loc_handle)
510 llog_cat_close(env, ctxt->loc_handle);
515 CDEBUG(D_HA, "%s get update log failed %d, retry\n",
516 dt->dd_lu_dev.ld_obd->obd_name, rc);
523 CERROR("%s: get update log duration %lld, retries %d, failed: rc = %d\n",
524 dt->dd_lu_dev.ld_obd->obd_name,
525 ktime_get_real_seconds() - start, retries, rc);
526 /* abort MDT recovery of this target, but not all targets,
527 * because recovery still has chance to succeed.
529 if (!obd_mdt_recovery_abort(top_device->ld_obd))
530 lod_sub_cancel_llog(env, lod, dt, lrd->lrd_idx);
533 "%s retrieved update log, duration %lld, retries %d\n",
534 dt->dd_lu_dev.ld_obd->obd_name,
535 ktime_get_real_seconds() - start, retries);
538 spin_lock(&lod->lod_lock);
540 lod->lod_child_got_update_log = 1;
542 lrd->lrd_ltd->ltd_got_update_log = 1;
544 if (!lod->lod_child_got_update_log) {
545 spin_unlock(&lod->lod_lock);
549 lod_foreach_mdt(lod, mdt) {
550 if (!mdt->ltd_got_update_log) {
551 spin_unlock(&lod->lod_lock);
555 lut->lut_tdtd->tdtd_replay_ready = 1;
556 spin_unlock(&lod->lod_lock);
558 CDEBUG(D_HA, "%s got update logs from all MDTs.\n",
559 lut->lut_obd->obd_name);
560 wake_up(&lut->lut_obd->obd_next_transno_waitq);
564 atomic_dec(&lut->lut_tdtd->tdtd_recovery_threads_count);
565 wake_up(&lut->lut_tdtd->tdtd_recovery_threads_waitq);
566 if (xchg(lrd->lrd_task, NULL) == NULL)
567 /* Someone is waiting for us to finish, need
568 * to synchronize cleanly.
570 wait_var_event(lrd, kthread_should_stop());
577 * finish sub llog context
579 * Stop update recovery thread for the sub device, then cleanup the
580 * correspondent llog ctxt.
582 * \param[in] env execution environment
583 * \param[in] lod lod device to do update recovery
584 * \param[in] thread recovery thread on this sub device
586 void lod_sub_fini_llog(const struct lu_env *env,
587 struct dt_device *dt, struct task_struct **thread)
589 struct obd_device *obd;
590 struct llog_ctxt *ctxt;
591 struct task_struct *task = NULL;
595 obd = dt->dd_lu_dev.ld_obd;
596 CDEBUG(D_INFO, "%s: finish sub llog\n", obd->obd_name);
597 /* Wait for recovery thread to complete */
599 task = xchg(thread, NULL);
603 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
607 if (ctxt->loc_handle)
608 llog_cat_close(env, ctxt->loc_handle);
610 llog_cleanup(env, ctxt);
616 * Extract MDT target index from a device name.
618 * a helper function to extract index from the given device name
619 * like "fsname-MDTxxxx-mdtlov"
621 * \param[in] lodname device name
622 * \param[out] mdt_index extracted index
624 * \retval 0 on success
625 * \retval -EINVAL if the name is invalid
627 int lodname2mdt_index(char *lodname, u32 *mdt_index)
630 const char *ptr, *tmp;
633 /* 1.8 configs don't have "-MDT0000" at the end */
634 ptr = strstr(lodname, "-MDT");
640 ptr = strrchr(lodname, '-');
643 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
647 if (strncmp(ptr, "-mdtlov", 7) != 0) {
649 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
653 if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
655 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
659 if (strncmp(ptr - 8, "-MDT", 4) != 0) {
661 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
665 rc = target_name2index(ptr - 7, &index, &tmp);
666 if (rc < 0 || rc & LDD_F_SV_ALL || *tmp != '-') {
668 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
676 * Init sub llog context
678 * Setup update llog ctxt for update recovery threads, then start the
679 * recovery thread (lod_sub_recovery_thread) to read update llog from
680 * the correspondent MDT to do update recovery.
682 * \param[in] env execution environment
683 * \param[in] lod lod device to do update recovery
684 * \param[in] dt sub dt device for which the recovery thread is
686 * \retval 0 if initialization succeeds.
687 * \retval negative errno if initialization fails.
689 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
690 struct dt_device *dt)
692 struct obd_device *obd;
693 struct lod_recovery_data *lrd = NULL;
694 DECLARE_COMPLETION_ONSTACK(started);
695 struct task_struct **taskp;
696 struct task_struct *task;
697 struct lod_tgt_desc *subtgt = NULL;
704 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &master_index);
712 if (lod->lod_child == dt) {
713 taskp = &lod->lod_child_recovery_task;
714 index = master_index;
716 struct lu_tgt_desc *mdt;
718 lod_foreach_mdt(lod, mdt) {
719 if (mdt->ltd_tgt == dt) {
720 index = mdt->ltd_index;
725 LASSERT(subtgt != NULL);
726 taskp = &subtgt->ltd_recovery_task;
729 CDEBUG(D_INFO, "%s init sub log %s\n", lod2obd(lod)->obd_name,
730 dt->dd_lu_dev.ld_obd->obd_name);
732 lrd->lrd_ltd = subtgt;
733 lrd->lrd_task = taskp;
734 lrd->lrd_idx = index;
735 lrd->lrd_started = &started;
737 obd = dt->dd_lu_dev.ld_obd;
738 obd->obd_lvfs_ctxt.dt = dt;
739 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
740 NULL, &llog_common_cat_ops);
742 CERROR("%s: cannot setup updatelog llog: rc = %d\n",
747 rc = lu_env_init(&lrd->lrd_env, LCT_LOCAL | LCT_MD_THREAD);
749 CERROR("%s: can't initialize env: rc = %d\n",
750 lod2obd(lod)->obd_name, rc);
754 /* Start the recovery thread */
755 task = kthread_create(lod_sub_recovery_thread, lrd, "lod%04x_rec%04x",
756 master_index, index);
759 CERROR("%s: cannot start recovery thread: rc = %d\n",
761 lu_env_fini(&lrd->lrd_env);
765 wake_up_process(task);
766 wait_for_completion(&started);
770 lod_sub_fini_llog(env, dt, taskp);
777 * Stop sub recovery thread
779 * Stop sub recovery thread on all subs.
781 * \param[in] env execution environment
782 * \param[in] lod lod device to do update recovery
784 static void lod_sub_stop_recovery_threads(const struct lu_env *env,
785 struct lod_device *lod)
787 struct task_struct *task;
788 struct lu_tgt_desc *mdt;
791 * Stop the update log commit cancel threads and finish master
794 task = xchg(&lod->lod_child_recovery_task, NULL);
798 lod_getref(&lod->lod_mdt_descs);
799 lod_foreach_mdt(lod, mdt) {
800 task = xchg(&mdt->ltd_recovery_task, NULL);
804 lod_putref(lod, &lod->lod_mdt_descs);
808 * finish all sub llog
810 * cleanup all of sub llog ctxt on the LOD.
812 * \param[in] env execution environment
813 * \param[in] lod lod device to do update recovery
815 static void lod_sub_fini_all_llogs(const struct lu_env *env,
816 struct lod_device *lod)
818 struct lu_tgt_desc *mdt;
821 * Stop the update log commit cancel threads and finish master
824 lod_sub_fini_llog(env, lod->lod_child,
825 &lod->lod_child_recovery_task);
826 lod_getref(&lod->lod_mdt_descs);
827 lod_foreach_mdt(lod, mdt)
828 lod_sub_fini_llog(env, mdt->ltd_tgt,
829 &mdt->ltd_recovery_task);
830 lod_putref(lod, &lod->lod_mdt_descs);
833 static char *lod_show_update_logs_retrievers(void *data, int *size, int *count)
835 struct lod_device *lod = (struct lod_device *)data;
836 struct lu_target *lut = lod2lu_dev(lod)->ld_site->ls_tgt;
837 struct lu_tgt_desc *mdt = NULL;
843 *count = atomic_read(&lut->lut_tdtd->tdtd_recovery_threads_count);
849 *size = 5 * *count + 1;
850 OBD_ALLOC(buf, *size);
855 memset(buf, 0, *size);
857 if (!lod->lod_child_got_update_log) {
858 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &i);
859 LASSERTF(rc == 0, "Fail to parse target index: rc = %d\n", rc);
861 rc = scnprintf(buf + len, *size - len, " %04x", i);
868 lod_foreach_mdt(lod, mdt) {
869 if (!mdt->ltd_got_update_log) {
870 rc = scnprintf(buf + len, *size - len, " %04x",
872 if (unlikely(rc <= 0))
884 * Prepare distribute txn
886 * Prepare distribute txn structure for LOD
888 * \param[in] env execution environment
889 * \param[in] lod_device LOD device
891 * \retval 0 if preparation succeeds.
892 * \retval negative errno if preparation fails.
894 static int lod_prepare_distribute_txn(const struct lu_env *env,
895 struct lod_device *lod)
897 struct target_distribute_txn_data *tdtd;
898 struct lu_target *lut;
903 /* Init update recovery data */
908 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
909 tdtd->tdtd_dt = &lod->lod_dt_dev;
910 rc = distribute_txn_init(env, lut, tdtd,
911 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id);
914 CERROR("%s: cannot init distribute txn: rc = %d\n",
915 lod2obd(lod)->obd_name, rc);
920 tdtd->tdtd_show_update_logs_retrievers =
921 lod_show_update_logs_retrievers;
922 tdtd->tdtd_show_retrievers_cbdata = lod;
924 lut->lut_tdtd = tdtd;
930 * Finish distribute txn
932 * Release the resource holding by distribute txn, i.e. stop distribute
935 * \param[in] env execution environment
936 * \param[in] lod lod device
938 static void lod_fini_distribute_txn(const struct lu_env *env,
939 struct lod_device *lod)
941 struct lu_target *lut;
943 lut = lod2lu_dev(lod)->ld_site->ls_tgt;
944 target_recovery_fini(lut->lut_obd);
948 distribute_txn_fini(env, lut->lut_tdtd);
950 OBD_FREE_PTR(lut->lut_tdtd);
951 lut->lut_tdtd = NULL;
955 * Implementation of lu_device_operations::ldo_process_config() for LOD
957 * The method is called by the configuration subsystem during setup,
958 * cleanup and when the configuration changes. The method processes
959 * few specific commands like adding/removing the targets, changing
960 * the runtime parameters.
962 * \param[in] env LU environment provided by the caller
963 * \param[in] dev lod device
964 * \param[in] lcfg configuration command to apply
966 * \retval 0 on success
967 * \retval negative negated errno on error
969 * The examples are below.
971 * Add osc config log:
972 * marker 20 (flags=0x01, v2.2.49.56) lustre-OST0001 'add osc'
973 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nidxxx
974 * attach 0:lustre-OST0001-osc-MDT0001 1:osc 2:lustre-MDT0001-mdtlov_UUID
975 * setup 0:lustre-OST0001-osc-MDT0001 1:lustre-OST0001_UUID 2:nid
976 * lov_modify_tgts add 0:lustre-MDT0001-mdtlov 1:lustre-OST0001_UUID 2:1 3:1
977 * marker 20 (flags=0x02, v2.2.49.56) lustre-OST0001 'add osc'
979 * Add mdc config log:
980 * marker 10 (flags=0x01, v2.2.49.56) lustre-MDT0000 'add osp'
981 * add_uuid nid=192.168.122.162@tcp(0x20000c0a87aa2) 0: 1:nid
982 * attach 0:lustre-MDT0000-osp-MDT0001 1:osp 2:lustre-MDT0001-mdtlov_UUID
983 * setup 0:lustre-MDT0000-osp-MDT0001 1:lustre-MDT0000_UUID 2:nid
984 * modify_mdc_tgts add 0:lustre-MDT0001 1:lustre-MDT0000_UUID 2:0 3:1
985 * marker 10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID 'add osp'
987 static int lod_process_config(const struct lu_env *env,
988 struct lu_device *dev,
989 struct lustre_cfg *lcfg)
991 struct lod_device *lod = lu2lod_dev(dev);
992 struct lu_device *next = &lod->lod_child->dd_lu_dev;
998 switch (lcfg->lcfg_command) {
999 case LCFG_LOV_DEL_OBD:
1000 case LCFG_LOV_ADD_INA:
1001 case LCFG_LOV_ADD_OBD:
1002 case LCFG_ADD_MDC: {
1007 * lov_modify_tgts add 0:lov_mdsA 1:osp 2:0 3:1
1008 * modify_mdc_tgts add 0:lustre-MDT0001
1009 * 1:lustre-MDT0001-mdc0002
1012 arg1 = lustre_cfg_string(lcfg, 1);
1014 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
1015 GOTO(out, rc = -EINVAL);
1016 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1017 GOTO(out, rc = -EINVAL);
1019 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
1022 rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
1027 rc = lod_add_device(env, lod, arg1, index, gen,
1028 mdt_index, LUSTRE_OSC_NAME, 1);
1029 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
1031 rc = lod_add_device(env, lod, arg1, index, gen,
1032 mdt_index, LUSTRE_MDC_NAME, 1);
1033 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
1034 /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
1036 rc = lod_add_device(env, lod, arg1, index, gen,
1037 mdt_index, LUSTRE_OSC_NAME, 0);
1039 rc = lod_del_device(env, lod, &lod->lod_ost_descs,
1047 struct obd_device *obd;
1052 * Check if it is activate/deactivate mdc
1053 * lustre-MDTXXXX-osp-MDTXXXX.active=1
1055 param = lustre_cfg_buf(lcfg, 1);
1056 if (strstr(param, "osp") && strstr(param, ".active=")) {
1057 struct lod_tgt_desc *sub_tgt = NULL;
1058 struct lu_tgt_desc *mdt;
1062 ptr = strstr(param, ".");
1064 obd = class_name2obd(param);
1066 CERROR("%s: can not find %s: rc = %d\n",
1067 lod2obd(lod)->obd_name, param, -EINVAL);
1072 lod_foreach_mdt(lod, mdt) {
1073 if (mdt->ltd_tgt->dd_lu_dev.ld_obd == obd) {
1080 CERROR("%s: can not find %s: rc = %d\n",
1081 lod2obd(lod)->obd_name, param, -EINVAL);
1087 tmp = strstr(param, "=");
1090 struct llog_ctxt *ctxt;
1092 obd = sub_tgt->ltd_tgt->dd_lu_dev.ld_obd;
1093 ctxt = llog_get_context(obd,
1094 LLOG_UPDATELOG_ORIG_CTXT);
1096 rc = llog_setup(env, obd, &obd->obd_olg,
1097 LLOG_UPDATELOG_ORIG_CTXT,
1098 NULL, &llog_common_cat_ops);
1102 llog_ctxt_put(ctxt);
1104 rc = lod_sub_prep_llog(env, lod,
1106 sub_tgt->ltd_index);
1107 sub_tgt->ltd_active = !rc;
1109 lod_sub_fini_llog(env, sub_tgt->ltd_tgt,
1111 sub_tgt->ltd_active = 0;
1117 if (strstr(param, PARAM_LOD) != NULL)
1118 count = class_modify_config(lcfg, PARAM_LOD,
1119 &lod->lod_dt_dev.dd_kobj);
1121 count = class_modify_config(lcfg, PARAM_LOV,
1122 &lod->lod_dt_dev.dd_kobj);
1123 rc = count > 0 ? 0 : count;
1126 case LCFG_PRE_CLEANUP: {
1127 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1128 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1129 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_RECOVERY_CONNECT, cfs_fail_val * 2);
1130 next = &lod->lod_child->dd_lu_dev;
1131 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1133 CDEBUG(D_HA, "%s: can't process %u: %d\n",
1134 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1136 lod_sub_stop_recovery_threads(env, lod);
1137 lod_fini_distribute_txn(env, lod);
1138 lod_sub_fini_all_llogs(env, lod);
1141 case LCFG_CLEANUP: {
1142 if (lod->lod_md_root) {
1143 dt_object_put(env, &lod->lod_md_root->ldo_obj);
1144 lod->lod_md_root = NULL;
1148 * do cleanup on underlying storage only when
1149 * all OSPs are cleaned up, as they use that OSD as well
1151 lu_dev_del_linkage(dev->ld_site, dev);
1152 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1153 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1154 next = &lod->lod_child->dd_lu_dev;
1155 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1157 CERROR("%s: can't process %u: rc = %d\n",
1158 lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1160 rc = obd_disconnect(lod->lod_child_exp);
1162 CERROR("error in disconnect from storage: rc = %d\n",
1167 CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
1168 lcfg->lcfg_command);
1178 * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
1180 * The method is called once the recovery is complete. This implementation
1181 * distributes the notification to all the known targets.
1183 * see include/lu_object.h for the details
1185 static int lod_recovery_complete(const struct lu_env *env,
1186 struct lu_device *dev)
1188 struct lod_device *lod = lu2lod_dev(dev);
1189 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1190 struct lod_tgt_desc *tgt;
1195 LASSERT(lod->lod_recovery_completed == 0);
1196 lod->lod_recovery_completed = 1;
1198 rc = next->ld_ops->ldo_recovery_complete(env, next);
1200 lod_getref(&lod->lod_ost_descs);
1201 if (lod->lod_ost_descs.ltd_tgts_size > 0) {
1202 lod_foreach_ost(lod, tgt) {
1203 LASSERT(tgt && tgt->ltd_tgt);
1204 next = &tgt->ltd_tgt->dd_lu_dev;
1205 rc = next->ld_ops->ldo_recovery_complete(env, next);
1207 CERROR("%s: can't complete recovery on #%d: rc = %d\n",
1208 lod2obd(lod)->obd_name, tgt->ltd_index,
1212 lod_putref(lod, &lod->lod_ost_descs);
1217 * Init update logs on all sub device
1219 * LOD initialize update logs on all of sub devices. Because the initialization
1220 * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
1221 * lod_object_init(), this API has to be called after LOD is initialized.
1222 * \param[in] env execution environment
1223 * \param[in] lod lod device
1225 * \retval 0 if update log is initialized successfully.
1226 * \retval negative errno if initialization fails.
1228 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
1230 struct lu_tgt_desc *mdt;
1236 * llog must be setup after LOD is initialized, because llog
1237 * initialization include FLD lookup
1239 LASSERT(lod->lod_initialized);
1241 /* Init the llog in its own stack */
1242 rc = lod_sub_init_llog(env, lod, lod->lod_child);
1246 lod_foreach_mdt(lod, mdt) {
1247 lod_sub_init_llog(env, lod, mdt->ltd_tgt);
1253 #define UPDATE_LOG_MAX_AGE (30 * 24 * 60 * 60) /* 30 days, in sec */
1255 static int lod_update_log_stale(const struct lu_env *env, struct dt_object *dto,
1258 struct lu_attr *attr = &lod_env_info(env)->lti_attr;
1259 struct llog_log_hdr *hdr;
1264 rc = dt_attr_get(env, dto, attr);
1268 if (!(attr->la_valid & (LA_CTIME | LA_SIZE)))
1271 /* by default update log ctime is not set */
1272 if (attr->la_ctime == 0)
1275 /* update log not expired yet */
1276 if (attr->la_ctime + UPDATE_LOG_MAX_AGE > ktime_get_real_seconds())
1279 if (attr->la_size == 0)
1282 rc = dt_read(env, dto, buf, &off);
1286 hdr = (struct llog_log_hdr *)buf->lb_buf;
1287 if (LLOG_REC_HDR_NEEDS_SWABBING(&hdr->llh_hdr))
1288 lustre_swab_llog_hdr(hdr);
1289 /* log header is sane and flag LLOG_F_MAX_AGE|LLOG_F_RM_ON_ERR is set */
1290 if (rc >= sizeof(*hdr) &&
1291 hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC &&
1292 (hdr->llh_flags & (LLOG_F_MAX_AGE | LLOG_F_RM_ON_ERR)) ==
1293 (LLOG_F_MAX_AGE | LLOG_F_RM_ON_ERR))
1300 * Reclaim stale update log.
1302 * When update log is canceld (upon recovery abort), it's not destroy, but
1303 * canceled from catlist, and set ctime and LLOG_F_MAX_AGE|LLOG_F_RM_ON_ERR,
1304 * which is kept for debug. If it expired (more than UPDATE_LOG_MAX_AGE seconds
1305 * passed), destroy it to save space.
1307 static int lod_update_log_gc(const struct lu_env *env, struct lod_device *lod,
1308 struct dt_object *dir, struct dt_object *dto,
1311 struct dt_device *dt = lod->lod_child;
1316 th = dt_trans_create(env, dt);
1318 RETURN(PTR_ERR(th));
1320 rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
1322 GOTO(out_trans, rc);
1324 rc = dt_declare_destroy(env, dto, th);
1326 GOTO(out_trans, rc);
1328 rc = dt_trans_start_local(env, dt, th);
1330 GOTO(out_trans, rc);
1332 rc = dt_delete(env, dir, (const struct dt_key *)name, th);
1334 GOTO(out_trans, rc);
1336 rc = dt_destroy(env, dto, th);
1337 GOTO(out_trans, rc);
1339 dt_trans_stop(env, dt, th);
1344 /* reclaim stale update llogs under "update_log_dir" */
1345 static int lod_update_log_dir_gc(const struct lu_env *env,
1346 struct lod_device *lod,
1347 struct dt_object *dir)
1349 struct lod_thread_info *info = lod_env_info(env);
1350 struct lu_buf *buf = &info->lti_linkea_buf;
1351 struct lu_dirent *ent = (struct lu_dirent *)info->lti_key;
1352 struct lu_fid *fid = &info->lti_fid;
1354 const struct dt_it_ops *iops;
1355 struct dt_object *dto;
1360 if (unlikely(!dt_try_as_dir(env, dir, true)))
1363 lu_buf_alloc(buf, sizeof(struct llog_log_hdr));
1367 iops = &dir->do_index_ops->dio_it;
1368 it = iops->init(env, dir, LUDA_64BITHASH);
1370 GOTO(out, rc = PTR_ERR(it));
1372 rc = iops->load(env, it, 0);
1374 rc = iops->next(env, it);
1379 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1383 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1384 if (ent->lde_name[0] == '.') {
1385 if (ent->lde_namelen == 1)
1388 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
1392 fid_le_to_cpu(fid, &ent->lde_fid);
1393 dto = dt_locate(env, lod->lod_child, fid);
1397 buf->lb_len = sizeof(struct llog_log_hdr);
1398 if (lod_update_log_stale(env, dto, buf) == 1)
1399 lod_update_log_gc(env, lod, dir, dto, ent->lde_name);
1400 dt_object_put(env, dto);
1402 rc = iops->next(env, it);
1406 iops->fini(env, it);
1408 buf->lb_len = sizeof(struct llog_log_hdr);
1411 RETURN(rc > 0 ? 0 : rc);
1415 * Implementation of lu_device_operations::ldo_prepare() for LOD
1417 * see include/lu_object.h for the details.
1419 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
1420 struct lu_device *cdev)
1422 struct lod_device *lod = lu2lod_dev(cdev);
1423 struct lu_device *next = &lod->lod_child->dd_lu_dev;
1424 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1426 struct dt_object *root;
1427 struct dt_object *dto;
1432 rc = next->ld_ops->ldo_prepare(env, pdev, next);
1434 CERROR("%s: prepare bottom error: rc = %d\n",
1435 lod2obd(lod)->obd_name, rc);
1439 lod->lod_initialized = 1;
1441 rc = dt_root_get(env, lod->lod_child, fid);
1445 root = dt_locate(env, lod->lod_child, fid);
1447 RETURN(PTR_ERR(root));
1449 /* Create update log object */
1450 index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1451 lu_update_log_fid(fid, index);
1453 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1455 lod_update_log_name,
1458 GOTO(out_put, rc = PTR_ERR(dto));
1460 dt_object_put(env, dto);
1462 /* Create update log dir */
1463 lu_update_log_dir_fid(fid, index);
1464 dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1466 lod_update_log_dir_name,
1469 GOTO(out_put, rc = PTR_ERR(dto));
1471 lod_update_log_dir_gc(env, lod, dto);
1472 dt_object_put(env, dto);
1474 rc = lod_prepare_distribute_txn(env, lod);
1478 rc = lod_sub_init_llogs(env, lod);
1483 dt_object_put(env, root);
1489 * Implementation of lu_device_operations::ldo_fid_alloc() for LOD
1491 * Find corresponding device by passed parent and name, and allocate FID from
1494 * see include/lu_object.h for the details.
1496 static int lod_fid_alloc(const struct lu_env *env, struct lu_device *d,
1497 struct lu_fid *fid, struct lu_object *parent,
1498 const struct lu_name *name)
1500 struct lod_device *lod = lu2lod_dev(d);
1501 struct lod_object *lo = lu2lod_obj(parent);
1502 struct dt_device *next;
1507 /* if @parent is remote, we don't know whether its layout was changed,
1508 * always reload layout.
1510 if (lu_object_remote(parent))
1511 lod_striping_free(env, lo);
1513 rc = lod_striping_load(env, lo);
1517 if (lo->ldo_dir_stripe_count > 0 && name) {
1518 struct dt_object *stripe;
1521 idx = __lmv_name_to_stripe_index(lo->ldo_dir_hash_type,
1522 lo->ldo_dir_stripe_count,
1523 lo->ldo_dir_migrate_hash,
1524 lo->ldo_dir_migrate_offset,
1526 name->ln_namelen, true);
1530 stripe = lo->ldo_stripe[idx];
1531 if (!stripe || !dt_object_exists(stripe))
1534 next = lu2dt_dev(stripe->do_lu.lo_dev);
1536 next = lod->lod_child;
1539 rc = dt_fid_alloc(env, next, fid, parent, name);
1544 const struct lu_device_operations lod_lu_ops = {
1545 .ldo_object_alloc = lod_object_alloc,
1546 .ldo_process_config = lod_process_config,
1547 .ldo_recovery_complete = lod_recovery_complete,
1548 .ldo_prepare = lod_prepare,
1549 .ldo_fid_alloc = lod_fid_alloc,
1553 * Implementation of dt_device_operations::dt_root_get() for LOD
1555 * see include/dt_object.h for the details.
1557 static int lod_root_get(const struct lu_env *env,
1558 struct dt_device *dev, struct lu_fid *f)
1560 return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
1563 static void lod_statfs_sum(struct obd_statfs *sfs,
1564 struct obd_statfs *ost_sfs, int *bs)
1566 while (ost_sfs->os_bsize < *bs) {
1568 sfs->os_bsize >>= 1;
1569 sfs->os_bavail <<= 1;
1570 sfs->os_blocks <<= 1;
1571 sfs->os_bfree <<= 1;
1572 sfs->os_granted <<= 1;
1574 while (ost_sfs->os_bsize > *bs) {
1575 ost_sfs->os_bsize >>= 1;
1576 ost_sfs->os_bavail <<= 1;
1577 ost_sfs->os_blocks <<= 1;
1578 ost_sfs->os_bfree <<= 1;
1579 ost_sfs->os_granted <<= 1;
1581 sfs->os_bavail += ost_sfs->os_bavail;
1582 sfs->os_blocks += ost_sfs->os_blocks;
1583 sfs->os_bfree += ost_sfs->os_bfree;
1584 sfs->os_granted += ost_sfs->os_granted;
1588 * Implementation of dt_device_operations::dt_statfs() for LOD
1590 * see include/dt_object.h for the details.
1592 static int lod_statfs(const struct lu_env *env, struct dt_device *dev,
1593 struct obd_statfs *sfs, struct obd_statfs_info *info)
1595 struct lod_device *lod = dt2lod_dev(dev);
1596 struct lu_tgt_desc *tgt;
1597 struct obd_statfs ost_sfs;
1602 rc = dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
1611 sfs->os_granted = 0;
1613 lod_getref(&lod->lod_mdt_descs);
1614 lod_foreach_mdt(lod, tgt) {
1615 rc = dt_statfs(env, tgt->ltd_tgt, &ost_sfs);
1619 sfs->os_files += ost_sfs.os_files;
1620 sfs->os_ffree += ost_sfs.os_ffree;
1621 lod_statfs_sum(sfs, &ost_sfs, &bs);
1623 lod_putref(lod, &lod->lod_mdt_descs);
1626 * at some point we can check whether DoM is enabled and
1627 * decide how to account MDT space. for simplicity let's
1628 * just fallback to pre-DoM policy if any OST is alive
1630 lod_getref(&lod->lod_ost_descs);
1631 lod_foreach_ost(lod, tgt) {
1632 rc = dt_statfs(env, tgt->ltd_tgt, &ost_sfs);
1634 if (rc || ost_sfs.os_bsize == 0)
1638 * if only MDTs with DoM then report only MDT blocks,
1639 * otherwise show only OST blocks, and DoM is "free"
1644 sfs->os_granted = 0;
1646 ost_files += ost_sfs.os_files;
1647 ost_ffree += ost_sfs.os_ffree;
1648 ost_sfs.os_bavail += ost_sfs.os_granted;
1649 lod_statfs_sum(sfs, &ost_sfs, &bs);
1650 LASSERTF(bs == ost_sfs.os_bsize, "%d != %d\n",
1651 (int)sfs->os_bsize, (int)ost_sfs.os_bsize);
1653 lod_putref(lod, &lod->lod_ost_descs);
1654 sfs->os_state |= OS_STATFS_SUM;
1656 /* If we have _some_ OSTs, but don't have as many free objects on the
1657 * OSTs as inodes on the MDTs, reduce the reported number of inodes
1658 * to compensate, so that the "inodes in use" number is correct.
1659 * This should be kept in sync with ll_statfs_internal().
1661 if (ost_files && ost_ffree < sfs->os_ffree) {
1662 sfs->os_files = (sfs->os_files - sfs->os_ffree) + ost_ffree;
1663 sfs->os_ffree = ost_ffree;
1666 /* a single successful statfs should be enough */
1674 * Implementation of dt_device_operations::dt_trans_create() for LOD
1676 * Creates a transaction using local (to this node) OSD.
1678 * see include/dt_object.h for the details.
1680 static struct thandle *lod_trans_create(const struct lu_env *env,
1681 struct dt_device *dt)
1683 struct lod_thread_info *info = lod_env_info(env);
1686 th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
1692 /* initialize some lod_thread_info members */
1693 info->lti_obj_count = 0;
1698 /* distributed transaction failure may cause object missing or disconnected
1699 * directories, check space before transaction start.
1701 static int lod_trans_space_check(const struct lu_env *env,
1702 struct lod_device *lod,
1705 struct lod_thread_info *info = lod_env_info(env);
1706 struct obd_statfs *sfs = &info->lti_osfs;
1707 struct top_thandle *top_th = container_of(th, struct top_thandle,
1709 struct top_multiple_thandle *tmt = top_th->tt_multiple_thandle;
1710 struct sub_thandle *st;
1716 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
1717 struct dt_device *sub_dt;
1719 if (st->st_sub_th == NULL)
1722 if (st->st_sub_th == top_th->tt_master_sub_thandle)
1725 sub_dt = st->st_sub_th->th_dev;
1726 rc = dt_statfs(env, sub_dt, sfs);
1728 CDEBUG(D_INFO, "%s: fail - statfs error: rc = %d\n",
1729 sub_dt->dd_lu_dev.ld_obd->obd_name, rc);
1730 /* statfs may fail during recovery, skip check */
1731 if (!lod->lod_recovery_completed)
1736 if (unlikely(sfs->os_state &
1737 (OS_STATFS_ENOINO | OS_STATFS_ENOSPC))) {
1738 CDEBUG(D_INFO, "%s: fail - target state %x: rc = %d\n",
1739 sub_dt->dd_lu_dev.ld_obd->obd_name,
1740 sfs->os_state, -ENOSPC);
1749 * Implementation of dt_device_operations::dt_trans_start() for LOD
1751 * Starts the set of local transactions using the targets involved
1752 * in declare phase. Initial support for the distributed transactions.
1754 * see include/dt_object.h for the details.
1756 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
1759 struct lod_device *lod = dt2lod_dev(dt);
1761 if (lod->lod_dist_txn_check_space) {
1764 rc = lod_trans_space_check(env, lod, th);
1769 return top_trans_start(env, lod->lod_child, th);
1772 static int lod_trans_cb_add(struct thandle *th,
1773 struct dt_txn_commit_cb *dcb)
1775 struct top_thandle *top_th = container_of(th, struct top_thandle,
1777 return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
1781 * add noop update to the update records
1783 * Add noop updates to the update records, which is only used in
1786 * \param[in] env execution environment
1787 * \param[in] dt dt device of lod
1788 * \param[in] th thandle
1789 * \param[in] count the count of update records to be added.
1791 * \retval 0 if adding succeeds.
1792 * \retval negative errno if adding fails.
1794 static int lod_add_noop_records(const struct lu_env *env,
1795 struct dt_device *dt, struct thandle *th,
1798 struct top_thandle *top_th;
1799 struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1803 top_th = container_of(th, struct top_thandle, tt_super);
1804 if (!top_th->tt_multiple_thandle)
1808 for (i = 0; i < count; i++) {
1809 rc = update_record_pack(noop, th, fid);
1817 * Implementation of dt_device_operations::dt_trans_stop() for LOD
1819 * Stops the set of local transactions using the targets involved
1820 * in declare phase. Initial support for the distributed transactions.
1822 * see include/dt_object.h for the details.
1824 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
1827 if (CFS_FAIL_CHECK(OBD_FAIL_SPLIT_UPDATE_REC)) {
1830 rc = lod_add_noop_records(env, dt, th, 5000);
1834 return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
1838 * Implementation of dt_device_operations::dt_conf_get() for LOD
1840 * Currently returns the configuration provided by the local OSD.
1842 * see include/dt_object.h for the details.
1844 static void lod_conf_get(const struct lu_env *env,
1845 const struct dt_device *dev,
1846 struct dt_device_param *param)
1848 dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1852 * Implementation of dt_device_operations::dt_sync() for LOD
1854 * Syncs all known OST targets. Very very expensive and used
1855 * rarely by LFSCK now. Should not be used in general.
1857 * see include/dt_object.h for the details.
1859 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1861 struct lod_device *lod = dt2lod_dev(dev);
1862 struct lu_tgt_desc *tgt;
1867 lod_getref(&lod->lod_ost_descs);
1868 lod_foreach_ost(lod, tgt) {
1869 if (tgt->ltd_discon)
1871 rc = dt_sync(env, tgt->ltd_tgt);
1873 if (rc != -ENOTCONN) {
1874 CERROR("%s: can't sync ost %u: rc = %d\n",
1875 lod2obd(lod)->obd_name, tgt->ltd_index,
1882 lod_putref(lod, &lod->lod_ost_descs);
1887 lod_getref(&lod->lod_mdt_descs);
1888 lod_foreach_mdt(lod, tgt) {
1889 if (tgt->ltd_discon)
1891 rc = dt_sync(env, tgt->ltd_tgt);
1893 if (rc != -ENOTCONN) {
1894 CERROR("%s: can't sync mdt %u: rc = %d\n",
1895 lod2obd(lod)->obd_name, tgt->ltd_index,
1902 lod_putref(lod, &lod->lod_mdt_descs);
1905 rc = dt_sync(env, lod->lod_child);
1911 * Implementation of dt_device_operations::dt_ro() for LOD
1913 * Turns local OSD read-only, used for the testing only.
1915 * see include/dt_object.h for the details.
1917 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1919 return dt_ro(env, dt2lod_dev(dev)->lod_child);
1923 * Implementation of dt_device_operations::dt_commit_async() for LOD
1925 * Asks local OSD to commit sooner.
1927 * see include/dt_object.h for the details.
1929 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1931 return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1934 static const struct dt_device_operations lod_dt_ops = {
1935 .dt_root_get = lod_root_get,
1936 .dt_statfs = lod_statfs,
1937 .dt_trans_create = lod_trans_create,
1938 .dt_trans_start = lod_trans_start,
1939 .dt_trans_stop = lod_trans_stop,
1940 .dt_conf_get = lod_conf_get,
1941 .dt_sync = lod_sync,
1943 .dt_commit_async = lod_commit_async,
1944 .dt_trans_cb_add = lod_trans_cb_add,
1948 * Connect to a local OSD.
1950 * Used to connect to the local OSD at mount. OSD name is taken from the
1951 * configuration command passed. This connection is used to identify LU
1952 * site and pin the OSD from early removal.
1954 * \param[in] env LU environment provided by the caller
1955 * \param[in] lod lod device
1956 * \param[in] cfg configuration command to apply
1958 * \retval 0 on success
1959 * \retval negative negated errno on error
1961 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1962 struct lustre_cfg *cfg)
1964 struct obd_connect_data *data = NULL;
1965 struct obd_device *obd;
1966 char *nextdev = NULL, *p, *s;
1972 LASSERT(lod->lod_child_exp == NULL);
1975 * compatibility hack: we still use old config logs
1976 * which specify LOV, but we need to learn underlying
1977 * OSD device, which is supposed to be:
1978 * <fsname>-MDTxxxx-osd
1980 * 2.x MGS generates lines like the following:
1981 * #03 (176)lov_setup 0:lustre-MDT0000-mdtlov 1:(struct lov_desc)
1982 * 1.8 MGS generates lines like the following:
1983 * #03 (168)lov_setup 0:lustre-mdtlov 1:(struct lov_desc)
1985 * we use "-MDT" to differentiate 2.x from 1.8
1987 p = lustre_cfg_string(cfg, 0);
1988 if (p && strstr(p, "-mdtlov")) {
1989 len = strlen(p) + 6;
1990 OBD_ALLOC(nextdev, len);
1992 GOTO(out, rc = -ENOMEM);
1995 s = strstr(nextdev, "-mdtlov");
1997 CERROR("%s: unable to parse device name: rc = %d\n",
1998 lustre_cfg_string(cfg, 0), -EINVAL);
1999 GOTO(out, rc = -EINVAL);
2002 if (strstr(nextdev, "-MDT")) {
2007 strcpy(s, "-MDT0000-osd");
2010 CERROR("%s: unable to parse device name: rc = %d\n",
2011 lustre_cfg_string(cfg, 0), -EINVAL);
2012 GOTO(out, rc = -EINVAL);
2015 OBD_ALLOC_PTR(data);
2017 GOTO(out, rc = -ENOMEM);
2019 obd = class_name2obd(nextdev);
2021 CERROR("%s: can not locate next device: rc = %d\n",
2022 nextdev, -ENOTCONN);
2023 GOTO(out, rc = -ENOTCONN);
2026 data->ocd_connect_flags = OBD_CONNECT_VERSION;
2027 data->ocd_version = LUSTRE_VERSION_CODE;
2029 rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
2032 CERROR("%s: cannot connect to next dev: rc = %d\n",
2037 lod->lod_dt_dev.dd_lu_dev.ld_site =
2038 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
2039 LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
2040 lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
2046 OBD_FREE(nextdev, len);
2050 static int lod_lsfs_init(const struct lu_env *env, struct lod_device *d)
2052 struct obd_statfs sfs;
2055 rc = dt_statfs(env, d->lod_child, &sfs);
2057 CDEBUG(D_LAYOUT, "%s: failed to get OSD statfs, rc = %d\n",
2058 lod2obd(d)->obd_name, rc);
2062 /* udpate local OSD cached statfs data */
2063 spin_lock_init(&d->lod_lsfs_lock);
2064 d->lod_lsfs_age = ktime_get_seconds();
2065 d->lod_lsfs_total_mb = (sfs.os_blocks * sfs.os_bsize) >> 20;
2066 d->lod_lsfs_free_mb = (sfs.os_bfree * sfs.os_bsize) >> 20;
2071 * Initialize LOD device at setup.
2073 * Initializes the given LOD device using the original configuration command.
2074 * The function initiates a connection to the local OSD and initializes few
2075 * internal structures like pools, target tables, etc.
2077 * \param[in] env LU environment provided by the caller
2078 * \param[in] lod lod device
2079 * \param[in] ldt not used
2080 * \param[in] cfg configuration command
2082 * \retval 0 on success
2083 * \retval negative negated errno on error
2085 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
2086 struct lu_device_type *ldt, struct lustre_cfg *cfg)
2088 struct dt_device_param ddp;
2089 struct obd_device *obd;
2094 obd = class_name2obd(lustre_cfg_string(cfg, 0));
2097 CERROR("Cannot find obd with name '%s': rc = %d\n",
2098 lustre_cfg_string(cfg, 0), rc);
2102 obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
2103 lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
2104 lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
2105 lod->lod_dt_dev.dd_ops = &lod_dt_ops;
2107 rc = lod_connect_to_osd(env, lod, cfg);
2111 dt_conf_get(env, &lod->lod_dt_dev, &ddp);
2112 lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
2113 lod->lod_dom_stripesize_max_kb = (1ULL << 10); /* 1Mb is default */
2114 lod->lod_max_stripecount = 0;
2116 /* initialize local statfs cached values */
2117 rc = lod_lsfs_init(env, lod);
2119 GOTO(out_disconnect, rc);
2121 /* default threshold as half of total space, in MiB */
2122 lod->lod_dom_threshold_free_mb = lod->lod_lsfs_total_mb / 2;
2123 /* set default DoM stripe size based on free space amount */
2124 lod_dom_stripesize_recalc(lod);
2126 /* setup obd to be used with old lov code */
2127 rc = lod_pools_init(lod, cfg);
2129 GOTO(out_disconnect, rc);
2131 rc = lod_procfs_init(lod);
2133 GOTO(out_pools, rc);
2135 spin_lock_init(&lod->lod_lock);
2136 spin_lock_init(&lod->lod_connects_lock);
2137 lu_tgt_descs_init(&lod->lod_mdt_descs, true);
2138 lu_tgt_descs_init(&lod->lod_ost_descs, false);
2139 lu_qos_rr_init(&lod->lod_mdt_descs.ltd_qos.lq_rr);
2140 lu_qos_rr_init(&lod->lod_ost_descs.ltd_qos.lq_rr);
2141 lod->lod_dist_txn_check_space = 1;
2146 lod_pools_fini(lod);
2148 obd_disconnect(lod->lod_child_exp);
2153 * Implementation of lu_device_type_operations::ldto_device_free() for LOD
2155 * Releases the memory allocated for LOD device.
2157 * see include/lu_object.h for the details.
2159 static struct lu_device *lod_device_free(const struct lu_env *env,
2160 struct lu_device *lu)
2162 struct lod_device *lod = lu2lod_dev(lu);
2163 struct lu_device *next = &lod->lod_child->dd_lu_dev;
2167 if (atomic_read(&lu->ld_site->ls_obj_hash.nelems)) {
2168 lu_site_print(env, lu->ld_site, &lu->ld_ref, D_ERROR,
2171 LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %px\n", lu);
2172 dt_device_fini(&lod->lod_dt_dev);
2178 * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
2180 * Allocates LOD device and calls the helpers to initialize it.
2182 * see include/lu_object.h for the details.
2184 static struct lu_device *lod_device_alloc(const struct lu_env *env,
2185 struct lu_device_type *type,
2186 struct lustre_cfg *lcfg)
2188 struct lod_device *lod;
2189 struct lu_device *lu_dev;
2193 lu_dev = ERR_PTR(-ENOMEM);
2197 lu_dev = lod2lu_dev(lod);
2198 dt_device_init(&lod->lod_dt_dev, type);
2199 rc = lod_init0(env, lod, type, lcfg);
2201 lod_device_free(env, lu_dev);
2202 lu_dev = ERR_PTR(rc);
2209 static void lod_avoid_guide_fini(struct lod_avoid_guide *lag)
2211 if (lag->lag_oss_avoid_array)
2212 OBD_FREE_PTR_ARRAY(lag->lag_oss_avoid_array,
2214 bitmap_free(lag->lag_ost_avoid_bitmap);
2218 * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
2220 * Releases the internal resources used by LOD device.
2222 * see include/lu_object.h for the details.
2224 static struct lu_device *lod_device_fini(const struct lu_env *env,
2225 struct lu_device *d)
2227 struct lod_device *lod = lu2lod_dev(d);
2232 lod_pools_fini(lod);
2234 lod_procfs_fini(lod);
2236 rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs);
2238 CERROR("%s: can not fini ost descriptors: rc = %d\n",
2239 lod2obd(lod)->obd_name, rc);
2241 rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs);
2243 CERROR("%s: can not fini mdt descriptors: rc = %d\n",
2244 lod2obd(lod)->obd_name, rc);
2250 * Implementation of obd_ops::o_connect() for LOD
2252 * Used to track all the users of this specific LOD device,
2253 * so the device stays up until the last user disconnected.
2255 * \param[in] env LU environment provided by the caller
2256 * \param[out] exp export the caller will be using to access LOD
2257 * \param[in] obd OBD device representing LOD device
2258 * \param[in] cluuid unique identifier of the caller
2259 * \param[in] data not used
2260 * \param[in] localdata not used
2262 * \retval 0 on success
2263 * \retval negative negated errno on error
2265 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
2266 struct obd_device *obd, struct obd_uuid *cluuid,
2267 struct obd_connect_data *data, void *localdata)
2269 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
2270 struct lustre_handle conn;
2275 CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
2277 rc = class_connect(&conn, obd, cluuid);
2281 *exp = class_conn2export(&conn);
2283 spin_lock(&lod->lod_connects_lock);
2284 lod->lod_connects++;
2285 /* at the moment we expect the only user */
2286 LASSERT(lod->lod_connects == 1);
2287 spin_unlock(&lod->lod_connects_lock);
2294 * Implementation of obd_ops::o_disconnect() for LOD
2296 * When the caller doesn't need to use this LOD instance, it calls
2297 * obd_disconnect() and LOD releases corresponding export/reference count.
2298 * Once all the users gone, LOD device is released.
2300 * \param[in] exp export provided to the caller in obd_connect()
2302 * \retval 0 on success
2303 * \retval negative negated errno on error
2305 static int lod_obd_disconnect(struct obd_export *exp)
2307 struct obd_device *obd = exp->exp_obd;
2308 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
2309 int rc, release = 0;
2313 /* Only disconnect the underlying layers on the final disconnect. */
2314 spin_lock(&lod->lod_connects_lock);
2315 lod->lod_connects--;
2316 if (lod->lod_connects != 0) {
2317 /* why should there be more than 1 connect? */
2318 spin_unlock(&lod->lod_connects_lock);
2319 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
2323 spin_unlock(&lod->lod_connects_lock);
2325 /* the last user of lod has gone, let's release the device */
2329 rc = class_disconnect(exp); /* bz 9811 */
2331 if (rc == 0 && release)
2332 class_manual_cleanup(obd);
2336 LU_KEY_INIT(lod, struct lod_thread_info);
2338 static void lod_key_fini(const struct lu_context *ctx,
2339 struct lu_context_key *key, void *data)
2341 struct lod_thread_info *info = data;
2342 struct lod_layout_component *lds =
2343 info->lti_def_striping.lds_def_comp_entries;
2346 * allocated in lod_get_lov_ea
2347 * XXX: this is overload, a tread may have such store but used only
2348 * once. Probably better would be pool of such stores per LOD.
2350 if (info->lti_ea_store) {
2351 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
2352 info->lti_ea_store = NULL;
2353 info->lti_ea_store_size = 0;
2355 lu_buf_free(&info->lti_linkea_buf);
2358 lod_free_def_comp_entries(&info->lti_def_striping);
2360 if (info->lti_comp_size > 0)
2361 OBD_FREE_PTR_ARRAY(info->lti_comp_idx,
2362 info->lti_comp_size);
2364 lod_avoid_guide_fini(&info->lti_avoid);
2369 /* context key: lod_thread_key */
2370 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
2372 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
2374 static const struct lu_device_type_operations lod_device_type_ops = {
2375 .ldto_init = lod_type_init,
2376 .ldto_fini = lod_type_fini,
2378 .ldto_start = lod_type_start,
2379 .ldto_stop = lod_type_stop,
2381 .ldto_device_alloc = lod_device_alloc,
2382 .ldto_device_free = lod_device_free,
2384 .ldto_device_fini = lod_device_fini
2387 static struct lu_device_type lod_device_type = {
2388 .ldt_tags = LU_DEVICE_DT,
2389 .ldt_name = LUSTRE_LOD_NAME,
2390 .ldt_ops = &lod_device_type_ops,
2391 .ldt_ctx_tags = LCT_MD_THREAD,
2395 * Implementation of obd_ops::o_get_info() for LOD
2397 * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
2398 * the caller binary status whether LOD has seen connection to any OST target.
2399 * It will also check if the MDT update log context being initialized (if
2402 * \param[in] env LU environment provided by the caller
2403 * \param[in] exp export of the caller
2404 * \param[in] keylen len of the key
2405 * \param[in] key the key
2406 * \param[in] vallen not used
2407 * \param[in] val not used
2409 * \retval 0 if a connection was seen
2410 * \retval -EAGAIN if LOD isn't running yet or no
2411 * connection has been seen yet
2412 * \retval -EINVAL if not supported key is requested
2414 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
2415 u32 keylen, void *key, u32 *vallen, void *val)
2419 if (KEY_IS(KEY_OSP_CONNECTED)) {
2420 struct obd_device *obd = exp->exp_obd;
2421 struct lod_device *d;
2422 struct lod_tgt_desc *tgt;
2425 if (!obd->obd_set_up || obd->obd_stopping)
2428 d = lu2lod_dev(obd->obd_lu_dev);
2429 lod_getref(&d->lod_ost_descs);
2430 lod_foreach_ost(d, tgt) {
2431 rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
2433 /* one healthy device is enough */
2437 lod_putref(d, &d->lod_ost_descs);
2439 lod_getref(&d->lod_mdt_descs);
2440 lod_foreach_mdt(d, tgt) {
2441 struct llog_ctxt *ctxt;
2442 struct obd_device *ld = tgt->ltd_tgt->dd_lu_dev.ld_obd;
2444 if (!tgt->ltd_active)
2447 ctxt = llog_get_context(ld, LLOG_UPDATELOG_ORIG_CTXT);
2448 LASSERT(ctxt != NULL);
2449 if (!ctxt->loc_handle) {
2450 CDEBUG(D_INFO, "%s: %s is not ready(%p).\n",
2451 obd->obd_name, ld->obd_name, ctxt);
2452 llog_ctxt_put(ctxt);
2456 llog_ctxt_put(ctxt);
2458 lod_putref(d, &d->lod_mdt_descs);
2466 static int lod_obd_set_info_async(const struct lu_env *env,
2467 struct obd_export *exp,
2468 u32 keylen, void *key,
2469 u32 vallen, void *val,
2470 struct ptlrpc_request_set *set)
2472 struct obd_device *obd = class_exp2obd(exp);
2473 struct lod_device *d;
2474 struct lod_tgt_desc *tgt;
2482 set = ptlrpc_prep_set();
2487 d = lu2lod_dev(obd->obd_lu_dev);
2488 lod_getref(&d->lod_ost_descs);
2489 lod_foreach_ost(d, tgt) {
2490 if (tgt->ltd_discon)
2493 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2495 if (rc2 != 0 && rc == 0)
2498 lod_putref(d, &d->lod_ost_descs);
2500 lod_getref(&d->lod_mdt_descs);
2501 lod_foreach_mdt(d, tgt) {
2502 if (tgt->ltd_discon)
2505 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2507 if (rc2 != 0 && rc == 0)
2510 lod_putref(d, &d->lod_mdt_descs);
2514 rc2 = ptlrpc_set_wait(env, set);
2515 if (rc2 == 0 && rc == 0)
2517 ptlrpc_set_destroy(set);
2523 #define QMT0_DEV_NAME_LEN (LUSTRE_MAXFSNAME + sizeof("-QMT0000"))
2524 static struct obd_device *obd_find_qmt0(char *obd_name)
2526 char qmt_name[QMT0_DEV_NAME_LEN];
2527 struct obd_device *qmt = NULL;
2529 if (!server_name2fsname(obd_name, qmt_name, NULL)) {
2530 strlcat(qmt_name, "-QMT0000", QMT0_DEV_NAME_LEN);
2531 qmt = class_name2obd(qmt_name);
2537 /* Run QMT0000 pool operations only for MDT0000 */
2538 static inline bool lod_pool_need_qmt0(const char *obd_name)
2543 type = server_name2index(obd_name, &idx, NULL);
2545 return type == LDD_F_SV_TYPE_MDT && idx == 0;
2548 static int lod_pool_new_q(struct obd_device *obd, char *poolname)
2550 int err = lod_pool_new(obd, poolname);
2552 if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2553 obd = obd_find_qmt0(obd->obd_name);
2555 obd_pool_new(obd, poolname);
2561 static int lod_pool_remove_q(struct obd_device *obd, char *poolname,
2564 int err = lod_pool_remove(obd, poolname, ostname);
2566 if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2567 obd = obd_find_qmt0(obd->obd_name);
2569 obd_pool_rem(obd, poolname, ostname);
2575 static int lod_pool_add_q(struct obd_device *obd, char *poolname, char *ostname)
2577 int err = lod_pool_add(obd, poolname, ostname);
2579 if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2580 obd = obd_find_qmt0(obd->obd_name);
2582 obd_pool_add(obd, poolname, ostname);
2588 static int lod_pool_del_q(struct obd_device *obd, char *poolname)
2590 int err = lod_pool_del(obd, poolname);
2592 if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2593 obd = obd_find_qmt0(obd->obd_name);
2595 obd_pool_del(obd, poolname);
2601 static inline int lod_sub_print_llog(const struct lu_env *env,
2602 struct dt_device *dt, void *data)
2604 struct llog_ctxt *ctxt;
2608 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
2609 LLOG_UPDATELOG_ORIG_CTXT);
2613 if (ctxt->loc_handle) {
2614 struct llog_print_data *lprd = data;
2615 struct obd_ioctl_data *ioc_data = lprd->lprd_data;
2621 if (ioc_data->ioc_inllen1 > 0) {
2622 remains = ioc_data->ioc_inllen4 +
2623 round_up(ioc_data->ioc_inllen1, 8) +
2624 round_up(ioc_data->ioc_inllen2, 8) +
2625 round_up(ioc_data->ioc_inllen3, 8);
2627 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
2631 /* second iteration from jt_llog_print_iter() */
2633 GOTO(ctxt_put, rc = 0);
2635 out = ioc_data->ioc_bulk;
2636 ioc_data->ioc_inllen1 = 0;
2638 out = ioc_data->ioc_bulk + ioc_data->ioc_offset;
2639 remains = ioc_data->ioc_count;
2642 l = snprintf(out, remains, "%s [catalog]: "DFID"\n",
2643 ctxt->loc_obd->obd_name,
2644 PLOGID(&ctxt->loc_handle->lgh_id));
2648 CERROR("%s: not enough space for print log records: rc = %d\n",
2649 ctxt->loc_obd->obd_name, -LLOG_EEMPTY);
2650 GOTO(ctxt_put, rc = -LLOG_EEMPTY);
2653 ioc_data->ioc_offset += l;
2654 ioc_data->ioc_count = remains;
2656 rc = llog_process_or_fork(env, ctxt->loc_handle, llog_print_cb,
2661 llog_ctxt_put(ctxt);
2666 /* print update catalog and update logs FID of all sub devices */
2667 static int lod_llog_print(const struct lu_env *env, struct lod_device *lod,
2670 struct lod_tgt_desc *mdt;
2675 rc = lod_sub_print_llog(env, lod->lod_child, data);
2678 } else if (rc == -LLOG_EEMPTY) {
2681 CERROR("%s: llog_print failed: rc = %d\n",
2682 lod2obd(lod)->obd_name, rc);
2686 lod_getref(&lod->lod_mdt_descs);
2687 lod_foreach_mdt(lod, mdt) {
2688 rc = lod_sub_print_llog(env, mdt->ltd_tgt, data);
2691 } else if (rc == -LLOG_EEMPTY) {
2694 CERROR("%s: llog_print of MDT %u failed: rc = %d\n",
2695 lod2obd(lod)->obd_name, mdt->ltd_index, rc);
2699 lod_putref(lod, &lod->lod_mdt_descs);
2701 RETURN(rc ? rc : empty ? -LLOG_EEMPTY : 0);
2704 /* cancel update catalog from update catlist */
2705 static int lod_llog_cancel(const struct lu_env *env, struct lod_device *lod)
2707 struct lod_tgt_desc *tgt;
2712 rc = lodname2mdt_index(lod2obd(lod)->obd_name, (__u32 *)&index);
2716 rc = lod_sub_cancel_llog(env, lod, lod->lod_child, index);
2718 lod_getref(&lod->lod_mdt_descs);
2719 lod_foreach_mdt(lod, tgt) {
2720 LASSERT(tgt && tgt->ltd_tgt);
2721 rc2 = lod_sub_cancel_llog(env, lod, tgt->ltd_tgt,
2726 lod_putref(lod, &lod->lod_mdt_descs);
2731 static int lod_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2732 void *karg, void __user *uarg)
2734 struct obd_device *obd = exp->exp_obd;
2735 struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
2736 struct obd_ioctl_data *data;
2741 CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
2742 obd->obd_name, cmd, len, karg, uarg);
2743 if (unlikely(karg == NULL))
2744 RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "karg=NULL", -EINVAL));
2747 rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
2749 CERROR("%s: can't initialize env: rc = %d\n",
2750 lod2obd(lod)->obd_name, rc);
2755 case OBD_IOC_LLOG_PRINT: {
2756 struct llog_print_data lprd = {
2758 .lprd_raw = data->ioc_u32_1,
2762 logname = data->ioc_inlbuf1;
2763 if (strcmp(logname, lod_update_log_name) != 0) {
2765 CERROR("%s: llog iocontrol support %s only: rc = %d\n",
2766 lod2obd(lod)->obd_name, lod_update_log_name, rc);
2770 LASSERT(data->ioc_inllen1 > 0);
2771 rc = lod_llog_print(&env, lod, &lprd);
2774 case OBD_IOC_LLOG_CANCEL:
2775 rc = lod_llog_cancel(&env, lod);
2778 rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
2786 static const struct obd_ops lod_obd_device_ops = {
2787 .o_owner = THIS_MODULE,
2788 .o_connect = lod_obd_connect,
2789 .o_disconnect = lod_obd_disconnect,
2790 .o_get_info = lod_obd_get_info,
2791 .o_set_info_async = lod_obd_set_info_async,
2792 .o_pool_new = lod_pool_new_q,
2793 .o_pool_rem = lod_pool_remove_q,
2794 .o_pool_add = lod_pool_add_q,
2795 .o_pool_del = lod_pool_del_q,
2796 .o_iocontrol = lod_iocontrol,
2799 static int __init lod_init(void)
2801 struct obd_type *sym;
2804 rc = libcfs_setup();
2808 rc = lu_kmem_init(lod_caches);
2812 rc = class_register_type(&lod_obd_device_ops, NULL, true,
2813 LUSTRE_LOD_NAME, &lod_device_type);
2815 lu_kmem_fini(lod_caches);
2819 /* create "lov" entry for compatibility purposes */
2820 sym = class_add_symlinks(LUSTRE_LOV_NAME, true);
2823 /* does real "lov" already exist ? */
2831 static void __exit lod_exit(void)
2833 struct obd_type *sym = class_search_type(LUSTRE_LOV_NAME);
2835 /* if this was never fully initialized by the lov layer
2836 * then we are responsible for freeing this obd_type
2839 /* final put if we manage this obd type */
2840 if (sym->typ_sym_filter)
2841 kobject_put(&sym->typ_kobj);
2842 /* put reference taken by class_search_type */
2843 kobject_put(&sym->typ_kobj);
2846 class_unregister_type(LUSTRE_LOD_NAME);
2847 lu_kmem_fini(lod_caches);
2850 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2851 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
2852 MODULE_VERSION(LUSTRE_VERSION_STRING);
2853 MODULE_LICENSE("GPL");
2855 module_init(lod_init);
2856 module_exit(lod_exit);