summaryrefslogtreecommitdiff
path: root/tools/libs/light/libxl_stream_write.c
blob: 634f3240d1056fd01110bd3c283cd4a650a6b939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*
 * Copyright (C) 2015      Citrix Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 */

#include "libxl_osdeps.h" /* must come before any other headers */

#include "libxl_internal.h"

/*
 * Infrastructure for writing a domain to a libxl migration v2 stream.
 *
 * Entry points from outside:
 *  - libxl__stream_write_start()
 *     - Start writing a stream from the start.
 *  - libxl__stream_write_start_checkpoint()
 *     - Write the records which form a checkpoint into a stream.
 *
 * In normal operation, there are two tasks running at once; this
 * stream processing, and the libxl-save-helper.  check_all_finished()
 * is used to join all the tasks in both success and error cases.
 *
 * Nomenclature for event callbacks:
 *  - $FOO_done(): Completion callback for $FOO
 *  - write_$FOO(): Set up the datacopier to write a $FOO
 *  - $BAR_header(): A $BAR record header only
 *  - $BAR_record(): A complete $BAR record with header and content
 *
 * The main loop for a plain VM writes:
 *  - Stream header
 *  - Libxc record
 *  - (optional) Emulator xenstore record
 *  - if (hvm)
 *      - Emulator context record
 *  - End record
 *
 * For checkpointed stream, there is a second loop which is triggered by a
 * save-helper checkpoint callback.  It writes:
 *  - (optional) Emulator xenstore record
 *  - if (hvm)
 *      - Emulator context record
 *  - Checkpoint end record
 *
 * For back channel stream:
 * - libxl__stream_write_start()
 *    - Set up the stream to running state
 *
 * - Use libxl__stream_write_checkpoint_state to write the record. When the
 *   record is written out, call stream->checkpoint_callback() to return.
 */

/* Success/error/cleanup handling. */
static void stream_success(libxl__egc *egc,
                           libxl__stream_write_state *stream);
static void stream_complete(libxl__egc *egc,
                            libxl__stream_write_state *stream, int rc);
static void stream_done(libxl__egc *egc,
                        libxl__stream_write_state *stream, int rc);
static void checkpoint_done(libxl__egc *egc,
                            libxl__stream_write_state *stream,
                            int rc);
static void check_all_finished(libxl__egc *egc,
                               libxl__stream_write_state *stream, int rc);

/* Event chain for a plain VM. */
static void stream_header_done(libxl__egc *egc,
                               libxl__datacopier_state *dc,
                               int rc, int onwrite, int errnoval);
static void libxc_header_done(libxl__egc *egc,
                              libxl__stream_write_state *stream);
/* libxl__xc_domain_save_done() lives here, event-order wise. */
static void write_emulator_xenstore_record(libxl__egc *egc,
                                           libxl__stream_write_state *stream);
static void emulator_xenstore_record_done(libxl__egc *egc,
                                          libxl__stream_write_state *stream);
static void write_emulator_context_record(libxl__egc *egc,
                                          libxl__stream_write_state *stream);
static void emulator_context_read_done(libxl__egc *egc,
                                       libxl__datacopier_state *dc,
                                       int rc, int onwrite, int errnoval);
static void emulator_context_record_done(libxl__egc *egc,
                                         libxl__stream_write_state *stream);
static void write_end_record(libxl__egc *egc,
                             libxl__stream_write_state *stream);

/* Event chain unique to checkpointed streams. */
static void write_checkpoint_end_record(libxl__egc *egc,
                                        libxl__stream_write_state *stream);
static void checkpoint_end_record_done(libxl__egc *egc,
                                       libxl__stream_write_state *stream);

/* checkpoint state */
static void write_checkpoint_state_done(libxl__egc *egc,
                                        libxl__stream_write_state *stream);
static void checkpoint_state_done(libxl__egc *egc,
                                  libxl__stream_write_state *stream, int rc);

/*----- Helpers -----*/

static void write_done(libxl__egc *egc,
                       libxl__datacopier_state *dc,
                       int rc, int onwrite, int errnoval);

/* Generic helper to set up writing some data to the stream. */
static void setup_generic_write(libxl__egc *egc,
                                libxl__stream_write_state *stream,
                                const char *what,
                                libxl__sr_rec_hdr *hdr,
                                libxl__sr_emulator_hdr *emu_hdr,
                                void *body,
                                sws_record_done_cb cb)
{
    static const uint8_t zero_padding[1U << REC_ALIGN_ORDER] = { 0 };

    libxl__datacopier_state *dc = &stream->dc;
    int rc;

    assert(stream->record_done_callback == NULL);

    dc->writewhat = what;
    dc->used      = 0;
    dc->callback  = write_done;
    rc = libxl__datacopier_start(dc);

    if (rc) {
        stream_complete(egc, stream, rc);
        return;
    }

    size_t padsz = ROUNDUP(hdr->length, REC_ALIGN_ORDER) - hdr->length;
    uint32_t length = hdr->length;

    /* Insert header */
    libxl__datacopier_prefixdata(egc, dc, hdr, sizeof(*hdr));

    /* Optional emulator sub-header */
    if (emu_hdr) {
        assert(length >= sizeof(*emu_hdr));
        libxl__datacopier_prefixdata(egc, dc, emu_hdr, sizeof(*emu_hdr));
        length -= sizeof(*emu_hdr);
    }

    /* Optional body */
    if (body)
        libxl__datacopier_prefixdata(egc, dc, body, length);

    /* Any required padding */
    if (padsz > 0)
        libxl__datacopier_prefixdata(egc, dc,
                                     zero_padding, padsz);
    stream->record_done_callback = cb;
}

/* Helper to set up writing a regular record to the stream. */
static void setup_write(libxl__egc *egc,
                        libxl__stream_write_state *stream,
                        const char *what,
                        libxl__sr_rec_hdr *hdr,
                        void *body,
                        sws_record_done_cb cb)
{
    setup_generic_write(egc, stream, what, hdr, NULL, body, cb);
}

/* Helper to set up writing a record with an emulator prefix to the stream. */
static void setup_emulator_write(libxl__egc *egc,
                                 libxl__stream_write_state *stream,
                                 const char *what,
                                 libxl__sr_rec_hdr *hdr,
                                 libxl__sr_emulator_hdr *emu_hdr,
                                 void *body,
                                 sws_record_done_cb cb)
{
    assert(stream->emu_sub_hdr.id != EMULATOR_UNKNOWN);
    setup_generic_write(egc, stream, what, hdr, emu_hdr, body, cb);
}


static void write_done(libxl__egc *egc,
                       libxl__datacopier_state *dc,
                       int rc, int onwrite, int errnoval)
{
    libxl__stream_write_state *stream = CONTAINER_OF(dc, *stream, dc);
    STATE_AO_GC(stream->ao);
    sws_record_done_cb cb = stream->record_done_callback;

    stream->record_done_callback = NULL;

    if (onwrite || errnoval)
        stream_complete(egc, stream, rc ?: ERROR_FAIL);
    else
        cb(egc, stream);
}

/*----- Entrypoints -----*/

void libxl__stream_write_init(libxl__stream_write_state *stream)
{
    assert(stream->ao);

    stream->shs.ao = stream->ao;
    libxl__save_helper_init(&stream->shs);

    stream->rc = 0;
    stream->running = false;
    stream->in_checkpoint = false;
    stream->sync_teardown = false;
    FILLZERO(stream->dc);
    stream->record_done_callback = NULL;
    FILLZERO(stream->emu_dc);
    stream->emu_carefd = NULL;
    FILLZERO(stream->emu_rec_hdr);
    FILLZERO(stream->emu_sub_hdr);
    stream->emu_body = NULL;
    stream->device_model_version = LIBXL_DEVICE_MODEL_VERSION_UNKNOWN;
}

void libxl__stream_write_start(libxl__egc *egc,
                               libxl__stream_write_state *stream)
{
    libxl__datacopier_state *dc = &stream->dc;
    libxl__domain_save_state *dss = stream->dss;
    STATE_AO_GC(stream->ao);
    struct libxl__sr_hdr hdr;
    int rc = 0;

    libxl__stream_write_init(stream);

    stream->running = true;

    dc->ao        = ao;
    dc->readfd    = -1;
    dc->writewhat = "stream header";
    dc->copywhat  = "save v2 stream";
    dc->writefd   = stream->fd;
    dc->maxsz     = -1;
    dc->callback  = stream_header_done;

    if (stream->back_channel)
        return;

    if (dss->type == LIBXL_DOMAIN_TYPE_HVM) {
        stream->device_model_version =
            libxl__device_model_version_running(gc, dss->domid);
        switch (stream->device_model_version) {
        case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL:
            stream->emu_sub_hdr.id = EMULATOR_QEMU_TRADITIONAL;
            break;

        case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
            stream->emu_sub_hdr.id = EMULATOR_QEMU_UPSTREAM;
            break;

        default:
            rc = ERROR_FAIL;
            LOGD(ERROR, dss->domid, "Unknown emulator for HVM domain");
            goto err;
        }
        stream->emu_sub_hdr.index = 0;
    }

    rc = libxl__datacopier_start(dc);
    if (rc)
        goto err;

    FILLZERO(hdr);
    hdr.ident   = htobe64(RESTORE_STREAM_IDENT);
    hdr.version = htobe32(RESTORE_STREAM_VERSION);
    hdr.options = htobe32(0);

    libxl__datacopier_prefixdata(egc, dc, &hdr, sizeof(hdr));
    return;

 err:
    assert(rc);
    stream_complete(egc, stream, rc);
}

void libxl__stream_write_start_checkpoint(libxl__egc *egc,
                                          libxl__stream_write_state *stream)
{
    assert(stream->running);
    assert(!stream->in_checkpoint);
    assert(!stream->back_channel);
    stream->in_checkpoint = true;

    write_emulator_xenstore_record(egc, stream);
}

void libxl__stream_write_abort(libxl__egc *egc,
                               libxl__stream_write_state *stream, int rc)
{
    assert(rc);

    if (stream->running)
        stream_complete(egc, stream, rc);
}

/*----- Event logic -----*/

static void stream_header_done(libxl__egc *egc,
                               libxl__datacopier_state *dc,
                               int rc, int onwrite, int errnoval)
{
    libxl__stream_write_state *stream = CONTAINER_OF(dc, *stream, dc);
    STATE_AO_GC(stream->ao);
    struct libxl__sr_rec_hdr rec;

    if (rc || errnoval) {
        stream_complete(egc, stream, rc ?: ERROR_FAIL);
        return;
    }

    FILLZERO(rec);
    rec.type = REC_TYPE_LIBXC_CONTEXT;

    setup_write(egc, stream, "libxc header",
                &rec, NULL, libxc_header_done);
}

static void libxc_header_done(libxl__egc *egc,
                              libxl__stream_write_state *stream)
{
    libxl__xc_domain_save(egc, stream->dss, &stream->shs);
}

void libxl__xc_domain_save_done(libxl__egc *egc, void *dss_void,
                                int rc, int retval, int errnoval)
{
    libxl__domain_save_state *dss = dss_void;
    libxl__stream_write_state *stream = &dss->sws;
    STATE_AO_GC(dss->ao);

    if (rc)
        goto err;

    if (retval) {
        LOGEVD(ERROR, errnoval, dss->domid, "saving domain: %s",
              dss->dsps.guest_responded ?
              "domain responded to suspend request" :
              "domain did not respond to suspend request");
        if (!dss->dsps.guest_responded)
            rc = ERROR_GUEST_TIMEDOUT;
        else if (dss->rc)
            rc = dss->rc;
        else
            rc = ERROR_FAIL;
        goto err;
    }

 err:
    check_all_finished(egc, stream, rc);

    /*
     * This function is the callback associated with the save helper
     * task, not the stream task.  We do not know whether the stream is
     * alive, and check_all_finished() may have torn it down around us.
     * If the stream is not still alive, we must not continue any work.
     */
    if (libxl__stream_write_inuse(stream)) {
        if (dss->checkpointed_stream != LIBXL_CHECKPOINTED_STREAM_NONE)
            /*
             * For remus, if libxl__xc_domain_save_done() completes,
             * there was an error sending data to the secondary.
             * Resume the primary ASAP. The caller doesn't care of the
             * return value (Please refer to libxl__remus_teardown())
             */
            stream_complete(egc, stream, 0);
        else
            write_emulator_xenstore_record(egc, stream);
    }
}

static void write_emulator_xenstore_record(libxl__egc *egc,
                                           libxl__stream_write_state *stream)
{
    libxl__domain_save_state *dss = stream->dss;
    STATE_AO_GC(stream->ao);
    struct libxl__sr_rec_hdr rec;
    int rc;
    char *buf = NULL;
    uint32_t len = 0;

    if (dss->type != LIBXL_DOMAIN_TYPE_HVM) {
        emulator_xenstore_record_done(egc, stream);
        return;
    }

    rc = libxl__save_emulator_xenstore_data(dss, &buf, &len);
    if (rc)
        goto err;

    /* No record? - All done. */
    if (len == 0) {
        emulator_xenstore_record_done(egc, stream);
        return;
    }

    FILLZERO(rec);
    rec.type = REC_TYPE_EMULATOR_XENSTORE_DATA;
    rec.length = len + sizeof(stream->emu_sub_hdr);

    setup_emulator_write(egc, stream, "emulator xenstore record",
                         &rec, &stream->emu_sub_hdr, buf,
                         emulator_xenstore_record_done);
    return;

 err:
    assert(rc);
    stream_complete(egc, stream, rc);
}

static void emulator_xenstore_record_done(libxl__egc *egc,
                                          libxl__stream_write_state *stream)
{
    libxl__domain_save_state *dss = stream->dss;

    if (dss->type == LIBXL_DOMAIN_TYPE_HVM)
        write_emulator_context_record(egc, stream);
    else {
        if (stream->in_checkpoint)
            write_checkpoint_end_record(egc, stream);
        else
            write_end_record(egc, stream);
    }
}

static void write_emulator_context_record(libxl__egc *egc,
                                          libxl__stream_write_state *stream)
{
    libxl__domain_save_state *dss = stream->dss;
    libxl__datacopier_state *dc = &stream->emu_dc;
    STATE_AO_GC(stream->ao);
    struct libxl__sr_rec_hdr *rec = &stream->emu_rec_hdr;
    struct stat st;
    int rc;

    if (dss->type != LIBXL_DOMAIN_TYPE_HVM) {
        emulator_context_record_done(egc, stream);
        return;
    }

    /* Convenience aliases */
    const char *const filename = dss->dsps.dm_savefile;

    libxl__carefd_begin();
    int readfd = open(filename, O_RDONLY);
    stream->emu_carefd = libxl__carefd_opened(CTX, readfd);
    if (readfd == -1) {
        rc = ERROR_FAIL;
        LOGED(ERROR, dss->domid, "unable to open %s", filename);
        goto err;
    }

    if (fstat(readfd, &st)) {
        rc = ERROR_FAIL;
        LOGED(ERROR, dss->domid, "unable to fstat %s", filename);
        goto err;
    }

    if (!S_ISREG(st.st_mode)) {
        rc = ERROR_FAIL;
        LOGD(ERROR, dss->domid, "%s is not a plain file!", filename);
        goto err;
    }

    rec->type = REC_TYPE_EMULATOR_CONTEXT;
    rec->length = st.st_size + sizeof(stream->emu_sub_hdr);
    stream->emu_body = libxl__malloc(NOGC, st.st_size);

    FILLZERO(*dc);
    dc->ao            = stream->ao;
    dc->readwhat      = "qemu save file";
    dc->copywhat      = "save v2 stream";
    dc->readfd        = readfd;
    dc->writefd       = -1;
    dc->maxsz         = -1;
    dc->readbuf       = stream->emu_body;
    dc->bytes_to_read = st.st_size;
    dc->callback      = emulator_context_read_done;

    rc = libxl__datacopier_start(dc);
    if (rc)
        goto err;

    return;

 err:
    assert(rc);
    stream_complete(egc, stream, rc);
}

static void emulator_context_read_done(libxl__egc *egc,
                                       libxl__datacopier_state *dc,
                                       int rc, int onwrite, int errnoval)
{
    libxl__stream_write_state *stream = CONTAINER_OF(dc, *stream, emu_dc);
    STATE_AO_GC(stream->ao);

    if (rc || onwrite || errnoval) {
        stream_complete(egc, stream, rc ?: ERROR_FAIL);
        return;
    }

    libxl__carefd_close(stream->emu_carefd);
    stream->emu_carefd = NULL;

    setup_emulator_write(egc, stream, "emulator record",
                         &stream->emu_rec_hdr,
                         &stream->emu_sub_hdr,
                         stream->emu_body,
                         emulator_context_record_done);
}

static void emulator_context_record_done(libxl__egc *egc,
                                         libxl__stream_write_state *stream)
{
    free(stream->emu_body);
    stream->emu_body = NULL;

    if (stream->in_checkpoint)
        write_checkpoint_end_record(egc, stream);
    else
        write_end_record(egc, stream);
}

static void write_end_record(libxl__egc *egc,
                             libxl__stream_write_state *stream)
{
    struct libxl__sr_rec_hdr rec;

    FILLZERO(rec);
    rec.type = REC_TYPE_END;

    setup_write(egc, stream, "end record",
                &rec, NULL, stream_success);
}

static void write_checkpoint_end_record(libxl__egc *egc,
                                        libxl__stream_write_state *stream)
{
    struct libxl__sr_rec_hdr rec;

    FILLZERO(rec);
    rec.type = REC_TYPE_CHECKPOINT_END;

    setup_write(egc, stream, "checkpoint end record",
                &rec, NULL, checkpoint_end_record_done);
}

static void checkpoint_end_record_done(libxl__egc *egc,
                                       libxl__stream_write_state *stream)
{
    checkpoint_done(egc, stream, 0);
}

/*----- Success/error/cleanup handling. -----*/

static void stream_success(libxl__egc *egc, libxl__stream_write_state *stream)
{
    stream_complete(egc, stream, 0);
}

static void stream_complete(libxl__egc *egc,
                            libxl__stream_write_state *stream, int rc)
{
    assert(stream->running);

    if (stream->in_checkpoint) {
        assert(rc);

        /*
         * If an error is encountered while in a checkpoint, pass it
         * back to libxc.  The failure will come back around to us via
         * libxl__xc_domain_save_done()
         */
        checkpoint_done(egc, stream, rc);
        return;
    }

    if (stream->in_checkpoint_state) {
        assert(rc);

        /*
         * If an error is encountered while in a checkpoint, pass it
         * back to libxc.  The failure will come back around to us via
         * 1. normal stream
         *    libxl__xc_domain_save_done()
         * 2. back_channel stream
         *    libxl__stream_write_abort()
         */
        checkpoint_state_done(egc, stream, rc);
        return;
    }

    stream_done(egc, stream, rc);
}

static void stream_done(libxl__egc *egc,
                        libxl__stream_write_state *stream, int rc)
{
    assert(stream->running);
    assert(!stream->in_checkpoint_state);
    stream->running = false;

    if (stream->emu_carefd)
        libxl__carefd_close(stream->emu_carefd);
    free(stream->emu_body);

    if (!stream->back_channel) {
        /*
         * 1. In stream_done(), stream->running is set to false, so
         *    the stream itself is not in use.
         * 2. Write stream is a back channel stream, this means it
         *    is only used by secondary(restore side) to send records
         *    back, so it doesn't have save helper.
         * So we don't need invoke check_all_finished here
         */
         check_all_finished(egc, stream, rc);
    }
}

static void checkpoint_done(libxl__egc *egc,
                            libxl__stream_write_state *stream,
                            int rc)
{
    assert(stream->in_checkpoint);

    stream->in_checkpoint = false;
    stream->checkpoint_callback(egc, stream, rc);
}

static void check_all_finished(libxl__egc *egc,
                               libxl__stream_write_state *stream,
                               int rc)
{
    STATE_AO_GC(stream->ao);

    /*
     * In the case of a failure, the _abort()'s below might cancel
     * synchronously on top of us, or asynchronously at a later point.
     *
     * We must avoid the situation where all _abort() cancel
     * synchronously and the completion_callback() gets called twice;
     * once by the first error and once by the final stacked abort(),
     * both of whom will find that all of the tasks have stopped.
     *
     * To avoid this problem, any stacked re-entry into this function is
     * ineligible to fire the completion callback.  The outermost
     * instance will take care of completing, once the stack has
     * unwound.
     */
    if (stream->sync_teardown)
        return;

    if (!stream->rc && rc) {
        /* First reported failure. Tear everything down. */
        stream->rc = rc;
        stream->sync_teardown = true;

        libxl__stream_write_abort(egc, stream, rc);
        libxl__save_helper_abort(egc, &stream->shs);

        stream->sync_teardown = false;
    }

    /* Don't fire the callback until all our parallel tasks have stopped. */
    if (libxl__stream_write_inuse(stream) ||
        libxl__save_helper_inuse(&stream->shs))
        return;

    if (stream->completion_callback)
        /* back channel stream doesn't have completion_callback() */
        stream->completion_callback(egc, stream, stream->rc);
}

/*----- checkpoint state -----*/

void libxl__stream_write_checkpoint_state(libxl__egc *egc,
                                          libxl__stream_write_state *stream,
                                          libxl_sr_checkpoint_state *srcs)
{
    struct libxl__sr_rec_hdr rec;

    assert(stream->running);
    assert(!stream->in_checkpoint);
    assert(!stream->in_checkpoint_state);
    stream->in_checkpoint_state = true;

    FILLZERO(rec);
    rec.type = REC_TYPE_CHECKPOINT_STATE;
    rec.length = sizeof(*srcs);

    setup_write(egc, stream, "checkpoint state", &rec,
                srcs, write_checkpoint_state_done);
}

static void write_checkpoint_state_done(libxl__egc *egc,
                                        libxl__stream_write_state *stream)
{
    checkpoint_state_done(egc, stream, 0);
}

static void checkpoint_state_done(libxl__egc *egc,
                                  libxl__stream_write_state *stream, int rc)
{
    assert(stream->in_checkpoint_state);
    stream->in_checkpoint_state = false;
    stream->checkpoint_callback(egc, stream, rc);
}

/*
 * Local variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */