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