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