Whamcloud - gitweb
LU-19098 hsm: don't print progname twice with lhsmtool
[fs/lustre-release.git] / lustre / target / update_trans.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright (c) 2015, 2017, Intel Corporation.
5  */
6
7 /*
8  * This file implements the update distribute transaction API.
9  *
10  * To manage the cross-MDT operation (distribute operation) transaction,
11  * the transaction will also be separated two layers on MD stack, top
12  * transaction and sub transaction.
13  *
14  * During the distribute operation, top transaction is created in the LOD
15  * layer, and represent the operation. Sub transaction is created by
16  * each OSD or OSP. Top transaction start/stop will trigger all of its sub
17  * transaction start/stop. Top transaction (the whole operation) is committed
18  * only all of its sub transaction are committed.
19  *
20  * there are three kinds of transactions
21  * 1. local transaction: All updates are in a single local OSD.
22  * 2. Remote transaction: All Updates are only in the remote OSD,
23  *    i.e. locally all updates are in OSP.
24  * 3. Mixed transaction: Updates are both in local OSD and remote
25  *    OSD.
26  *
27  * Author: Di Wang <di.wang@intel.com>
28  */
29
30 #define DEBUG_SUBSYSTEM S_CLASS
31
32 #include <linux/kthread.h>
33 #include <lu_target.h>
34 #include <lustre_log.h>
35 #include <lustre_update.h>
36 #include <obd.h>
37 #include <obd_class.h>
38 #include <tgt_internal.h>
39
40 #include <tgt_internal.h>
41 /**
42  * Dump top mulitple thandle
43  *
44  * Dump top multiple thandle and all of its sub thandle to the debug log.
45  *
46  * \param[in]mask       debug mask
47  * \param[in]top_th     top_thandle to be dumped
48  */
49 static void top_multiple_thandle_dump(struct top_multiple_thandle *tmt,
50                                       __u32 mask)
51 {
52         struct sub_thandle      *st;
53
54         LASSERT(tmt->tmt_magic == TOP_THANDLE_MAGIC);
55         CDEBUG(mask, "%s tmt %p refcount %d committed %d result %d batchid %llu\n",
56                tmt->tmt_master_sub_dt ?
57                tmt->tmt_master_sub_dt->dd_lu_dev.ld_obd->obd_name :
58                "NULL",
59                tmt, kref_read(&tmt->tmt_refcount), tmt->tmt_committed,
60                tmt->tmt_result, tmt->tmt_batchid);
61
62         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
63                 struct sub_thandle_cookie *stc;
64
65                 CDEBUG(mask, "st %p obd %s committed %d started %d stopped %d "
66                        "result %d sub_th %p\n",
67                        st, st->st_dt->dd_lu_dev.ld_obd->obd_name,
68                        st->st_committed, st->st_started, st->st_stopped,
69                        st->st_result, st->st_sub_th);
70
71                 list_for_each_entry(stc, &st->st_cookie_list, stc_list) {
72                         CDEBUG(mask, " cookie "DFID".%u\n",
73                                PLOGID(&stc->stc_cookie.lgc_lgl),
74                                stc->stc_cookie.lgc_index);
75                 }
76         }
77 }
78
79 /**
80  * Declare write update to sub device
81  *
82  * Declare Write updates llog records to the sub device during distribute
83  * transaction.
84  *
85  * \param[in] env       execution environment
86  * \param[in] record    update records being written
87  * \param[in] sub_th    sub transaction handle
88  * \param[in] record_size total update record size
89  *
90  * \retval              0 if writing succeeds
91  * \retval              negative errno if writing fails
92  */
93 static int sub_declare_updates_write(const struct lu_env *env,
94                                      struct llog_update_record *record,
95                                      struct thandle *sub_th, size_t record_size)
96 {
97         struct llog_ctxt        *ctxt;
98         struct dt_device        *dt = sub_th->th_dev;
99         int                     left = record_size;
100         int rc;
101
102         /* If ctxt is NULL, it means not need to write update,
103          * for example if the the OSP is used to connect to OST */
104         ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
105                                 LLOG_UPDATELOG_ORIG_CTXT);
106
107         /* Not ready to record updates yet. */
108         if (ctxt == NULL || ctxt->loc_handle == NULL) {
109                 llog_ctxt_put(ctxt);
110                 return 0;
111         }
112
113         rc = llog_declare_add(env, ctxt->loc_handle,
114                               &record->lur_hdr, sub_th);
115         if (rc < 0)
116                 GOTO(out_put, rc);
117
118         while (left > ctxt->loc_chunk_size) {
119                 rc = llog_declare_add(env, ctxt->loc_handle,
120                                       &record->lur_hdr, sub_th);
121                 if (rc < 0)
122                         GOTO(out_put, rc);
123
124                 left -= ctxt->loc_chunk_size;
125         }
126
127 out_put:
128         llog_ctxt_put(ctxt);
129
130         return rc;
131 }
132
133 /**
134  * write update to sub device
135  *
136  * Write llog update record to the sub device during distribute
137  * transaction. If it succeeds, llog cookie of the record will be
138  * returned by @cookie.
139  *
140  * \param[in] env       execution environment
141  * \param[in] record    update records being written
142  * \param[in] sub_th    sub transaction handle
143  * \param[out] cookie   llog cookie of the update record.
144  *
145  * \retval              1 if writing succeeds
146  * \retval              negative errno if writing fails
147  */
148 static int sub_updates_write(const struct lu_env *env,
149                              struct llog_update_record *record,
150                              struct sub_thandle *sub_th)
151 {
152         struct dt_device *dt = sub_th->st_dt;
153         struct llog_ctxt *ctxt;
154         struct llog_update_record *lur = NULL;
155         __u32 update_count = 0;
156         __u32 param_count = 0;
157         __u32 last_update_count = 0;
158         __u32 last_param_count = 0;
159         char *start;
160         char *cur;
161         char *next;
162         struct sub_thandle_cookie *stc;
163         size_t reclen;
164         bool eof = false;
165         int rc;
166         ENTRY;
167
168         ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
169                                 LLOG_UPDATELOG_ORIG_CTXT);
170         /* If ctxt == NULL, then it means updates on OST (only happens
171          * during migration), and we do not track those updates for now */
172         /* If ctxt->loc_handle == NULL, then it does not need to record
173          * update, usually happens in error handler path */
174         if (ctxt == NULL || ctxt->loc_handle == NULL) {
175                 llog_ctxt_put(ctxt);
176                 RETURN(0);
177         }
178
179         /* Since the cross-MDT updates will includes both local
180          * and remote updates, the update ops count must > 1 */
181         LASSERT(record->lur_update_rec.ur_update_count > 1);
182         LASSERTF(record->lur_hdr.lrh_len == llog_update_record_size(record),
183                  "lrh_len %u record_size %zu\n", record->lur_hdr.lrh_len,
184                  llog_update_record_size(record));
185
186         /*
187          * If its size > llog chunk_size, then write current chunk to the update
188          * llog, NB the padding should >= LLOG_MIN_REC_SIZE.
189          *
190          * So check padding length is either >= LLOG_MIN_REC_SIZE or is 0
191          * (record length just matches the chunk size).
192          */
193
194         reclen = record->lur_hdr.lrh_len;
195         if (reclen + LLOG_MIN_REC_SIZE <= ctxt->loc_chunk_size ||
196             reclen == ctxt->loc_chunk_size) {
197                 OBD_ALLOC_PTR(stc);
198                 if (stc == NULL)
199                         GOTO(llog_put, rc = -ENOMEM);
200                 INIT_LIST_HEAD(&stc->stc_list);
201
202                 rc = llog_add(env, ctxt->loc_handle, &record->lur_hdr,
203                               &stc->stc_cookie, sub_th->st_sub_th);
204
205                 CDEBUG(D_INFO, "%s: Add update log "DFID".%u: rc = %d\n",
206                        dt->dd_lu_dev.ld_obd->obd_name,
207                        PLOGID(&stc->stc_cookie.lgc_lgl),
208                        stc->stc_cookie.lgc_index, rc);
209
210                 if (rc > 0) {
211                         list_add(&stc->stc_list, &sub_th->st_cookie_list);
212                         rc = 0;
213                 } else {
214                         OBD_FREE_PTR(stc);
215                 }
216
217                 GOTO(llog_put, rc);
218         }
219
220         /* Split the records into chunk_size update record */
221         OBD_ALLOC_LARGE(lur, ctxt->loc_chunk_size);
222         if (lur == NULL)
223                 GOTO(llog_put, rc = -ENOMEM);
224
225         memcpy(lur, &record->lur_hdr, sizeof(record->lur_hdr));
226         lur->lur_update_rec.ur_update_count = 0;
227         lur->lur_update_rec.ur_param_count = 0;
228         start = (char *)&record->lur_update_rec.ur_ops;
229         cur = next = start;
230         do {
231                 if (update_count < record->lur_update_rec.ur_update_count)
232                         next = (char *)update_op_next_op(
233                                                 (struct update_op *)cur);
234                 else if (param_count < record->lur_update_rec.ur_param_count)
235                         next = (char *)update_param_next_param(
236                                                 (struct update_param *)cur);
237                 else
238                         eof = true;
239
240                 reclen = __llog_update_record_size(
241                                 __update_records_size(next - start));
242                 if ((reclen + LLOG_MIN_REC_SIZE <= ctxt->loc_chunk_size ||
243                      reclen == ctxt->loc_chunk_size) &&
244                     !eof) {
245                         cur = next;
246
247                         if (update_count <
248                             record->lur_update_rec.ur_update_count)
249                                 update_count++;
250                         else if (param_count <
251                                  record->lur_update_rec.ur_param_count)
252                                 param_count++;
253                         continue;
254                 }
255
256                 lur->lur_update_rec.ur_update_count = update_count -
257                                                       last_update_count;
258                 lur->lur_update_rec.ur_param_count = param_count -
259                                                      last_param_count;
260                 memcpy(&lur->lur_update_rec.ur_ops, start, cur - start);
261                 lur->lur_hdr.lrh_len = llog_update_record_size(lur);
262
263                 LASSERT(lur->lur_hdr.lrh_len ==
264                          __llog_update_record_size(
265                                 __update_records_size(cur - start)));
266                 LASSERT(lur->lur_hdr.lrh_len <= ctxt->loc_chunk_size);
267
268                 update_records_dump(&lur->lur_update_rec, D_INFO, true);
269
270                 OBD_ALLOC_PTR(stc);
271                 if (stc == NULL)
272                         GOTO(llog_put, rc = -ENOMEM);
273                 INIT_LIST_HEAD(&stc->stc_list);
274
275                 rc = llog_add(env, ctxt->loc_handle, &lur->lur_hdr,
276                               &stc->stc_cookie, sub_th->st_sub_th);
277
278                 CDEBUG(D_INFO, "%s: Add update log "DFID".%u: rc = %d\n",
279                         dt->dd_lu_dev.ld_obd->obd_name,
280                         PLOGID(&stc->stc_cookie.lgc_lgl),
281                         stc->stc_cookie.lgc_index, rc);
282
283                 if (rc > 0) {
284                         list_add(&stc->stc_list, &sub_th->st_cookie_list);
285                         rc = 0;
286                 } else {
287                         OBD_FREE_PTR(stc);
288                         GOTO(llog_put, rc);
289                 }
290
291                 last_update_count = update_count;
292                 last_param_count = param_count;
293                 start = cur;
294                 lur->lur_update_rec.ur_update_count = 0;
295                 lur->lur_update_rec.ur_param_count = 0;
296                 lur->lur_update_rec.ur_flags |= UPDATE_RECORD_CONTINUE;
297         } while (!eof);
298
299 llog_put:
300         if (lur != NULL)
301                 OBD_FREE_LARGE(lur, ctxt->loc_chunk_size);
302         llog_ctxt_put(ctxt);
303
304         RETURN(rc);
305 }
306
307 /**
308  * Prepare the update records.
309  *
310  * Merge params and ops into the update records, then initializing
311  * the update buffer.
312  *
313  * During transaction execution phase, parameters and update ops
314  * are collected in two different buffers (see lod_updates_pack()),
315  * during transaction stop, it needs to be merged in one buffer,
316  * so it will be written in the update log.
317  *
318  * \param[in] env       execution environment
319  * \param[in] tmt       top_multiple_thandle for distribute txn
320  *
321  * \retval              0 if merging succeeds.
322  * \retval              negaitive errno if merging fails.
323  */
324 static int prepare_writing_updates(const struct lu_env *env,
325                                    struct top_multiple_thandle *tmt)
326 {
327         struct thandle_update_records   *tur = tmt->tmt_update_records;
328         struct llog_update_record       *lur;
329         struct update_params *params;
330         size_t params_size;
331         size_t update_size;
332
333         if (tur == NULL || tur->tur_update_records == NULL ||
334             tur->tur_update_params == NULL)
335                 return 0;
336
337         lur = tur->tur_update_records;
338         /* Extends the update records buffer if needed */
339         params_size = update_params_size(tur->tur_update_params,
340                                          tur->tur_update_param_count);
341         LASSERT(lur->lur_update_rec.ur_param_count == 0);
342         update_size = round_up(llog_update_record_size(lur) + params_size, 8);
343         if (update_size > tur->tur_update_records_buf_size) {
344                 int rc;
345
346                 rc = tur_update_records_extend(tur, update_size);
347                 if (rc < 0)
348                         return rc;
349
350                 lur = tur->tur_update_records;
351         }
352
353         params = update_records_get_params(&lur->lur_update_rec);
354         memcpy(params, tur->tur_update_params, params_size);
355
356         lur->lur_update_rec.ur_param_count = tur->tur_update_param_count;
357         lur->lur_update_rec.ur_batchid = tmt->tmt_batchid;
358         /* Init update record header */
359         lur->lur_hdr.lrh_len = llog_update_record_size(lur);
360         lur->lur_hdr.lrh_type = UPDATE_REC;
361
362         /* Dump updates for debugging purpose */
363         update_records_dump(&lur->lur_update_rec, D_INFO, true);
364
365         return 0;
366 }
367
368 /**
369  * Top thandle commit callback
370  *
371  * This callback will be called when all of sub transactions are committed.
372  *
373  * \param[in] th        top thandle to be committed.
374  */
375 static void top_trans_committed_cb(struct top_multiple_thandle *tmt)
376 {
377         struct lu_target *lut;
378         ENTRY;
379
380         LASSERT(kref_read(&tmt->tmt_refcount) > 0);
381
382         top_multiple_thandle_dump(tmt, D_HA);
383         tmt->tmt_committed = 1;
384         lut = dt2lu_dev(tmt->tmt_master_sub_dt)->ld_site->ls_tgt;
385         if (lut->lut_tdtd && lut->lut_tdtd->tdtd_commit_task)
386                 wake_up_process(lut->lut_tdtd->tdtd_commit_task);
387
388         RETURN_EXIT;
389 }
390
391 struct sub_thandle *lookup_sub_thandle(struct top_multiple_thandle *tmt,
392                                        struct dt_device *dt_dev)
393 {
394         struct sub_thandle *st;
395
396         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
397                 if (st->st_dt == dt_dev)
398                         return st;
399         }
400         return NULL;
401 }
402 EXPORT_SYMBOL(lookup_sub_thandle);
403
404 struct sub_thandle *create_sub_thandle(struct top_multiple_thandle *tmt,
405                                        struct dt_device *dt_dev)
406 {
407         struct sub_thandle *st;
408
409         OBD_ALLOC_PTR(st);
410         if (st == NULL)
411                 RETURN(ERR_PTR(-ENOMEM));
412
413         INIT_LIST_HEAD(&st->st_sub_list);
414         INIT_LIST_HEAD(&st->st_cookie_list);
415         st->st_dt = dt_dev;
416
417         list_add(&st->st_sub_list, &tmt->tmt_sub_thandle_list);
418         return st;
419 }
420
421 static void sub_trans_commit_cb_internal(struct top_multiple_thandle *tmt,
422                                          struct thandle *sub_th, int err)
423 {
424         struct sub_thandle      *st;
425         bool                    all_committed = true;
426
427         /* Check if all sub thandles are committed */
428         spin_lock(&tmt->tmt_sub_lock);
429         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
430                 if (st->st_sub_th == sub_th) {
431                         st->st_committed = 1;
432                         st->st_result = err;
433                 }
434                 if (!st->st_committed)
435                         all_committed = false;
436         }
437         spin_unlock(&tmt->tmt_sub_lock);
438
439         if (tmt->tmt_result == 0)
440                 tmt->tmt_result = err;
441
442         if (all_committed)
443                 top_trans_committed_cb(tmt);
444
445         top_multiple_thandle_dump(tmt, D_INFO);
446         top_multiple_thandle_put(tmt);
447         RETURN_EXIT;
448 }
449
450 /**
451  * sub thandle commit callback
452  *
453  * Mark the sub thandle to be committed and if all sub thandle are committed
454  * notify the top thandle.
455  *
456  * \param[in] env       execution environment
457  * \param[in] sub_th    sub thandle being committed
458  * \param[in] cb        commit callback
459  * \param[in] err       trans result
460  */
461 static void sub_trans_commit_cb(struct lu_env *env,
462                                 struct thandle *sub_th,
463                                 struct dt_txn_commit_cb *cb, int err)
464 {
465         struct top_multiple_thandle *tmt = cb->dcb_data;
466
467         sub_trans_commit_cb_internal(tmt, sub_th, err);
468 }
469
470 static void sub_thandle_register_commit_cb(struct sub_thandle *st,
471                                     struct top_multiple_thandle *tmt)
472 {
473         LASSERT(st->st_sub_th != NULL);
474         top_multiple_thandle_get(tmt);
475         st->st_commit_dcb.dcb_func = sub_trans_commit_cb;
476         st->st_commit_dcb.dcb_data = tmt;
477         INIT_LIST_HEAD(&st->st_commit_dcb.dcb_linkage);
478         dt_trans_cb_add(st->st_sub_th, &st->st_commit_dcb);
479 }
480
481 /**
482  * Sub thandle stop call back
483  *
484  * After sub thandle is stopped, it will call this callback to notify
485  * the top thandle.
486  *
487  * \param[in] th        sub thandle to be stopped
488  * \param[in] rc        result of sub trans
489  */
490 static void sub_trans_stop_cb(struct lu_env *env,
491                               struct thandle *sub_th,
492                               struct dt_txn_commit_cb *cb, int err)
493 {
494         struct sub_thandle              *st;
495         struct top_multiple_thandle     *tmt = cb->dcb_data;
496         ENTRY;
497
498         spin_lock(&tmt->tmt_sub_lock);
499         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
500                 if (st->st_stopped)
501                         continue;
502
503                 if (st->st_dt == sub_th->th_dev) {
504                         st->st_stopped = 1;
505                         st->st_result = err;
506                         break;
507                 }
508         }
509         spin_unlock(&tmt->tmt_sub_lock);
510
511         wake_up(&tmt->tmt_stop_waitq);
512         RETURN_EXIT;
513 }
514
515 static void sub_thandle_register_stop_cb(struct sub_thandle *st,
516                                          struct top_multiple_thandle *tmt)
517 {
518         st->st_stop_dcb.dcb_func = sub_trans_stop_cb;
519         st->st_stop_dcb.dcb_data = tmt;
520         st->st_stop_dcb.dcb_flags = DCB_TRANS_STOP;
521         INIT_LIST_HEAD(&st->st_stop_dcb.dcb_linkage);
522         dt_trans_cb_add(st->st_sub_th, &st->st_stop_dcb);
523 }
524
525 /**
526  * Create sub thandle
527  *
528  * Create transaction handle for sub_thandle
529  *
530  * \param[in] env       execution environment
531  * \param[in] th        top thandle
532  * \param[in] st        sub_thandle
533  *
534  * \retval              0 if creation succeeds.
535  * \retval              negative errno if creation fails.
536  */
537 int sub_thandle_trans_create(const struct lu_env *env,
538                              struct top_thandle *top_th,
539                              struct sub_thandle *st)
540 {
541         struct thandle *sub_th;
542
543         sub_th = dt_trans_create(env, st->st_dt);
544         if (IS_ERR(sub_th))
545                 return PTR_ERR(sub_th);
546
547         sub_th->th_top = &top_th->tt_super;
548         st->st_sub_th = sub_th;
549
550         sub_th->th_wait_submit = 1;
551         sub_thandle_register_stop_cb(st, top_th->tt_multiple_thandle);
552         return 0;
553 }
554
555 /**
556  * Create the top transaction.
557  *
558  * Create the top transaction on the master device. It will create a top
559  * thandle and a sub thandle on the master device.
560  *
561  * \param[in] env               execution environment
562  * \param[in] master_dev        master_dev the top thandle will be created
563  *
564  * \retval                      pointer to the created thandle.
565  * \retval                      ERR_PTR(errno) if creation failed.
566  */
567 struct thandle *
568 top_trans_create(const struct lu_env *env, struct dt_device *master_dev)
569 {
570         struct top_thandle      *top_th;
571         struct thandle          *child_th;
572
573         OBD_ALLOC_GFP(top_th, sizeof(*top_th), GFP_NOFS);
574         if (top_th == NULL)
575                 return ERR_PTR(-ENOMEM);
576
577         top_th->tt_super.th_top = &top_th->tt_super;
578
579         if (master_dev != NULL) {
580                 child_th = dt_trans_create(env, master_dev);
581                 if (IS_ERR(child_th)) {
582                         OBD_FREE_PTR(top_th);
583                         return child_th;
584                 }
585
586                 child_th->th_top = &top_th->tt_super;
587                 child_th->th_wait_submit = 1;
588                 top_th->tt_master_sub_thandle = child_th;
589         }
590         return &top_th->tt_super;
591 }
592 EXPORT_SYMBOL(top_trans_create);
593
594 /**
595  * Declare write update transaction
596  *
597  * Check if there are updates being recorded in this transaction,
598  * it will write the record into the disk.
599  *
600  * \param[in] env       execution environment
601  * \param[in] tmt       top multiple transaction handle
602  *
603  * \retval              0 if writing succeeds
604  * \retval              negative errno if writing fails
605  */
606 static int declare_updates_write(const struct lu_env *env,
607                                  struct top_multiple_thandle *tmt)
608 {
609         struct llog_update_record *record;
610         struct sub_thandle *st;
611         int rc = 0;
612
613         record = tmt->tmt_update_records->tur_update_records;
614         /* Declare update write for all other target */
615         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
616                 if (st->st_sub_th == NULL)
617                         continue;
618
619                 rc = sub_declare_updates_write(env, record, st->st_sub_th,
620                                                tmt->tmt_record_size);
621                 if (rc < 0)
622                         break;
623         }
624
625         return rc;
626 }
627
628 /**
629  * Assign batchid to the distribute transaction.
630  *
631  * Assign batchid to the distribute transaction
632  *
633  * \param[in] tmt       distribute transaction
634  */
635 static void distribute_txn_assign_batchid(struct top_multiple_thandle *new)
636 {
637         struct target_distribute_txn_data *tdtd;
638         struct dt_device *dt = new->tmt_master_sub_dt;
639         struct sub_thandle *st;
640
641         LASSERT(dt != NULL);
642         tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
643         spin_lock(&tdtd->tdtd_batchid_lock);
644         new->tmt_batchid = tdtd->tdtd_batchid++;
645         list_add_tail(&new->tmt_commit_list, &tdtd->tdtd_list);
646         spin_unlock(&tdtd->tdtd_batchid_lock);
647         list_for_each_entry(st, &new->tmt_sub_thandle_list, st_sub_list) {
648                 if (st->st_sub_th != NULL)
649                         sub_thandle_register_commit_cb(st, new);
650         }
651         top_multiple_thandle_get(new);
652         top_multiple_thandle_dump(new, D_INFO);
653 }
654
655 /**
656  * Insert distribute transaction to the distribute txn list.
657  *
658  * Insert distribute transaction to the distribute txn list.
659  *
660  * \param[in] new       the distribute txn to be inserted.
661  */
662 void distribute_txn_insert_by_batchid(struct top_multiple_thandle *new)
663 {
664         struct dt_device *dt = new->tmt_master_sub_dt;
665         struct top_multiple_thandle *tmt;
666         struct target_distribute_txn_data *tdtd;
667         struct sub_thandle *st;
668         bool    at_head = false;
669
670         LASSERT(dt != NULL);
671         tdtd = dt2lu_dev(dt)->ld_site->ls_tgt->lut_tdtd;
672
673         /* have a reference so the competing commit thread can't release it*/
674         top_multiple_thandle_get(new);
675
676         spin_lock(&tdtd->tdtd_batchid_lock);
677         list_for_each_entry_reverse(tmt, &tdtd->tdtd_list, tmt_commit_list) {
678                 if (new->tmt_batchid > tmt->tmt_batchid) {
679                         list_add(&new->tmt_commit_list, &tmt->tmt_commit_list);
680                         break;
681                 }
682         }
683         if (list_empty(&new->tmt_commit_list)) {
684                 at_head = true;
685                 list_add(&new->tmt_commit_list, &tdtd->tdtd_list);
686         }
687         spin_unlock(&tdtd->tdtd_batchid_lock);
688
689         list_for_each_entry(st, &new->tmt_sub_thandle_list, st_sub_list) {
690                 if (st->st_sub_th != NULL)
691                         sub_thandle_register_commit_cb(st, new);
692         }
693
694         top_multiple_thandle_dump(new, D_INFO);
695         if (new->tmt_committed && at_head && tdtd->tdtd_commit_task)
696                 wake_up_process(tdtd->tdtd_commit_task);
697 }
698
699 /**
700  * Prepare cross-MDT operation.
701  *
702  * Create the update record buffer to record updates for cross-MDT operation,
703  * add master sub transaction to tt_sub_trans_list, and declare the update
704  * writes.
705  *
706  * During updates packing, all of parameters will be packed in
707  * tur_update_params, and updates will be packed in tur_update_records.
708  * Then in transaction stop, parameters and updates will be merged
709  * into one updates buffer.
710  *
711  * And also master thandle will be added to the sub_th list, so it will be
712  * easy to track the commit status.
713  *
714  * \param[in] env       execution environment
715  * \param[in] th        top transaction handle
716  *
717  * \retval              0 if preparation succeeds.
718  * \retval              negative errno if preparation fails.
719  */
720 static int prepare_multiple_node_trans(const struct lu_env *env,
721                                        struct top_multiple_thandle *tmt)
722 {
723         struct thandle_update_records   *tur;
724         int                             rc;
725         ENTRY;
726
727         if (tmt->tmt_update_records == NULL) {
728                 tur = &update_env_info(env)->uti_tur;
729                 rc = check_and_prepare_update_record(env, tur);
730                 if (rc < 0)
731                         RETURN(rc);
732
733                 tmt->tmt_update_records = tur;
734                 distribute_txn_assign_batchid(tmt);
735         }
736
737         rc = declare_updates_write(env, tmt);
738
739         RETURN(rc);
740 }
741
742 /**
743  * start the top transaction.
744  *
745  * Start all of its sub transactions, then start master sub transaction.
746  *
747  * \param[in] env               execution environment
748  * \param[in] master_dev        master_dev the top thandle will be start
749  * \param[in] th                top thandle
750  *
751  * \retval                      0 if transaction start succeeds.
752  * \retval                      negative errno if start fails.
753  */
754 int top_trans_start(const struct lu_env *env, struct dt_device *master_dev,
755                     struct thandle *th)
756 {
757         struct top_thandle      *top_th = container_of(th, struct top_thandle,
758                                                        tt_super);
759         struct sub_thandle              *st;
760         struct top_multiple_thandle     *tmt = top_th->tt_multiple_thandle;
761         int                             rc = 0;
762         ENTRY;
763
764         if (tmt == NULL) {
765                 if (th->th_sync)
766                         top_th->tt_master_sub_thandle->th_sync = th->th_sync;
767                 if (th->th_local)
768                         top_th->tt_master_sub_thandle->th_local = th->th_local;
769                 rc = dt_trans_start(env, top_th->tt_master_sub_thandle->th_dev,
770                                     top_th->tt_master_sub_thandle);
771                 RETURN(rc);
772         }
773
774         tmt = top_th->tt_multiple_thandle;
775         rc = prepare_multiple_node_trans(env, tmt);
776         if (rc < 0)
777                 RETURN(rc);
778
779         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
780                 if (st->st_sub_th == NULL)
781                         continue;
782                 if (th->th_sync)
783                         st->st_sub_th->th_sync = th->th_sync;
784                 if (th->th_local)
785                         st->st_sub_th->th_local = th->th_local;
786                 rc = dt_trans_start(env, st->st_sub_th->th_dev,
787                                     st->st_sub_th);
788                 if (rc != 0)
789                         GOTO(out, rc);
790
791                 LASSERT(st->st_started == 0);
792                 st->st_started = 1;
793         }
794 out:
795         th->th_result = rc;
796         RETURN(rc);
797 }
798 EXPORT_SYMBOL(top_trans_start);
799
800 /**
801  * Check whether we need write updates record
802  *
803  * Check if the updates for the top_thandle needs to be writen
804  * to all targets. Only if the transaction succeeds and the updates
805  * number > 2, it will write the updates,
806  *
807  * \params [in] top_th  top thandle.
808  *
809  * \retval              true if it needs to write updates
810  * \retval              false if it does not need to write updates
811  **/
812 static bool top_check_write_updates(struct top_thandle *top_th)
813 {
814         struct top_multiple_thandle     *tmt;
815         struct thandle_update_records   *tur;
816
817         /* Do not write updates to records if the transaction fails */
818         if (top_th->tt_super.th_result != 0)
819                 return false;
820
821         tmt = top_th->tt_multiple_thandle;
822         if (tmt == NULL)
823                 return false;
824
825         tur = tmt->tmt_update_records;
826         if (tur == NULL)
827                 return false;
828
829         /* Hmm, false update records, since the cross-MDT operation
830          * should includes both local and remote updates, so the
831          * updates count should >= 2 */
832         if (tur->tur_update_records == NULL ||
833             tur->tur_update_records->lur_update_rec.ur_update_count <= 1)
834                 return false;
835
836         return true;
837 }
838
839 /**
840  * Check if top transaction is stopped
841  *
842  * Check if top transaction is stopped, only if all sub transaction
843  * is stopped, then the top transaction is stopped.
844  *
845  * \param [in] top_th   top thandle
846  *
847  * \retval              true if the top transaction is stopped.
848  * \retval              false if the top transaction is not stopped.
849  */
850 static bool top_trans_is_stopped(struct top_thandle *top_th)
851 {
852         struct top_multiple_thandle     *tmt;
853         struct sub_thandle              *st;
854         bool                    all_stopped = true;
855
856         tmt = top_th->tt_multiple_thandle;
857         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
858                 if (!st->st_stopped && st->st_sub_th != NULL) {
859                         all_stopped = false;
860                         break;
861                 }
862
863                 if (st->st_result != 0 &&
864                     top_th->tt_super.th_result == 0)
865                         top_th->tt_super.th_result = st->st_result;
866         }
867
868         return all_stopped;
869 }
870
871 /**
872  * Wait result of top transaction
873  *
874  * Wait until all sub transaction get its result.
875  *
876  * \param [in] top_th   top thandle.
877  *
878  * \retval              the result of top thandle.
879  */
880 static int top_trans_wait_result(struct top_thandle *top_th)
881 {
882         wait_event_idle(top_th->tt_multiple_thandle->tmt_stop_waitq,
883                         top_trans_is_stopped(top_th));
884
885         RETURN(top_th->tt_super.th_result);
886 }
887
888 /**
889  * Stop the top transaction.
890  *
891  * Stop the transaction on the master device first, then stop transactions
892  * on other sub devices.
893  *
894  * \param[in] env               execution environment
895  * \param[in] master_dev        master_dev the top thandle will be created
896  * \param[in] th                top thandle
897  *
898  * \retval                      0 if stop transaction succeeds.
899  * \retval                      negative errno if stop transaction fails.
900  */
901 int top_trans_stop(const struct lu_env *env, struct dt_device *master_dev,
902                    struct thandle *th)
903 {
904         struct top_thandle      *top_th = container_of(th, struct top_thandle,
905                                                        tt_super);
906         struct sub_thandle              *st;
907         struct sub_thandle              *master_st;
908         struct top_multiple_thandle     *tmt;
909         struct thandle_update_records   *tur;
910         bool                            write_updates = false;
911         int                     rc = 0;
912         ENTRY;
913
914         if (likely(top_th->tt_multiple_thandle == NULL)) {
915                 LASSERT(master_dev != NULL);
916
917                 if (th->th_sync)
918                         top_th->tt_master_sub_thandle->th_sync = th->th_sync;
919                 if (th->th_local)
920                         top_th->tt_master_sub_thandle->th_local = th->th_local;
921                 rc = dt_trans_stop(env, master_dev,
922                                    top_th->tt_master_sub_thandle);
923                 OBD_FREE_PTR(top_th);
924                 RETURN(rc);
925         }
926
927         tmt = top_th->tt_multiple_thandle;
928         tur = tmt->tmt_update_records;
929
930         /* Note: we need stop the master thandle first, then the stop
931          * callback will fill the master transno in the update logs,
932          * then these update logs will be sent to other MDTs */
933         /* get the master sub thandle */
934         master_st = lookup_sub_thandle(tmt, tmt->tmt_master_sub_dt);
935         write_updates = top_check_write_updates(top_th);
936
937         /* Step 1: write the updates log on Master MDT */
938         if (master_st != NULL && master_st->st_sub_th != NULL &&
939             write_updates) {
940                 struct llog_update_record *lur;
941
942                 /* Merge the parameters and updates into one buffer */
943                 rc = prepare_writing_updates(env, tmt);
944                 if (rc < 0) {
945                         CERROR("%s: cannot prepare updates: rc = %d\n",
946                                master_dev->dd_lu_dev.ld_obd->obd_name, rc);
947                         th->th_result = rc;
948                         write_updates = false;
949                         GOTO(stop_master_trans, rc);
950                 }
951
952                 lur = tur->tur_update_records;
953                 /* Write updates to the master MDT */
954                 rc = sub_updates_write(env, lur, master_st);
955
956                 /* Cleanup the common parameters in the update records,
957                  * master transno callback might add more parameters.
958                  * and we need merge the update records again in the
959                  * following */
960                 if (tur->tur_update_params != NULL)
961                         lur->lur_update_rec.ur_param_count = 0;
962
963                 if (rc < 0) {
964                         CERROR("%s: write updates failed: rc = %d\n",
965                                master_dev->dd_lu_dev.ld_obd->obd_name, rc);
966                         th->th_result = rc;
967                         write_updates = false;
968                         GOTO(stop_master_trans, rc);
969                 }
970         }
971
972 stop_master_trans:
973         /* Step 2: Stop the transaction on the master MDT, and fill the
974          * master transno in the update logs to other MDT. */
975         if (master_st != NULL && master_st->st_sub_th != NULL) {
976                 if (th->th_local)
977                         master_st->st_sub_th->th_local = th->th_local;
978                 if (th->th_sync)
979                         master_st->st_sub_th->th_sync = th->th_sync;
980                 master_st->st_sub_th->th_result = th->th_result;
981                 rc = dt_trans_stop(env, master_st->st_dt, master_st->st_sub_th);
982                 /* If it does not write_updates, then we call submit callback
983                  * here, otherwise callback is done through
984                  * osd(osp)_trans_commit_cb() */
985                 if (!master_st->st_started &&
986                     !list_empty(&tmt->tmt_commit_list))
987                         sub_trans_commit_cb_internal(tmt,
988                                                 master_st->st_sub_th, rc);
989                 if (rc < 0) {
990                         CERROR("%s: stop trans failed: rc = %d\n",
991                                master_dev->dd_lu_dev.ld_obd->obd_name, rc);
992                         th->th_result = rc;
993                         GOTO(stop_other_trans, rc);
994                 } else if (tur != NULL && tur->tur_update_records != NULL) {
995                         struct llog_update_record *lur;
996
997                         lur = tur->tur_update_records;
998                         if (lur->lur_update_rec.ur_master_transno == 0)
999                                 /* Update master transno after master stop
1000                                  * callback */
1001                                 lur->lur_update_rec.ur_master_transno =
1002                                                 tgt_th_info(env)->tti_transno;
1003                 }
1004         }
1005
1006         /* Step 3: write updates to other MDTs */
1007         if (write_updates) {
1008                 struct llog_update_record *lur;
1009                 if (CFS_FAIL_PRECHECK(OBD_FAIL_OUT_OBJECT_MISS)) {
1010                         if (cfs_fail_val == 1) {
1011                                 long timeout = cfs_time_seconds(1) / 10;
1012
1013                                 CFS_RACE(OBD_FAIL_OUT_OBJECT_MISS);
1014                                 set_current_state(TASK_UNINTERRUPTIBLE);
1015                                 schedule_timeout(schedule_timeout(timeout));
1016                                 cfs_fail_loc = 0;
1017                         }
1018                         cfs_fail_val++;
1019                 }
1020
1021                 /* Stop callback of master will add more updates and also update
1022                  * master transno, so merge the parameters and updates into one
1023                  * buffer again */
1024                 rc = prepare_writing_updates(env, tmt);
1025                 if (rc < 0) {
1026                         CERROR("%s: prepare updates failed: rc = %d\n",
1027                                master_dev->dd_lu_dev.ld_obd->obd_name, rc);
1028                         th->th_result = rc;
1029                         GOTO(stop_other_trans, rc);
1030                 }
1031                 lur = tur->tur_update_records;
1032                 list_for_each_entry(st, &tmt->tmt_sub_thandle_list,
1033                                     st_sub_list) {
1034                         if (st->st_sub_th == NULL || st == master_st ||
1035                             st->st_sub_th->th_result < 0)
1036                                 continue;
1037
1038                         rc = sub_updates_write(env, lur, st);
1039                         if (rc < 0) {
1040                                 CERROR("%s: write updates failed: rc = %d\n",
1041                                        st->st_dt->dd_lu_dev.ld_obd->obd_name,
1042                                        rc);
1043                                 th->th_result = rc;
1044                                 break;
1045                         }
1046                 }
1047         }
1048
1049 stop_other_trans:
1050         /* Step 4: Stop the transaction on other MDTs */
1051         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
1052                 if (st == master_st || st->st_sub_th == NULL)
1053                         continue;
1054
1055                 if (th->th_sync)
1056                         st->st_sub_th->th_sync = th->th_sync;
1057                 if (th->th_local)
1058                         st->st_sub_th->th_local = th->th_local;
1059                 st->st_sub_th->th_result = th->th_result;
1060                 rc = dt_trans_stop(env, st->st_sub_th->th_dev,
1061                                    st->st_sub_th);
1062                 if (rc < 0) {
1063                         CERROR("%s: stop trans failed: rc = %d\n",
1064                                st->st_dt->dd_lu_dev.ld_obd->obd_name, rc);
1065                         if (th->th_result == 0)
1066                                 th->th_result = rc;
1067                 }
1068         }
1069
1070         rc = top_trans_wait_result(top_th);
1071
1072         tmt->tmt_result = rc;
1073
1074         /* Balance for the refcount in top_trans_create, Note: if it is NOT
1075          * multiple node transaction, the top transaction will be destroyed. */
1076         top_multiple_thandle_put(tmt);
1077         OBD_FREE_PTR(top_th);
1078         RETURN(rc);
1079 }
1080 EXPORT_SYMBOL(top_trans_stop);
1081
1082 /**
1083  * Create top_multiple_thandle for top_thandle
1084  *
1085  * Create top_mutilple_thandle to manage the mutiple node transaction
1086  * for top_thandle, and it also needs to add master sub thandle to the
1087  * sub trans list now.
1088  *
1089  * \param[in] env       execution environment
1090  * \param[in] top_th    the top thandle
1091  *
1092  * \retval      0 if creation succeeds
1093  * \retval      negative errno if creation fails
1094  */
1095 int top_trans_create_tmt(const struct lu_env *env,
1096                          struct top_thandle *top_th)
1097 {
1098         struct top_multiple_thandle *tmt;
1099
1100         OBD_ALLOC_PTR(tmt);
1101         if (tmt == NULL)
1102                 return -ENOMEM;
1103
1104         tmt->tmt_magic = TOP_THANDLE_MAGIC;
1105         INIT_LIST_HEAD(&tmt->tmt_sub_thandle_list);
1106         INIT_LIST_HEAD(&tmt->tmt_commit_list);
1107         kref_init(&tmt->tmt_refcount);
1108         spin_lock_init(&tmt->tmt_sub_lock);
1109         init_waitqueue_head(&tmt->tmt_stop_waitq);
1110
1111         top_th->tt_multiple_thandle = tmt;
1112
1113         return 0;
1114 }
1115
1116 static struct sub_thandle *
1117 create_sub_thandle_with_thandle(struct top_thandle *top_th,
1118                                 struct thandle *sub_th)
1119 {
1120         struct sub_thandle *st;
1121
1122         /* create and init sub th to the top trans list */
1123         st = create_sub_thandle(top_th->tt_multiple_thandle,
1124                                 sub_th->th_dev);
1125         if (IS_ERR(st))
1126                 return st;
1127
1128         st->st_sub_th = sub_th;
1129
1130         sub_th->th_top = &top_th->tt_super;
1131         sub_thandle_register_stop_cb(st, top_th->tt_multiple_thandle);
1132         return st;
1133 }
1134
1135 /**
1136  * Get sub thandle.
1137  *
1138  * Get sub thandle from the top thandle according to the sub dt_device.
1139  *
1140  * \param[in] env       execution environment
1141  * \param[in] th        thandle on the top layer.
1142  * \param[in] sub_dt    sub dt_device used to get sub transaction
1143  *
1144  * \retval              thandle of sub transaction if succeed
1145  * \retval              PTR_ERR(errno) if failed
1146  */
1147 struct thandle *thandle_get_sub_by_dt(const struct lu_env *env,
1148                                       struct thandle *th,
1149                                       struct dt_device *sub_dt)
1150 {
1151         struct sub_thandle      *st = NULL;
1152         struct sub_thandle      *master_st = NULL;
1153         struct top_thandle      *top_th;
1154         struct thandle          *sub_th = NULL;
1155         int                     rc = 0;
1156         ENTRY;
1157
1158         top_th = container_of(th, struct top_thandle, tt_super);
1159
1160         if (likely(sub_dt == top_th->tt_master_sub_thandle->th_dev))
1161                 RETURN(top_th->tt_master_sub_thandle);
1162
1163         if (top_th->tt_multiple_thandle != NULL) {
1164                 st = lookup_sub_thandle(top_th->tt_multiple_thandle, sub_dt);
1165                 if (st != NULL)
1166                         RETURN(st->st_sub_th);
1167         }
1168
1169         sub_th = dt_trans_create(env, sub_dt);
1170         if (IS_ERR(sub_th))
1171                 RETURN(sub_th);
1172
1173         /* Create top_multiple_thandle if necessary */
1174         if (top_th->tt_multiple_thandle == NULL) {
1175                 struct top_multiple_thandle *tmt;
1176
1177                 rc = top_trans_create_tmt(env, top_th);
1178                 if (rc < 0)
1179                         GOTO(stop_trans, rc);
1180
1181                 tmt = top_th->tt_multiple_thandle;
1182
1183                 /* Add master sub th to the top trans list */
1184                 tmt->tmt_master_sub_dt =
1185                         top_th->tt_master_sub_thandle->th_dev;
1186                 master_st = create_sub_thandle_with_thandle(top_th,
1187                                         top_th->tt_master_sub_thandle);
1188                 if (IS_ERR(master_st)) {
1189                         rc = PTR_ERR(master_st);
1190                         master_st = NULL;
1191                         GOTO(stop_trans, rc);
1192                 }
1193         }
1194
1195         /* create and init sub th to the top trans list */
1196         st = create_sub_thandle_with_thandle(top_th, sub_th);
1197         if (IS_ERR(st)) {
1198                 rc = PTR_ERR(st);
1199                 st = NULL;
1200                 GOTO(stop_trans, rc);
1201         }
1202         st->st_sub_th->th_wait_submit = 1;
1203 stop_trans:
1204         if (rc < 0) {
1205                 if (master_st != NULL) {
1206                         list_del(&master_st->st_sub_list);
1207                         OBD_FREE_PTR(master_st);
1208                 }
1209                 sub_th->th_result = rc;
1210                 dt_trans_stop(env, sub_dt, sub_th);
1211                 sub_th = ERR_PTR(rc);
1212         }
1213
1214         RETURN(sub_th);
1215 }
1216 EXPORT_SYMBOL(thandle_get_sub_by_dt);
1217
1218 /**
1219  * Top multiple thandle destroy
1220  *
1221  * Destroy multiple thandle and all its sub thandle.
1222  *
1223  * \param[in] kref      Pointer to struct kref
1224  */
1225 void top_multiple_thandle_destroy(struct kref *kref)
1226 {
1227         struct top_multiple_thandle *tmt;
1228         struct sub_thandle *st;
1229         struct sub_thandle *tmp;
1230
1231         tmt = container_of(kref, struct top_multiple_thandle, tmt_refcount);
1232
1233         LASSERT(tmt->tmt_magic == TOP_THANDLE_MAGIC);
1234         list_for_each_entry_safe(st, tmp, &tmt->tmt_sub_thandle_list,
1235                                  st_sub_list) {
1236                 struct sub_thandle_cookie *stc;
1237                 struct sub_thandle_cookie *tmp;
1238
1239                 list_del(&st->st_sub_list);
1240                 list_for_each_entry_safe(stc, tmp, &st->st_cookie_list,
1241                                          stc_list) {
1242                         list_del(&stc->stc_list);
1243                         OBD_FREE_PTR(stc);
1244                 }
1245                 OBD_FREE_PTR(st);
1246         }
1247         OBD_FREE_PTR(tmt);
1248 }
1249 EXPORT_SYMBOL(top_multiple_thandle_destroy);
1250
1251 /**
1252  * Cancel the update log on MDTs
1253  *
1254  * Cancel the update log on MDTs then destroy the thandle.
1255  *
1256  * \param[in] env       execution environment
1257  * \param[in] tmt       the top multiple thandle whose updates records
1258  *                      will be cancelled.
1259  *
1260  * \retval              0 if cancellation succeeds.
1261  * \retval              negative errno if cancellation fails.
1262  */
1263 static int distribute_txn_cancel_records(const struct lu_env *env,
1264                                          struct top_multiple_thandle *tmt)
1265 {
1266         struct sub_thandle *st;
1267         ENTRY;
1268
1269         if (CFS_FAIL_CHECK(OBD_FAIL_TGT_TXN_NO_CANCEL))
1270                 RETURN(0);
1271
1272         top_multiple_thandle_dump(tmt, D_INFO);
1273         /* Cancel update logs on other MDTs */
1274         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
1275                 struct llog_ctxt        *ctxt;
1276                 struct obd_device       *obd;
1277                 struct llog_cookie      *cookie;
1278                 struct sub_thandle_cookie *stc;
1279                 int rc;
1280
1281                 obd = st->st_dt->dd_lu_dev.ld_obd;
1282                 ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
1283                 if (ctxt == NULL)
1284                         continue;
1285                 list_for_each_entry(stc, &st->st_cookie_list, stc_list) {
1286                         cookie = &stc->stc_cookie;
1287                         if (fid_is_zero(&cookie->lgc_lgl.lgl_oi.oi_fid))
1288                                 continue;
1289
1290                         rc = llog_cat_cancel_records(env, ctxt->loc_handle, 1,
1291                                                      cookie);
1292                         CDEBUG(D_HA, "%s: batchid %llu cancel update log "
1293                                DFID".%u: rc = %d\n", obd->obd_name,
1294                                tmt->tmt_batchid, PLOGID(&cookie->lgc_lgl),
1295                                cookie->lgc_index, rc);
1296                 }
1297
1298                 llog_ctxt_put(ctxt);
1299         }
1300
1301         RETURN(0);
1302 }
1303
1304 struct distribute_txn_bid_data {
1305         struct dt_txn_commit_cb  dtbd_cb;
1306         struct target_distribute_txn_data      *dtbd_tdtd;
1307         __u64                    dtbd_batchid;
1308 };
1309
1310 /**
1311  * callback of updating commit batchid
1312  *
1313  * Updating commit batchid then wake up the commit thread to cancel the
1314  * records.
1315  *
1316  * \param[in]env        execution environment
1317  * \param[in]th         thandle to updating commit batchid
1318  * \param[in]cb         commit callback
1319  * \param[in]err        result of thandle
1320  */
1321 static void distribute_txn_batchid_cb(struct lu_env *env,
1322                                       struct thandle *th,
1323                                       struct dt_txn_commit_cb *cb,
1324                                       int err)
1325 {
1326         struct distribute_txn_bid_data          *dtbd = NULL;
1327         struct target_distribute_txn_data       *tdtd;
1328
1329         dtbd = container_of(cb, struct distribute_txn_bid_data, dtbd_cb);
1330         tdtd = dtbd->dtbd_tdtd;
1331
1332         CDEBUG(D_HA, "%s: %llu batchid updated\n",
1333               tdtd->tdtd_lut->lut_obd->obd_name, dtbd->dtbd_batchid);
1334         spin_lock(&tdtd->tdtd_batchid_lock);
1335         if (dtbd->dtbd_batchid > tdtd->tdtd_committed_batchid &&
1336             !tdtd->tdtd_lut->lut_obd->obd_no_transno)
1337                 tdtd->tdtd_committed_batchid = dtbd->dtbd_batchid;
1338         spin_unlock(&tdtd->tdtd_batchid_lock);
1339         if (atomic_dec_and_test(&tdtd->tdtd_refcount))
1340                 wake_up_process(tdtd->tdtd_commit_task);
1341
1342         OBD_FREE_PTR(dtbd);
1343 }
1344
1345 /**
1346  * Update the commit batchid in disk
1347  *
1348  * Update commit batchid in the disk, after this is committed, it can start
1349  * to cancel the update records.
1350  *
1351  * \param[in] env       execution environment
1352  * \param[in] tdtd      distribute transaction structure
1353  * \param[in] batchid   commit batchid to be updated
1354  *
1355  * \retval              0 if update succeeds.
1356  * \retval              negative errno if update fails.
1357  */
1358 static int
1359 distribute_txn_commit_batchid_update(const struct lu_env *env,
1360                               struct target_distribute_txn_data *tdtd,
1361                               __u64 batchid)
1362 {
1363         struct distribute_txn_bid_data  *dtbd = NULL;
1364         struct thandle          *th;
1365         struct lu_buf            buf;
1366         __u64                    tmp;
1367         __u64                    off;
1368         int                      rc;
1369         ENTRY;
1370
1371         OBD_ALLOC_PTR(dtbd);
1372         if (dtbd == NULL)
1373                 RETURN(-ENOMEM);
1374         dtbd->dtbd_batchid = batchid;
1375         dtbd->dtbd_tdtd = tdtd;
1376         dtbd->dtbd_cb.dcb_func = distribute_txn_batchid_cb;
1377         atomic_inc(&tdtd->tdtd_refcount);
1378
1379         th = dt_trans_create(env, tdtd->tdtd_lut->lut_bottom);
1380         if (IS_ERR(th)) {
1381                 atomic_dec(&tdtd->tdtd_refcount);
1382                 OBD_FREE_PTR(dtbd);
1383                 RETURN(PTR_ERR(th));
1384         }
1385
1386         tmp = cpu_to_le64(batchid);
1387         buf.lb_buf = &tmp;
1388         buf.lb_len = sizeof(tmp);
1389         off = 0;
1390
1391         rc = dt_declare_record_write(env, tdtd->tdtd_batchid_obj, &buf, off,
1392                                      th);
1393         if (rc < 0)
1394                 GOTO(stop, rc);
1395
1396         rc = dt_trans_start_local(env, tdtd->tdtd_lut->lut_bottom, th);
1397         if (rc < 0)
1398                 GOTO(stop, rc);
1399
1400         rc = dt_trans_cb_add(th, &dtbd->dtbd_cb);
1401         if (rc < 0)
1402                 GOTO(stop, rc);
1403
1404         rc = dt_record_write(env, tdtd->tdtd_batchid_obj, &buf,
1405                              &off, th);
1406
1407         CDEBUG(D_INFO, "%s: update batchid %llu: rc = %d\n",
1408                tdtd->tdtd_lut->lut_obd->obd_name, batchid, rc);
1409
1410 stop:
1411         dt_trans_stop(env, tdtd->tdtd_lut->lut_bottom, th);
1412         if (rc < 0) {
1413                 atomic_dec(&tdtd->tdtd_refcount);
1414                 OBD_FREE_PTR(dtbd);
1415         }
1416         RETURN(rc);
1417 }
1418
1419 /**
1420  * Init commit batchid for distribute transaction.
1421  *
1422  * Initialize the batchid object and get commit batchid from the object.
1423  *
1424  * \param[in] env       execution environment
1425  * \param[in] tdtd      distribute transaction whose batchid is initialized.
1426  *
1427  * \retval              0 if initialization succeeds.
1428  * \retval              negative errno if initialization fails.
1429  **/
1430 static int
1431 distribute_txn_commit_batchid_init(const struct lu_env *env,
1432                                    struct target_distribute_txn_data *tdtd)
1433 {
1434         struct tgt_thread_info  *tti = tgt_th_info(env);
1435         struct lu_target        *lut = tdtd->tdtd_lut;
1436         struct lu_attr          *attr = &tti->tti_attr;
1437         struct lu_fid           *fid = &tti->tti_fid1;
1438         struct dt_object_format *dof = &tti->tti_u.update.tti_update_dof;
1439         struct dt_object        *dt_obj = NULL;
1440         struct lu_buf           buf;
1441         __u64                   tmp;
1442         __u64                   off;
1443         int                     rc;
1444         ENTRY;
1445
1446         memset(attr, 0, sizeof(*attr));
1447         attr->la_valid = LA_MODE;
1448         attr->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1449         dof->dof_type = dt_mode_to_dft(S_IFREG);
1450
1451         lu_local_obj_fid(fid, BATCHID_COMMITTED_OID);
1452
1453         dt_obj = dt_find_or_create(env, lut->lut_bottom, fid, dof,
1454                                    attr);
1455         if (IS_ERR(dt_obj)) {
1456                 rc = PTR_ERR(dt_obj);
1457                 dt_obj = NULL;
1458                 GOTO(out_put, rc);
1459         }
1460
1461         tdtd->tdtd_batchid_obj = dt_obj;
1462
1463         buf.lb_buf = &tmp;
1464         buf.lb_len = sizeof(tmp);
1465         off = 0;
1466         rc = dt_read(env, dt_obj, &buf, &off);
1467         if (rc < 0 || (rc < buf.lb_len && rc > 0)) {
1468                 CERROR("%s can't read last committed batchid: rc = %d\n",
1469                        tdtd->tdtd_lut->lut_obd->obd_name, rc);
1470                 if (rc > 0)
1471                         rc = -EINVAL;
1472                 GOTO(out_put, rc);
1473         } else if (rc == buf.lb_len) {
1474                 tdtd->tdtd_committed_batchid = le64_to_cpu(tmp);
1475                 CDEBUG(D_HA, "%s: committed batchid %llu\n",
1476                        tdtd->tdtd_lut->lut_obd->obd_name,
1477                        tdtd->tdtd_committed_batchid);
1478                 rc = 0;
1479         }
1480
1481 out_put:
1482         if (rc < 0 && dt_obj != NULL) {
1483                 dt_object_put(env, dt_obj);
1484                 tdtd->tdtd_batchid_obj = NULL;
1485         }
1486         return rc;
1487 }
1488
1489 #ifndef TASK_IDLE
1490 #define TASK_IDLE TASK_INTERRUPTIBLE
1491 #endif
1492
1493 /**
1494  * manage the distribute transaction thread
1495  *
1496  * Distribute transaction are linked to the list, and once the distribute
1497  * transaction is committed, it will update the last committed batchid first,
1498  * after it is committed, it will cancel the records.
1499  *
1500  * \param[in] _arg      argument for commit thread
1501  *
1502  * \retval              0 if thread is running successfully
1503  * \retval              negative errno if the thread can not be run.
1504  */
1505 static int distribute_txn_commit_thread(void *_arg)
1506 {
1507         struct target_distribute_txn_data *tdtd = _arg;
1508         struct lu_env           *env = &tdtd->tdtd_env;
1509         LIST_HEAD(list);
1510         int                      rc;
1511         struct top_multiple_thandle *tmt;
1512         struct top_multiple_thandle *tmp;
1513         __u64                    batchid = 0, committed;
1514
1515         ENTRY;
1516
1517
1518         CDEBUG(D_HA, "%s: start commit thread committed batchid %llu\n",
1519                tdtd->tdtd_lut->lut_obd->obd_name,
1520                tdtd->tdtd_committed_batchid);
1521
1522         while (({set_current_state(TASK_IDLE);
1523                  !kthread_should_stop(); })) {
1524                 spin_lock(&tdtd->tdtd_batchid_lock);
1525                 list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1526                                          tmt_commit_list) {
1527                         if (tmt->tmt_committed == 0)
1528                                 break;
1529
1530                         /* Note: right now, replay is based on master MDT
1531                          * transno, but cancellation is based on batchid.
1532                          * so we do not try to cancel the update log until
1533                          * the recoverying is done, unless the update records
1534                          * batchid < committed_batchid. */
1535                         if (tmt->tmt_batchid <= tdtd->tdtd_committed_batchid) {
1536                                 __set_current_state(TASK_RUNNING);
1537                                 list_move_tail(&tmt->tmt_commit_list, &list);
1538                         } else if (!test_bit(OBDF_RECOVERING, tdtd->tdtd_lut->lut_obd->obd_flags)) {
1539                                 __set_current_state(TASK_RUNNING);
1540                                 LASSERTF(tmt->tmt_batchid >= batchid,
1541                                          "tmt %px tmt_batchid: %llu, batchid %llu\n",
1542                                          tmt, tmt->tmt_batchid, batchid);
1543                                 /* There are three types of distribution
1544                                  * transaction result
1545                                  *
1546                                  * 1. If tmt_result < 0, it means the
1547                                  * distribution transaction fails, which should
1548                                  * be rare, because once declare phase succeeds,
1549                                  * the operation should succeeds anyway. Note in
1550                                  * this case, we will still update batchid so
1551                                  * cancellation would be stopped.
1552                                  *
1553                                  * 2. If tmt_result == 0, it means the
1554                                  * distribution transaction succeeds, and we
1555                                  * will update batchid.
1556                                  *
1557                                  * 3. If tmt_result > 0, it means distribute
1558                                  * transaction is not yet committed on every
1559                                  * node, but we need release this tmt before
1560                                  * that, which usuually happens during umount.
1561                                  */
1562                                 if (tmt->tmt_result <= 0)
1563                                         batchid = tmt->tmt_batchid;
1564                                 list_move_tail(&tmt->tmt_commit_list, &list);
1565                         }
1566                 }
1567                 spin_unlock(&tdtd->tdtd_batchid_lock);
1568
1569                 CDEBUG(D_HA, "%s: batchid: %llu committed batchid "
1570                        "%llu\n", tdtd->tdtd_lut->lut_obd->obd_name, batchid,
1571                        tdtd->tdtd_committed_batchid);
1572                 /* update globally committed on a storage */
1573                 if (batchid > tdtd->tdtd_committed_batchid) {
1574                         rc = distribute_txn_commit_batchid_update(env, tdtd,
1575                                                              batchid);
1576                         if (rc == 0)
1577                                 batchid = 0;
1578                 }
1579                 /* cancel the records for committed batchid's */
1580                 /* XXX: should we postpone cancel's till the end of recovery? */
1581                 committed = tdtd->tdtd_committed_batchid;
1582                 list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1583                         if (tmt->tmt_batchid > committed)
1584                                 break;
1585                         __set_current_state(TASK_RUNNING);
1586                         list_del_init(&tmt->tmt_commit_list);
1587                         if (tmt->tmt_result <= 0)
1588                                 distribute_txn_cancel_records(env, tmt);
1589                         top_multiple_thandle_put(tmt);
1590                 }
1591
1592                 if (!task_is_running(current))
1593                         schedule();
1594
1595                 if (CFS_FAIL_PRECHECK(OBD_FAIL_OUT_OBJECT_MISS)) {
1596                         set_current_state(TASK_UNINTERRUPTIBLE);
1597                         schedule_timeout(cfs_time_seconds(5));
1598                 }
1599         }
1600
1601         while (({set_current_state(TASK_IDLE);
1602                  atomic_read(&tdtd->tdtd_refcount) != 0; }))
1603                 schedule();
1604         __set_current_state(TASK_RUNNING);
1605
1606         spin_lock(&tdtd->tdtd_batchid_lock);
1607         list_for_each_entry_safe(tmt, tmp, &tdtd->tdtd_list,
1608                                  tmt_commit_list)
1609                 list_move_tail(&tmt->tmt_commit_list, &list);
1610         spin_unlock(&tdtd->tdtd_batchid_lock);
1611
1612         CDEBUG(D_INFO, "%s stopping distribute txn commit thread.\n",
1613                tdtd->tdtd_lut->lut_obd->obd_name);
1614         list_for_each_entry_safe(tmt, tmp, &list, tmt_commit_list) {
1615                 list_del_init(&tmt->tmt_commit_list);
1616                 top_multiple_thandle_dump(tmt, D_HA);
1617                 top_multiple_thandle_put(tmt);
1618         }
1619         RETURN(0);
1620 }
1621
1622 /**
1623  * Start llog cancel thread
1624  *
1625  * Start llog cancel(master/slave) thread on LOD
1626  *
1627  * \param[in]lclt       cancel log thread to be started.
1628  *
1629  * \retval              0 if the thread is started successfully.
1630  * \retval              negative errno if the thread is not being
1631  *                      started.
1632  */
1633 int distribute_txn_init(const struct lu_env *env,
1634                         struct lu_target *lut,
1635                         struct target_distribute_txn_data *tdtd,
1636                         __u32 index)
1637 {
1638         struct task_struct      *task;
1639         int                     rc;
1640         ENTRY;
1641
1642         INIT_LIST_HEAD(&tdtd->tdtd_list);
1643         INIT_LIST_HEAD(&tdtd->tdtd_replay_finish_list);
1644         INIT_LIST_HEAD(&tdtd->tdtd_replay_list);
1645         spin_lock_init(&tdtd->tdtd_batchid_lock);
1646         spin_lock_init(&tdtd->tdtd_replay_list_lock);
1647         tdtd->tdtd_replay_handler = distribute_txn_replay_handle;
1648         tdtd->tdtd_replay_ready = 0;
1649
1650         tdtd->tdtd_batchid = lut->lut_last_transno + 1;
1651
1652         init_waitqueue_head(&tdtd->tdtd_recovery_threads_waitq);
1653         atomic_set(&tdtd->tdtd_refcount, 0);
1654         atomic_set(&tdtd->tdtd_recovery_threads_count, 0);
1655
1656         tdtd->tdtd_lut = lut;
1657         if (lut->lut_bottom->dd_rdonly)
1658                 RETURN(0);
1659
1660         rc = distribute_txn_commit_batchid_init(env, tdtd);
1661         if (rc != 0)
1662                 RETURN(rc);
1663
1664         rc = lu_env_init(&tdtd->tdtd_env, LCT_LOCAL | LCT_MD_THREAD);
1665         if (rc)
1666                 RETURN(rc);
1667
1668         task = kthread_create(distribute_txn_commit_thread, tdtd, "dist_txn-%u",
1669                               index);
1670         if (IS_ERR(task)) {
1671                 lu_env_fini(&tdtd->tdtd_env);
1672                 RETURN(PTR_ERR(task));
1673         }
1674         tdtd->tdtd_commit_task = task;
1675         wake_up_process(task);
1676
1677         RETURN(0);
1678 }
1679 EXPORT_SYMBOL(distribute_txn_init);
1680
1681 /**
1682  * Stop llog cancel thread
1683  *
1684  * Stop llog cancel(master/slave) thread on LOD and also destory
1685  * all of transaction in the list.
1686  *
1687  * \param[in]lclt       cancel log thread to be stopped.
1688  */
1689 void distribute_txn_fini(const struct lu_env *env,
1690                          struct target_distribute_txn_data *tdtd)
1691 {
1692         struct top_multiple_thandle *tmt;
1693         LIST_HEAD(list);
1694
1695         /* Stop cancel thread */
1696         if (!tdtd->tdtd_commit_task)
1697                 return;
1698
1699         kthread_stop(tdtd->tdtd_commit_task);
1700         tdtd->tdtd_commit_task = NULL;
1701
1702         spin_lock(&tdtd->tdtd_batchid_lock);
1703         list_splice_init(&tdtd->tdtd_list, &list);
1704         spin_unlock(&tdtd->tdtd_batchid_lock);
1705
1706         CDEBUG(D_INFO, "%s stopping distribute txn commit thread.\n",
1707                tdtd->tdtd_lut->lut_obd->obd_name);
1708         while ((tmt = list_first_entry_or_null(&list,
1709                                                struct top_multiple_thandle,
1710                                                tmt_commit_list)) != NULL) {
1711                 list_del_init(&tmt->tmt_commit_list);
1712                 top_multiple_thandle_dump(tmt, D_HA);
1713                 top_multiple_thandle_put(tmt);
1714         }
1715
1716         lu_env_fini(&tdtd->tdtd_env);
1717
1718         dtrq_list_destroy(tdtd);
1719         if (tdtd->tdtd_batchid_obj != NULL) {
1720                 dt_object_put(env, tdtd->tdtd_batchid_obj);
1721                 tdtd->tdtd_batchid_obj = NULL;
1722         }
1723 }
1724 EXPORT_SYMBOL(distribute_txn_fini);