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