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