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