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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2014, Intel Corporation.
26 * lustre/target/update_trans.c
28 * This file implements the update distribute transaction API.
30 * To manage the cross-MDT operation (distribute operation) transaction,
31 * the transaction will also be separated two layers on MD stack, top
32 * transaction and sub transaction.
34 * During the distribute operation, top transaction is created in the LOD
35 * layer, and represent the operation. Sub transaction is created by
36 * each OSD or OSP. Top transaction start/stop will trigger all of its sub
37 * transaction start/stop. Top transaction (the whole operation) is committed
38 * only all of its sub transaction are committed.
40 * there are three kinds of transactions
41 * 1. local transaction: All updates are in a single local OSD.
42 * 2. Remote transaction: All Updates are only in the remote OSD,
43 * i.e. locally all updates are in OSP.
44 * 3. Mixed transaction: Updates are both in local OSD and remote
47 * Author: Di Wang <di.wang@intel.com>
50 #define DEBUG_SUBSYSTEM S_CLASS
52 #include <linux/kthread.h>
53 #include <lu_target.h>
54 #include <lustre_log.h>
55 #include <lustre_update.h>
57 #include <obd_class.h>
58 #include <tgt_internal.h>
60 #include <tgt_internal.h>
62 * Dump top mulitple thandle
64 * Dump top multiple thandle and all of its sub thandle to the debug log.
66 * \param[in]mask debug mask
67 * \param[in]top_th top_thandle to be dumped
69 static void top_multiple_thandle_dump(struct top_multiple_thandle *tmt,
72 struct sub_thandle *st;
74 LASSERT(tmt->tmt_magic == TOP_THANDLE_MAGIC);
75 CDEBUG(mask, "%s tmt %p refcount %d committed %d result %d"
77 tmt->tmt_master_sub_dt ?
78 tmt->tmt_master_sub_dt->dd_lu_dev.ld_obd->obd_name :
80 tmt, atomic_read(&tmt->tmt_refcount), tmt->tmt_committed,
81 tmt->tmt_result, tmt->tmt_batchid);
83 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
84 CDEBUG(mask, "st %p obd %s committed %d sub_th %p "
85 " cookie "DOSTID": %u\n",
86 st, st->st_dt->dd_lu_dev.ld_obd->obd_name,
87 st->st_committed, st->st_sub_th,
88 POSTID(&st->st_cookie.lgc_lgl.lgl_oi),
89 st->st_cookie.lgc_index);
94 * Declare write update to sub device
96 * Declare Write updates llog records to the sub device during distribute
99 * \param[in] env execution environment
100 * \param[in] record update records being written
101 * \param[in] sub_th sub transaction handle
103 * \retval 0 if writing succeeds
104 * \retval negative errno if writing fails
106 static int sub_declare_updates_write(const struct lu_env *env,
107 struct llog_update_record *record,
108 struct thandle *sub_th)
110 struct llog_ctxt *ctxt;
111 struct dt_device *dt = sub_th->th_dev;
114 /* If ctxt is NULL, it means not need to write update,
115 * for example if the the OSP is used to connect to OST */
116 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
117 LLOG_UPDATELOG_ORIG_CTXT);
118 LASSERT(ctxt != NULL);
120 /* Not ready to record updates yet. */
121 if (ctxt->loc_handle == NULL) {
126 rc = llog_declare_add(env, ctxt->loc_handle, &record->lur_hdr,
135 * write update to sub device
137 * Write llog update record to the sub device during distribute
138 * transaction. If it succeeds, llog cookie of the record will be
139 * returned by @cookie.
141 * \param[in] env execution environment
142 * \param[in] record update records being written
143 * \param[in] sub_th sub transaction handle
144 * \param[out] cookie llog cookie of the update record.
146 * \retval 1 if writing succeeds
147 * \retval negative errno if writing fails
149 static int sub_updates_write(const struct lu_env *env,
150 struct llog_update_record *record,
151 struct thandle *sub_th,
152 struct llog_cookie *cookie)
154 struct dt_device *dt = sub_th->th_dev;
155 struct llog_ctxt *ctxt;
159 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
160 LLOG_UPDATELOG_ORIG_CTXT);
161 LASSERT(ctxt != NULL);
163 /* Not ready to record updates yet, usually happens
164 * in error handler path */
165 if (ctxt->loc_handle == NULL) {
170 /* Since the cross-MDT updates will includes both local
171 * and remote updates, the update ops count must > 1 */
172 LASSERT(record->lur_update_rec.ur_update_count > 1);
173 LASSERTF(record->lur_hdr.lrh_len == llog_update_record_size(record),
174 "lrh_len %u record_size %zu\n", record->lur_hdr.lrh_len,
175 llog_update_record_size(record));
177 rc = llog_add(env, ctxt->loc_handle, &record->lur_hdr,
181 CDEBUG(D_INFO, "%s: Add update log "DOSTID":%u.\n",
182 dt->dd_lu_dev.ld_obd->obd_name,
183 POSTID(&cookie->lgc_lgl.lgl_oi), cookie->lgc_index);
192 * Prepare the update records.
194 * Merge params and ops into the update records, then initializing
197 * During transaction execution phase, parameters and update ops
198 * are collected in two different buffers (see lod_updates_pack()),
199 * during transaction stop, it needs to be merged in one buffer,
200 * so it will be written in the update log.
202 * \param[in] env execution environment
203 * \param[in] tmt top_multiple_thandle for distribute txn
205 * \retval 0 if merging succeeds.
206 * \retval negaitive errno if merging fails.
208 static int prepare_writing_updates(const struct lu_env *env,
209 struct top_multiple_thandle *tmt)
211 struct thandle_update_records *tur = tmt->tmt_update_records;
212 struct llog_update_record *lur;
213 struct update_params *params;
217 if (tur == NULL || tur->tur_update_records == NULL ||
218 tur->tur_update_params == NULL)
221 lur = tur->tur_update_records;
222 /* Extends the update records buffer if needed */
223 params_size = update_params_size(tur->tur_update_params,
224 tur->tur_update_param_count);
225 LASSERT(lur->lur_update_rec.ur_param_count == 0);
226 update_size = llog_update_record_size(lur);
227 if (cfs_size_round(update_size + params_size) >
228 tur->tur_update_records_buf_size) {
231 rc = tur_update_records_extend(tur,
232 cfs_size_round(update_size + params_size));
236 lur = tur->tur_update_records;
239 params = update_records_get_params(&lur->lur_update_rec);
240 memcpy(params, tur->tur_update_params, params_size);
242 lur->lur_update_rec.ur_param_count = tur->tur_update_param_count;
243 lur->lur_update_rec.ur_batchid = tmt->tmt_batchid;
244 /* Init update record header */
245 lur->lur_hdr.lrh_len = llog_update_record_size(lur);
246 lur->lur_hdr.lrh_type = UPDATE_REC;
248 /* Dump updates for debugging purpose */
249 update_records_dump(&lur->lur_update_rec, D_INFO, true);
255 distribute_txn_commit_thread_running(struct lu_target *lut)
257 return lut->lut_tdtd_commit_thread.t_flags & SVC_RUNNING;
261 distribute_txn_commit_thread_stopped(struct lu_target *lut)
263 return lut->lut_tdtd_commit_thread.t_flags & SVC_STOPPED;
267 * Top thandle commit callback
269 * This callback will be called when all of sub transactions are committed.
271 * \param[in] th top thandle to be committed.
273 static void top_trans_committed_cb(struct top_multiple_thandle *tmt)
275 struct lu_target *lut;
278 LASSERT(atomic_read(&tmt->tmt_refcount) > 0);
280 top_multiple_thandle_dump(tmt, D_HA);
281 tmt->tmt_committed = 1;
282 lut = dt2lu_dev(tmt->tmt_master_sub_dt)->ld_site->ls_tgt;
283 if (distribute_txn_commit_thread_running(lut))
284 wake_up(&lut->lut_tdtd->tdtd_commit_thread_waitq);
288 struct sub_thandle *lookup_sub_thandle(struct top_multiple_thandle *tmt,
289 struct dt_device *dt_dev)
291 struct sub_thandle *st;
293 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
294 if (st->st_dt == dt_dev)
299 EXPORT_SYMBOL(lookup_sub_thandle);
301 struct sub_thandle *create_sub_thandle(struct top_multiple_thandle *tmt,
302 struct dt_device *dt_dev)
304 struct sub_thandle *st;
308 RETURN(ERR_PTR(-ENOMEM));
310 INIT_LIST_HEAD(&st->st_sub_list);
313 list_add(&st->st_sub_list, &tmt->tmt_sub_thandle_list);
318 * sub thandle commit callback
320 * Mark the sub thandle to be committed and if all sub thandle are committed
321 * notify the top thandle.
323 * \param[in] sub_th sub thandle being committed.
325 static void sub_trans_commit_cb(struct lu_env *env,
326 struct thandle *sub_th,
327 struct dt_txn_commit_cb *cb, int err)
329 struct sub_thandle *st;
330 struct top_multiple_thandle *tmt = cb->dcb_data;
331 bool all_committed = true;
334 /* Check if all sub thandles are committed */
335 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
336 if (st->st_sub_th == sub_th) {
337 st->st_committed = 1;
340 if (!st->st_committed)
341 all_committed = false;
344 if (tmt->tmt_result == 0)
345 tmt->tmt_result = err;
348 top_trans_committed_cb(tmt);
350 top_multiple_thandle_dump(tmt, D_INFO);
351 top_multiple_thandle_put(tmt);
355 static void sub_thandle_register_commit_cb(struct sub_thandle *st,
356 struct top_multiple_thandle *tmt)
358 LASSERT(st->st_sub_th != NULL);
359 top_multiple_thandle_get(tmt);
360 st->st_commit_dcb.dcb_func = sub_trans_commit_cb;
361 st->st_commit_dcb.dcb_data = tmt;
362 INIT_LIST_HEAD(&st->st_commit_dcb.dcb_linkage);
363 dt_trans_cb_add(st->st_sub_th, &st->st_commit_dcb);
369 * Create transaction handle for sub_thandle
371 * \param[in] env execution environment
372 * \param[in] th top thandle
373 * \param[in] st sub_thandle
375 * \retval 0 if creation succeeds.
376 * \retval negative errno if creation fails.
378 int sub_thandle_trans_create(const struct lu_env *env,
379 struct top_thandle *top_th,
380 struct sub_thandle *st)
382 struct thandle *sub_th;
384 sub_th = dt_trans_create(env, st->st_dt);
386 return PTR_ERR(sub_th);
388 sub_th->th_top = &top_th->tt_super;
389 st->st_sub_th = sub_th;
391 sub_th->th_wait_submit = 1;
397 * Create the top transaction.
399 * Create the top transaction on the master device. It will create a top
400 * thandle and a sub thandle on the master device.
402 * \param[in] env execution environment
403 * \param[in] master_dev master_dev the top thandle will be created
405 * \retval pointer to the created thandle.
406 * \retval ERR_PTR(errno) if creation failed.
409 top_trans_create(const struct lu_env *env, struct dt_device *master_dev)
411 struct top_thandle *top_th;
412 struct thandle *child_th;
414 OBD_ALLOC_GFP(top_th, sizeof(*top_th), __GFP_IO);
416 return ERR_PTR(-ENOMEM);
418 top_th->tt_super.th_top = &top_th->tt_super;
420 if (master_dev != NULL) {
421 child_th = dt_trans_create(env, master_dev);
422 if (IS_ERR(child_th)) {
423 OBD_FREE_PTR(top_th);
427 child_th->th_top = &top_th->tt_super;
428 child_th->th_wait_submit = 1;
429 top_th->tt_master_sub_thandle = child_th;
431 top_th->tt_super.th_tags |= child_th->th_tags;
433 return &top_th->tt_super;
435 EXPORT_SYMBOL(top_trans_create);
438 * Declare write update transaction
440 * Check if there are updates being recorded in this transaction,
441 * it will write the record into the disk.
443 * \param[in] env execution environment
444 * \param[in] tmt top multiple transaction handle
446 * \retval 0 if writing succeeds
447 * \retval negative errno if writing fails
449 static int declare_updates_write(const struct lu_env *env,
450 struct top_multiple_thandle *tmt)
452 struct llog_update_record *record;
453 struct sub_thandle *st;
456 record = tmt->tmt_update_records->tur_update_records;
457 /* Declare update write for all other target */
458 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
459 if (st->st_sub_th == NULL)
462 rc = sub_declare_updates_write(env, record, st->st_sub_th);
471 * Assign batchid to the distribute transaction.
473 * Assign batchid to the distribute transaction
475 * \param[in] tmt distribute transaction
477 static void distribute_txn_assign_batchid(struct top_multiple_thandle *new)
479 struct target_distribute_txn_data *tdtd;
480 struct dt_device *dt = new->tmt_master_sub_dt;
483 tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
484 spin_lock(&tdtd->tdtd_batchid_lock);
485 new->tmt_batchid = tdtd->tdtd_batchid++;
486 list_add_tail(&new->tmt_commit_list, &tdtd->tdtd_list);
487 spin_unlock(&tdtd->tdtd_batchid_lock);
488 top_multiple_thandle_get(new);
489 top_multiple_thandle_dump(new, D_INFO);
493 * Insert distribute transaction to the distribute txn list.
495 * Insert distribute transaction to the distribute txn list.
497 * \param[in] new the distribute txn to be inserted.
499 void distribute_txn_insert_by_batchid(struct top_multiple_thandle *new)
501 struct dt_device *dt = new->tmt_master_sub_dt;
502 struct top_multiple_thandle *tmt;
503 struct target_distribute_txn_data *tdtd;
504 bool at_head = false;
507 tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
509 spin_lock(&tdtd->tdtd_batchid_lock);
510 list_for_each_entry_reverse(tmt, &tdtd->tdtd_list, tmt_commit_list) {
511 if (new->tmt_batchid > tmt->tmt_batchid) {
512 list_add(&new->tmt_commit_list, &tmt->tmt_commit_list);
516 if (list_empty(&new->tmt_commit_list)) {
518 list_add(&new->tmt_commit_list, &tdtd->tdtd_list);
520 spin_unlock(&tdtd->tdtd_batchid_lock);
521 top_multiple_thandle_get(new);
522 top_multiple_thandle_dump(new, D_INFO);
523 if (new->tmt_committed && at_head)
524 wake_up(&tdtd->tdtd_commit_thread_waitq);
528 * Prepare cross-MDT operation.
530 * Create the update record buffer to record updates for cross-MDT operation,
531 * add master sub transaction to tt_sub_trans_list, and declare the update
534 * During updates packing, all of parameters will be packed in
535 * tur_update_params, and updates will be packed in tur_update_records.
536 * Then in transaction stop, parameters and updates will be merged
537 * into one updates buffer.
539 * And also master thandle will be added to the sub_th list, so it will be
540 * easy to track the commit status.
542 * \param[in] env execution environment
543 * \param[in] th top transaction handle
545 * \retval 0 if preparation succeeds.
546 * \retval negative errno if preparation fails.
548 static int prepare_multiple_node_trans(const struct lu_env *env,
549 struct top_multiple_thandle *tmt)
551 struct thandle_update_records *tur;
555 if (tmt->tmt_update_records == NULL) {
556 tur = &update_env_info(env)->uti_tur;
557 rc = check_and_prepare_update_record(env, tur);
561 tmt->tmt_update_records = tur;
562 distribute_txn_assign_batchid(tmt);
565 rc = declare_updates_write(env, tmt);
571 * start the top transaction.
573 * Start all of its sub transactions, then start master sub transaction.
575 * \param[in] env execution environment
576 * \param[in] master_dev master_dev the top thandle will be start
577 * \param[in] th top thandle
579 * \retval 0 if transaction start succeeds.
580 * \retval negative errno if start fails.
582 int top_trans_start(const struct lu_env *env, struct dt_device *master_dev,
585 struct top_thandle *top_th = container_of(th, struct top_thandle,
587 struct sub_thandle *st;
588 struct top_multiple_thandle *tmt = top_th->tt_multiple_thandle;
593 rc = dt_trans_start(env, top_th->tt_master_sub_thandle->th_dev,
594 top_th->tt_master_sub_thandle);
598 tmt = top_th->tt_multiple_thandle;
599 rc = prepare_multiple_node_trans(env, tmt);
603 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
604 if (st->st_sub_th == NULL)
606 st->st_sub_th->th_sync = th->th_sync;
607 st->st_sub_th->th_local = th->th_local;
608 st->st_sub_th->th_tags = th->th_tags;
609 rc = dt_trans_start(env, st->st_sub_th->th_dev,
614 sub_thandle_register_commit_cb(st, tmt);
620 EXPORT_SYMBOL(top_trans_start);
623 * Check whether we need write updates record
625 * Check if the updates for the top_thandle needs to be writen
626 * to all targets. Only if the transaction succeeds and the updates
627 * number > 2, it will write the updates,
629 * \params [in] top_th top thandle.
631 * \retval true if it needs to write updates
632 * \retval false if it does not need to write updates
634 static bool top_check_write_updates(struct top_thandle *top_th)
636 struct top_multiple_thandle *tmt;
637 struct thandle_update_records *tur;
639 /* Do not write updates to records if the transaction fails */
640 if (top_th->tt_super.th_result != 0)
643 tmt = top_th->tt_multiple_thandle;
647 tur = tmt->tmt_update_records;
651 /* Hmm, false update records, since the cross-MDT operation
652 * should includes both local and remote updates, so the
653 * updates count should >= 2 */
654 if (tur->tur_update_records == NULL ||
655 tur->tur_update_records->lur_update_rec.ur_update_count <= 1)
662 * Stop the top transaction.
664 * Stop the transaction on the master device first, then stop transactions
665 * on other sub devices.
667 * \param[in] env execution environment
668 * \param[in] master_dev master_dev the top thandle will be created
669 * \param[in] th top thandle
671 * \retval 0 if stop transaction succeeds.
672 * \retval negative errno if stop transaction fails.
674 int top_trans_stop(const struct lu_env *env, struct dt_device *master_dev,
677 struct top_thandle *top_th = container_of(th, struct top_thandle,
679 struct sub_thandle *st;
680 struct sub_thandle *master_st;
681 struct top_multiple_thandle *tmt;
682 struct thandle_update_records *tur;
683 bool write_updates = false;
687 if (likely(top_th->tt_multiple_thandle == NULL)) {
688 LASSERT(master_dev != NULL);
689 rc = dt_trans_stop(env, master_dev,
690 top_th->tt_master_sub_thandle);
691 OBD_FREE_PTR(top_th);
695 tmt = top_th->tt_multiple_thandle;
696 tur = tmt->tmt_update_records;
698 /* Note: we need stop the master thandle first, then the stop
699 * callback will fill the master transno in the update logs,
700 * then these update logs will be sent to other MDTs */
701 /* get the master sub thandle */
702 master_st = lookup_sub_thandle(tmt, tmt->tmt_master_sub_dt);
703 write_updates = top_check_write_updates(top_th);
705 /* Step 1: write the updates log on Master MDT */
706 if (master_st != NULL && master_st->st_sub_th != NULL &&
708 struct llog_update_record *lur;
710 /* Merge the parameters and updates into one buffer */
711 rc = prepare_writing_updates(env, tmt);
713 CERROR("%s: cannot prepare updates: rc = %d\n",
714 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
716 GOTO(stop_master_trans, rc);
719 lur = tur->tur_update_records;
720 /* Write updates to the master MDT */
721 rc = sub_updates_write(env, lur, master_st->st_sub_th,
722 &master_st->st_cookie);
724 /* Cleanup the common parameters in the update records,
725 * master transno callback might add more parameters.
726 * and we need merge the update records again in the
728 if (tur->tur_update_params != NULL)
729 lur->lur_update_rec.ur_param_count = 0;
732 CERROR("%s: write updates failed: rc = %d\n",
733 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
735 GOTO(stop_master_trans, rc);
740 /* Step 2: Stop the transaction on the master MDT, and fill the
741 * master transno in the update logs to other MDT. */
742 if (master_st != NULL && master_st->st_sub_th != NULL) {
743 master_st->st_sub_th->th_local = th->th_local;
744 master_st->st_sub_th->th_sync = th->th_sync;
745 master_st->st_sub_th->th_tags = th->th_tags;
746 master_st->st_sub_th->th_result = th->th_result;
747 rc = dt_trans_stop(env, master_st->st_dt, master_st->st_sub_th);
750 GOTO(stop_other_trans, rc);
751 } else if (tur != NULL && tur->tur_update_records != NULL) {
752 struct llog_update_record *lur;
754 lur = tur->tur_update_records;
755 if (lur->lur_update_rec.ur_master_transno == 0)
756 /* Update master transno after master stop
758 lur->lur_update_rec.ur_master_transno =
759 tgt_th_info(env)->tti_transno;
763 /* Step 3: write updates to other MDTs */
765 struct llog_update_record *lur;
767 /* Stop callback of master will add more updates and also update
768 * master transno, so merge the parameters and updates into one
770 rc = prepare_writing_updates(env, tmt);
772 CERROR("%s: prepare updates failed: rc = %d\n",
773 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
775 GOTO(stop_other_trans, rc);
777 lur = tur->tur_update_records;
778 list_for_each_entry(st, &tmt->tmt_sub_thandle_list,
780 if (st->st_sub_th == NULL || st == master_st ||
781 st->st_sub_th->th_result < 0)
784 rc = sub_updates_write(env, lur, st->st_sub_th,
794 /* Step 4: Stop the transaction on other MDTs */
795 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
796 if (st == master_st || st->st_sub_th == NULL)
799 st->st_sub_th->th_sync = th->th_sync;
800 st->st_sub_th->th_local = th->th_local;
801 st->st_sub_th->th_tags = th->th_tags;
802 st->st_sub_th->th_result = th->th_result;
803 rc = dt_trans_stop(env, st->st_sub_th->th_dev,
805 if (unlikely(rc < 0 && th->th_result == 0))
809 tmt->tmt_result = rc;
810 /* Balance for the refcount in top_trans_create, Note: if it is NOT
811 * multiple node transaction, the top transaction will be destroyed. */
812 top_multiple_thandle_put(tmt);
813 OBD_FREE_PTR(top_th);
816 EXPORT_SYMBOL(top_trans_stop);
819 * Create top_multiple_thandle for top_thandle
821 * Create top_mutilple_thandle to manage the mutiple node transaction
822 * for top_thandle, and it also needs to add master sub thandle to the
823 * sub trans list now.
825 * \param[in] env execution environment
826 * \param[in] top_th the top thandle
828 * \retval 0 if creation succeeds
829 * \retval negative errno if creation fails
831 int top_trans_create_tmt(const struct lu_env *env,
832 struct top_thandle *top_th)
834 struct top_multiple_thandle *tmt;
840 tmt->tmt_magic = TOP_THANDLE_MAGIC;
841 INIT_LIST_HEAD(&tmt->tmt_sub_thandle_list);
842 INIT_LIST_HEAD(&tmt->tmt_commit_list);
843 atomic_set(&tmt->tmt_refcount, 1);
845 top_th->tt_multiple_thandle = tmt;
850 static struct sub_thandle *
851 create_sub_thandle_with_thandle(struct top_thandle *top_th,
852 struct thandle *sub_th)
854 struct sub_thandle *st;
856 /* create and init sub th to the top trans list */
857 st = create_sub_thandle(top_th->tt_multiple_thandle,
862 st->st_sub_th = sub_th;
863 sub_th->th_top = &top_th->tt_super;
870 * Get sub thandle from the top thandle according to the sub dt_device.
872 * \param[in] env execution environment
873 * \param[in] th thandle on the top layer.
874 * \param[in] sub_dt sub dt_device used to get sub transaction
876 * \retval thandle of sub transaction if succeed
877 * \retval PTR_ERR(errno) if failed
879 struct thandle *thandle_get_sub_by_dt(const struct lu_env *env,
881 struct dt_device *sub_dt)
883 struct sub_thandle *st = NULL;
884 struct top_thandle *top_th;
885 struct thandle *sub_th = NULL;
889 top_th = container_of(th, struct top_thandle, tt_super);
891 if (likely(sub_dt == top_th->tt_master_sub_thandle->th_dev))
892 RETURN(top_th->tt_master_sub_thandle);
894 if (top_th->tt_multiple_thandle != NULL) {
895 st = lookup_sub_thandle(top_th->tt_multiple_thandle, sub_dt);
897 RETURN(st->st_sub_th);
900 sub_th = dt_trans_create(env, sub_dt);
904 /* Create top_multiple_thandle if necessary */
905 if (top_th->tt_multiple_thandle == NULL) {
906 struct top_multiple_thandle *tmt;
908 rc = top_trans_create_tmt(env, top_th);
910 GOTO(stop_trans, rc);
912 tmt = top_th->tt_multiple_thandle;
914 /* Add master sub th to the top trans list */
915 tmt->tmt_master_sub_dt =
916 top_th->tt_master_sub_thandle->th_dev;
917 st = create_sub_thandle_with_thandle(top_th,
918 top_th->tt_master_sub_thandle);
920 GOTO(stop_trans, rc = PTR_ERR(st));
921 top_th->tt_master_sub_thandle->th_sync = 1;
922 top_th->tt_super.th_sync = 1;
925 /* create and init sub th to the top trans list */
926 st = create_sub_thandle_with_thandle(top_th, sub_th);
927 st->st_sub_th->th_wait_submit = 1;
932 sub_th->th_result = rc;
933 dt_trans_stop(env, sub_dt, sub_th);
934 sub_th = ERR_PTR(rc);
939 EXPORT_SYMBOL(thandle_get_sub_by_dt);
942 * Top multiple thandle destroy
944 * Destroy multiple thandle and all its sub thandle.
946 * \param[in] tmt top_multiple_thandle to be destroyed.
948 void top_multiple_thandle_destroy(struct top_multiple_thandle *tmt)
950 struct sub_thandle *st;
951 struct sub_thandle *tmp;
953 LASSERT(tmt->tmt_magic == TOP_THANDLE_MAGIC);
954 list_for_each_entry_safe(st, tmp, &tmt->tmt_sub_thandle_list,
956 list_del(&st->st_sub_list);
961 EXPORT_SYMBOL(top_multiple_thandle_destroy);
964 * Cancel the update log on MDTs
966 * Cancel the update log on MDTs then destroy the thandle.
968 * \param[in] env execution environment
969 * \param[in] tmt the top multiple thandle whose updates records
972 * \retval 0 if cancellation succeeds.
973 * \retval negative errno if cancellation fails.
975 static int distribute_txn_cancel_records(const struct lu_env *env,
976 struct top_multiple_thandle *tmt)
978 struct sub_thandle *st;
981 top_multiple_thandle_dump(tmt, D_INFO);
982 /* Cancel update logs on other MDTs */
983 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
984 struct llog_ctxt *ctxt;
985 struct obd_device *obd;
986 struct llog_cookie *cookie;
989 cookie = &st->st_cookie;
990 if (fid_is_zero(&cookie->lgc_lgl.lgl_oi.oi_fid))
993 obd = st->st_dt->dd_lu_dev.ld_obd;
994 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
997 rc = llog_cat_cancel_records(env, ctxt->loc_handle, 1,
1000 llog_ctxt_put(ctxt);
1001 CDEBUG(D_HA, "%s: batchid %llu cancel update log "DOSTID
1002 ".%u : rc = %d\n", obd->obd_name, tmt->tmt_batchid,
1003 POSTID(&cookie->lgc_lgl.lgl_oi), cookie->lgc_index, rc);
1010 * Check if there are committed transaction
1012 * Check if there are committed transaction in the distribute transaction
1013 * list, then cancel the update records for those committed transaction.
1014 * Because the distribute transaction in the list are sorted by batchid,
1015 * and cancellation will be done by batchid order, so we only check the first
1016 * the transaction(with lowest batchid) in the list.
1018 * \param[in] lod lod device where cancel thread is
1020 * \retval true if it is ready
1021 * \retval false if it is not ready
1023 static bool tdtd_ready_for_cancel_log(struct target_distribute_txn_data *tdtd)
1025 struct top_multiple_thandle *tmt = NULL;
1026 struct obd_device *obd = tdtd->tdtd_lut->lut_obd;
1029 spin_lock(&tdtd->tdtd_batchid_lock);
1030 if (!list_empty(&tdtd->tdtd_list)) {
1031 tmt = list_entry(tdtd->tdtd_list.next,
1032 struct top_multiple_thandle, tmt_commit_list);
1033 if (tmt->tmt_committed &&
1034 (!obd->obd_recovering || (obd->obd_recovering &&
1035 tmt->tmt_batchid <= tdtd->tdtd_committed_batchid)))
1038 spin_unlock(&tdtd->tdtd_batchid_lock);
1043 struct distribute_txn_bid_data {
1044 struct dt_txn_commit_cb dtbd_cb;
1045 struct target_distribute_txn_data *dtbd_tdtd;
1050 * callback of updating commit batchid
1052 * Updating commit batchid then wake up the commit thread to cancel the
1055 * \param[in]env execution environment
1056 * \param[in]th thandle to updating commit batchid
1057 * \param[in]cb commit callback
1058 * \param[in]err result of thandle
1060 static void distribute_txn_batchid_cb(struct lu_env *env,
1062 struct dt_txn_commit_cb *cb,
1065 struct distribute_txn_bid_data *dtbd = NULL;
1066 struct target_distribute_txn_data *tdtd;
1068 dtbd = container_of0(cb, struct distribute_txn_bid_data, dtbd_cb);
1069 tdtd = dtbd->dtbd_tdtd;
1071 CDEBUG(D_HA, "%s: %llu batchid updated\n",
1072 tdtd->tdtd_lut->lut_obd->obd_name, dtbd->dtbd_batchid);
1073 spin_lock(&tdtd->tdtd_batchid_lock);
1074 if (dtbd->dtbd_batchid > tdtd->tdtd_committed_batchid &&
1075 !tdtd->tdtd_lut->lut_obd->obd_no_transno)
1076 tdtd->tdtd_committed_batchid = dtbd->dtbd_batchid;
1077 spin_unlock(&tdtd->tdtd_batchid_lock);
1078 atomic_dec(&tdtd->tdtd_refcount);
1079 wake_up(&tdtd->tdtd_commit_thread_waitq);
1085 * Update the commit batchid in disk
1087 * Update commit batchid in the disk, after this is committed, it can start
1088 * to cancel the update records.
1090 * \param[in] env execution environment
1091 * \param[in] tdtd distribute transaction structure
1092 * \param[in] batchid commit batchid to be updated
1094 * \retval 0 if update succeeds.
1095 * \retval negative errno if update fails.
1098 distribute_txn_commit_batchid_update(const struct lu_env *env,
1099 struct target_distribute_txn_data *tdtd,
1102 struct distribute_txn_bid_data *dtbd = NULL;
1110 OBD_ALLOC_PTR(dtbd);
1113 dtbd->dtbd_batchid = batchid;
1114 dtbd->dtbd_tdtd = tdtd;
1115 dtbd->dtbd_cb.dcb_func = distribute_txn_batchid_cb;
1116 atomic_inc(&tdtd->tdtd_refcount);
1118 th = dt_trans_create(env, tdtd->tdtd_lut->lut_bottom);
1121 RETURN(PTR_ERR(th));
1124 tmp = cpu_to_le64(batchid);
1126 buf.lb_len = sizeof(tmp);
1129 rc = dt_declare_record_write(env, tdtd->tdtd_batchid_obj, &buf, off,
1134 rc = dt_trans_start_local(env, tdtd->tdtd_lut->lut_bottom, th);
1138 dt_trans_cb_add(th, &dtbd->dtbd_cb);
1142 rc = dt_record_write(env, tdtd->tdtd_batchid_obj, &buf,
1145 CDEBUG(D_INFO, "%s: update batchid "LPU64": rc = %d\n",
1146 tdtd->tdtd_lut->lut_obd->obd_name, batchid, rc);
1149 dt_trans_stop(env, tdtd->tdtd_lut->lut_bottom, th);
1156 * Init commit batchid for distribute transaction.
1158 * Initialize the batchid object and get commit batchid from the object.
1160 * \param[in] env execution environment
1161 * \param[in] tdtd distribute transaction whose batchid is initialized.
1163 * \retval 0 if initialization succeeds.
1164 * \retval negative errno if initialization fails.
1167 distribute_txn_commit_batchid_init(const struct lu_env *env,
1168 struct target_distribute_txn_data *tdtd)
1170 struct tgt_thread_info *tti = tgt_th_info(env);
1171 struct lu_target *lut = tdtd->tdtd_lut;
1172 struct lu_attr *attr = &tti->tti_attr;
1173 struct lu_fid *fid = &tti->tti_fid1;
1174 struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
1175 struct dt_object *dt_obj = NULL;
1182 memset(attr, 0, sizeof(*attr));
1183 attr->la_valid = LA_MODE;
1184 attr->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1185 dof->dof_type = dt_mode_to_dft(S_IFREG);
1187 lu_local_obj_fid(fid, BATCHID_COMMITTED_OID);
1189 dt_obj = dt_find_or_create(env, lut->lut_bottom, fid, dof,
1192 GOTO(out_put, rc = PTR_ERR(dt_obj));
1194 tdtd->tdtd_batchid_obj = dt_obj;
1197 buf.lb_len = sizeof(tmp);
1199 rc = dt_read(env, dt_obj, &buf, &off);
1200 if (rc < 0 || (rc < buf.lb_len && rc > 0)) {
1201 CERROR("%s can't read last committed batchid: rc = %d\n",
1202 tdtd->tdtd_lut->lut_obd->obd_name, rc);
1206 } else if (rc == buf.lb_len) {
1207 tdtd->tdtd_committed_batchid = le64_to_cpu(tmp);
1208 CDEBUG(D_HA, "%s: committed batchid %llu\n",
1209 tdtd->tdtd_lut->lut_obd->obd_name,
1210 tdtd->tdtd_committed_batchid);
1215 if (rc < 0 && dt_obj != NULL) {
1216 lu_object_put(env, &dt_obj->do_lu);
1217 tdtd->tdtd_batchid_obj = NULL;
1223 * manage the distribute transaction thread
1225 * Distribute transaction are linked to the list, and once the distribute
1226 * transaction is committed, it will update the last committed batchid first,
1227 * after it is committed, it will cancel the records.
1229 * \param[in] _arg argument for commit thread
1231 * \retval 0 if thread is running successfully
1232 * \retval negative errno if the thread can not be run.
1234 static int distribute_txn_commit_thread(void *_arg)
1236 struct target_distribute_txn_data *tdtd = _arg;
1237 struct lu_target *lut = tdtd->tdtd_lut;
1238 struct ptlrpc_thread *thread = &lut->lut_tdtd_commit_thread;
1239 struct l_wait_info lwi = { 0 };
1241 struct list_head list;
1243 struct top_multiple_thandle *tmt;
1244 struct top_multiple_thandle *tmp;
1245 __u64 batchid = 0, committed;
1249 rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
1253 spin_lock(&tdtd->tdtd_batchid_lock);
1254 thread->t_flags = SVC_RUNNING;
1255 spin_unlock(&tdtd->tdtd_batchid_lock);
1256 wake_up(&thread->t_ctl_waitq);
1257 INIT_LIST_HEAD(&list);
1259 CDEBUG(D_HA, "%s: start commit thread committed batchid "LPU64"\n",
1260 tdtd->tdtd_lut->lut_obd->obd_name,
1261 tdtd->tdtd_committed_batchid);
1263 while (distribute_txn_commit_thread_running(lut)) {
1264 spin_lock(&tdtd->tdtd_batchid_lock);
1265 list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1267 if (tmt->tmt_committed == 0)
1270 /* Note: right now, replay is based on master MDT
1271 * transno, but cancellation is based on batchid.
1272 * so we do not try to cancel the update log until
1273 * the recoverying is done, unless the update records
1274 * batchid < committed_batchid. */
1275 if (tmt->tmt_batchid <= tdtd->tdtd_committed_batchid) {
1276 list_move_tail(&tmt->tmt_commit_list, &list);
1277 } else if (!tdtd->tdtd_lut->lut_obd->obd_recovering) {
1278 LASSERTF(tmt->tmt_batchid >= batchid,
1279 "tmt %p tmt_batchid: "LPU64", batchid "
1280 LPU64"\n", tmt, tmt->tmt_batchid,
1282 /* There are three types of distribution
1283 * transaction result
1285 * 1. If tmt_result < 0, it means the
1286 * distribution transaction fails, which should
1287 * be rare, because once declare phase succeeds,
1288 * the operation should succeeds anyway. Note in
1289 * this case, we will still update batchid so
1290 * cancellation would be stopped.
1292 * 2. If tmt_result == 0, it means the
1293 * distribution transaction succeeds, and we
1294 * will update batchid.
1296 * 3. If tmt_result > 0, it means distribute
1297 * transaction is not yet committed on every
1298 * node, but we need release this tmt before
1299 * that, which usuually happens during umount.
1301 if (tmt->tmt_result <= 0)
1302 batchid = tmt->tmt_batchid;
1303 list_move_tail(&tmt->tmt_commit_list, &list);
1306 spin_unlock(&tdtd->tdtd_batchid_lock);
1308 CDEBUG(D_HA, "%s: batchid: "LPU64" committed batchid "
1309 LPU64"\n", tdtd->tdtd_lut->lut_obd->obd_name, batchid,
1310 tdtd->tdtd_committed_batchid);
1311 /* update globally committed on a storage */
1312 if (batchid > tdtd->tdtd_committed_batchid) {
1313 distribute_txn_commit_batchid_update(&env, tdtd,
1315 spin_lock(&tdtd->tdtd_batchid_lock);
1316 if (batchid > tdtd->tdtd_batchid) {
1317 /* This might happen during recovery,
1318 * batchid is initialized as last transno,
1319 * and the batchid in the update records
1320 * on other MDTs might be bigger than
1321 * the batchid, so we need update it to
1322 * avoid duplicate batchid. */
1323 CDEBUG(D_HA, "%s update batchid from "LPU64
1325 tdtd->tdtd_lut->lut_obd->obd_name,
1326 tdtd->tdtd_batchid, batchid);
1327 tdtd->tdtd_batchid = batchid;
1329 spin_unlock(&tdtd->tdtd_batchid_lock);
1331 /* cancel the records for committed batchid's */
1332 /* XXX: should we postpone cancel's till the end of recovery? */
1333 committed = tdtd->tdtd_committed_batchid;
1334 list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1335 if (tmt->tmt_batchid > committed)
1337 list_del_init(&tmt->tmt_commit_list);
1338 if (tmt->tmt_result <= 0)
1339 distribute_txn_cancel_records(&env, tmt);
1340 top_multiple_thandle_put(tmt);
1343 l_wait_event(tdtd->tdtd_commit_thread_waitq,
1344 !distribute_txn_commit_thread_running(lut) ||
1345 committed < tdtd->tdtd_committed_batchid ||
1346 tdtd_ready_for_cancel_log(tdtd), &lwi);
1349 l_wait_event(tdtd->tdtd_commit_thread_waitq,
1350 atomic_read(&tdtd->tdtd_refcount) == 0, &lwi);
1352 spin_lock(&tdtd->tdtd_batchid_lock);
1353 list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1355 list_move_tail(&tmt->tmt_commit_list, &list);
1356 spin_unlock(&tdtd->tdtd_batchid_lock);
1358 CDEBUG(D_INFO, "%s stopping distribute txn commit thread.\n",
1359 tdtd->tdtd_lut->lut_obd->obd_name);
1360 list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1361 list_del_init(&tmt->tmt_commit_list);
1362 top_multiple_thandle_dump(tmt, D_HA);
1363 top_multiple_thandle_put(tmt);
1366 thread->t_flags = SVC_STOPPED;
1368 wake_up(&thread->t_ctl_waitq);
1374 * Start llog cancel thread
1376 * Start llog cancel(master/slave) thread on LOD
1378 * \param[in]lclt cancel log thread to be started.
1380 * \retval 0 if the thread is started successfully.
1381 * \retval negative errno if the thread is not being
1384 int distribute_txn_init(const struct lu_env *env,
1385 struct lu_target *lut,
1386 struct target_distribute_txn_data *tdtd,
1389 struct task_struct *task;
1390 struct l_wait_info lwi = { 0 };
1394 spin_lock_init(&tdtd->tdtd_batchid_lock);
1395 INIT_LIST_HEAD(&tdtd->tdtd_list);
1397 tdtd->tdtd_batchid = lut->lut_last_transno + 1;
1399 init_waitqueue_head(&lut->lut_tdtd_commit_thread.t_ctl_waitq);
1400 init_waitqueue_head(&tdtd->tdtd_commit_thread_waitq);
1401 atomic_set(&tdtd->tdtd_refcount, 0);
1403 tdtd->tdtd_lut = lut;
1404 rc = distribute_txn_commit_batchid_init(env, tdtd);
1408 task = kthread_run(distribute_txn_commit_thread, tdtd, "tdtd-%u",
1411 RETURN(PTR_ERR(task));
1413 l_wait_event(lut->lut_tdtd_commit_thread.t_ctl_waitq,
1414 distribute_txn_commit_thread_running(lut) ||
1415 distribute_txn_commit_thread_stopped(lut), &lwi);
1418 EXPORT_SYMBOL(distribute_txn_init);
1421 * Stop llog cancel thread
1423 * Stop llog cancel(master/slave) thread on LOD and also destory
1424 * all of transaction in the list.
1426 * \param[in]lclt cancel log thread to be stopped.
1428 void distribute_txn_fini(const struct lu_env *env,
1429 struct target_distribute_txn_data *tdtd)
1431 struct lu_target *lut = tdtd->tdtd_lut;
1433 /* Stop cancel thread */
1434 if (lut == NULL || !distribute_txn_commit_thread_running(lut))
1437 spin_lock(&tdtd->tdtd_batchid_lock);
1438 lut->lut_tdtd_commit_thread.t_flags = SVC_STOPPING;
1439 spin_unlock(&tdtd->tdtd_batchid_lock);
1440 wake_up(&tdtd->tdtd_commit_thread_waitq);
1441 wait_event(lut->lut_tdtd_commit_thread.t_ctl_waitq,
1442 lut->lut_tdtd_commit_thread.t_flags & SVC_STOPPED);
1444 dtrq_list_destroy(tdtd);
1445 if (tdtd->tdtd_batchid_obj != NULL) {
1446 lu_object_put(env, &tdtd->tdtd_batchid_obj->do_lu);
1447 tdtd->tdtd_batchid_obj = NULL;
1450 EXPORT_SYMBOL(distribute_txn_fini);