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, 2015, 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 struct sub_thandle_cookie *stc;
86 CDEBUG(mask, "st %p obd %s committed %d sub_th %p\n",
87 st, st->st_dt->dd_lu_dev.ld_obd->obd_name,
88 st->st_committed, st->st_sub_th);
90 list_for_each_entry(stc, &st->st_cookie_list, stc_list) {
91 CDEBUG(mask, " cookie "DOSTID": %u\n",
92 POSTID(&stc->stc_cookie.lgc_lgl.lgl_oi),
93 stc->stc_cookie.lgc_index);
99 * Declare write update to sub device
101 * Declare Write updates llog records to the sub device during distribute
104 * \param[in] env execution environment
105 * \param[in] record update records being written
106 * \param[in] sub_th sub transaction handle
107 * \param[in] record_size total update record size
109 * \retval 0 if writing succeeds
110 * \retval negative errno if writing fails
112 static int sub_declare_updates_write(const struct lu_env *env,
113 struct llog_update_record *record,
114 struct thandle *sub_th, size_t record_size)
116 struct llog_ctxt *ctxt;
117 struct dt_device *dt = sub_th->th_dev;
118 int left = record_size;
121 /* If ctxt is NULL, it means not need to write update,
122 * for example if the the OSP is used to connect to OST */
123 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
124 LLOG_UPDATELOG_ORIG_CTXT);
126 /* Not ready to record updates yet. */
127 if (ctxt == NULL || ctxt->loc_handle == NULL) {
132 rc = llog_declare_add(env, ctxt->loc_handle,
133 &record->lur_hdr, sub_th);
137 while (left > ctxt->loc_chunk_size) {
138 rc = llog_declare_add(env, ctxt->loc_handle,
139 &record->lur_hdr, sub_th);
143 left -= ctxt->loc_chunk_size;
153 * write update to sub device
155 * Write llog update record to the sub device during distribute
156 * transaction. If it succeeds, llog cookie of the record will be
157 * returned by @cookie.
159 * \param[in] env execution environment
160 * \param[in] record update records being written
161 * \param[in] sub_th sub transaction handle
162 * \param[out] cookie llog cookie of the update record.
164 * \retval 1 if writing succeeds
165 * \retval negative errno if writing fails
167 static int sub_updates_write(const struct lu_env *env,
168 struct llog_update_record *record,
169 struct sub_thandle *sub_th)
171 struct dt_device *dt = sub_th->st_dt;
172 struct llog_ctxt *ctxt;
174 struct llog_update_record *lur = NULL;
175 struct update_params *params = NULL;
176 __u32 update_count = 0;
177 __u32 param_count = 0;
178 __u32 last_update_count = 0;
179 __u32 last_param_count = 0;
183 struct sub_thandle_cookie *stc;
186 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
187 LLOG_UPDATELOG_ORIG_CTXT);
188 /* If ctxt == NULL, then it means updates on OST (only happens
189 * during migration), and we do not track those updates for now */
190 /* If ctxt->loc_handle == NULL, then it does not need to record
191 * update, usually happens in error handler path */
192 if (ctxt == NULL || ctxt->loc_handle == NULL) {
197 /* Since the cross-MDT updates will includes both local
198 * and remote updates, the update ops count must > 1 */
199 LASSERT(record->lur_update_rec.ur_update_count > 1);
200 LASSERTF(record->lur_hdr.lrh_len == llog_update_record_size(record),
201 "lrh_len %u record_size %zu\n", record->lur_hdr.lrh_len,
202 llog_update_record_size(record));
204 if (likely(record->lur_hdr.lrh_len <= ctxt->loc_chunk_size)) {
207 GOTO(llog_put, rc = -ENOMEM);
208 INIT_LIST_HEAD(&stc->stc_list);
210 rc = llog_add(env, ctxt->loc_handle, &record->lur_hdr,
211 &stc->stc_cookie, sub_th->st_sub_th);
213 CDEBUG(D_INFO, "%s: Add update log "DOSTID":%u: rc = %d\n",
214 dt->dd_lu_dev.ld_obd->obd_name,
215 POSTID(&stc->stc_cookie.lgc_lgl.lgl_oi),
216 stc->stc_cookie.lgc_index, rc);
219 list_add(&stc->stc_list, &sub_th->st_cookie_list);
228 /* Split the records into chunk_size update record */
229 OBD_ALLOC_LARGE(lur, ctxt->loc_chunk_size);
231 GOTO(llog_put, rc = -ENOMEM);
233 memcpy(lur, &record->lur_hdr, sizeof(record->lur_hdr));
234 lur->lur_update_rec.ur_update_count = 0;
235 lur->lur_update_rec.ur_param_count = 0;
236 src = &record->lur_update_rec.ur_ops;
238 lur->lur_hdr.lrh_len = llog_update_record_size(lur);
239 params = update_records_get_params(&record->lur_update_rec);
243 if (update_count < record->lur_update_rec.ur_update_count) {
244 next = update_op_next_op((struct update_op *)src);
246 if (param_count == 0)
247 next = update_records_get_params(
248 &record->lur_update_rec);
251 object_update_param_size(
252 (struct object_update_param *)src);
255 rec_len = cfs_size_round((unsigned long)(next - src));
256 /* If its size > llog chunk_size, then write current chunk to
257 * the update llog. */
258 if (lur->lur_hdr.lrh_len + rec_len + LLOG_MIN_REC_SIZE >
259 ctxt->loc_chunk_size ||
260 param_count == record->lur_update_rec.ur_param_count) {
261 lur->lur_update_rec.ur_update_count =
262 update_count > last_update_count ?
263 update_count - last_update_count : 0;
264 lur->lur_update_rec.ur_param_count = param_count -
267 memcpy(&lur->lur_update_rec.ur_ops, start,
268 (unsigned long)(src - start));
269 if (last_update_count != 0)
270 lur->lur_update_rec.ur_flags |=
271 UPDATE_RECORD_CONTINUE;
273 update_records_dump(&lur->lur_update_rec, D_INFO, true);
274 lur->lur_hdr.lrh_len = llog_update_record_size(lur);
275 LASSERT(lur->lur_hdr.lrh_len <= ctxt->loc_chunk_size);
279 GOTO(llog_put, rc = -ENOMEM);
280 INIT_LIST_HEAD(&stc->stc_list);
282 rc = llog_add(env, ctxt->loc_handle,
284 &stc->stc_cookie, sub_th->st_sub_th);
286 CDEBUG(D_INFO, "%s: Add update log "DOSTID":%u"
287 " rc = %d\n", dt->dd_lu_dev.ld_obd->obd_name,
288 POSTID(&stc->stc_cookie.lgc_lgl.lgl_oi),
289 stc->stc_cookie.lgc_index, rc);
292 list_add(&stc->stc_list,
293 &sub_th->st_cookie_list);
300 last_update_count = update_count;
301 last_param_count = param_count;
303 lur->lur_update_rec.ur_update_count = 0;
304 lur->lur_update_rec.ur_param_count = 0;
305 lur->lur_hdr.lrh_len = llog_update_record_size(lur);
309 lur->lur_hdr.lrh_len += cfs_size_round(rec_len);
310 if (update_count < record->lur_update_rec.ur_update_count)
312 else if (param_count < record->lur_update_rec.ur_param_count)
320 OBD_FREE_LARGE(lur, ctxt->loc_chunk_size);
327 * Prepare the update records.
329 * Merge params and ops into the update records, then initializing
332 * During transaction execution phase, parameters and update ops
333 * are collected in two different buffers (see lod_updates_pack()),
334 * during transaction stop, it needs to be merged in one buffer,
335 * so it will be written in the update log.
337 * \param[in] env execution environment
338 * \param[in] tmt top_multiple_thandle for distribute txn
340 * \retval 0 if merging succeeds.
341 * \retval negaitive errno if merging fails.
343 static int prepare_writing_updates(const struct lu_env *env,
344 struct top_multiple_thandle *tmt)
346 struct thandle_update_records *tur = tmt->tmt_update_records;
347 struct llog_update_record *lur;
348 struct update_params *params;
352 if (tur == NULL || tur->tur_update_records == NULL ||
353 tur->tur_update_params == NULL)
356 lur = tur->tur_update_records;
357 /* Extends the update records buffer if needed */
358 params_size = update_params_size(tur->tur_update_params,
359 tur->tur_update_param_count);
360 LASSERT(lur->lur_update_rec.ur_param_count == 0);
361 update_size = llog_update_record_size(lur);
362 if (cfs_size_round(update_size + params_size) >
363 tur->tur_update_records_buf_size) {
366 rc = tur_update_records_extend(tur,
367 cfs_size_round(update_size + params_size));
371 lur = tur->tur_update_records;
374 params = update_records_get_params(&lur->lur_update_rec);
375 memcpy(params, tur->tur_update_params, params_size);
377 lur->lur_update_rec.ur_param_count = tur->tur_update_param_count;
378 lur->lur_update_rec.ur_batchid = tmt->tmt_batchid;
379 /* Init update record header */
380 lur->lur_hdr.lrh_len = llog_update_record_size(lur);
381 lur->lur_hdr.lrh_type = UPDATE_REC;
383 /* Dump updates for debugging purpose */
384 update_records_dump(&lur->lur_update_rec, D_INFO, true);
390 distribute_txn_commit_thread_running(struct lu_target *lut)
392 return lut->lut_tdtd_commit_thread.t_flags & SVC_RUNNING;
396 distribute_txn_commit_thread_stopped(struct lu_target *lut)
398 return lut->lut_tdtd_commit_thread.t_flags & SVC_STOPPED;
402 * Top thandle commit callback
404 * This callback will be called when all of sub transactions are committed.
406 * \param[in] th top thandle to be committed.
408 static void top_trans_committed_cb(struct top_multiple_thandle *tmt)
410 struct lu_target *lut;
413 LASSERT(atomic_read(&tmt->tmt_refcount) > 0);
415 top_multiple_thandle_dump(tmt, D_HA);
416 tmt->tmt_committed = 1;
417 lut = dt2lu_dev(tmt->tmt_master_sub_dt)->ld_site->ls_tgt;
418 if (distribute_txn_commit_thread_running(lut))
419 wake_up(&lut->lut_tdtd->tdtd_commit_thread_waitq);
423 struct sub_thandle *lookup_sub_thandle(struct top_multiple_thandle *tmt,
424 struct dt_device *dt_dev)
426 struct sub_thandle *st;
428 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
429 if (st->st_dt == dt_dev)
434 EXPORT_SYMBOL(lookup_sub_thandle);
436 struct sub_thandle *create_sub_thandle(struct top_multiple_thandle *tmt,
437 struct dt_device *dt_dev)
439 struct sub_thandle *st;
443 RETURN(ERR_PTR(-ENOMEM));
445 INIT_LIST_HEAD(&st->st_sub_list);
446 INIT_LIST_HEAD(&st->st_cookie_list);
449 list_add(&st->st_sub_list, &tmt->tmt_sub_thandle_list);
454 * sub thandle commit callback
456 * Mark the sub thandle to be committed and if all sub thandle are committed
457 * notify the top thandle.
459 * \param[in] env execution environment
460 * \param[in] sub_th sub thandle being committed
461 * \param[in] cb commit callback
462 * \param[in] err trans result
464 static void sub_trans_commit_cb(struct lu_env *env,
465 struct thandle *sub_th,
466 struct dt_txn_commit_cb *cb, int err)
468 struct sub_thandle *st;
469 struct top_multiple_thandle *tmt = cb->dcb_data;
470 bool all_committed = true;
473 /* Check if all sub thandles are committed */
474 spin_lock(&tmt->tmt_sub_lock);
475 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
476 if (st->st_sub_th == sub_th) {
477 st->st_committed = 1;
480 if (!st->st_committed)
481 all_committed = false;
483 spin_unlock(&tmt->tmt_sub_lock);
485 if (tmt->tmt_result == 0)
486 tmt->tmt_result = err;
489 top_trans_committed_cb(tmt);
491 top_multiple_thandle_dump(tmt, D_INFO);
492 top_multiple_thandle_put(tmt);
496 static void sub_thandle_register_commit_cb(struct sub_thandle *st,
497 struct top_multiple_thandle *tmt)
499 LASSERT(st->st_sub_th != NULL);
500 top_multiple_thandle_get(tmt);
501 st->st_commit_dcb.dcb_func = sub_trans_commit_cb;
502 st->st_commit_dcb.dcb_data = tmt;
503 INIT_LIST_HEAD(&st->st_commit_dcb.dcb_linkage);
504 dt_trans_cb_add(st->st_sub_th, &st->st_commit_dcb);
508 * Sub thandle stop call back
510 * After sub thandle is stopped, it will call this callback to notify
513 * \param[in] th sub thandle to be stopped
514 * \param[in] rc result of sub trans
516 static void sub_trans_stop_cb(struct lu_env *env,
517 struct thandle *sub_th,
518 struct dt_txn_commit_cb *cb, int err)
520 struct sub_thandle *st;
521 struct top_multiple_thandle *tmt = cb->dcb_data;
524 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
528 if (st->st_dt == sub_th->th_dev) {
535 wake_up(&tmt->tmt_stop_waitq);
539 static void sub_thandle_register_stop_cb(struct sub_thandle *st,
540 struct top_multiple_thandle *tmt)
542 st->st_stop_dcb.dcb_func = sub_trans_stop_cb;
543 st->st_stop_dcb.dcb_data = tmt;
544 st->st_stop_dcb.dcb_flags = DCB_TRANS_STOP;
545 INIT_LIST_HEAD(&st->st_stop_dcb.dcb_linkage);
546 dt_trans_cb_add(st->st_sub_th, &st->st_stop_dcb);
552 * Create transaction handle for sub_thandle
554 * \param[in] env execution environment
555 * \param[in] th top thandle
556 * \param[in] st sub_thandle
558 * \retval 0 if creation succeeds.
559 * \retval negative errno if creation fails.
561 int sub_thandle_trans_create(const struct lu_env *env,
562 struct top_thandle *top_th,
563 struct sub_thandle *st)
565 struct thandle *sub_th;
567 sub_th = dt_trans_create(env, st->st_dt);
569 return PTR_ERR(sub_th);
571 sub_th->th_top = &top_th->tt_super;
572 st->st_sub_th = sub_th;
574 sub_th->th_wait_submit = 1;
575 sub_thandle_register_stop_cb(st, top_th->tt_multiple_thandle);
580 * Create the top transaction.
582 * Create the top transaction on the master device. It will create a top
583 * thandle and a sub thandle on the master device.
585 * \param[in] env execution environment
586 * \param[in] master_dev master_dev the top thandle will be created
588 * \retval pointer to the created thandle.
589 * \retval ERR_PTR(errno) if creation failed.
592 top_trans_create(const struct lu_env *env, struct dt_device *master_dev)
594 struct top_thandle *top_th;
595 struct thandle *child_th;
597 OBD_ALLOC_GFP(top_th, sizeof(*top_th), __GFP_IO);
599 return ERR_PTR(-ENOMEM);
601 top_th->tt_super.th_top = &top_th->tt_super;
603 if (master_dev != NULL) {
604 child_th = dt_trans_create(env, master_dev);
605 if (IS_ERR(child_th)) {
606 OBD_FREE_PTR(top_th);
610 child_th->th_top = &top_th->tt_super;
611 child_th->th_wait_submit = 1;
612 top_th->tt_master_sub_thandle = child_th;
614 top_th->tt_super.th_tags |= child_th->th_tags;
616 return &top_th->tt_super;
618 EXPORT_SYMBOL(top_trans_create);
621 * Declare write update transaction
623 * Check if there are updates being recorded in this transaction,
624 * it will write the record into the disk.
626 * \param[in] env execution environment
627 * \param[in] tmt top multiple transaction handle
629 * \retval 0 if writing succeeds
630 * \retval negative errno if writing fails
632 static int declare_updates_write(const struct lu_env *env,
633 struct top_multiple_thandle *tmt)
635 struct llog_update_record *record;
636 struct sub_thandle *st;
639 record = tmt->tmt_update_records->tur_update_records;
640 /* Declare update write for all other target */
641 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
642 if (st->st_sub_th == NULL)
645 rc = sub_declare_updates_write(env, record, st->st_sub_th,
646 tmt->tmt_record_size);
655 * Assign batchid to the distribute transaction.
657 * Assign batchid to the distribute transaction
659 * \param[in] tmt distribute transaction
661 static void distribute_txn_assign_batchid(struct top_multiple_thandle *new)
663 struct target_distribute_txn_data *tdtd;
664 struct dt_device *dt = new->tmt_master_sub_dt;
665 struct sub_thandle *st;
668 tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
669 spin_lock(&tdtd->tdtd_batchid_lock);
670 new->tmt_batchid = tdtd->tdtd_batchid++;
671 list_add_tail(&new->tmt_commit_list, &tdtd->tdtd_list);
672 spin_unlock(&tdtd->tdtd_batchid_lock);
673 list_for_each_entry(st, &new->tmt_sub_thandle_list, st_sub_list) {
674 if (st->st_sub_th != NULL)
675 sub_thandle_register_commit_cb(st, new);
677 top_multiple_thandle_get(new);
678 top_multiple_thandle_dump(new, D_INFO);
682 * Insert distribute transaction to the distribute txn list.
684 * Insert distribute transaction to the distribute txn list.
686 * \param[in] new the distribute txn to be inserted.
688 void distribute_txn_insert_by_batchid(struct top_multiple_thandle *new)
690 struct dt_device *dt = new->tmt_master_sub_dt;
691 struct top_multiple_thandle *tmt;
692 struct target_distribute_txn_data *tdtd;
693 struct sub_thandle *st;
694 bool at_head = false;
697 tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
699 spin_lock(&tdtd->tdtd_batchid_lock);
700 list_for_each_entry_reverse(tmt, &tdtd->tdtd_list, tmt_commit_list) {
701 if (new->tmt_batchid > tmt->tmt_batchid) {
702 list_add(&new->tmt_commit_list, &tmt->tmt_commit_list);
706 if (list_empty(&new->tmt_commit_list)) {
708 list_add(&new->tmt_commit_list, &tdtd->tdtd_list);
710 spin_unlock(&tdtd->tdtd_batchid_lock);
712 list_for_each_entry(st, &new->tmt_sub_thandle_list, st_sub_list) {
713 if (st->st_sub_th != NULL)
714 sub_thandle_register_commit_cb(st, new);
717 top_multiple_thandle_get(new);
718 top_multiple_thandle_dump(new, D_INFO);
719 if (new->tmt_committed && at_head)
720 wake_up(&tdtd->tdtd_commit_thread_waitq);
724 * Prepare cross-MDT operation.
726 * Create the update record buffer to record updates for cross-MDT operation,
727 * add master sub transaction to tt_sub_trans_list, and declare the update
730 * During updates packing, all of parameters will be packed in
731 * tur_update_params, and updates will be packed in tur_update_records.
732 * Then in transaction stop, parameters and updates will be merged
733 * into one updates buffer.
735 * And also master thandle will be added to the sub_th list, so it will be
736 * easy to track the commit status.
738 * \param[in] env execution environment
739 * \param[in] th top transaction handle
741 * \retval 0 if preparation succeeds.
742 * \retval negative errno if preparation fails.
744 static int prepare_multiple_node_trans(const struct lu_env *env,
745 struct top_multiple_thandle *tmt)
747 struct thandle_update_records *tur;
751 if (tmt->tmt_update_records == NULL) {
752 tur = &update_env_info(env)->uti_tur;
753 rc = check_and_prepare_update_record(env, tur);
757 tmt->tmt_update_records = tur;
758 distribute_txn_assign_batchid(tmt);
761 rc = declare_updates_write(env, tmt);
767 * start the top transaction.
769 * Start all of its sub transactions, then start master sub transaction.
771 * \param[in] env execution environment
772 * \param[in] master_dev master_dev the top thandle will be start
773 * \param[in] th top thandle
775 * \retval 0 if transaction start succeeds.
776 * \retval negative errno if start fails.
778 int top_trans_start(const struct lu_env *env, struct dt_device *master_dev,
781 struct top_thandle *top_th = container_of(th, struct top_thandle,
783 struct sub_thandle *st;
784 struct top_multiple_thandle *tmt = top_th->tt_multiple_thandle;
790 top_th->tt_master_sub_thandle->th_sync = th->th_sync;
792 top_th->tt_master_sub_thandle->th_local = th->th_local;
793 top_th->tt_master_sub_thandle->th_tags = th->th_tags;
794 rc = dt_trans_start(env, top_th->tt_master_sub_thandle->th_dev,
795 top_th->tt_master_sub_thandle);
799 tmt = top_th->tt_multiple_thandle;
800 rc = prepare_multiple_node_trans(env, tmt);
804 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
805 if (st->st_sub_th == NULL)
808 st->st_sub_th->th_sync = th->th_sync;
810 st->st_sub_th->th_local = th->th_local;
811 st->st_sub_th->th_tags = th->th_tags;
812 rc = dt_trans_start(env, st->st_sub_th->th_dev,
821 EXPORT_SYMBOL(top_trans_start);
824 * Check whether we need write updates record
826 * Check if the updates for the top_thandle needs to be writen
827 * to all targets. Only if the transaction succeeds and the updates
828 * number > 2, it will write the updates,
830 * \params [in] top_th top thandle.
832 * \retval true if it needs to write updates
833 * \retval false if it does not need to write updates
835 static bool top_check_write_updates(struct top_thandle *top_th)
837 struct top_multiple_thandle *tmt;
838 struct thandle_update_records *tur;
840 /* Do not write updates to records if the transaction fails */
841 if (top_th->tt_super.th_result != 0)
844 tmt = top_th->tt_multiple_thandle;
848 tur = tmt->tmt_update_records;
852 /* Hmm, false update records, since the cross-MDT operation
853 * should includes both local and remote updates, so the
854 * updates count should >= 2 */
855 if (tur->tur_update_records == NULL ||
856 tur->tur_update_records->lur_update_rec.ur_update_count <= 1)
863 * Check if top transaction is stopped
865 * Check if top transaction is stopped, only if all sub transaction
866 * is stopped, then the top transaction is stopped.
868 * \param [in] top_th top thandle
870 * \retval true if the top transaction is stopped.
871 * \retval false if the top transaction is not stopped.
873 static bool top_trans_is_stopped(struct top_thandle *top_th)
875 struct top_multiple_thandle *tmt;
876 struct sub_thandle *st;
877 bool all_stopped = true;
879 tmt = top_th->tt_multiple_thandle;
880 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
881 if (!st->st_stopped && st->st_sub_th != NULL) {
886 if (st->st_result != 0 &&
887 top_th->tt_super.th_result == 0)
888 top_th->tt_super.th_result = st->st_result;
895 * Wait result of top transaction
897 * Wait until all sub transaction get its result.
899 * \param [in] top_th top thandle.
901 * \retval the result of top thandle.
903 static int top_trans_wait_result(struct top_thandle *top_th)
905 struct l_wait_info lwi = {0};
907 l_wait_event(top_th->tt_multiple_thandle->tmt_stop_waitq,
908 top_trans_is_stopped(top_th), &lwi);
910 RETURN(top_th->tt_super.th_result);
914 * Stop the top transaction.
916 * Stop the transaction on the master device first, then stop transactions
917 * on other sub devices.
919 * \param[in] env execution environment
920 * \param[in] master_dev master_dev the top thandle will be created
921 * \param[in] th top thandle
923 * \retval 0 if stop transaction succeeds.
924 * \retval negative errno if stop transaction fails.
926 int top_trans_stop(const struct lu_env *env, struct dt_device *master_dev,
929 struct top_thandle *top_th = container_of(th, struct top_thandle,
931 struct sub_thandle *st;
932 struct sub_thandle *master_st;
933 struct top_multiple_thandle *tmt;
934 struct thandle_update_records *tur;
935 bool write_updates = false;
939 if (likely(top_th->tt_multiple_thandle == NULL)) {
940 LASSERT(master_dev != NULL);
943 top_th->tt_master_sub_thandle->th_sync = th->th_sync;
945 top_th->tt_master_sub_thandle->th_local = th->th_local;
946 top_th->tt_master_sub_thandle->th_tags = th->th_tags;
947 rc = dt_trans_stop(env, master_dev,
948 top_th->tt_master_sub_thandle);
949 OBD_FREE_PTR(top_th);
953 tmt = top_th->tt_multiple_thandle;
954 tur = tmt->tmt_update_records;
956 /* Note: we need stop the master thandle first, then the stop
957 * callback will fill the master transno in the update logs,
958 * then these update logs will be sent to other MDTs */
959 /* get the master sub thandle */
960 master_st = lookup_sub_thandle(tmt, tmt->tmt_master_sub_dt);
961 write_updates = top_check_write_updates(top_th);
963 /* Step 1: write the updates log on Master MDT */
964 if (master_st != NULL && master_st->st_sub_th != NULL &&
966 struct llog_update_record *lur;
968 /* Merge the parameters and updates into one buffer */
969 rc = prepare_writing_updates(env, tmt);
971 CERROR("%s: cannot prepare updates: rc = %d\n",
972 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
974 GOTO(stop_master_trans, rc);
977 lur = tur->tur_update_records;
978 /* Write updates to the master MDT */
979 rc = sub_updates_write(env, lur, master_st);
981 /* Cleanup the common parameters in the update records,
982 * master transno callback might add more parameters.
983 * and we need merge the update records again in the
985 if (tur->tur_update_params != NULL)
986 lur->lur_update_rec.ur_param_count = 0;
989 CERROR("%s: write updates failed: rc = %d\n",
990 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
992 GOTO(stop_master_trans, rc);
997 /* Step 2: Stop the transaction on the master MDT, and fill the
998 * master transno in the update logs to other MDT. */
999 if (master_st != NULL && master_st->st_sub_th != NULL) {
1001 master_st->st_sub_th->th_local = th->th_local;
1003 master_st->st_sub_th->th_sync = th->th_sync;
1004 master_st->st_sub_th->th_tags = th->th_tags;
1005 master_st->st_sub_th->th_result = th->th_result;
1006 rc = dt_trans_stop(env, master_st->st_dt, master_st->st_sub_th);
1009 GOTO(stop_other_trans, rc);
1010 } else if (tur != NULL && tur->tur_update_records != NULL) {
1011 struct llog_update_record *lur;
1013 lur = tur->tur_update_records;
1014 if (lur->lur_update_rec.ur_master_transno == 0)
1015 /* Update master transno after master stop
1017 lur->lur_update_rec.ur_master_transno =
1018 tgt_th_info(env)->tti_transno;
1022 /* Step 3: write updates to other MDTs */
1023 if (write_updates) {
1024 struct llog_update_record *lur;
1026 /* Stop callback of master will add more updates and also update
1027 * master transno, so merge the parameters and updates into one
1029 rc = prepare_writing_updates(env, tmt);
1031 CERROR("%s: prepare updates failed: rc = %d\n",
1032 master_dev->dd_lu_dev.ld_obd->obd_name, rc);
1034 GOTO(stop_other_trans, rc);
1036 lur = tur->tur_update_records;
1037 list_for_each_entry(st, &tmt->tmt_sub_thandle_list,
1039 if (st->st_sub_th == NULL || st == master_st ||
1040 st->st_sub_th->th_result < 0)
1043 rc = sub_updates_write(env, lur, st);
1052 /* Step 4: Stop the transaction on other MDTs */
1053 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
1054 if (st == master_st || st->st_sub_th == NULL)
1058 st->st_sub_th->th_sync = th->th_sync;
1060 st->st_sub_th->th_local = th->th_local;
1061 st->st_sub_th->th_tags = th->th_tags;
1062 st->st_sub_th->th_result = th->th_result;
1063 rc = dt_trans_stop(env, st->st_sub_th->th_dev,
1065 if (unlikely(rc < 0 && th->th_result == 0))
1069 rc = top_trans_wait_result(top_th);
1071 tmt->tmt_result = rc;
1073 /* Balance for the refcount in top_trans_create, Note: if it is NOT
1074 * multiple node transaction, the top transaction will be destroyed. */
1075 top_multiple_thandle_put(tmt);
1076 OBD_FREE_PTR(top_th);
1079 EXPORT_SYMBOL(top_trans_stop);
1082 * Create top_multiple_thandle for top_thandle
1084 * Create top_mutilple_thandle to manage the mutiple node transaction
1085 * for top_thandle, and it also needs to add master sub thandle to the
1086 * sub trans list now.
1088 * \param[in] env execution environment
1089 * \param[in] top_th the top thandle
1091 * \retval 0 if creation succeeds
1092 * \retval negative errno if creation fails
1094 int top_trans_create_tmt(const struct lu_env *env,
1095 struct top_thandle *top_th)
1097 struct top_multiple_thandle *tmt;
1103 tmt->tmt_magic = TOP_THANDLE_MAGIC;
1104 INIT_LIST_HEAD(&tmt->tmt_sub_thandle_list);
1105 INIT_LIST_HEAD(&tmt->tmt_commit_list);
1106 atomic_set(&tmt->tmt_refcount, 1);
1107 spin_lock_init(&tmt->tmt_sub_lock);
1108 init_waitqueue_head(&tmt->tmt_stop_waitq);
1110 top_th->tt_multiple_thandle = tmt;
1115 static struct sub_thandle *
1116 create_sub_thandle_with_thandle(struct top_thandle *top_th,
1117 struct thandle *sub_th)
1119 struct sub_thandle *st;
1121 /* create and init sub th to the top trans list */
1122 st = create_sub_thandle(top_th->tt_multiple_thandle,
1127 st->st_sub_th = sub_th;
1129 sub_th->th_top = &top_th->tt_super;
1130 sub_thandle_register_stop_cb(st, top_th->tt_multiple_thandle);
1137 * Get sub thandle from the top thandle according to the sub dt_device.
1139 * \param[in] env execution environment
1140 * \param[in] th thandle on the top layer.
1141 * \param[in] sub_dt sub dt_device used to get sub transaction
1143 * \retval thandle of sub transaction if succeed
1144 * \retval PTR_ERR(errno) if failed
1146 struct thandle *thandle_get_sub_by_dt(const struct lu_env *env,
1148 struct dt_device *sub_dt)
1150 struct sub_thandle *st = NULL;
1151 struct sub_thandle *master_st = NULL;
1152 struct top_thandle *top_th;
1153 struct thandle *sub_th = NULL;
1157 top_th = container_of(th, struct top_thandle, tt_super);
1159 if (likely(sub_dt == top_th->tt_master_sub_thandle->th_dev))
1160 RETURN(top_th->tt_master_sub_thandle);
1162 if (top_th->tt_multiple_thandle != NULL) {
1163 st = lookup_sub_thandle(top_th->tt_multiple_thandle, sub_dt);
1165 RETURN(st->st_sub_th);
1168 sub_th = dt_trans_create(env, sub_dt);
1172 /* Create top_multiple_thandle if necessary */
1173 if (top_th->tt_multiple_thandle == NULL) {
1174 struct top_multiple_thandle *tmt;
1176 rc = top_trans_create_tmt(env, top_th);
1178 GOTO(stop_trans, rc);
1180 tmt = top_th->tt_multiple_thandle;
1182 /* Add master sub th to the top trans list */
1183 tmt->tmt_master_sub_dt =
1184 top_th->tt_master_sub_thandle->th_dev;
1185 master_st = create_sub_thandle_with_thandle(top_th,
1186 top_th->tt_master_sub_thandle);
1187 if (IS_ERR(master_st)) {
1188 rc = PTR_ERR(master_st);
1190 GOTO(stop_trans, rc);
1194 /* create and init sub th to the top trans list */
1195 st = create_sub_thandle_with_thandle(top_th, sub_th);
1199 GOTO(stop_trans, rc);
1201 st->st_sub_th->th_wait_submit = 1;
1204 if (master_st != NULL) {
1205 list_del(&master_st->st_sub_list);
1206 OBD_FREE_PTR(master_st);
1208 sub_th->th_result = rc;
1209 dt_trans_stop(env, sub_dt, sub_th);
1210 sub_th = ERR_PTR(rc);
1215 EXPORT_SYMBOL(thandle_get_sub_by_dt);
1218 * Top multiple thandle destroy
1220 * Destroy multiple thandle and all its sub thandle.
1222 * \param[in] tmt top_multiple_thandle to be destroyed.
1224 void top_multiple_thandle_destroy(struct top_multiple_thandle *tmt)
1226 struct sub_thandle *st;
1227 struct sub_thandle *tmp;
1229 LASSERT(tmt->tmt_magic == TOP_THANDLE_MAGIC);
1230 list_for_each_entry_safe(st, tmp, &tmt->tmt_sub_thandle_list,
1232 struct sub_thandle_cookie *stc;
1233 struct sub_thandle_cookie *tmp;
1235 list_del(&st->st_sub_list);
1236 list_for_each_entry_safe(stc, tmp, &st->st_cookie_list,
1238 list_del(&stc->stc_list);
1245 EXPORT_SYMBOL(top_multiple_thandle_destroy);
1248 * Cancel the update log on MDTs
1250 * Cancel the update log on MDTs then destroy the thandle.
1252 * \param[in] env execution environment
1253 * \param[in] tmt the top multiple thandle whose updates records
1254 * will be cancelled.
1256 * \retval 0 if cancellation succeeds.
1257 * \retval negative errno if cancellation fails.
1259 static int distribute_txn_cancel_records(const struct lu_env *env,
1260 struct top_multiple_thandle *tmt)
1262 struct sub_thandle *st;
1265 top_multiple_thandle_dump(tmt, D_INFO);
1266 /* Cancel update logs on other MDTs */
1267 list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
1268 struct llog_ctxt *ctxt;
1269 struct obd_device *obd;
1270 struct llog_cookie *cookie;
1271 struct sub_thandle_cookie *stc;
1274 obd = st->st_dt->dd_lu_dev.ld_obd;
1275 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
1278 list_for_each_entry(stc, &st->st_cookie_list, stc_list) {
1279 cookie = &stc->stc_cookie;
1280 if (fid_is_zero(&cookie->lgc_lgl.lgl_oi.oi_fid))
1283 rc = llog_cat_cancel_records(env, ctxt->loc_handle, 1,
1285 CDEBUG(D_HA, "%s: batchid %llu cancel update log "
1286 DOSTID ".%u : rc = %d\n", obd->obd_name,
1288 POSTID(&cookie->lgc_lgl.lgl_oi),
1289 cookie->lgc_index, rc);
1292 llog_ctxt_put(ctxt);
1299 * Check if there are committed transaction
1301 * Check if there are committed transaction in the distribute transaction
1302 * list, then cancel the update records for those committed transaction.
1303 * Because the distribute transaction in the list are sorted by batchid,
1304 * and cancellation will be done by batchid order, so we only check the first
1305 * the transaction(with lowest batchid) in the list.
1307 * \param[in] lod lod device where cancel thread is
1309 * \retval true if it is ready
1310 * \retval false if it is not ready
1312 static bool tdtd_ready_for_cancel_log(struct target_distribute_txn_data *tdtd)
1314 struct top_multiple_thandle *tmt = NULL;
1315 struct obd_device *obd = tdtd->tdtd_lut->lut_obd;
1318 spin_lock(&tdtd->tdtd_batchid_lock);
1319 if (!list_empty(&tdtd->tdtd_list)) {
1320 tmt = list_entry(tdtd->tdtd_list.next,
1321 struct top_multiple_thandle, tmt_commit_list);
1322 if (tmt->tmt_committed &&
1323 (!obd->obd_recovering || (obd->obd_recovering &&
1324 tmt->tmt_batchid <= tdtd->tdtd_committed_batchid)))
1327 spin_unlock(&tdtd->tdtd_batchid_lock);
1332 struct distribute_txn_bid_data {
1333 struct dt_txn_commit_cb dtbd_cb;
1334 struct target_distribute_txn_data *dtbd_tdtd;
1339 * callback of updating commit batchid
1341 * Updating commit batchid then wake up the commit thread to cancel the
1344 * \param[in]env execution environment
1345 * \param[in]th thandle to updating commit batchid
1346 * \param[in]cb commit callback
1347 * \param[in]err result of thandle
1349 static void distribute_txn_batchid_cb(struct lu_env *env,
1351 struct dt_txn_commit_cb *cb,
1354 struct distribute_txn_bid_data *dtbd = NULL;
1355 struct target_distribute_txn_data *tdtd;
1357 dtbd = container_of0(cb, struct distribute_txn_bid_data, dtbd_cb);
1358 tdtd = dtbd->dtbd_tdtd;
1360 CDEBUG(D_HA, "%s: %llu batchid updated\n",
1361 tdtd->tdtd_lut->lut_obd->obd_name, dtbd->dtbd_batchid);
1362 spin_lock(&tdtd->tdtd_batchid_lock);
1363 if (dtbd->dtbd_batchid > tdtd->tdtd_committed_batchid &&
1364 !tdtd->tdtd_lut->lut_obd->obd_no_transno)
1365 tdtd->tdtd_committed_batchid = dtbd->dtbd_batchid;
1366 spin_unlock(&tdtd->tdtd_batchid_lock);
1367 atomic_dec(&tdtd->tdtd_refcount);
1368 wake_up(&tdtd->tdtd_commit_thread_waitq);
1374 * Update the commit batchid in disk
1376 * Update commit batchid in the disk, after this is committed, it can start
1377 * to cancel the update records.
1379 * \param[in] env execution environment
1380 * \param[in] tdtd distribute transaction structure
1381 * \param[in] batchid commit batchid to be updated
1383 * \retval 0 if update succeeds.
1384 * \retval negative errno if update fails.
1387 distribute_txn_commit_batchid_update(const struct lu_env *env,
1388 struct target_distribute_txn_data *tdtd,
1391 struct distribute_txn_bid_data *dtbd = NULL;
1399 OBD_ALLOC_PTR(dtbd);
1402 dtbd->dtbd_batchid = batchid;
1403 dtbd->dtbd_tdtd = tdtd;
1404 dtbd->dtbd_cb.dcb_func = distribute_txn_batchid_cb;
1405 atomic_inc(&tdtd->tdtd_refcount);
1407 th = dt_trans_create(env, tdtd->tdtd_lut->lut_bottom);
1410 RETURN(PTR_ERR(th));
1413 tmp = cpu_to_le64(batchid);
1415 buf.lb_len = sizeof(tmp);
1418 rc = dt_declare_record_write(env, tdtd->tdtd_batchid_obj, &buf, off,
1423 rc = dt_trans_start_local(env, tdtd->tdtd_lut->lut_bottom, th);
1427 rc = dt_trans_cb_add(th, &dtbd->dtbd_cb);
1431 rc = dt_record_write(env, tdtd->tdtd_batchid_obj, &buf,
1434 CDEBUG(D_INFO, "%s: update batchid "LPU64": rc = %d\n",
1435 tdtd->tdtd_lut->lut_obd->obd_name, batchid, rc);
1438 dt_trans_stop(env, tdtd->tdtd_lut->lut_bottom, th);
1445 * Init commit batchid for distribute transaction.
1447 * Initialize the batchid object and get commit batchid from the object.
1449 * \param[in] env execution environment
1450 * \param[in] tdtd distribute transaction whose batchid is initialized.
1452 * \retval 0 if initialization succeeds.
1453 * \retval negative errno if initialization fails.
1456 distribute_txn_commit_batchid_init(const struct lu_env *env,
1457 struct target_distribute_txn_data *tdtd)
1459 struct tgt_thread_info *tti = tgt_th_info(env);
1460 struct lu_target *lut = tdtd->tdtd_lut;
1461 struct lu_attr *attr = &tti->tti_attr;
1462 struct lu_fid *fid = &tti->tti_fid1;
1463 struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
1464 struct dt_object *dt_obj = NULL;
1471 memset(attr, 0, sizeof(*attr));
1472 attr->la_valid = LA_MODE;
1473 attr->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1474 dof->dof_type = dt_mode_to_dft(S_IFREG);
1476 lu_local_obj_fid(fid, BATCHID_COMMITTED_OID);
1478 dt_obj = dt_find_or_create(env, lut->lut_bottom, fid, dof,
1480 if (IS_ERR(dt_obj)) {
1481 rc = PTR_ERR(dt_obj);
1486 tdtd->tdtd_batchid_obj = dt_obj;
1489 buf.lb_len = sizeof(tmp);
1491 rc = dt_read(env, dt_obj, &buf, &off);
1492 if (rc < 0 || (rc < buf.lb_len && rc > 0)) {
1493 CERROR("%s can't read last committed batchid: rc = %d\n",
1494 tdtd->tdtd_lut->lut_obd->obd_name, rc);
1498 } else if (rc == buf.lb_len) {
1499 tdtd->tdtd_committed_batchid = le64_to_cpu(tmp);
1500 CDEBUG(D_HA, "%s: committed batchid %llu\n",
1501 tdtd->tdtd_lut->lut_obd->obd_name,
1502 tdtd->tdtd_committed_batchid);
1507 if (rc < 0 && dt_obj != NULL) {
1508 lu_object_put(env, &dt_obj->do_lu);
1509 tdtd->tdtd_batchid_obj = NULL;
1515 * manage the distribute transaction thread
1517 * Distribute transaction are linked to the list, and once the distribute
1518 * transaction is committed, it will update the last committed batchid first,
1519 * after it is committed, it will cancel the records.
1521 * \param[in] _arg argument for commit thread
1523 * \retval 0 if thread is running successfully
1524 * \retval negative errno if the thread can not be run.
1526 static int distribute_txn_commit_thread(void *_arg)
1528 struct target_distribute_txn_data *tdtd = _arg;
1529 struct lu_target *lut = tdtd->tdtd_lut;
1530 struct ptlrpc_thread *thread = &lut->lut_tdtd_commit_thread;
1531 struct l_wait_info lwi = { 0 };
1533 struct list_head list;
1535 struct top_multiple_thandle *tmt;
1536 struct top_multiple_thandle *tmp;
1537 __u64 batchid = 0, committed;
1541 rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
1545 spin_lock(&tdtd->tdtd_batchid_lock);
1546 thread->t_flags = SVC_RUNNING;
1547 spin_unlock(&tdtd->tdtd_batchid_lock);
1548 wake_up(&thread->t_ctl_waitq);
1549 INIT_LIST_HEAD(&list);
1551 CDEBUG(D_HA, "%s: start commit thread committed batchid "LPU64"\n",
1552 tdtd->tdtd_lut->lut_obd->obd_name,
1553 tdtd->tdtd_committed_batchid);
1555 while (distribute_txn_commit_thread_running(lut)) {
1556 spin_lock(&tdtd->tdtd_batchid_lock);
1557 list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1559 if (tmt->tmt_committed == 0)
1562 /* Note: right now, replay is based on master MDT
1563 * transno, but cancellation is based on batchid.
1564 * so we do not try to cancel the update log until
1565 * the recoverying is done, unless the update records
1566 * batchid < committed_batchid. */
1567 if (tmt->tmt_batchid <= tdtd->tdtd_committed_batchid) {
1568 list_move_tail(&tmt->tmt_commit_list, &list);
1569 } else if (!tdtd->tdtd_lut->lut_obd->obd_recovering) {
1570 LASSERTF(tmt->tmt_batchid >= batchid,
1571 "tmt %p tmt_batchid: "LPU64", batchid "
1572 LPU64"\n", tmt, tmt->tmt_batchid,
1574 /* There are three types of distribution
1575 * transaction result
1577 * 1. If tmt_result < 0, it means the
1578 * distribution transaction fails, which should
1579 * be rare, because once declare phase succeeds,
1580 * the operation should succeeds anyway. Note in
1581 * this case, we will still update batchid so
1582 * cancellation would be stopped.
1584 * 2. If tmt_result == 0, it means the
1585 * distribution transaction succeeds, and we
1586 * will update batchid.
1588 * 3. If tmt_result > 0, it means distribute
1589 * transaction is not yet committed on every
1590 * node, but we need release this tmt before
1591 * that, which usuually happens during umount.
1593 if (tmt->tmt_result <= 0)
1594 batchid = tmt->tmt_batchid;
1595 list_move_tail(&tmt->tmt_commit_list, &list);
1598 spin_unlock(&tdtd->tdtd_batchid_lock);
1600 CDEBUG(D_HA, "%s: batchid: "LPU64" committed batchid "
1601 LPU64"\n", tdtd->tdtd_lut->lut_obd->obd_name, batchid,
1602 tdtd->tdtd_committed_batchid);
1603 /* update globally committed on a storage */
1604 if (batchid > tdtd->tdtd_committed_batchid) {
1605 rc = distribute_txn_commit_batchid_update(&env, tdtd,
1610 /* cancel the records for committed batchid's */
1611 /* XXX: should we postpone cancel's till the end of recovery? */
1612 committed = tdtd->tdtd_committed_batchid;
1613 list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1614 if (tmt->tmt_batchid > committed)
1616 list_del_init(&tmt->tmt_commit_list);
1617 if (tmt->tmt_result <= 0)
1618 distribute_txn_cancel_records(&env, tmt);
1619 top_multiple_thandle_put(tmt);
1622 l_wait_event(tdtd->tdtd_commit_thread_waitq,
1623 !distribute_txn_commit_thread_running(lut) ||
1624 committed < tdtd->tdtd_committed_batchid ||
1625 tdtd_ready_for_cancel_log(tdtd), &lwi);
1628 l_wait_event(tdtd->tdtd_commit_thread_waitq,
1629 atomic_read(&tdtd->tdtd_refcount) == 0, &lwi);
1631 spin_lock(&tdtd->tdtd_batchid_lock);
1632 list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1634 list_move_tail(&tmt->tmt_commit_list, &list);
1635 spin_unlock(&tdtd->tdtd_batchid_lock);
1637 CDEBUG(D_INFO, "%s stopping distribute txn commit thread.\n",
1638 tdtd->tdtd_lut->lut_obd->obd_name);
1639 list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1640 list_del_init(&tmt->tmt_commit_list);
1641 top_multiple_thandle_dump(tmt, D_HA);
1642 top_multiple_thandle_put(tmt);
1645 thread->t_flags = SVC_STOPPED;
1647 wake_up(&thread->t_ctl_waitq);
1653 * Start llog cancel thread
1655 * Start llog cancel(master/slave) thread on LOD
1657 * \param[in]lclt cancel log thread to be started.
1659 * \retval 0 if the thread is started successfully.
1660 * \retval negative errno if the thread is not being
1663 int distribute_txn_init(const struct lu_env *env,
1664 struct lu_target *lut,
1665 struct target_distribute_txn_data *tdtd,
1668 struct task_struct *task;
1669 struct l_wait_info lwi = { 0 };
1673 INIT_LIST_HEAD(&tdtd->tdtd_list);
1674 INIT_LIST_HEAD(&tdtd->tdtd_replay_finish_list);
1675 INIT_LIST_HEAD(&tdtd->tdtd_replay_list);
1676 spin_lock_init(&tdtd->tdtd_batchid_lock);
1677 spin_lock_init(&tdtd->tdtd_replay_list_lock);
1678 tdtd->tdtd_replay_handler = distribute_txn_replay_handle;
1679 tdtd->tdtd_replay_ready = 0;
1681 tdtd->tdtd_batchid = lut->lut_last_transno + 1;
1683 init_waitqueue_head(&lut->lut_tdtd_commit_thread.t_ctl_waitq);
1684 init_waitqueue_head(&tdtd->tdtd_commit_thread_waitq);
1685 atomic_set(&tdtd->tdtd_refcount, 0);
1687 tdtd->tdtd_lut = lut;
1688 rc = distribute_txn_commit_batchid_init(env, tdtd);
1692 task = kthread_run(distribute_txn_commit_thread, tdtd, "tdtd-%u",
1695 RETURN(PTR_ERR(task));
1697 l_wait_event(lut->lut_tdtd_commit_thread.t_ctl_waitq,
1698 distribute_txn_commit_thread_running(lut) ||
1699 distribute_txn_commit_thread_stopped(lut), &lwi);
1702 EXPORT_SYMBOL(distribute_txn_init);
1705 * Stop llog cancel thread
1707 * Stop llog cancel(master/slave) thread on LOD and also destory
1708 * all of transaction in the list.
1710 * \param[in]lclt cancel log thread to be stopped.
1712 void distribute_txn_fini(const struct lu_env *env,
1713 struct target_distribute_txn_data *tdtd)
1715 struct lu_target *lut = tdtd->tdtd_lut;
1717 /* Stop cancel thread */
1718 if (lut == NULL || !distribute_txn_commit_thread_running(lut))
1721 spin_lock(&tdtd->tdtd_batchid_lock);
1722 lut->lut_tdtd_commit_thread.t_flags = SVC_STOPPING;
1723 spin_unlock(&tdtd->tdtd_batchid_lock);
1724 wake_up(&tdtd->tdtd_commit_thread_waitq);
1725 wait_event(lut->lut_tdtd_commit_thread.t_ctl_waitq,
1726 lut->lut_tdtd_commit_thread.t_flags & SVC_STOPPED);
1728 dtrq_list_destroy(tdtd);
1729 if (tdtd->tdtd_batchid_obj != NULL) {
1730 lu_object_put(env, &tdtd->tdtd_batchid_obj->do_lu);
1731 tdtd->tdtd_batchid_obj = NULL;
1734 EXPORT_SYMBOL(distribute_txn_fini);