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