Whamcloud - gitweb
LU-6473 mdt: enqueue lookup lock on the parent MDT
[fs/lustre-release.git] / lustre / target / update_trans.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2014, Intel Corporation.
24  */
25 /*
26  * lustre/target/update_trans.c
27  *
28  * This file implements the update distribute transaction API.
29  *
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.
33  *
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.
39  *
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
45  *    OSD.
46  *
47  * Author: Di Wang <di.wang@intel.com>
48  */
49
50 #define DEBUG_SUBSYSTEM S_CLASS
51
52 #include <linux/kthread.h>
53 #include <lu_target.h>
54 #include <lustre_log.h>
55 #include <lustre_update.h>
56 #include <obd.h>
57 #include <obd_class.h>
58 #include <tgt_internal.h>
59
60 #include <tgt_internal.h>
61 /**
62  * Dump top mulitple thandle
63  *
64  * Dump top multiple thandle and all of its sub thandle to the debug log.
65  *
66  * \param[in]mask       debug mask
67  * \param[in]top_th     top_thandle to be dumped
68  */
69 static void top_multiple_thandle_dump(struct top_multiple_thandle *tmt,
70                                       __u32 mask)
71 {
72         struct sub_thandle      *st;
73
74         LASSERT(tmt->tmt_magic == TOP_THANDLE_MAGIC);
75         CDEBUG(mask, "%s tmt %p refcount %d committed %d result %d"
76                "batchid "LPU64"\n",
77                tmt->tmt_master_sub_dt ?
78                tmt->tmt_master_sub_dt->dd_lu_dev.ld_obd->obd_name :
79                "NULL",
80                tmt, atomic_read(&tmt->tmt_refcount), tmt->tmt_committed,
81                tmt->tmt_result, tmt->tmt_batchid);
82
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);
90         }
91 }
92
93 /**
94  * Declare write update to sub device
95  *
96  * Declare Write updates llog records to the sub device during distribute
97  * transaction.
98  *
99  * \param[in] env       execution environment
100  * \param[in] record    update records being written
101  * \param[in] sub_th    sub transaction handle
102  *
103  * \retval              0 if writing succeeds
104  * \retval              negative errno if writing fails
105  */
106 static int sub_declare_updates_write(const struct lu_env *env,
107                                      struct llog_update_record *record,
108                                      struct thandle *sub_th)
109 {
110         struct llog_ctxt        *ctxt;
111         struct dt_device        *dt = sub_th->th_dev;
112         int rc;
113
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);
119
120         /* Not ready to record updates yet. */
121         if (ctxt->loc_handle == NULL) {
122                 llog_ctxt_put(ctxt);
123                 return 0;
124         }
125
126         rc = llog_declare_add(env, ctxt->loc_handle, &record->lur_hdr,
127                               sub_th);
128
129         llog_ctxt_put(ctxt);
130
131         return rc;
132 }
133
134 /**
135  * write update to sub device
136  *
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.
140  *
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.
145  *
146  * \retval              1 if writing succeeds
147  * \retval              negative errno if writing fails
148  */
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)
153 {
154         struct dt_device        *dt = sub_th->th_dev;
155         struct llog_ctxt        *ctxt;
156         int                     rc;
157         ENTRY;
158
159         ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
160                                 LLOG_UPDATELOG_ORIG_CTXT);
161         LASSERT(ctxt != NULL);
162
163         /* Not ready to record updates yet, usually happens
164          * in error handler path */
165         if (ctxt->loc_handle == NULL) {
166                 llog_ctxt_put(ctxt);
167                 RETURN(0);
168         }
169
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));
176
177         rc = llog_add(env, ctxt->loc_handle, &record->lur_hdr,
178                       cookie, sub_th);
179         llog_ctxt_put(ctxt);
180
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);
184
185         if (rc > 0)
186                 rc = 0;
187
188         RETURN(rc);
189 }
190
191 /**
192  * Prepare the update records.
193  *
194  * Merge params and ops into the update records, then initializing
195  * the update buffer.
196  *
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.
201  *
202  * \param[in] env       execution environment
203  * \param[in] tmt       top_multiple_thandle for distribute txn
204  *
205  * \retval              0 if merging succeeds.
206  * \retval              negaitive errno if merging fails.
207  */
208 static int prepare_writing_updates(const struct lu_env *env,
209                                    struct top_multiple_thandle *tmt)
210 {
211         struct thandle_update_records   *tur = tmt->tmt_update_records;
212         struct llog_update_record       *lur;
213         struct update_params *params;
214         size_t params_size;
215         size_t update_size;
216
217         if (tur == NULL || tur->tur_update_records == NULL ||
218             tur->tur_update_params == NULL)
219                 return 0;
220
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) {
229                 int rc;
230
231                 rc = tur_update_records_extend(tur,
232                         cfs_size_round(update_size + params_size));
233                 if (rc < 0)
234                         return rc;
235
236                 lur = tur->tur_update_records;
237         }
238
239         params = update_records_get_params(&lur->lur_update_rec);
240         memcpy(params, tur->tur_update_params, params_size);
241
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;
247
248         /* Dump updates for debugging purpose */
249         update_records_dump(&lur->lur_update_rec, D_INFO, true);
250
251         return 0;
252 }
253
254 static inline int
255 distribute_txn_commit_thread_running(struct lu_target *lut)
256 {
257         return lut->lut_tdtd_commit_thread.t_flags & SVC_RUNNING;
258 }
259
260 static inline int
261 distribute_txn_commit_thread_stopped(struct lu_target *lut)
262 {
263         return lut->lut_tdtd_commit_thread.t_flags & SVC_STOPPED;
264 }
265
266 /**
267  * Top thandle commit callback
268  *
269  * This callback will be called when all of sub transactions are committed.
270  *
271  * \param[in] th        top thandle to be committed.
272  */
273 static void top_trans_committed_cb(struct top_multiple_thandle *tmt)
274 {
275         struct lu_target *lut;
276         ENTRY;
277
278         LASSERT(atomic_read(&tmt->tmt_refcount) > 0);
279
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);
285         RETURN_EXIT;
286 }
287
288 struct sub_thandle *lookup_sub_thandle(struct top_multiple_thandle *tmt,
289                                        struct dt_device *dt_dev)
290 {
291         struct sub_thandle *st;
292
293         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
294                 if (st->st_dt == dt_dev)
295                         return st;
296         }
297         return NULL;
298 }
299 EXPORT_SYMBOL(lookup_sub_thandle);
300
301 struct sub_thandle *create_sub_thandle(struct top_multiple_thandle *tmt,
302                                        struct dt_device *dt_dev)
303 {
304         struct sub_thandle *st;
305
306         OBD_ALLOC_PTR(st);
307         if (st == NULL)
308                 RETURN(ERR_PTR(-ENOMEM));
309
310         INIT_LIST_HEAD(&st->st_sub_list);
311         st->st_dt = dt_dev;
312
313         list_add(&st->st_sub_list, &tmt->tmt_sub_thandle_list);
314         return st;
315 }
316
317 /**
318  * Create sub thandle
319  *
320  * Create transaction handle for sub_thandle
321  *
322  * \param[in] env       execution environment
323  * \param[in] th        top thandle
324  * \param[in] st        sub_thandle
325  *
326  * \retval              0 if creation succeeds.
327  * \retval              negative errno if creation fails.
328  */
329 int sub_thandle_trans_create(const struct lu_env *env,
330                              struct top_thandle *top_th,
331                              struct sub_thandle *st)
332 {
333         struct thandle *sub_th;
334
335         sub_th = dt_trans_create(env, st->st_dt);
336         if (IS_ERR(sub_th))
337                 return PTR_ERR(sub_th);
338
339         sub_th->th_top = &top_th->tt_super;
340         st->st_sub_th = sub_th;
341         return 0;
342 }
343
344 /**
345  * sub thandle commit callback
346  *
347  * Mark the sub thandle to be committed and if all sub thandle are committed
348  * notify the top thandle.
349  *
350  * \param[in] sub_th    sub thandle being committed.
351  */
352 static void sub_trans_commit_cb(struct lu_env *env, struct thandle *sub_th,
353                                 struct dt_txn_commit_cb *cb, int err)
354 {
355         struct sub_thandle      *st;
356         struct top_multiple_thandle *tmt = cb->dcb_data;
357         bool                    all_committed = true;
358         ENTRY;
359
360         /* Check if all sub thandles are committed */
361         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
362                 if (st->st_sub_th == sub_th) {
363                         st->st_committed = 1;
364                         st->st_result = err;
365                 }
366                 if (!st->st_committed)
367                         all_committed = false;
368         }
369
370         if (tmt->tmt_result == 0)
371                 tmt->tmt_result = err;
372
373         if (all_committed)
374                 top_trans_committed_cb(tmt);
375
376         top_multiple_thandle_dump(tmt, D_INFO);
377         top_multiple_thandle_put(tmt);
378         RETURN_EXIT;
379 }
380
381 static void sub_thandle_register_commit_cb(struct sub_thandle *st,
382                                     struct top_multiple_thandle *tmt)
383 {
384         LASSERT(st->st_sub_th != NULL);
385         top_multiple_thandle_get(tmt);
386         st->st_commit_dcb.dcb_func = sub_trans_commit_cb;
387         st->st_commit_dcb.dcb_data = tmt;
388         INIT_LIST_HEAD(&st->st_commit_dcb.dcb_linkage);
389         dt_trans_cb_add(st->st_sub_th, &st->st_commit_dcb);
390 }
391
392 /**
393  * Create the top transaction.
394  *
395  * Create the top transaction on the master device. It will create a top
396  * thandle and a sub thandle on the master device.
397  *
398  * \param[in] env               execution environment
399  * \param[in] master_dev        master_dev the top thandle will be created
400  *
401  * \retval                      pointer to the created thandle.
402  * \retval                      ERR_PTR(errno) if creation failed.
403  */
404 struct thandle *
405 top_trans_create(const struct lu_env *env, struct dt_device *master_dev)
406 {
407         struct top_thandle      *top_th;
408         struct thandle          *child_th;
409
410         OBD_ALLOC_GFP(top_th, sizeof(*top_th), __GFP_IO);
411         if (top_th == NULL)
412                 return ERR_PTR(-ENOMEM);
413
414         top_th->tt_super.th_top = &top_th->tt_super;
415
416         if (master_dev != NULL) {
417                 child_th = dt_trans_create(env, master_dev);
418                 if (IS_ERR(child_th)) {
419                         OBD_FREE_PTR(top_th);
420                         return child_th;
421                 }
422
423                 child_th->th_top = &top_th->tt_super;
424                 child_th->th_wait_submit = 1;
425                 top_th->tt_master_sub_thandle = child_th;
426
427                 top_th->tt_super.th_tags |= child_th->th_tags;
428         }
429         return &top_th->tt_super;
430 }
431 EXPORT_SYMBOL(top_trans_create);
432
433 /**
434  * Declare write update transaction
435  *
436  * Check if there are updates being recorded in this transaction,
437  * it will write the record into the disk.
438  *
439  * \param[in] env       execution environment
440  * \param[in] tmt       top multiple transaction handle
441  *
442  * \retval              0 if writing succeeds
443  * \retval              negative errno if writing fails
444  */
445 static int declare_updates_write(const struct lu_env *env,
446                                  struct top_multiple_thandle *tmt)
447 {
448         struct llog_update_record *record;
449         struct sub_thandle *st;
450         int rc = 0;
451
452         record = tmt->tmt_update_records->tur_update_records;
453         /* Declare update write for all other target */
454         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
455                 if (st->st_sub_th == NULL)
456                         continue;
457
458                 rc = sub_declare_updates_write(env, record, st->st_sub_th);
459                 if (rc < 0)
460                         break;
461         }
462
463         return rc;
464 }
465
466 /**
467  * Assign batchid to the distribute transaction.
468  *
469  * Assign batchid to the distribute transaction
470  *
471  * \param[in] tmt       distribute transaction
472  *
473  */
474 static void distribute_txn_assign_batchid(struct top_multiple_thandle *new)
475 {
476         struct target_distribute_txn_data *tdtd;
477         struct dt_device *dt = new->tmt_master_sub_dt;
478
479         LASSERT(dt != NULL);
480         tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
481         spin_lock(&tdtd->tdtd_batchid_lock);
482         new->tmt_batchid = tdtd->tdtd_batchid++;
483         list_add_tail(&new->tmt_commit_list, &tdtd->tdtd_list);
484         spin_unlock(&tdtd->tdtd_batchid_lock);
485         top_multiple_thandle_get(new);
486         top_multiple_thandle_dump(new, D_INFO);
487 }
488
489 /**
490  * Insert distribute transaction to the distribute txn list.
491  *
492  * Insert distribute transaction to the distribute txn list.
493  *
494  * \param[in] new       the distribute txn to be inserted.
495  */
496 void distribute_txn_insert_by_batchid(struct top_multiple_thandle *new)
497 {
498         struct dt_device *dt = new->tmt_master_sub_dt;
499         struct top_multiple_thandle *tmt;
500         struct target_distribute_txn_data *tdtd;
501         bool    at_head = false;
502
503         LASSERT(dt != NULL);
504         tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
505
506         spin_lock(&tdtd->tdtd_batchid_lock);
507         list_for_each_entry_reverse(tmt, &tdtd->tdtd_list, tmt_commit_list) {
508                 if (new->tmt_batchid > tmt->tmt_batchid) {
509                         list_add(&new->tmt_commit_list, &tmt->tmt_commit_list);
510                         break;
511                 }
512         }
513         if (list_empty(&new->tmt_commit_list)) {
514                 at_head = true;
515                 list_add(&new->tmt_commit_list, &tdtd->tdtd_list);
516         }
517         spin_unlock(&tdtd->tdtd_batchid_lock);
518         top_multiple_thandle_get(new);
519         top_multiple_thandle_dump(new, D_INFO);
520         if (new->tmt_committed && at_head)
521                 wake_up(&tdtd->tdtd_commit_thread_waitq);
522 }
523
524 /**
525  * Prepare cross-MDT operation.
526  *
527  * Create the update record buffer to record updates for cross-MDT operation,
528  * add master sub transaction to tt_sub_trans_list, and declare the update
529  * writes.
530  *
531  * During updates packing, all of parameters will be packed in
532  * tur_update_params, and updates will be packed in tur_update_records.
533  * Then in transaction stop, parameters and updates will be merged
534  * into one updates buffer.
535  *
536  * And also master thandle will be added to the sub_th list, so it will be
537  * easy to track the commit status.
538  *
539  * \param[in] env       execution environment
540  * \param[in] th        top transaction handle
541  *
542  * \retval              0 if preparation succeeds.
543  * \retval              negative errno if preparation fails.
544  */
545 static int prepare_multiple_node_trans(const struct lu_env *env,
546                                        struct top_multiple_thandle *tmt)
547 {
548         struct thandle_update_records   *tur;
549         int                             rc;
550         ENTRY;
551
552         /* Prepare the update buffer for recording updates */
553         if (tmt->tmt_update_records != NULL)
554                 RETURN(0);
555
556         tur = &update_env_info(env)->uti_tur;
557         rc = check_and_prepare_update_record(env, tur);
558         if (rc < 0)
559                 RETURN(rc);
560
561         tmt->tmt_update_records = tur;
562
563         distribute_txn_assign_batchid(tmt);
564         rc = declare_updates_write(env, tmt);
565
566         RETURN(rc);
567 }
568
569 /**
570  * start the top transaction.
571  *
572  * Start all of its sub transactions, then start master sub transaction.
573  *
574  * \param[in] env               execution environment
575  * \param[in] master_dev        master_dev the top thandle will be start
576  * \param[in] th                top thandle
577  *
578  * \retval                      0 if transaction start succeeds.
579  * \retval                      negative errno if start fails.
580  */
581 int top_trans_start(const struct lu_env *env, struct dt_device *master_dev,
582                     struct thandle *th)
583 {
584         struct top_thandle      *top_th = container_of(th, struct top_thandle,
585                                                        tt_super);
586         struct sub_thandle              *st;
587         struct top_multiple_thandle     *tmt = top_th->tt_multiple_thandle;
588         int                             rc = 0;
589         ENTRY;
590
591         /* Walk through all of sub transaction to see if it needs to
592          * record updates for this transaction */
593         if (tmt == NULL) {
594                 rc = dt_trans_start(env, top_th->tt_master_sub_thandle->th_dev,
595                                     top_th->tt_master_sub_thandle);
596                 RETURN(rc);
597         }
598
599         rc = prepare_multiple_node_trans(env, tmt);
600         if (rc < 0)
601                 RETURN(rc);
602
603         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
604                 if (st->st_sub_th == NULL)
605                         continue;
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,
610                                     st->st_sub_th);
611                 if (rc != 0)
612                         RETURN(rc);
613
614                 sub_thandle_register_commit_cb(st, tmt);
615         }
616
617         RETURN(rc);
618 }
619 EXPORT_SYMBOL(top_trans_start);
620
621 /**
622  * Check whether we need write updates record
623  *
624  * Check if the updates for the top_thandle needs to be writen
625  * to all targets. Only if the transaction succeeds and the updates
626  * number > 2, it will write the updates,
627  *
628  * \params [in] top_th  top thandle.
629  *
630  * \retval              true if it needs to write updates
631  * \retval              false if it does not need to write updates
632  **/
633 static bool top_check_write_updates(struct top_thandle *top_th)
634 {
635         struct top_multiple_thandle     *tmt;
636         struct thandle_update_records   *tur;
637
638         /* Do not write updates to records if the transaction fails */
639         if (top_th->tt_super.th_result != 0)
640                 return false;
641
642         tmt = top_th->tt_multiple_thandle;
643         if (tmt == NULL)
644                 return false;
645
646         tur = tmt->tmt_update_records;
647         if (tur == NULL)
648                 return false;
649
650         /* Hmm, false update records, since the cross-MDT operation
651          * should includes both local and remote updates, so the
652          * updates count should >= 2 */
653         if (tur->tur_update_records == NULL ||
654             tur->tur_update_records->lur_update_rec.ur_update_count <= 1)
655                 return false;
656
657         return true;
658 }
659
660 /**
661  * Stop the top transaction.
662  *
663  * Stop the transaction on the master device first, then stop transactions
664  * on other sub devices.
665  *
666  * \param[in] env               execution environment
667  * \param[in] master_dev        master_dev the top thandle will be created
668  * \param[in] th                top thandle
669  *
670  * \retval                      0 if stop transaction succeeds.
671  * \retval                      negative errno if stop transaction fails.
672  */
673 int top_trans_stop(const struct lu_env *env, struct dt_device *master_dev,
674                    struct thandle *th)
675 {
676         struct top_thandle      *top_th = container_of(th, struct top_thandle,
677                                                        tt_super);
678         struct sub_thandle              *st;
679         struct sub_thandle              *master_st;
680         struct top_multiple_thandle     *tmt;
681         struct thandle_update_records   *tur;
682         bool                            write_updates = false;
683         int                     rc = 0;
684         ENTRY;
685
686         if (likely(top_th->tt_multiple_thandle == NULL)) {
687                 rc = dt_trans_stop(env, master_dev,
688                                    top_th->tt_master_sub_thandle);
689                 OBD_FREE_PTR(top_th);
690                 RETURN(rc);
691         }
692
693         tmt = top_th->tt_multiple_thandle;
694         tur = tmt->tmt_update_records;
695
696         /* Note: we need stop the master thandle first, then the stop
697          * callback will fill the master transno in the update logs,
698          * then these update logs will be sent to other MDTs */
699         /* get the master sub thandle */
700         master_st = lookup_sub_thandle(tmt, tmt->tmt_master_sub_dt);
701         if (master_st == NULL) {
702                 top_multiple_thandle_dump(tmt, D_ERROR);
703                 if (th->th_result == 0)
704                         LBUG();
705         }
706
707         write_updates = top_check_write_updates(top_th);
708
709         /* Step 1: write the updates log on Master MDT */
710         if (master_st != NULL && master_st->st_sub_th != NULL &&
711             write_updates) {
712                 struct llog_update_record *lur;
713
714                 /* Merge the parameters and updates into one buffer */
715                 rc = prepare_writing_updates(env, tmt);
716                 if (rc < 0) {
717                         CERROR("%s: cannot prepare updates: rc = %d\n",
718                                master_dev->dd_lu_dev.ld_obd->obd_name, rc);
719                         th->th_result = rc;
720                         GOTO(stop_master_trans, rc);
721                 }
722
723                 lur = tur->tur_update_records;
724                 /* Write updates to the master MDT */
725                 rc = sub_updates_write(env, lur, top_th->tt_master_sub_thandle,
726                                        &master_st->st_cookie);
727
728                 /* Cleanup the common parameters in the update records,
729                  * master transno callback might add more parameters.
730                  * and we need merge the update records again in the
731                  * following */
732                 if (tur->tur_update_params != NULL)
733                         lur->lur_update_rec.ur_param_count = 0;
734
735                 if (rc < 0) {
736                         CERROR("%s: write updates failed: rc = %d\n",
737                                master_dev->dd_lu_dev.ld_obd->obd_name, rc);
738                         th->th_result = rc;
739                         GOTO(stop_master_trans, rc);
740                 }
741         }
742
743 stop_master_trans:
744         /* Step 2: Stop the transaction on the master MDT, and fill the
745          * master transno in the update logs to other MDT. */
746         if (master_st != NULL && master_st->st_sub_th != NULL) {
747                 master_st->st_sub_th->th_local = th->th_local;
748                 master_st->st_sub_th->th_sync = th->th_sync;
749                 master_st->st_sub_th->th_tags = th->th_tags;
750                 master_st->st_sub_th->th_result = th->th_result;
751                 rc = dt_trans_stop(env, master_st->st_dt, master_st->st_sub_th);
752                 if (rc < 0) {
753                         th->th_result = rc;
754                         GOTO(stop_other_trans, rc);
755                 } else if (tur != NULL && tur->tur_update_records != NULL) {
756                         struct llog_update_record *lur;
757
758                         lur = tur->tur_update_records;
759                         if (lur->lur_update_rec.ur_master_transno == 0)
760                                 /* Update master transno after master stop
761                                  * callback */
762                                 lur->lur_update_rec.ur_master_transno =
763                                                 tgt_th_info(env)->tti_transno;
764                 }
765         }
766
767         /* Step 3: write updates to other MDTs */
768         if (write_updates) {
769                 struct llog_update_record *lur;
770
771                 /* Stop callback of master will add more updates and also update
772                  * master transno, so merge the parameters and updates into one
773                  * buffer again */
774                 rc = prepare_writing_updates(env, tmt);
775                 if (rc < 0) {
776                         CERROR("%s: prepare updates failed: rc = %d\n",
777                                master_dev->dd_lu_dev.ld_obd->obd_name, rc);
778                         th->th_result = rc;
779                         GOTO(stop_other_trans, rc);
780                 }
781                 lur = tur->tur_update_records;
782                 list_for_each_entry(st, &tmt->tmt_sub_thandle_list,
783                                     st_sub_list) {
784                         if (st->st_sub_th == NULL || st == master_st ||
785                             st->st_sub_th->th_result < 0)
786                                 continue;
787
788                         rc = sub_updates_write(env, lur, st->st_sub_th,
789                                                &st->st_cookie);
790                         if (rc < 0)
791                                 break;
792                 }
793         }
794
795 stop_other_trans:
796         /* Step 4: Stop the transaction on other MDTs */
797         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
798                 if (st == master_st || st->st_sub_th == NULL)
799                         continue;
800
801                 st->st_sub_th->th_sync = th->th_sync;
802                 st->st_sub_th->th_local = th->th_local;
803                 st->st_sub_th->th_tags = th->th_tags;
804                 st->st_sub_th->th_result = th->th_result;
805                 rc = dt_trans_stop(env, st->st_sub_th->th_dev,
806                                    st->st_sub_th);
807                 if (unlikely(rc < 0 && th->th_result == 0))
808                         th->th_result = rc;
809         }
810
811         tmt->tmt_result = rc;
812         /* Balance for the refcount in top_trans_create, Note: if it is NOT
813          * multiple node transaction, the top transaction will be destroyed. */
814         top_multiple_thandle_put(tmt);
815         OBD_FREE_PTR(top_th);
816         RETURN(rc);
817 }
818 EXPORT_SYMBOL(top_trans_stop);
819
820 /**
821  * Create top_multiple_thandle for top_thandle
822  *
823  * Create top_mutilple_thandle to manage the mutiple node transaction
824  * for top_thandle, and it also needs to add master sub thandle to the
825  * sub trans list now.
826  *
827  * \param[in] env       execution environment
828  * \param[in] top_th    the top thandle
829  *
830  * \retval      0 if creation succeeds
831  * \retval      negative errno if creation fails
832  */
833 static int top_trans_create_tmt(const struct lu_env *env,
834                                 struct top_thandle *top_th)
835 {
836         struct top_multiple_thandle *tmt;
837
838         OBD_ALLOC_PTR(tmt);
839         if (tmt == NULL)
840                 return -ENOMEM;
841
842         tmt->tmt_magic = TOP_THANDLE_MAGIC;
843         INIT_LIST_HEAD(&tmt->tmt_sub_thandle_list);
844         INIT_LIST_HEAD(&tmt->tmt_commit_list);
845         atomic_set(&tmt->tmt_refcount, 1);
846
847         top_th->tt_multiple_thandle = tmt;
848
849         return 0;
850 }
851
852 static struct sub_thandle *
853 create_sub_thandle_with_thandle(struct top_thandle *top_th,
854                                 struct thandle *sub_th)
855 {
856         struct sub_thandle *st;
857
858         /* create and init sub th to the top trans list */
859         st = create_sub_thandle(top_th->tt_multiple_thandle,
860                                 sub_th->th_dev);
861         if (IS_ERR(st))
862                 return st;
863
864         st->st_sub_th = sub_th;
865         sub_th->th_top = &top_th->tt_super;
866         return st;
867 }
868
869 /**
870  * Get sub thandle.
871  *
872  * Get sub thandle from the top thandle according to the sub dt_device.
873  *
874  * \param[in] env       execution environment
875  * \param[in] th        thandle on the top layer.
876  * \param[in] sub_dt    sub dt_device used to get sub transaction
877  *
878  * \retval              thandle of sub transaction if succeed
879  * \retval              PTR_ERR(errno) if failed
880  */
881 struct thandle *thandle_get_sub_by_dt(const struct lu_env *env,
882                                       struct thandle *th,
883                                       struct dt_device *sub_dt)
884 {
885         struct sub_thandle      *st = NULL;
886         struct top_thandle      *top_th;
887         struct thandle          *sub_th = NULL;
888         int                     rc = 0;
889         ENTRY;
890
891         top_th = container_of(th, struct top_thandle, tt_super);
892
893         if (likely(sub_dt == top_th->tt_master_sub_thandle->th_dev))
894                 RETURN(top_th->tt_master_sub_thandle);
895
896         if (top_th->tt_multiple_thandle != NULL) {
897                 st = lookup_sub_thandle(top_th->tt_multiple_thandle, sub_dt);
898                 if (st != NULL)
899                         RETURN(st->st_sub_th);
900         }
901
902         sub_th = dt_trans_create(env, sub_dt);
903         if (IS_ERR(sub_th))
904                 RETURN(sub_th);
905
906         /* Create top_multiple_thandle if necessary */
907         if (top_th->tt_multiple_thandle == NULL) {
908                 struct top_multiple_thandle *tmt;
909
910                 rc = top_trans_create_tmt(env, top_th);
911                 if (rc < 0)
912                         GOTO(stop_trans, rc);
913
914                 tmt = top_th->tt_multiple_thandle;
915
916                 /* Add master sub th to the top trans list */
917                 tmt->tmt_master_sub_dt =
918                         top_th->tt_master_sub_thandle->th_dev;
919                 st = create_sub_thandle_with_thandle(top_th,
920                                 top_th->tt_master_sub_thandle);
921                 if (IS_ERR(st))
922                         GOTO(stop_trans, rc = PTR_ERR(st));
923                 top_th->tt_master_sub_thandle->th_sync = 1;
924                 top_th->tt_super.th_sync = 1;
925         }
926
927         /* create and init sub th to the top trans list */
928         st = create_sub_thandle_with_thandle(top_th, sub_th);
929         st->st_sub_th->th_wait_submit = 1;
930 stop_trans:
931         if (rc < 0) {
932                 if (st != NULL)
933                         OBD_FREE_PTR(st);
934                 sub_th->th_result = rc;
935                 dt_trans_stop(env, sub_dt, sub_th);
936                 sub_th = ERR_PTR(rc);
937         }
938
939         RETURN(sub_th);
940 }
941 EXPORT_SYMBOL(thandle_get_sub_by_dt);
942
943 /**
944  * Top multiple thandle destroy
945  *
946  * Destroy multiple thandle and all its sub thandle.
947  *
948  * \param[in] tmt       top_multiple_thandle to be destroyed.
949  */
950 void top_multiple_thandle_destroy(struct top_multiple_thandle *tmt)
951 {
952         struct sub_thandle *st;
953         struct sub_thandle *tmp;
954
955         LASSERT(tmt->tmt_magic == TOP_THANDLE_MAGIC);
956         list_for_each_entry_safe(st, tmp, &tmt->tmt_sub_thandle_list,
957                                  st_sub_list) {
958                 list_del(&st->st_sub_list);
959                 OBD_FREE_PTR(st);
960         }
961         OBD_FREE_PTR(tmt);
962 }
963 EXPORT_SYMBOL(top_multiple_thandle_destroy);
964
965 /**
966  * Cancel the update log on MDTs
967  *
968  * Cancel the update log on MDTs then destroy the thandle.
969  *
970  * \param[in] env       execution environment
971  * \param[in] tmt       the top multiple thandle whose updates records
972  *                      will be cancelled.
973  *
974  * \retval              0 if cancellation succeeds.
975  * \retval              negative errno if cancellation fails.
976  */
977 static int distribute_txn_cancel_records(const struct lu_env *env,
978                                          struct top_multiple_thandle *tmt)
979 {
980         struct sub_thandle *st;
981         ENTRY;
982
983         top_multiple_thandle_dump(tmt, D_INFO);
984         /* Cancel update logs on other MDTs */
985         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
986                 struct llog_ctxt        *ctxt;
987                 struct obd_device       *obd;
988                 struct llog_cookie      *cookie;
989                 int rc;
990
991                 cookie = &st->st_cookie;
992                 if (fid_is_zero(&cookie->lgc_lgl.lgl_oi.oi_fid))
993                         continue;
994
995                 obd = st->st_dt->dd_lu_dev.ld_obd;
996                 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
997                 LASSERT(ctxt);
998
999                 rc = llog_cat_cancel_records(env, ctxt->loc_handle, 1,
1000                                              cookie);
1001
1002                 llog_ctxt_put(ctxt);
1003                 CDEBUG(D_HA, "%s: batchid %llu cancel update log "DOSTID
1004                        ".%u : rc = %d\n", obd->obd_name, tmt->tmt_batchid,
1005                        POSTID(&cookie->lgc_lgl.lgl_oi), cookie->lgc_index, rc);
1006         }
1007
1008         RETURN(0);
1009 }
1010
1011 /**
1012  * Check if there are committed transaction
1013  *
1014  * Check if there are committed transaction in the distribute transaction
1015  * list, then cancel the update records for those committed transaction.
1016  * Because the distribute transaction in the list are sorted by batchid,
1017  * and cancellation will be done by batchid order, so we only check the first
1018  * the transaction(with lowest batchid) in the list.
1019  *
1020  * \param[in] lod       lod device where cancel thread is
1021  *
1022  * \retval              true if it is ready
1023  * \retval              false if it is not ready
1024  */
1025 static bool tdtd_ready_for_cancel_log(struct target_distribute_txn_data *tdtd)
1026 {
1027         struct top_multiple_thandle     *tmt = NULL;
1028         struct obd_device               *obd = tdtd->tdtd_lut->lut_obd;
1029         bool    ready = false;
1030
1031         spin_lock(&tdtd->tdtd_batchid_lock);
1032         if (!list_empty(&tdtd->tdtd_list)) {
1033                 tmt = list_entry(tdtd->tdtd_list.next,
1034                                  struct top_multiple_thandle, tmt_commit_list);
1035                 if (tmt->tmt_committed &&
1036                     (!obd->obd_recovering || (obd->obd_recovering &&
1037                     tmt->tmt_batchid <= tdtd->tdtd_committed_batchid)))
1038                         ready = true;
1039         }
1040         spin_unlock(&tdtd->tdtd_batchid_lock);
1041
1042         return ready;
1043 }
1044
1045 struct distribute_txn_bid_data {
1046         struct dt_txn_commit_cb  dtbd_cb;
1047         struct target_distribute_txn_data      *dtbd_tdtd;
1048         __u64                    dtbd_batchid;
1049 };
1050
1051 /**
1052  * callback of updating commit batchid
1053  *
1054  * Updating commit batchid then wake up the commit thread to cancel the
1055  * records.
1056  *
1057  * \param[in]env        execution environment
1058  * \param[in]th         thandle to updating commit batchid
1059  * \param[in]cb         commit callback
1060  * \param[in]err        result of thandle
1061  */
1062 static void distribute_txn_batchid_cb(struct lu_env *env,
1063                                       struct thandle *th,
1064                                       struct dt_txn_commit_cb *cb,
1065                                       int err)
1066 {
1067         struct distribute_txn_bid_data          *dtbd = NULL;
1068         struct target_distribute_txn_data       *tdtd;
1069
1070         dtbd = container_of0(cb, struct distribute_txn_bid_data, dtbd_cb);
1071         tdtd = dtbd->dtbd_tdtd;
1072
1073         CDEBUG(D_HA, "%s: %llu batchid updated\n",
1074               tdtd->tdtd_lut->lut_obd->obd_name, dtbd->dtbd_batchid);
1075         spin_lock(&tdtd->tdtd_batchid_lock);
1076         if (dtbd->dtbd_batchid > tdtd->tdtd_committed_batchid &&
1077             !tdtd->tdtd_lut->lut_obd->obd_no_transno)
1078                 tdtd->tdtd_committed_batchid = dtbd->dtbd_batchid;
1079         spin_unlock(&tdtd->tdtd_batchid_lock);
1080         atomic_dec(&tdtd->tdtd_refcount);
1081         wake_up(&tdtd->tdtd_commit_thread_waitq);
1082
1083         OBD_FREE_PTR(dtbd);
1084 }
1085
1086 /**
1087  * Update the commit batchid in disk
1088  *
1089  * Update commit batchid in the disk, after this is committed, it can start
1090  * to cancel the update records.
1091  *
1092  * \param[in] env       execution environment
1093  * \param[in] tdtd      distribute transaction structure
1094  * \param[in] batchid   commit batchid to be updated
1095  *
1096  * \retval              0 if update succeeds.
1097  * \retval              negative errno if update fails.
1098  */
1099 static int
1100 distribute_txn_commit_batchid_update(const struct lu_env *env,
1101                               struct target_distribute_txn_data *tdtd,
1102                               __u64 batchid)
1103 {
1104         struct distribute_txn_bid_data  *dtbd = NULL;
1105         struct thandle          *th;
1106         struct lu_buf            buf;
1107         __u64                    tmp;
1108         __u64                    off;
1109         int                      rc;
1110         ENTRY;
1111
1112         OBD_ALLOC_PTR(dtbd);
1113         if (dtbd == NULL)
1114                 RETURN(-ENOMEM);
1115         dtbd->dtbd_batchid = batchid;
1116         dtbd->dtbd_tdtd = tdtd;
1117         dtbd->dtbd_cb.dcb_func = distribute_txn_batchid_cb;
1118         atomic_inc(&tdtd->tdtd_refcount);
1119
1120         th = dt_trans_create(env, tdtd->tdtd_lut->lut_bottom);
1121         if (IS_ERR(th)) {
1122                 OBD_FREE_PTR(dtbd);
1123                 RETURN(PTR_ERR(th));
1124         }
1125
1126         tmp = cpu_to_le64(batchid);
1127         buf.lb_buf = &tmp;
1128         buf.lb_len = sizeof(tmp);
1129         off = 0;
1130
1131         rc = dt_declare_record_write(env, tdtd->tdtd_batchid_obj, &buf, off,
1132                                      th);
1133         if (rc < 0)
1134                 GOTO(stop, rc);
1135
1136         rc = dt_trans_start_local(env, tdtd->tdtd_lut->lut_bottom, th);
1137         if (rc < 0)
1138                 GOTO(stop, rc);
1139
1140         dt_trans_cb_add(th, &dtbd->dtbd_cb);
1141         if (rc < 0)
1142                 GOTO(stop, rc);
1143
1144         rc = dt_record_write(env, tdtd->tdtd_batchid_obj, &buf,
1145                              &off, th);
1146
1147         CDEBUG(D_INFO, "%s: update batchid "LPU64": rc = %d\n",
1148                tdtd->tdtd_lut->lut_obd->obd_name, batchid, rc);
1149
1150 stop:
1151         dt_trans_stop(env, tdtd->tdtd_lut->lut_bottom, th);
1152         if (rc < 0)
1153                 OBD_FREE_PTR(dtbd);
1154         RETURN(rc);
1155 }
1156
1157 /**
1158  * Init commit batchid for distribute transaction.
1159  *
1160  * Initialize the batchid object and get commit batchid from the object.
1161  *
1162  * \param[in] env       execution environment
1163  * \param[in] tdtd      distribute transaction whose batchid is initialized.
1164  *
1165  * \retval              0 if initialization succeeds.
1166  * \retval              negative errno if initialization fails.
1167  **/
1168 static int
1169 distribute_txn_commit_batchid_init(const struct lu_env *env,
1170                                    struct target_distribute_txn_data *tdtd)
1171 {
1172         struct tgt_thread_info  *tti = tgt_th_info(env);
1173         struct lu_target        *lut = tdtd->tdtd_lut;
1174         struct lu_attr          *attr = &tti->tti_attr;
1175         struct lu_fid           *fid = &tti->tti_fid1;
1176         struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
1177         struct dt_object        *dt_obj = NULL;
1178         struct lu_buf           buf;
1179         __u64                   tmp;
1180         __u64                   off;
1181         int                     rc;
1182         ENTRY;
1183
1184         memset(attr, 0, sizeof(*attr));
1185         attr->la_valid = LA_MODE;
1186         attr->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1187         dof->dof_type = dt_mode_to_dft(S_IFREG);
1188
1189         lu_local_obj_fid(fid, BATCHID_COMMITTED_OID);
1190
1191         dt_obj = dt_find_or_create(env, lut->lut_bottom, fid, dof,
1192                                    attr);
1193         if (IS_ERR(dt_obj))
1194                 GOTO(out_put, rc = PTR_ERR(dt_obj));
1195
1196         tdtd->tdtd_batchid_obj = dt_obj;
1197
1198         buf.lb_buf = &tmp;
1199         buf.lb_len = sizeof(tmp);
1200         off = 0;
1201         rc = dt_read(env, dt_obj, &buf, &off);
1202         if (rc < 0 || (rc < buf.lb_len && rc > 0)) {
1203                 CERROR("%s can't read last committed batchid: rc = %d\n",
1204                        tdtd->tdtd_lut->lut_obd->obd_name, rc);
1205                 if (rc > 0)
1206                         rc = -EINVAL;
1207                 GOTO(out_put, rc);
1208         } else if (rc == buf.lb_len) {
1209                 tdtd->tdtd_committed_batchid = le64_to_cpu(tmp);
1210                 CDEBUG(D_HA, "%s: committed batchid %llu\n",
1211                        tdtd->tdtd_lut->lut_obd->obd_name,
1212                        tdtd->tdtd_committed_batchid);
1213                 rc = 0;
1214         }
1215
1216 out_put:
1217         if (rc < 0 && dt_obj != NULL) {
1218                 lu_object_put(env, &dt_obj->do_lu);
1219                 tdtd->tdtd_batchid_obj = NULL;
1220         }
1221         return rc;
1222 }
1223
1224 /**
1225  * manage the distribute transaction thread
1226  *
1227  * Distribute transaction are linked to the list, and once the distribute
1228  * transaction is committed, it will update the last committed batchid first,
1229  * after it is committed, it will cancel the records.
1230  *
1231  * \param[in] _arg      argument for commit thread
1232  *
1233  * \retval              0 if thread is running successfully
1234  * \retval              negative errno if the thread can not be run.
1235  */
1236 static int distribute_txn_commit_thread(void *_arg)
1237 {
1238         struct target_distribute_txn_data *tdtd = _arg;
1239         struct lu_target        *lut = tdtd->tdtd_lut;
1240         struct ptlrpc_thread    *thread = &lut->lut_tdtd_commit_thread;
1241         struct l_wait_info       lwi = { 0 };
1242         struct lu_env            env;
1243         struct list_head         list;
1244         int                      rc;
1245         struct top_multiple_thandle *tmt;
1246         struct top_multiple_thandle *tmp;
1247         __u64                    batchid = 0, committed;
1248
1249         ENTRY;
1250
1251         rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
1252         if (rc != 0)
1253                 RETURN(rc);
1254
1255         spin_lock(&tdtd->tdtd_batchid_lock);
1256         thread->t_flags = SVC_RUNNING;
1257         spin_unlock(&tdtd->tdtd_batchid_lock);
1258         wake_up(&thread->t_ctl_waitq);
1259         INIT_LIST_HEAD(&list);
1260
1261         CDEBUG(D_HA, "%s: start commit thread committed batchid "LPU64"\n",
1262                tdtd->tdtd_lut->lut_obd->obd_name,
1263                tdtd->tdtd_committed_batchid);
1264
1265         while (distribute_txn_commit_thread_running(lut)) {
1266                 spin_lock(&tdtd->tdtd_batchid_lock);
1267                 list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1268                                          tmt_commit_list) {
1269                         if (tmt->tmt_committed == 0)
1270                                 break;
1271
1272                         /* Note: right now, replay is based on master MDT
1273                          * transno, but cancellation is based on batchid.
1274                          * so we do not try to cancel the update log until
1275                          * the recoverying is done, unless the update records
1276                          * batchid < committed_batchid. */
1277                         if (tmt->tmt_batchid <= tdtd->tdtd_committed_batchid) {
1278                                 list_move_tail(&tmt->tmt_commit_list, &list);
1279                         } else if (!tdtd->tdtd_lut->lut_obd->obd_recovering) {
1280                                 LASSERTF(tmt->tmt_batchid >= batchid,
1281                                          "tmt %p tmt_batchid: "LPU64", batchid "
1282                                           LPU64"\n", tmt, tmt->tmt_batchid,
1283                                          batchid);
1284                                 /* There are three types of distribution
1285                                  * transaction result
1286                                  *
1287                                  * 1. If tmt_result < 0, it means the
1288                                  * distribution transaction fails, which should
1289                                  * be rare, because once declare phase succeeds,
1290                                  * the operation should succeeds anyway. Note in
1291                                  * this case, we will still update batchid so
1292                                  * cancellation would be stopped.
1293                                  *
1294                                  * 2. If tmt_result == 0, it means the
1295                                  * distribution transaction succeeds, and we
1296                                  * will update batchid.
1297                                  *
1298                                  * 3. If tmt_result > 0, it means distribute
1299                                  * transaction is not yet committed on every
1300                                  * node, but we need release this tmt before
1301                                  * that, which usuually happens during umount.
1302                                  */
1303                                 if (tmt->tmt_result <= 0)
1304                                         batchid = tmt->tmt_batchid;
1305                                 list_move_tail(&tmt->tmt_commit_list, &list);
1306                         }
1307                 }
1308                 spin_unlock(&tdtd->tdtd_batchid_lock);
1309
1310                 CDEBUG(D_HA, "%s: batchid: "LPU64" committed batchid "
1311                        LPU64"\n", tdtd->tdtd_lut->lut_obd->obd_name, batchid,
1312                        tdtd->tdtd_committed_batchid);
1313                 /* update globally committed on a storage */
1314                 if (batchid > tdtd->tdtd_committed_batchid) {
1315                         distribute_txn_commit_batchid_update(&env, tdtd,
1316                                                              batchid);
1317                         spin_lock(&tdtd->tdtd_batchid_lock);
1318                         if (batchid > tdtd->tdtd_batchid) {
1319                                 /* This might happen during recovery,
1320                                  * batchid is initialized as last transno,
1321                                  * and the batchid in the update records
1322                                  * on other MDTs might be bigger than
1323                                  * the batchid, so we need update it to
1324                                  * avoid duplicate batchid. */
1325                                 CDEBUG(D_HA, "%s update batchid from "LPU64
1326                                        " to "LPU64"\n",
1327                                        tdtd->tdtd_lut->lut_obd->obd_name,
1328                                        tdtd->tdtd_batchid, batchid);
1329                                 tdtd->tdtd_batchid = batchid;
1330                         }
1331                         spin_unlock(&tdtd->tdtd_batchid_lock);
1332                 }
1333                 /* cancel the records for committed batchid's */
1334                 /* XXX: should we postpone cancel's till the end of recovery? */
1335                 committed = tdtd->tdtd_committed_batchid;
1336                 list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1337                         if (tmt->tmt_batchid > committed)
1338                                 break;
1339                         list_del_init(&tmt->tmt_commit_list);
1340                         if (tmt->tmt_result <= 0)
1341                                 distribute_txn_cancel_records(&env, tmt);
1342                         top_multiple_thandle_put(tmt);
1343                 }
1344
1345                 l_wait_event(tdtd->tdtd_commit_thread_waitq,
1346                              !distribute_txn_commit_thread_running(lut) ||
1347                              committed < tdtd->tdtd_committed_batchid ||
1348                              tdtd_ready_for_cancel_log(tdtd), &lwi);
1349         };
1350
1351         l_wait_event(tdtd->tdtd_commit_thread_waitq,
1352                      atomic_read(&tdtd->tdtd_refcount) == 0, &lwi);
1353
1354         spin_lock(&tdtd->tdtd_batchid_lock);
1355         list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1356                                  tmt_commit_list)
1357                 list_move_tail(&tmt->tmt_commit_list, &list);
1358         spin_unlock(&tdtd->tdtd_batchid_lock);
1359
1360         CDEBUG(D_INFO, "%s stopping distribute txn commit thread.\n",
1361                tdtd->tdtd_lut->lut_obd->obd_name);
1362         list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1363                 list_del_init(&tmt->tmt_commit_list);
1364                 top_multiple_thandle_dump(tmt, D_HA);
1365                 top_multiple_thandle_put(tmt);
1366         }
1367
1368         thread->t_flags = SVC_STOPPED;
1369         lu_env_fini(&env);
1370         wake_up(&thread->t_ctl_waitq);
1371
1372         RETURN(0);
1373 }
1374
1375 /**
1376  * Start llog cancel thread
1377  *
1378  * Start llog cancel(master/slave) thread on LOD
1379  *
1380  * \param[in]lclt       cancel log thread to be started.
1381  *
1382  * \retval              0 if the thread is started successfully.
1383  * \retval              negative errno if the thread is not being
1384  *                      started.
1385  */
1386 int distribute_txn_init(const struct lu_env *env,
1387                         struct lu_target *lut,
1388                         struct target_distribute_txn_data *tdtd,
1389                         __u32 index)
1390 {
1391         struct task_struct      *task;
1392         struct l_wait_info       lwi = { 0 };
1393         int                     rc;
1394         ENTRY;
1395
1396         spin_lock_init(&tdtd->tdtd_batchid_lock);
1397         INIT_LIST_HEAD(&tdtd->tdtd_list);
1398
1399         tdtd->tdtd_batchid = lut->lut_last_transno + 1;
1400
1401         init_waitqueue_head(&lut->lut_tdtd_commit_thread.t_ctl_waitq);
1402         init_waitqueue_head(&tdtd->tdtd_commit_thread_waitq);
1403         atomic_set(&tdtd->tdtd_refcount, 0);
1404
1405         tdtd->tdtd_lut = lut;
1406         rc = distribute_txn_commit_batchid_init(env, tdtd);
1407         if (rc != 0)
1408                 RETURN(rc);
1409
1410         task = kthread_run(distribute_txn_commit_thread, tdtd, "tdtd-%u",
1411                            index);
1412         if (IS_ERR(task))
1413                 RETURN(PTR_ERR(task));
1414
1415         l_wait_event(lut->lut_tdtd_commit_thread.t_ctl_waitq,
1416                      distribute_txn_commit_thread_running(lut) ||
1417                      distribute_txn_commit_thread_stopped(lut), &lwi);
1418         RETURN(0);
1419 }
1420 EXPORT_SYMBOL(distribute_txn_init);
1421
1422 /**
1423  * Stop llog cancel thread
1424  *
1425  * Stop llog cancel(master/slave) thread on LOD and also destory
1426  * all of transaction in the list.
1427  *
1428  * \param[in]lclt       cancel log thread to be stopped.
1429  */
1430 void distribute_txn_fini(const struct lu_env *env,
1431                          struct target_distribute_txn_data *tdtd)
1432 {
1433         struct lu_target *lut = tdtd->tdtd_lut;
1434
1435         /* Stop cancel thread */
1436         if (lut == NULL || !distribute_txn_commit_thread_running(lut))
1437                 return;
1438
1439         spin_lock(&tdtd->tdtd_batchid_lock);
1440         lut->lut_tdtd_commit_thread.t_flags = SVC_STOPPING;
1441         spin_unlock(&tdtd->tdtd_batchid_lock);
1442         wake_up(&tdtd->tdtd_commit_thread_waitq);
1443         wait_event(lut->lut_tdtd_commit_thread.t_ctl_waitq,
1444                    lut->lut_tdtd_commit_thread.t_flags & SVC_STOPPED);
1445
1446         if (tdtd->tdtd_batchid_obj != NULL)
1447                 lu_object_put(env, &tdtd->tdtd_batchid_obj->do_lu);
1448 }
1449 EXPORT_SYMBOL(distribute_txn_fini);