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