1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2015, 2017, Intel Corporation.
8 * This file implements the update distribute transaction API.
10 * To manage the cross-MDT operation (distribute operation) transaction,
11 * the transaction will also be separated two layers on MD stack, top
12 * transaction and sub transaction.
14 * During the distribute operation, top transaction is created in the LOD
15 * layer, and represent the operation. Sub transaction is created by
16 * each OSD or OSP. Top transaction start/stop will trigger all of its sub
17 * transaction start/stop. Top transaction (the whole operation) is committed
18 * only all of its sub transaction are committed.
20 * there are three kinds of transactions
21 * 1. local transaction: All updates are in a single local OSD.
22 * 2. Remote transaction: All Updates are only in the remote OSD,
23 * i.e. locally all updates are in OSP.
24 * 3. Mixed transaction: Updates are both in local OSD and remote
27 * Author: Di Wang <di.wang@intel.com>
30 #define DEBUG_SUBSYSTEM S_CLASS
32 #include <linux/kthread.h>
33 #include <lu_target.h>
34 #include <lustre_log.h>
35 #include <lustre_update.h>
37 #include <obd_class.h>
38 #include <tgt_internal.h>
40 #include <tgt_internal.h>
42 * Dump top mulitple thandle
44 * Dump top multiple thandle and all of its sub thandle to the debug log.
46 * \param[in]mask debug mask
47 * \param[in]top_th top_thandle to be dumped
49 static void top_multiple_thandle_dump(struct top_multiple_thandle *tmt,
52 struct sub_thandle *st;
54 LASSERT(tmt->tmt_magic == TOP_THANDLE_MAGIC);
55 CDEBUG(mask, "%s tmt %p refcount %d committed %d result %d batchid %llu\n",
56 tmt->tmt_master_sub_dt ?
57 tmt->tmt_master_sub_dt->dd_lu_dev.ld_obd->obd_name :
59 tmt, kref_read(&tmt->tmt_refcount), tmt->tmt_committed,
60 tmt->tmt_result, tmt->tmt_batchid);
62 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
63 struct sub_thandle_cookie *stc;
65 CDEBUG(mask, "st %p obd %s committed %d started %d stopped %d "
66 "result %d sub_th %p\n",
67 st, st->st_dt->dd_lu_dev.ld_obd->obd_name,
68 st->st_committed, st->st_started, st->st_stopped,
69 st->st_result, st->st_sub_th);
71 list_for_each_entry(stc, &st->st_cookie_list, stc_list) {
72 CDEBUG(mask, " cookie "DFID".%u\n",
73 PLOGID(&stc->stc_cookie.lgc_lgl),
74 stc->stc_cookie.lgc_index);
80 * Declare write update to sub device
82 * Declare Write updates llog records to the sub device during distribute
85 * \param[in] env execution environment
86 * \param[in] record update records being written
87 * \param[in] sub_th sub transaction handle
88 * \param[in] record_size total update record size
90 * \retval 0 if writing succeeds
91 * \retval negative errno if writing fails
93 static int sub_declare_updates_write(const struct lu_env *env,
94 struct llog_update_record *record,
95 struct thandle *sub_th, size_t record_size)
97 struct llog_ctxt *ctxt;
98 struct dt_device *dt = sub_th->th_dev;
99 int left = record_size;
102 /* If ctxt is NULL, it means not need to write update,
103 * for example if the the OSP is used to connect to OST */
104 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
105 LLOG_UPDATELOG_ORIG_CTXT);
107 /* Not ready to record updates yet. */
108 if (ctxt == NULL || ctxt->loc_handle == NULL) {
113 rc = llog_declare_add(env, ctxt->loc_handle,
114 &record->lur_hdr, sub_th);
118 while (left > ctxt->loc_chunk_size) {
119 rc = llog_declare_add(env, ctxt->loc_handle,
120 &record->lur_hdr, sub_th);
124 left -= ctxt->loc_chunk_size;
134 * write update to sub device
136 * Write llog update record to the sub device during distribute
137 * transaction. If it succeeds, llog cookie of the record will be
138 * returned by @cookie.
140 * \param[in] env execution environment
141 * \param[in] record update records being written
142 * \param[in] sub_th sub transaction handle
143 * \param[out] cookie llog cookie of the update record.
145 * \retval 1 if writing succeeds
146 * \retval negative errno if writing fails
148 static int sub_updates_write(const struct lu_env *env,
149 struct llog_update_record *record,
150 struct sub_thandle *sub_th)
152 struct dt_device *dt = sub_th->st_dt;
153 struct llog_ctxt *ctxt;
154 struct llog_update_record *lur = NULL;
155 __u32 update_count = 0;
156 __u32 param_count = 0;
157 __u32 last_update_count = 0;
158 __u32 last_param_count = 0;
162 struct sub_thandle_cookie *stc;
168 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
169 LLOG_UPDATELOG_ORIG_CTXT);
170 /* If ctxt == NULL, then it means updates on OST (only happens
171 * during migration), and we do not track those updates for now */
172 /* If ctxt->loc_handle == NULL, then it does not need to record
173 * update, usually happens in error handler path */
174 if (ctxt == NULL || ctxt->loc_handle == NULL) {
179 /* Since the cross-MDT updates will includes both local
180 * and remote updates, the update ops count must > 1 */
181 LASSERT(record->lur_update_rec.ur_update_count > 1);
182 LASSERTF(record->lur_hdr.lrh_len == llog_update_record_size(record),
183 "lrh_len %u record_size %zu\n", record->lur_hdr.lrh_len,
184 llog_update_record_size(record));
187 * If its size > llog chunk_size, then write current chunk to the update
188 * llog, NB the padding should >= LLOG_MIN_REC_SIZE.
190 * So check padding length is either >= LLOG_MIN_REC_SIZE or is 0
191 * (record length just matches the chunk size).
194 reclen = record->lur_hdr.lrh_len;
195 if (reclen + LLOG_MIN_REC_SIZE <= ctxt->loc_chunk_size ||
196 reclen == ctxt->loc_chunk_size) {
199 GOTO(llog_put, rc = -ENOMEM);
200 INIT_LIST_HEAD(&stc->stc_list);
202 rc = llog_add(env, ctxt->loc_handle, &record->lur_hdr,
203 &stc->stc_cookie, sub_th->st_sub_th);
205 CDEBUG(D_INFO, "%s: Add update log "DFID".%u: rc = %d\n",
206 dt->dd_lu_dev.ld_obd->obd_name,
207 PLOGID(&stc->stc_cookie.lgc_lgl),
208 stc->stc_cookie.lgc_index, rc);
211 list_add(&stc->stc_list, &sub_th->st_cookie_list);
220 /* Split the records into chunk_size update record */
221 OBD_ALLOC_LARGE(lur, ctxt->loc_chunk_size);
223 GOTO(llog_put, rc = -ENOMEM);
225 memcpy(lur, &record->lur_hdr, sizeof(record->lur_hdr));
226 lur->lur_update_rec.ur_update_count = 0;
227 lur->lur_update_rec.ur_param_count = 0;
228 start = (char *)&record->lur_update_rec.ur_ops;
231 if (update_count < record->lur_update_rec.ur_update_count)
232 next = (char *)update_op_next_op(
233 (struct update_op *)cur);
234 else if (param_count < record->lur_update_rec.ur_param_count)
235 next = (char *)update_param_next_param(
236 (struct update_param *)cur);
240 reclen = __llog_update_record_size(
241 __update_records_size(next - start));
242 if ((reclen + LLOG_MIN_REC_SIZE <= ctxt->loc_chunk_size ||
243 reclen == ctxt->loc_chunk_size) &&
248 record->lur_update_rec.ur_update_count)
250 else if (param_count <
251 record->lur_update_rec.ur_param_count)
256 lur->lur_update_rec.ur_update_count = update_count -
258 lur->lur_update_rec.ur_param_count = param_count -
260 memcpy(&lur->lur_update_rec.ur_ops, start, cur - start);
261 lur->lur_hdr.lrh_len = llog_update_record_size(lur);
263 LASSERT(lur->lur_hdr.lrh_len ==
264 __llog_update_record_size(
265 __update_records_size(cur - start)));
266 LASSERT(lur->lur_hdr.lrh_len <= ctxt->loc_chunk_size);
268 update_records_dump(&lur->lur_update_rec, D_INFO, true);
272 GOTO(llog_put, rc = -ENOMEM);
273 INIT_LIST_HEAD(&stc->stc_list);
275 rc = llog_add(env, ctxt->loc_handle, &lur->lur_hdr,
276 &stc->stc_cookie, sub_th->st_sub_th);
278 CDEBUG(D_INFO, "%s: Add update log "DFID".%u: rc = %d\n",
279 dt->dd_lu_dev.ld_obd->obd_name,
280 PLOGID(&stc->stc_cookie.lgc_lgl),
281 stc->stc_cookie.lgc_index, rc);
284 list_add(&stc->stc_list, &sub_th->st_cookie_list);
291 last_update_count = update_count;
292 last_param_count = param_count;
294 lur->lur_update_rec.ur_update_count = 0;
295 lur->lur_update_rec.ur_param_count = 0;
296 lur->lur_update_rec.ur_flags |= UPDATE_RECORD_CONTINUE;
301 OBD_FREE_LARGE(lur, ctxt->loc_chunk_size);
308 * Prepare the update records.
310 * Merge params and ops into the update records, then initializing
313 * During transaction execution phase, parameters and update ops
314 * are collected in two different buffers (see lod_updates_pack()),
315 * during transaction stop, it needs to be merged in one buffer,
316 * so it will be written in the update log.
318 * \param[in] env execution environment
319 * \param[in] tmt top_multiple_thandle for distribute txn
321 * \retval 0 if merging succeeds.
322 * \retval negaitive errno if merging fails.
324 static int prepare_writing_updates(const struct lu_env *env,
325 struct top_multiple_thandle *tmt)
327 struct thandle_update_records *tur = tmt->tmt_update_records;
328 struct llog_update_record *lur;
329 struct update_params *params;
333 if (tur == NULL || tur->tur_update_records == NULL ||
334 tur->tur_update_params == NULL)
337 lur = tur->tur_update_records;
338 /* Extends the update records buffer if needed */
339 params_size = update_params_size(tur->tur_update_params,
340 tur->tur_update_param_count);
341 LASSERT(lur->lur_update_rec.ur_param_count == 0);
342 update_size = round_up(llog_update_record_size(lur) + params_size, 8);
343 if (update_size > tur->tur_update_records_buf_size) {
346 rc = tur_update_records_extend(tur, update_size);
350 lur = tur->tur_update_records;
353 params = update_records_get_params(&lur->lur_update_rec);
354 memcpy(params, tur->tur_update_params, params_size);
356 lur->lur_update_rec.ur_param_count = tur->tur_update_param_count;
357 lur->lur_update_rec.ur_batchid = tmt->tmt_batchid;
358 /* Init update record header */
359 lur->lur_hdr.lrh_len = llog_update_record_size(lur);
360 lur->lur_hdr.lrh_type = UPDATE_REC;
362 /* Dump updates for debugging purpose */
363 update_records_dump(&lur->lur_update_rec, D_INFO, true);
369 * Top thandle commit callback
371 * This callback will be called when all of sub transactions are committed.
373 * \param[in] th top thandle to be committed.
375 static void top_trans_committed_cb(struct top_multiple_thandle *tmt)
377 struct lu_target *lut;
380 LASSERT(kref_read(&tmt->tmt_refcount) > 0);
382 top_multiple_thandle_dump(tmt, D_HA);
383 tmt->tmt_committed = 1;
384 lut = dt2lu_dev(tmt->tmt_master_sub_dt)->ld_site->ls_tgt;
385 if (lut->lut_tdtd && lut->lut_tdtd->tdtd_commit_task)
386 wake_up_process(lut->lut_tdtd->tdtd_commit_task);
391 struct sub_thandle *lookup_sub_thandle(struct top_multiple_thandle *tmt,
392 struct dt_device *dt_dev)
394 struct sub_thandle *st;
396 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
397 if (st->st_dt == dt_dev)
402 EXPORT_SYMBOL(lookup_sub_thandle);
404 struct sub_thandle *create_sub_thandle(struct top_multiple_thandle *tmt,
405 struct dt_device *dt_dev)
407 struct sub_thandle *st;
411 RETURN(ERR_PTR(-ENOMEM));
413 INIT_LIST_HEAD(&st->st_sub_list);
414 INIT_LIST_HEAD(&st->st_cookie_list);
417 list_add(&st->st_sub_list, &tmt->tmt_sub_thandle_list);
421 static void sub_trans_commit_cb_internal(struct top_multiple_thandle *tmt,
422 struct thandle *sub_th, int err)
424 struct sub_thandle *st;
425 bool all_committed = true;
427 /* Check if all sub thandles are committed */
428 spin_lock(&tmt->tmt_sub_lock);
429 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
430 if (st->st_sub_th == sub_th) {
431 st->st_committed = 1;
434 if (!st->st_committed)
435 all_committed = false;
437 spin_unlock(&tmt->tmt_sub_lock);
439 if (tmt->tmt_result == 0)
440 tmt->tmt_result = err;
443 top_trans_committed_cb(tmt);
445 top_multiple_thandle_dump(tmt, D_INFO);
446 top_multiple_thandle_put(tmt);
451 * sub thandle commit callback
453 * Mark the sub thandle to be committed and if all sub thandle are committed
454 * notify the top thandle.
456 * \param[in] env execution environment
457 * \param[in] sub_th sub thandle being committed
458 * \param[in] cb commit callback
459 * \param[in] err trans result
461 static void sub_trans_commit_cb(struct lu_env *env,
462 struct thandle *sub_th,
463 struct dt_txn_commit_cb *cb, int err)
465 struct top_multiple_thandle *tmt = cb->dcb_data;
467 sub_trans_commit_cb_internal(tmt, sub_th, err);
470 static void sub_thandle_register_commit_cb(struct sub_thandle *st,
471 struct top_multiple_thandle *tmt)
473 LASSERT(st->st_sub_th != NULL);
474 top_multiple_thandle_get(tmt);
475 st->st_commit_dcb.dcb_func = sub_trans_commit_cb;
476 st->st_commit_dcb.dcb_data = tmt;
477 INIT_LIST_HEAD(&st->st_commit_dcb.dcb_linkage);
478 dt_trans_cb_add(st->st_sub_th, &st->st_commit_dcb);
482 * Sub thandle stop call back
484 * After sub thandle is stopped, it will call this callback to notify
487 * \param[in] th sub thandle to be stopped
488 * \param[in] rc result of sub trans
490 static void sub_trans_stop_cb(struct lu_env *env,
491 struct thandle *sub_th,
492 struct dt_txn_commit_cb *cb, int err)
494 struct sub_thandle *st;
495 struct top_multiple_thandle *tmt = cb->dcb_data;
498 spin_lock(&tmt->tmt_sub_lock);
499 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
503 if (st->st_dt == sub_th->th_dev) {
509 spin_unlock(&tmt->tmt_sub_lock);
511 wake_up(&tmt->tmt_stop_waitq);
515 static void sub_thandle_register_stop_cb(struct sub_thandle *st,
516 struct top_multiple_thandle *tmt)
518 st->st_stop_dcb.dcb_func = sub_trans_stop_cb;
519 st->st_stop_dcb.dcb_data = tmt;
520 st->st_stop_dcb.dcb_flags = DCB_TRANS_STOP;
521 INIT_LIST_HEAD(&st->st_stop_dcb.dcb_linkage);
522 dt_trans_cb_add(st->st_sub_th, &st->st_stop_dcb);
528 * Create transaction handle for sub_thandle
530 * \param[in] env execution environment
531 * \param[in] th top thandle
532 * \param[in] st sub_thandle
534 * \retval 0 if creation succeeds.
535 * \retval negative errno if creation fails.
537 int sub_thandle_trans_create(const struct lu_env *env,
538 struct top_thandle *top_th,
539 struct sub_thandle *st)
541 struct thandle *sub_th;
543 sub_th = dt_trans_create(env, st->st_dt);
545 return PTR_ERR(sub_th);
547 sub_th->th_top = &top_th->tt_super;
548 st->st_sub_th = sub_th;
550 sub_th->th_wait_submit = 1;
551 sub_thandle_register_stop_cb(st, top_th->tt_multiple_thandle);
556 * Create the top transaction.
558 * Create the top transaction on the master device. It will create a top
559 * thandle and a sub thandle on the master device.
561 * \param[in] env execution environment
562 * \param[in] master_dev master_dev the top thandle will be created
564 * \retval pointer to the created thandle.
565 * \retval ERR_PTR(errno) if creation failed.
568 top_trans_create(const struct lu_env *env, struct dt_device *master_dev)
570 struct top_thandle *top_th;
571 struct thandle *child_th;
573 OBD_ALLOC_GFP(top_th, sizeof(*top_th), GFP_NOFS);
575 return ERR_PTR(-ENOMEM);
577 top_th->tt_super.th_top = &top_th->tt_super;
579 if (master_dev != NULL) {
580 child_th = dt_trans_create(env, master_dev);
581 if (IS_ERR(child_th)) {
582 OBD_FREE_PTR(top_th);
586 child_th->th_top = &top_th->tt_super;
587 child_th->th_wait_submit = 1;
588 top_th->tt_master_sub_thandle = child_th;
590 return &top_th->tt_super;
592 EXPORT_SYMBOL(top_trans_create);
595 * Declare write update transaction
597 * Check if there are updates being recorded in this transaction,
598 * it will write the record into the disk.
600 * \param[in] env execution environment
601 * \param[in] tmt top multiple transaction handle
603 * \retval 0 if writing succeeds
604 * \retval negative errno if writing fails
606 static int declare_updates_write(const struct lu_env *env,
607 struct top_multiple_thandle *tmt)
609 struct llog_update_record *record;
610 struct sub_thandle *st;
613 record = tmt->tmt_update_records->tur_update_records;
614 /* Declare update write for all other target */
615 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
616 if (st->st_sub_th == NULL)
619 rc = sub_declare_updates_write(env, record, st->st_sub_th,
620 tmt->tmt_record_size);
629 * Assign batchid to the distribute transaction.
631 * Assign batchid to the distribute transaction
633 * \param[in] tmt distribute transaction
635 static void distribute_txn_assign_batchid(struct top_multiple_thandle *new)
637 struct target_distribute_txn_data *tdtd;
638 struct dt_device *dt = new->tmt_master_sub_dt;
639 struct sub_thandle *st;
642 tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
643 spin_lock(&tdtd->tdtd_batchid_lock);
644 new->tmt_batchid = tdtd->tdtd_batchid++;
645 list_add_tail(&new->tmt_commit_list, &tdtd->tdtd_list);
646 spin_unlock(&tdtd->tdtd_batchid_lock);
647 list_for_each_entry(st, &new->tmt_sub_thandle_list, st_sub_list) {
648 if (st->st_sub_th != NULL)
649 sub_thandle_register_commit_cb(st, new);
651 top_multiple_thandle_get(new);
652 top_multiple_thandle_dump(new, D_INFO);
656 * Insert distribute transaction to the distribute txn list.
658 * Insert distribute transaction to the distribute txn list.
660 * \param[in] new the distribute txn to be inserted.
662 void distribute_txn_insert_by_batchid(struct top_multiple_thandle *new)
664 struct dt_device *dt = new->tmt_master_sub_dt;
665 struct top_multiple_thandle *tmt;
666 struct target_distribute_txn_data *tdtd;
667 struct sub_thandle *st;
668 bool at_head = false;
671 tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
673 /* have a reference so the competing commit thread can't release it*/
674 top_multiple_thandle_get(new);
676 spin_lock(&tdtd->tdtd_batchid_lock);
677 list_for_each_entry_reverse(tmt, &tdtd->tdtd_list, tmt_commit_list) {
678 if (new->tmt_batchid > tmt->tmt_batchid) {
679 list_add(&new->tmt_commit_list, &tmt->tmt_commit_list);
683 if (list_empty(&new->tmt_commit_list)) {
685 list_add(&new->tmt_commit_list, &tdtd->tdtd_list);
687 spin_unlock(&tdtd->tdtd_batchid_lock);
689 list_for_each_entry(st, &new->tmt_sub_thandle_list, st_sub_list) {
690 if (st->st_sub_th != NULL)
691 sub_thandle_register_commit_cb(st, new);
694 top_multiple_thandle_dump(new, D_INFO);
695 if (new->tmt_committed && at_head && tdtd->tdtd_commit_task)
696 wake_up_process(tdtd->tdtd_commit_task);
700 * Prepare cross-MDT operation.
702 * Create the update record buffer to record updates for cross-MDT operation,
703 * add master sub transaction to tt_sub_trans_list, and declare the update
706 * During updates packing, all of parameters will be packed in
707 * tur_update_params, and updates will be packed in tur_update_records.
708 * Then in transaction stop, parameters and updates will be merged
709 * into one updates buffer.
711 * And also master thandle will be added to the sub_th list, so it will be
712 * easy to track the commit status.
714 * \param[in] env execution environment
715 * \param[in] th top transaction handle
717 * \retval 0 if preparation succeeds.
718 * \retval negative errno if preparation fails.
720 static int prepare_multiple_node_trans(const struct lu_env *env,
721 struct top_multiple_thandle *tmt)
723 struct thandle_update_records *tur;
727 if (tmt->tmt_update_records == NULL) {
728 tur = &update_env_info(env)->uti_tur;
729 rc = check_and_prepare_update_record(env, tur);
733 tmt->tmt_update_records = tur;
734 distribute_txn_assign_batchid(tmt);
737 rc = declare_updates_write(env, tmt);
743 * start the top transaction.
745 * Start all of its sub transactions, then start master sub transaction.
747 * \param[in] env execution environment
748 * \param[in] master_dev master_dev the top thandle will be start
749 * \param[in] th top thandle
751 * \retval 0 if transaction start succeeds.
752 * \retval negative errno if start fails.
754 int top_trans_start(const struct lu_env *env, struct dt_device *master_dev,
757 struct top_thandle *top_th = container_of(th, struct top_thandle,
759 struct sub_thandle *st;
760 struct top_multiple_thandle *tmt = top_th->tt_multiple_thandle;
766 top_th->tt_master_sub_thandle->th_sync = th->th_sync;
768 top_th->tt_master_sub_thandle->th_local = th->th_local;
769 rc = dt_trans_start(env, top_th->tt_master_sub_thandle->th_dev,
770 top_th->tt_master_sub_thandle);
774 tmt = top_th->tt_multiple_thandle;
775 rc = prepare_multiple_node_trans(env, tmt);
779 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
780 if (st->st_sub_th == NULL)
783 st->st_sub_th->th_sync = th->th_sync;
785 st->st_sub_th->th_local = th->th_local;
786 rc = dt_trans_start(env, st->st_sub_th->th_dev,
791 LASSERT(st->st_started == 0);
798 EXPORT_SYMBOL(top_trans_start);
801 * Check whether we need write updates record
803 * Check if the updates for the top_thandle needs to be writen
804 * to all targets. Only if the transaction succeeds and the updates
805 * number > 2, it will write the updates,
807 * \params [in] top_th top thandle.
809 * \retval true if it needs to write updates
810 * \retval false if it does not need to write updates
812 static bool top_check_write_updates(struct top_thandle *top_th)
814 struct top_multiple_thandle *tmt;
815 struct thandle_update_records *tur;
817 /* Do not write updates to records if the transaction fails */
818 if (top_th->tt_super.th_result != 0)
821 tmt = top_th->tt_multiple_thandle;
825 tur = tmt->tmt_update_records;
829 /* Hmm, false update records, since the cross-MDT operation
830 * should includes both local and remote updates, so the
831 * updates count should >= 2 */
832 if (tur->tur_update_records == NULL ||
833 tur->tur_update_records->lur_update_rec.ur_update_count <= 1)
840 * Check if top transaction is stopped
842 * Check if top transaction is stopped, only if all sub transaction
843 * is stopped, then the top transaction is stopped.
845 * \param [in] top_th top thandle
847 * \retval true if the top transaction is stopped.
848 * \retval false if the top transaction is not stopped.
850 static bool top_trans_is_stopped(struct top_thandle *top_th)
852 struct top_multiple_thandle *tmt;
853 struct sub_thandle *st;
854 bool all_stopped = true;
856 tmt = top_th->tt_multiple_thandle;
857 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
858 if (!st->st_stopped && st->st_sub_th != NULL) {
863 if (st->st_result != 0 &&
864 top_th->tt_super.th_result == 0)
865 top_th->tt_super.th_result = st->st_result;
872 * Wait result of top transaction
874 * Wait until all sub transaction get its result.
876 * \param [in] top_th top thandle.
878 * \retval the result of top thandle.
880 static int top_trans_wait_result(struct top_thandle *top_th)
882 wait_event_idle(top_th->tt_multiple_thandle->tmt_stop_waitq,
883 top_trans_is_stopped(top_th));
885 RETURN(top_th->tt_super.th_result);
889 * Stop the top transaction.
891 * Stop the transaction on the master device first, then stop transactions
892 * on other sub devices.
894 * \param[in] env execution environment
895 * \param[in] master_dev master_dev the top thandle will be created
896 * \param[in] th top thandle
898 * \retval 0 if stop transaction succeeds.
899 * \retval negative errno if stop transaction fails.
901 int top_trans_stop(const struct lu_env *env, struct dt_device *master_dev,
904 struct top_thandle *top_th = container_of(th, struct top_thandle,
906 struct sub_thandle *st;
907 struct sub_thandle *master_st;
908 struct top_multiple_thandle *tmt;
909 struct thandle_update_records *tur;
910 bool write_updates = false;
914 if (likely(top_th->tt_multiple_thandle == NULL)) {
915 LASSERT(master_dev != NULL);
918 top_th->tt_master_sub_thandle->th_sync = th->th_sync;
920 top_th->tt_master_sub_thandle->th_local = th->th_local;
921 rc = dt_trans_stop(env, master_dev,
922 top_th->tt_master_sub_thandle);
923 OBD_FREE_PTR(top_th);
927 tmt = top_th->tt_multiple_thandle;
928 tur = tmt->tmt_update_records;
930 /* Note: we need stop the master thandle first, then the stop
931 * callback will fill the master transno in the update logs,
932 * then these update logs will be sent to other MDTs */
933 /* get the master sub thandle */
934 master_st = lookup_sub_thandle(tmt, tmt->tmt_master_sub_dt);
935 write_updates = top_check_write_updates(top_th);
937 /* Step 1: write the updates log on Master MDT */
938 if (master_st != NULL && master_st->st_sub_th != NULL &&
940 struct llog_update_record *lur;
942 /* Merge the parameters and updates into one buffer */
943 rc = prepare_writing_updates(env, tmt);
945 CERROR("%s: cannot prepare updates: rc = %d\n",
946 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
948 write_updates = false;
949 GOTO(stop_master_trans, rc);
952 lur = tur->tur_update_records;
953 /* Write updates to the master MDT */
954 rc = sub_updates_write(env, lur, master_st);
956 /* Cleanup the common parameters in the update records,
957 * master transno callback might add more parameters.
958 * and we need merge the update records again in the
960 if (tur->tur_update_params != NULL)
961 lur->lur_update_rec.ur_param_count = 0;
964 CERROR("%s: write updates failed: rc = %d\n",
965 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
967 write_updates = false;
968 GOTO(stop_master_trans, rc);
973 /* Step 2: Stop the transaction on the master MDT, and fill the
974 * master transno in the update logs to other MDT. */
975 if (master_st != NULL && master_st->st_sub_th != NULL) {
977 master_st->st_sub_th->th_local = th->th_local;
979 master_st->st_sub_th->th_sync = th->th_sync;
980 master_st->st_sub_th->th_result = th->th_result;
981 rc = dt_trans_stop(env, master_st->st_dt, master_st->st_sub_th);
982 /* If it does not write_updates, then we call submit callback
983 * here, otherwise callback is done through
984 * osd(osp)_trans_commit_cb() */
985 if (!master_st->st_started &&
986 !list_empty(&tmt->tmt_commit_list))
987 sub_trans_commit_cb_internal(tmt,
988 master_st->st_sub_th, rc);
990 CERROR("%s: stop trans failed: rc = %d\n",
991 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
993 GOTO(stop_other_trans, rc);
994 } else if (tur != NULL && tur->tur_update_records != NULL) {
995 struct llog_update_record *lur;
997 lur = tur->tur_update_records;
998 if (lur->lur_update_rec.ur_master_transno == 0)
999 /* Update master transno after master stop
1001 lur->lur_update_rec.ur_master_transno =
1002 tgt_th_info(env)->tti_transno;
1006 /* Step 3: write updates to other MDTs */
1007 if (write_updates) {
1008 struct llog_update_record *lur;
1009 if (CFS_FAIL_PRECHECK(OBD_FAIL_OUT_OBJECT_MISS)) {
1010 if (cfs_fail_val == 1) {
1011 long timeout = cfs_time_seconds(1) / 10;
1013 CFS_RACE(OBD_FAIL_OUT_OBJECT_MISS);
1014 set_current_state(TASK_UNINTERRUPTIBLE);
1015 schedule_timeout(schedule_timeout(timeout));
1021 /* Stop callback of master will add more updates and also update
1022 * master transno, so merge the parameters and updates into one
1024 rc = prepare_writing_updates(env, tmt);
1026 CERROR("%s: prepare updates failed: rc = %d\n",
1027 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
1029 GOTO(stop_other_trans, rc);
1031 lur = tur->tur_update_records;
1032 list_for_each_entry(st, &tmt->tmt_sub_thandle_list,
1034 if (st->st_sub_th == NULL || st == master_st ||
1035 st->st_sub_th->th_result < 0)
1038 rc = sub_updates_write(env, lur, st);
1040 CERROR("%s: write updates failed: rc = %d\n",
1041 st->st_dt->dd_lu_dev.ld_obd->obd_name,
1050 /* Step 4: Stop the transaction on other MDTs */
1051 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
1052 if (st == master_st || st->st_sub_th == NULL)
1056 st->st_sub_th->th_sync = th->th_sync;
1058 st->st_sub_th->th_local = th->th_local;
1059 st->st_sub_th->th_result = th->th_result;
1060 rc = dt_trans_stop(env, st->st_sub_th->th_dev,
1063 CERROR("%s: stop trans failed: rc = %d\n",
1064 st->st_dt->dd_lu_dev.ld_obd->obd_name, rc);
1065 if (th->th_result == 0)
1070 rc = top_trans_wait_result(top_th);
1072 tmt->tmt_result = rc;
1074 /* Balance for the refcount in top_trans_create, Note: if it is NOT
1075 * multiple node transaction, the top transaction will be destroyed. */
1076 top_multiple_thandle_put(tmt);
1077 OBD_FREE_PTR(top_th);
1080 EXPORT_SYMBOL(top_trans_stop);
1083 * Create top_multiple_thandle for top_thandle
1085 * Create top_mutilple_thandle to manage the mutiple node transaction
1086 * for top_thandle, and it also needs to add master sub thandle to the
1087 * sub trans list now.
1089 * \param[in] env execution environment
1090 * \param[in] top_th the top thandle
1092 * \retval 0 if creation succeeds
1093 * \retval negative errno if creation fails
1095 int top_trans_create_tmt(const struct lu_env *env,
1096 struct top_thandle *top_th)
1098 struct top_multiple_thandle *tmt;
1104 tmt->tmt_magic = TOP_THANDLE_MAGIC;
1105 INIT_LIST_HEAD(&tmt->tmt_sub_thandle_list);
1106 INIT_LIST_HEAD(&tmt->tmt_commit_list);
1107 kref_init(&tmt->tmt_refcount);
1108 spin_lock_init(&tmt->tmt_sub_lock);
1109 init_waitqueue_head(&tmt->tmt_stop_waitq);
1111 top_th->tt_multiple_thandle = tmt;
1116 static struct sub_thandle *
1117 create_sub_thandle_with_thandle(struct top_thandle *top_th,
1118 struct thandle *sub_th)
1120 struct sub_thandle *st;
1122 /* create and init sub th to the top trans list */
1123 st = create_sub_thandle(top_th->tt_multiple_thandle,
1128 st->st_sub_th = sub_th;
1130 sub_th->th_top = &top_th->tt_super;
1131 sub_thandle_register_stop_cb(st, top_th->tt_multiple_thandle);
1138 * Get sub thandle from the top thandle according to the sub dt_device.
1140 * \param[in] env execution environment
1141 * \param[in] th thandle on the top layer.
1142 * \param[in] sub_dt sub dt_device used to get sub transaction
1144 * \retval thandle of sub transaction if succeed
1145 * \retval PTR_ERR(errno) if failed
1147 struct thandle *thandle_get_sub_by_dt(const struct lu_env *env,
1149 struct dt_device *sub_dt)
1151 struct sub_thandle *st = NULL;
1152 struct sub_thandle *master_st = NULL;
1153 struct top_thandle *top_th;
1154 struct thandle *sub_th = NULL;
1158 top_th = container_of(th, struct top_thandle, tt_super);
1160 if (likely(sub_dt == top_th->tt_master_sub_thandle->th_dev))
1161 RETURN(top_th->tt_master_sub_thandle);
1163 if (top_th->tt_multiple_thandle != NULL) {
1164 st = lookup_sub_thandle(top_th->tt_multiple_thandle, sub_dt);
1166 RETURN(st->st_sub_th);
1169 sub_th = dt_trans_create(env, sub_dt);
1173 /* Create top_multiple_thandle if necessary */
1174 if (top_th->tt_multiple_thandle == NULL) {
1175 struct top_multiple_thandle *tmt;
1177 rc = top_trans_create_tmt(env, top_th);
1179 GOTO(stop_trans, rc);
1181 tmt = top_th->tt_multiple_thandle;
1183 /* Add master sub th to the top trans list */
1184 tmt->tmt_master_sub_dt =
1185 top_th->tt_master_sub_thandle->th_dev;
1186 master_st = create_sub_thandle_with_thandle(top_th,
1187 top_th->tt_master_sub_thandle);
1188 if (IS_ERR(master_st)) {
1189 rc = PTR_ERR(master_st);
1191 GOTO(stop_trans, rc);
1195 /* create and init sub th to the top trans list */
1196 st = create_sub_thandle_with_thandle(top_th, sub_th);
1200 GOTO(stop_trans, rc);
1202 st->st_sub_th->th_wait_submit = 1;
1205 if (master_st != NULL) {
1206 list_del(&master_st->st_sub_list);
1207 OBD_FREE_PTR(master_st);
1209 sub_th->th_result = rc;
1210 dt_trans_stop(env, sub_dt, sub_th);
1211 sub_th = ERR_PTR(rc);
1216 EXPORT_SYMBOL(thandle_get_sub_by_dt);
1219 * Top multiple thandle destroy
1221 * Destroy multiple thandle and all its sub thandle.
1223 * \param[in] kref Pointer to struct kref
1225 void top_multiple_thandle_destroy(struct kref *kref)
1227 struct top_multiple_thandle *tmt;
1228 struct sub_thandle *st;
1229 struct sub_thandle *tmp;
1231 tmt = container_of(kref, struct top_multiple_thandle, tmt_refcount);
1233 LASSERT(tmt->tmt_magic == TOP_THANDLE_MAGIC);
1234 list_for_each_entry_safe(st, tmp, &tmt->tmt_sub_thandle_list,
1236 struct sub_thandle_cookie *stc;
1237 struct sub_thandle_cookie *tmp;
1239 list_del(&st->st_sub_list);
1240 list_for_each_entry_safe(stc, tmp, &st->st_cookie_list,
1242 list_del(&stc->stc_list);
1249 EXPORT_SYMBOL(top_multiple_thandle_destroy);
1252 * Cancel the update log on MDTs
1254 * Cancel the update log on MDTs then destroy the thandle.
1256 * \param[in] env execution environment
1257 * \param[in] tmt the top multiple thandle whose updates records
1258 * will be cancelled.
1260 * \retval 0 if cancellation succeeds.
1261 * \retval negative errno if cancellation fails.
1263 static int distribute_txn_cancel_records(const struct lu_env *env,
1264 struct top_multiple_thandle *tmt)
1266 struct sub_thandle *st;
1269 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_TXN_NO_CANCEL))
1272 top_multiple_thandle_dump(tmt, D_INFO);
1273 /* Cancel update logs on other MDTs */
1274 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
1275 struct llog_ctxt *ctxt;
1276 struct obd_device *obd;
1277 struct llog_cookie *cookie;
1278 struct sub_thandle_cookie *stc;
1281 obd = st->st_dt->dd_lu_dev.ld_obd;
1282 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
1285 list_for_each_entry(stc, &st->st_cookie_list, stc_list) {
1286 cookie = &stc->stc_cookie;
1287 if (fid_is_zero(&cookie->lgc_lgl.lgl_oi.oi_fid))
1290 rc = llog_cat_cancel_records(env, ctxt->loc_handle, 1,
1292 CDEBUG(D_HA, "%s: batchid %llu cancel update log "
1293 DFID".%u: rc = %d\n", obd->obd_name,
1294 tmt->tmt_batchid, PLOGID(&cookie->lgc_lgl),
1295 cookie->lgc_index, rc);
1298 llog_ctxt_put(ctxt);
1304 struct distribute_txn_bid_data {
1305 struct dt_txn_commit_cb dtbd_cb;
1306 struct target_distribute_txn_data *dtbd_tdtd;
1311 * callback of updating commit batchid
1313 * Updating commit batchid then wake up the commit thread to cancel the
1316 * \param[in]env execution environment
1317 * \param[in]th thandle to updating commit batchid
1318 * \param[in]cb commit callback
1319 * \param[in]err result of thandle
1321 static void distribute_txn_batchid_cb(struct lu_env *env,
1323 struct dt_txn_commit_cb *cb,
1326 struct distribute_txn_bid_data *dtbd = NULL;
1327 struct target_distribute_txn_data *tdtd;
1329 dtbd = container_of(cb, struct distribute_txn_bid_data, dtbd_cb);
1330 tdtd = dtbd->dtbd_tdtd;
1332 CDEBUG(D_HA, "%s: %llu batchid updated\n",
1333 tdtd->tdtd_lut->lut_obd->obd_name, dtbd->dtbd_batchid);
1334 spin_lock(&tdtd->tdtd_batchid_lock);
1335 if (dtbd->dtbd_batchid > tdtd->tdtd_committed_batchid &&
1336 !tdtd->tdtd_lut->lut_obd->obd_no_transno)
1337 tdtd->tdtd_committed_batchid = dtbd->dtbd_batchid;
1338 spin_unlock(&tdtd->tdtd_batchid_lock);
1339 if (atomic_dec_and_test(&tdtd->tdtd_refcount))
1340 wake_up_process(tdtd->tdtd_commit_task);
1346 * Update the commit batchid in disk
1348 * Update commit batchid in the disk, after this is committed, it can start
1349 * to cancel the update records.
1351 * \param[in] env execution environment
1352 * \param[in] tdtd distribute transaction structure
1353 * \param[in] batchid commit batchid to be updated
1355 * \retval 0 if update succeeds.
1356 * \retval negative errno if update fails.
1359 distribute_txn_commit_batchid_update(const struct lu_env *env,
1360 struct target_distribute_txn_data *tdtd,
1363 struct distribute_txn_bid_data *dtbd = NULL;
1371 OBD_ALLOC_PTR(dtbd);
1374 dtbd->dtbd_batchid = batchid;
1375 dtbd->dtbd_tdtd = tdtd;
1376 dtbd->dtbd_cb.dcb_func = distribute_txn_batchid_cb;
1377 atomic_inc(&tdtd->tdtd_refcount);
1379 th = dt_trans_create(env, tdtd->tdtd_lut->lut_bottom);
1381 atomic_dec(&tdtd->tdtd_refcount);
1383 RETURN(PTR_ERR(th));
1386 tmp = cpu_to_le64(batchid);
1388 buf.lb_len = sizeof(tmp);
1391 rc = dt_declare_record_write(env, tdtd->tdtd_batchid_obj, &buf, off,
1396 rc = dt_trans_start_local(env, tdtd->tdtd_lut->lut_bottom, th);
1400 rc = dt_trans_cb_add(th, &dtbd->dtbd_cb);
1404 rc = dt_record_write(env, tdtd->tdtd_batchid_obj, &buf,
1407 CDEBUG(D_INFO, "%s: update batchid %llu: rc = %d\n",
1408 tdtd->tdtd_lut->lut_obd->obd_name, batchid, rc);
1411 dt_trans_stop(env, tdtd->tdtd_lut->lut_bottom, th);
1413 atomic_dec(&tdtd->tdtd_refcount);
1420 * Init commit batchid for distribute transaction.
1422 * Initialize the batchid object and get commit batchid from the object.
1424 * \param[in] env execution environment
1425 * \param[in] tdtd distribute transaction whose batchid is initialized.
1427 * \retval 0 if initialization succeeds.
1428 * \retval negative errno if initialization fails.
1431 distribute_txn_commit_batchid_init(const struct lu_env *env,
1432 struct target_distribute_txn_data *tdtd)
1434 struct tgt_thread_info *tti = tgt_th_info(env);
1435 struct lu_target *lut = tdtd->tdtd_lut;
1436 struct lu_attr *attr = &tti->tti_attr;
1437 struct lu_fid *fid = &tti->tti_fid1;
1438 struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
1439 struct dt_object *dt_obj = NULL;
1446 memset(attr, 0, sizeof(*attr));
1447 attr->la_valid = LA_MODE;
1448 attr->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1449 dof->dof_type = dt_mode_to_dft(S_IFREG);
1451 lu_local_obj_fid(fid, BATCHID_COMMITTED_OID);
1453 dt_obj = dt_find_or_create(env, lut->lut_bottom, fid, dof,
1455 if (IS_ERR(dt_obj)) {
1456 rc = PTR_ERR(dt_obj);
1461 tdtd->tdtd_batchid_obj = dt_obj;
1464 buf.lb_len = sizeof(tmp);
1466 rc = dt_read(env, dt_obj, &buf, &off);
1467 if (rc < 0 || (rc < buf.lb_len && rc > 0)) {
1468 CERROR("%s can't read last committed batchid: rc = %d\n",
1469 tdtd->tdtd_lut->lut_obd->obd_name, rc);
1473 } else if (rc == buf.lb_len) {
1474 tdtd->tdtd_committed_batchid = le64_to_cpu(tmp);
1475 CDEBUG(D_HA, "%s: committed batchid %llu\n",
1476 tdtd->tdtd_lut->lut_obd->obd_name,
1477 tdtd->tdtd_committed_batchid);
1482 if (rc < 0 && dt_obj != NULL) {
1483 dt_object_put(env, dt_obj);
1484 tdtd->tdtd_batchid_obj = NULL;
1490 #define TASK_IDLE TASK_INTERRUPTIBLE
1494 * manage the distribute transaction thread
1496 * Distribute transaction are linked to the list, and once the distribute
1497 * transaction is committed, it will update the last committed batchid first,
1498 * after it is committed, it will cancel the records.
1500 * \param[in] _arg argument for commit thread
1502 * \retval 0 if thread is running successfully
1503 * \retval negative errno if the thread can not be run.
1505 static int distribute_txn_commit_thread(void *_arg)
1507 struct target_distribute_txn_data *tdtd = _arg;
1508 struct lu_env *env = &tdtd->tdtd_env;
1511 struct top_multiple_thandle *tmt;
1512 struct top_multiple_thandle *tmp;
1513 __u64 batchid = 0, committed;
1518 CDEBUG(D_HA, "%s: start commit thread committed batchid %llu\n",
1519 tdtd->tdtd_lut->lut_obd->obd_name,
1520 tdtd->tdtd_committed_batchid);
1522 while (({set_current_state(TASK_IDLE);
1523 !kthread_should_stop(); })) {
1524 spin_lock(&tdtd->tdtd_batchid_lock);
1525 list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1527 if (tmt->tmt_committed == 0)
1530 /* Note: right now, replay is based on master MDT
1531 * transno, but cancellation is based on batchid.
1532 * so we do not try to cancel the update log until
1533 * the recoverying is done, unless the update records
1534 * batchid < committed_batchid. */
1535 if (tmt->tmt_batchid <= tdtd->tdtd_committed_batchid) {
1536 __set_current_state(TASK_RUNNING);
1537 list_move_tail(&tmt->tmt_commit_list, &list);
1538 } else if (!test_bit(OBDF_RECOVERING, tdtd->tdtd_lut->lut_obd->obd_flags)) {
1539 __set_current_state(TASK_RUNNING);
1540 LASSERTF(tmt->tmt_batchid >= batchid,
1541 "tmt %px tmt_batchid: %llu, batchid %llu\n",
1542 tmt, tmt->tmt_batchid, batchid);
1543 /* There are three types of distribution
1544 * transaction result
1546 * 1. If tmt_result < 0, it means the
1547 * distribution transaction fails, which should
1548 * be rare, because once declare phase succeeds,
1549 * the operation should succeeds anyway. Note in
1550 * this case, we will still update batchid so
1551 * cancellation would be stopped.
1553 * 2. If tmt_result == 0, it means the
1554 * distribution transaction succeeds, and we
1555 * will update batchid.
1557 * 3. If tmt_result > 0, it means distribute
1558 * transaction is not yet committed on every
1559 * node, but we need release this tmt before
1560 * that, which usuually happens during umount.
1562 if (tmt->tmt_result <= 0)
1563 batchid = tmt->tmt_batchid;
1564 list_move_tail(&tmt->tmt_commit_list, &list);
1567 spin_unlock(&tdtd->tdtd_batchid_lock);
1569 CDEBUG(D_HA, "%s: batchid: %llu committed batchid "
1570 "%llu\n", tdtd->tdtd_lut->lut_obd->obd_name, batchid,
1571 tdtd->tdtd_committed_batchid);
1572 /* update globally committed on a storage */
1573 if (batchid > tdtd->tdtd_committed_batchid) {
1574 rc = distribute_txn_commit_batchid_update(env, tdtd,
1579 /* cancel the records for committed batchid's */
1580 /* XXX: should we postpone cancel's till the end of recovery? */
1581 committed = tdtd->tdtd_committed_batchid;
1582 list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1583 if (tmt->tmt_batchid > committed)
1585 __set_current_state(TASK_RUNNING);
1586 list_del_init(&tmt->tmt_commit_list);
1587 if (tmt->tmt_result <= 0)
1588 distribute_txn_cancel_records(env, tmt);
1589 top_multiple_thandle_put(tmt);
1592 if (!task_is_running(current))
1595 if (CFS_FAIL_PRECHECK(OBD_FAIL_OUT_OBJECT_MISS)) {
1596 set_current_state(TASK_UNINTERRUPTIBLE);
1597 schedule_timeout(cfs_time_seconds(5));
1601 while (({set_current_state(TASK_IDLE);
1602 atomic_read(&tdtd->tdtd_refcount) != 0; }))
1604 __set_current_state(TASK_RUNNING);
1606 spin_lock(&tdtd->tdtd_batchid_lock);
1607 list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1609 list_move_tail(&tmt->tmt_commit_list, &list);
1610 spin_unlock(&tdtd->tdtd_batchid_lock);
1612 CDEBUG(D_INFO, "%s stopping distribute txn commit thread.\n",
1613 tdtd->tdtd_lut->lut_obd->obd_name);
1614 list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1615 list_del_init(&tmt->tmt_commit_list);
1616 top_multiple_thandle_dump(tmt, D_HA);
1617 top_multiple_thandle_put(tmt);
1623 * Start llog cancel thread
1625 * Start llog cancel(master/slave) thread on LOD
1627 * \param[in]lclt cancel log thread to be started.
1629 * \retval 0 if the thread is started successfully.
1630 * \retval negative errno if the thread is not being
1633 int distribute_txn_init(const struct lu_env *env,
1634 struct lu_target *lut,
1635 struct target_distribute_txn_data *tdtd,
1638 struct task_struct *task;
1642 INIT_LIST_HEAD(&tdtd->tdtd_list);
1643 INIT_LIST_HEAD(&tdtd->tdtd_replay_finish_list);
1644 INIT_LIST_HEAD(&tdtd->tdtd_replay_list);
1645 spin_lock_init(&tdtd->tdtd_batchid_lock);
1646 spin_lock_init(&tdtd->tdtd_replay_list_lock);
1647 tdtd->tdtd_replay_handler = distribute_txn_replay_handle;
1648 tdtd->tdtd_replay_ready = 0;
1650 tdtd->tdtd_batchid = lut->lut_last_transno + 1;
1652 init_waitqueue_head(&tdtd->tdtd_recovery_threads_waitq);
1653 atomic_set(&tdtd->tdtd_refcount, 0);
1654 atomic_set(&tdtd->tdtd_recovery_threads_count, 0);
1656 tdtd->tdtd_lut = lut;
1657 if (lut->lut_bottom->dd_rdonly)
1660 rc = distribute_txn_commit_batchid_init(env, tdtd);
1664 rc = lu_env_init(&tdtd->tdtd_env, LCT_LOCAL | LCT_MD_THREAD);
1668 task = kthread_create(distribute_txn_commit_thread, tdtd, "dist_txn-%u",
1671 lu_env_fini(&tdtd->tdtd_env);
1672 RETURN(PTR_ERR(task));
1674 tdtd->tdtd_commit_task = task;
1675 wake_up_process(task);
1679 EXPORT_SYMBOL(distribute_txn_init);
1682 * Stop llog cancel thread
1684 * Stop llog cancel(master/slave) thread on LOD and also destory
1685 * all of transaction in the list.
1687 * \param[in]lclt cancel log thread to be stopped.
1689 void distribute_txn_fini(const struct lu_env *env,
1690 struct target_distribute_txn_data *tdtd)
1692 struct top_multiple_thandle *tmt;
1695 /* Stop cancel thread */
1696 if (!tdtd->tdtd_commit_task)
1699 kthread_stop(tdtd->tdtd_commit_task);
1700 tdtd->tdtd_commit_task = NULL;
1702 spin_lock(&tdtd->tdtd_batchid_lock);
1703 list_splice_init(&tdtd->tdtd_list, &list);
1704 spin_unlock(&tdtd->tdtd_batchid_lock);
1706 CDEBUG(D_INFO, "%s stopping distribute txn commit thread.\n",
1707 tdtd->tdtd_lut->lut_obd->obd_name);
1708 while ((tmt = list_first_entry_or_null(&list,
1709 struct top_multiple_thandle,
1710 tmt_commit_list)) != NULL) {
1711 list_del_init(&tmt->tmt_commit_list);
1712 top_multiple_thandle_dump(tmt, D_HA);
1713 top_multiple_thandle_put(tmt);
1716 lu_env_fini(&tdtd->tdtd_env);
1718 dtrq_list_destroy(tdtd);
1719 if (tdtd->tdtd_batchid_obj != NULL) {
1720 dt_object_put(env, tdtd->tdtd_batchid_obj);
1721 tdtd->tdtd_batchid_obj = NULL;
1724 EXPORT_SYMBOL(distribute_txn_fini);