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