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