cocoon-tpm-storage 0.1.3

Cocoon TPM project - secure persistent storage
Documentation
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023-2025 SUSE LLC
// Author: Nicolai Stange <nstange@suse.de>

//! Implementation of [`TransactionAuthTreeDataBlocksDigestsUpdatesIterator`]
//! and [`TransactionJournalUpdateAuthDigestsScriptIterator`].

extern crate alloc;

use alloc::boxed::Box;

use super::{
    Transaction,
    auth_tree_data_blocks_update_states::{AuthTreeDataBlocksUpdateStates, AuthTreeDataBlocksUpdateStatesIndex},
};
use crate::{
    blkdev,
    fs::{
        NvFsError,
        cocoonfs::{
            alloc_bitmap, auth_tree,
            fs::{CocoonFsConfig, CocoonFsSyncStateMemberRef, CocoonFsSyncStateReadFuture},
            journal, layout, read_buffer,
        },
    },
    nvfs_err_internal,
    utils_async::sync_types,
    utils_common::{bitmanip::BitManip as _, fixed_vec::FixedVec},
};
use core::{marker, mem, pin, task};

#[cfg(doc)]
use super::auth_tree_data_blocks_update_states::AuthTreeDataBlockUpdateState;
#[cfg(doc)]
use layout::ImageLayout;

/// Compute the updated digest of an [Authentication Tree Data
/// Blocks](ImageLayout::auth_tree_data_block_allocation_blocks_log2) with some
/// deallocations but no data modifications in it.
struct RedigestAuthTreeDataBlockFuture<ST: sync_types::SyncTypes, B: blkdev::NvBlkDev> {
    auth_tree_data_block_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
    updated_auth_tree_data_block_alloc_bitmap: alloc_bitmap::BitmapWord,
    auth_tree_data_block_allocation_blocks_bufs: FixedVec<Option<FixedVec<u8, 7>>, 0>,
    fut_state: RedigestAuthTreeDataBlockFutureState<B>,
    _phantom: marker::PhantomData<fn() -> *const ST>,
}

/// [`RedigestAuthTreeDataBlockFuture`] state-machine state.
#[allow(clippy::large_enum_variant)]
enum RedigestAuthTreeDataBlockFutureState<B: blkdev::NvBlkDev> {
    DetermineNextReadAuthenticateSubrange {
        next_allocation_block_index: layout::PhysicalAllocBlockIndex,
    },
    ReadAuthenticateSubrange {
        subrange: layout::PhysicalAllocBlockRange,
        read_auth_subrange_fut: read_buffer::BufferedReadAuthenticateDataFuture<B>,
    },
    Done,
}

impl<ST: sync_types::SyncTypes, B: blkdev::NvBlkDev> RedigestAuthTreeDataBlockFuture<ST, B> {
    /// Instantiate a [`RedigestAuthTreeDataBlockFuture`].
    ///
    /// # Arguments:
    ///
    /// * `fs_config` - The filesystem instance's [`CocoonFsConfig`].
    /// * `auth_tree_data_block_allocation_blocks_begin` - Storage location of
    ///   the [Authentication Tree Data
    ///   Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2) whose
    ///   digest to recompute.
    /// * `updated_auth_tree_data_block_alloc_bitmap` -
    ///   [`BitmapWord`](alloc_bitmap::BitmapWord) representing the updated
    ///   allocation status of the [Authentication Tree Data
    ///   Block's](ImageLayout::auth_tree_data_block_allocation_blocks_log2)
    ///   [Allocation Blocks](ImageLayout::allocation_block_size_128b_log2)
    ///   each.
    fn new(
        fs_config: &CocoonFsConfig,
        auth_tree_data_block_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
        mut updated_auth_tree_data_block_alloc_bitmap: alloc_bitmap::BitmapWord,
    ) -> Result<Self, NvFsError> {
        // Anything before image_header_end is considered unallocated for the purpose of
        // computing the authentication digest.
        let image_header_end = fs_config.image_header_end;
        if auth_tree_data_block_allocation_blocks_begin < image_header_end {
            updated_auth_tree_data_block_alloc_bitmap &= !alloc_bitmap::BitmapWord::trailing_bits_mask(
                u64::from(image_header_end - auth_tree_data_block_allocation_blocks_begin)
                    .min(alloc_bitmap::BitmapWord::BITS as u64) as u32,
            );
        }

        let auth_tree_data_block_allocation_blocks =
            1usize << (fs_config.image_layout.auth_tree_data_block_allocation_blocks_log2 as u32);
        let auth_tree_data_block_allocation_blocks_bufs =
            FixedVec::new_with_default(auth_tree_data_block_allocation_blocks)?;
        Ok(Self {
            auth_tree_data_block_allocation_blocks_begin,
            updated_auth_tree_data_block_alloc_bitmap,
            auth_tree_data_block_allocation_blocks_bufs,
            fut_state: RedigestAuthTreeDataBlockFutureState::DetermineNextReadAuthenticateSubrange {
                next_allocation_block_index: auth_tree_data_block_allocation_blocks_begin,
            },
            _phantom: marker::PhantomData,
        })
    }
}

impl<ST: sync_types::SyncTypes, B: blkdev::NvBlkDev> CocoonFsSyncStateReadFuture<ST, B>
    for RedigestAuthTreeDataBlockFuture<ST, B>
{
    /// Output type of [`poll()`](Self::poll).
    ///
    /// On successful completion, the recomputed digest over the [Authentication
    /// Tree Data
    /// Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2) is
    /// returned.
    type Output = Result<FixedVec<u8, 5>, NvFsError>;
    type AuxPollData<'a> = ();

    fn poll<'a>(
        self: pin::Pin<&mut Self>,
        fs_instance_sync_state: &mut CocoonFsSyncStateMemberRef<'_, ST, B>,
        _aux_data: &mut Self::AuxPollData<'a>,
        cx: &mut task::Context<'_>,
    ) -> task::Poll<Self::Output> {
        let this = pin::Pin::into_inner(self);

        loop {
            match &mut this.fut_state {
                RedigestAuthTreeDataBlockFutureState::DetermineNextReadAuthenticateSubrange {
                    next_allocation_block_index,
                } => {
                    // Fits an u32, because an Authentication Tree Data Block's individual
                    // Allocation Blocks can get tracked in a single BitmapWord, whose width fits an
                    // u32.
                    let offset_in_auth_tree_data_block =
                        u64::from(*next_allocation_block_index - this.auth_tree_data_block_allocation_blocks_begin)
                            as u32;

                    let updated_auth_tree_data_block_alloc_bitmap = this.updated_auth_tree_data_block_alloc_bitmap
                        & !alloc_bitmap::BitmapWord::trailing_bits_mask(offset_in_auth_tree_data_block);
                    if updated_auth_tree_data_block_alloc_bitmap == 0 {
                        // All of the Authentication Tree Data Block's allocated Allocation Blocks
                        // have been loaded and authenticated. Compute the Authentication Tree Data
                        // Block digest and return.
                        let fs_instance = fs_instance_sync_state.get_fs_ref();
                        let auth_tree_config = fs_instance_sync_state.auth_tree.get_config();
                        let data_block_index = auth_tree_config
                            .translate_physical_to_data_block_index(this.auth_tree_data_block_allocation_blocks_begin);
                        let auth_digest = match auth_tree_config.digest_data_block(
                            data_block_index,
                            this.auth_tree_data_block_allocation_blocks_bufs
                                .iter()
                                .map(|buf| Ok(buf.as_deref())),
                            fs_instance.fs_config.image_header_end,
                        ) {
                            Ok(auth_digest) => auth_digest,
                            Err(e) => {
                                this.fut_state = RedigestAuthTreeDataBlockFutureState::Done;
                                return task::Poll::Ready(Err(e));
                            }
                        };
                        this.fut_state = RedigestAuthTreeDataBlockFutureState::Done;
                        return task::Poll::Ready(Ok(auth_digest));
                    }

                    // There are some more Allocation Blocks to read and authenticate left.
                    // Determine the next (least significant) run of 1s in the bitmap word.
                    let next_subrange_begin_in_auth_tree_data_block =
                        updated_auth_tree_data_block_alloc_bitmap.trailing_zeros();
                    let next_subrange_end_in_auth_tree_data_block = next_subrange_begin_in_auth_tree_data_block
                        + (!updated_auth_tree_data_block_alloc_bitmap >> next_subrange_begin_in_auth_tree_data_block)
                            .trailing_zeros();
                    let next_subrange = layout::PhysicalAllocBlockRange::new(
                        this.auth_tree_data_block_allocation_blocks_begin
                            + layout::AllocBlockCount::from(next_subrange_begin_in_auth_tree_data_block as u64),
                        this.auth_tree_data_block_allocation_blocks_begin
                            + layout::AllocBlockCount::from(next_subrange_end_in_auth_tree_data_block as u64),
                    );
                    let fs_instance = fs_instance_sync_state.get_fs_ref();
                    let read_auth_next_subrange_fut = match read_buffer::BufferedReadAuthenticateDataFuture::new(
                        &next_subrange,
                        &fs_instance.fs_config.image_layout,
                        fs_instance_sync_state.auth_tree.get_config(),
                        &fs_instance.blkdev,
                    ) {
                        Ok(read_auth_next_subrange_fut) => read_auth_next_subrange_fut,
                        Err(e) => {
                            this.fut_state = RedigestAuthTreeDataBlockFutureState::Done;
                            return task::Poll::Ready(Err(e));
                        }
                    };
                    this.fut_state = RedigestAuthTreeDataBlockFutureState::ReadAuthenticateSubrange {
                        subrange: next_subrange,
                        read_auth_subrange_fut: read_auth_next_subrange_fut,
                    };
                }
                RedigestAuthTreeDataBlockFutureState::ReadAuthenticateSubrange {
                    subrange,
                    read_auth_subrange_fut,
                } => {
                    let (
                        fs_instance,
                        _fs_sync_state_image_size,
                        fs_sync_state_alloc_bitmap,
                        _fs_sync_state_alloc_bitmap_file,
                        mut fs_sync_state_auth_tree,
                        _fs_sync_state_inode_index,
                        fs_sync_state_read_buffer,
                        _fs_sync_state_keys_cache,
                    ) = fs_instance_sync_state.fs_instance_and_destructure_borrow();
                    let fs_config = &fs_instance.fs_config;
                    let mut subrange_allocation_blocks_bufs =
                        match read_buffer::BufferedReadAuthenticateDataFuture::poll(
                            pin::Pin::new(read_auth_subrange_fut),
                            &fs_instance.blkdev,
                            &fs_config.image_layout,
                            fs_config.image_header_end,
                            fs_sync_state_alloc_bitmap,
                            &mut fs_sync_state_auth_tree,
                            fs_sync_state_read_buffer,
                            cx,
                        ) {
                            task::Poll::Ready(Ok(subrange_allocation_blocks_bufs)) => subrange_allocation_blocks_bufs,
                            task::Poll::Ready(Err(e)) => {
                                this.fut_state = RedigestAuthTreeDataBlockFutureState::Done;
                                return task::Poll::Ready(Err(e));
                            }
                            task::Poll::Pending => return task::Poll::Pending,
                        };

                    debug_assert_eq!(
                        subrange_allocation_blocks_bufs.len(),
                        u64::from(subrange.block_count()) as usize
                    );
                    let offset_in_auth_tree_data_block =
                        u64::from(subrange.begin() - this.auth_tree_data_block_allocation_blocks_begin) as usize;
                    for (i, allocation_block_buf) in subrange_allocation_blocks_bufs.iter_mut().enumerate() {
                        this.auth_tree_data_block_allocation_blocks_bufs[offset_in_auth_tree_data_block + i] =
                            Some(mem::take(allocation_block_buf));
                    }

                    let next_allocation_block_index = subrange.end();
                    this.fut_state = RedigestAuthTreeDataBlockFutureState::DetermineNextReadAuthenticateSubrange {
                        next_allocation_block_index,
                    };
                }
                RedigestAuthTreeDataBlockFutureState::Done => unreachable!(),
            };
        }
    }
}

/// [`AuthTreeDataBlocksUpdatesIterator`](auth_tree::AuthTreeDataBlocksUpdatesIterator) implementation over
/// a [`Transaction`].
///
/// Used for [preparing](auth_tree::AuthTreePrepareUpdatesFuture) updates to the
/// authentication tree upon [`Transaction`] commit.
pub struct TransactionAuthTreeDataBlocksDigestsUpdatesIterator<ST: sync_types::SyncTypes, B: blkdev::NvBlkDev> {
    transaction: Option<Box<Transaction>>,
    next_auth_tree_data_block_allocation_blocks_begin: layout::PhysicalAllocBlockIndex,
    next_update_states_index: AuthTreeDataBlocksUpdateStatesIndex,
    next_pending_free: Option<(layout::PhysicalAllocBlockIndex, alloc_bitmap::BitmapWord)>,
    redigest_auth_tree_data_block_fut: Option<RedigestAuthTreeDataBlockFuture<ST, B>>,
}

impl<ST: sync_types::SyncTypes, B: blkdev::NvBlkDev> TransactionAuthTreeDataBlocksDigestsUpdatesIterator<ST, B> {
    /// Instantiate a new
    /// [`TransactionAuthTreeDataBlocksDigestsUpdatesIterator`].
    ///
    /// The [`TransactionAuthTreeDataBlocksDigestsUpdatesIterator`] assumes
    /// ownership of the `transaction` for the duration of its lifetime. It
    /// may eventually get obtained back via
    /// [`into_transaction()`](Self::into_transaction).
    ///
    /// # Arguments:
    ///
    /// * `transaction` - The [`Transaction`] to compute authentication tree
    ///   updates for.
    /// * `auth_tree_data_block_allocation_blocks_log2` - Verbatim value of
    ///   [`ImageLayout::auth_tree_data_block_allocation_blocks_log2`].
    /// * `image_header_end` - [End of the filesystem image header on
    ///   storage](crate::fs::cocoonfs::image_header::MutableImageHeader::physical_location).
    pub fn new(
        transaction: Box<Transaction>,
        auth_tree_data_block_allocation_blocks_log2: u8,
        image_header_end: layout::PhysicalAllocBlockIndex,
    ) -> Self {
        let auth_tree_data_block_allocation_blocks_log2 = auth_tree_data_block_allocation_blocks_log2 as u32;
        let auth_tree_data_block_allocation_blocks =
            layout::AllocBlockCount::from(1u64 << auth_tree_data_block_allocation_blocks_log2);

        let next_update_states_index = Self::skip_to_auth_tree_update_state_with_modified_data(
            &transaction.auth_tree_data_blocks_update_states,
            AuthTreeDataBlocksUpdateStatesIndex::from(0usize),
            image_header_end,
            auth_tree_data_block_allocation_blocks,
        );

        let next_pending_free = transaction
            .allocs
            .pending_frees
            .block_iter(auth_tree_data_block_allocation_blocks_log2)
            .next();

        Self {
            transaction: Some(transaction),
            next_auth_tree_data_block_allocation_blocks_begin: layout::PhysicalAllocBlockIndex::from(0u64),
            next_update_states_index,
            next_pending_free,
            redigest_auth_tree_data_block_fut: None,
        }
    }

    /// Obtain the [`Transaction`] initially passed to [`new()`](Self::new)
    /// back.
    pub fn into_transaction(self) -> Result<Box<Transaction>, NvFsError> {
        self.transaction.ok_or_else(|| nvfs_err_internal!())
    }

    /// Skip over the [`Transaction`]'s [`AuthTreeDataBlockUpdateState`]s with
    /// no recorded data modifications.
    ///
    /// Return the new index corresponding to the new position in
    /// `update_states`.
    ///
    /// Arguments:
    ///
    /// * `update_states` - Reference to
    ///   [`Transaction::auth_tree_data_blocks_update_states`].
    /// * `update_states_index` - Current position in `update_states`.
    /// * `image_header_end` - [End of the filesystem image header on
    ///   storage](crate::fs::cocoonfs::image_header::MutableImageHeader::physical_location).
    /// * `auth_tree_data_block_allocation_blocks` - Number of [Allocation
    ///   Blocks](ImageLayout::allocation_block_size_128b_log2) in an
    ///   [Authentication Tree Data
    ///   Block](ImageLayout::auth_tree_data_block_allocation_blocks_log2).
    fn skip_to_auth_tree_update_state_with_modified_data(
        update_states: &AuthTreeDataBlocksUpdateStates,
        mut update_states_index: AuthTreeDataBlocksUpdateStatesIndex,
        image_header_end: layout::PhysicalAllocBlockIndex,
        auth_tree_data_block_allocation_blocks: layout::AllocBlockCount,
    ) -> AuthTreeDataBlocksUpdateStatesIndex {
        while usize::from(update_states_index) < update_states.len() {
            let update_state = &update_states[update_states_index];
            if update_state
                .iter_allocation_blocks()
                .skip(
                    u64::from(image_header_end)
                        .saturating_sub(u64::from(update_state.get_target_allocation_blocks_begin()))
                        .min(u64::from(auth_tree_data_block_allocation_blocks)) as usize,
                )
                .any(|allocation_block_update_state| allocation_block_update_state.has_modified_data())
            {
                break;
            }
            update_states_index = update_states_index.step();
        }
        update_states_index
    }
}

impl<ST: sync_types::SyncTypes, B: blkdev::NvBlkDev> auth_tree::AuthTreeDataBlocksUpdatesIterator<ST, B>
    for TransactionAuthTreeDataBlocksDigestsUpdatesIterator<ST, B>
{
    fn poll_for_next(
        self: pin::Pin<&mut Self>,
        fs_instance_sync_state: &mut CocoonFsSyncStateMemberRef<'_, ST, B>,
        cx: &mut task::Context<'_>,
    ) -> task::Poll<Result<Option<auth_tree::PhysicalAuthTreeDataBlockUpdate>, NvFsError>> {
        let this = pin::Pin::into_inner(self);

        let transaction = match this.transaction.as_mut() {
            Some(transaction) => transaction,
            None => return task::Poll::Ready(Err(nvfs_err_internal!())),
        };

        let auth_tree_data_block_allocation_blocks_log2 = fs_instance_sync_state
            .get_fs_ref()
            .fs_config
            .image_layout
            .auth_tree_data_block_allocation_blocks_log2
            as u32;
        let auth_tree_data_block_allocation_blocks =
            layout::AllocBlockCount::from(1u64 << auth_tree_data_block_allocation_blocks_log2);

        loop {
            if let Some(redigest_auth_tree_data_block_fut) = this.redigest_auth_tree_data_block_fut.as_mut() {
                let auth_digest = match CocoonFsSyncStateReadFuture::poll(
                    pin::Pin::new(redigest_auth_tree_data_block_fut),
                    fs_instance_sync_state,
                    &mut (),
                    cx,
                ) {
                    task::Poll::Ready(Ok(auth_digest)) => auth_digest,
                    task::Poll::Ready(Err(e)) => return task::Poll::Ready(Err(e)),
                    task::Poll::Pending => return task::Poll::Pending,
                };
                this.redigest_auth_tree_data_block_fut = None;

                let cur_auth_tree_data_block_allocation_blocks_begin =
                    this.next_auth_tree_data_block_allocation_blocks_begin;
                this.next_auth_tree_data_block_allocation_blocks_begin += auth_tree_data_block_allocation_blocks;
                this.next_pending_free = transaction
                    .allocs
                    .pending_frees
                    .block_iter_at(
                        this.next_auth_tree_data_block_allocation_blocks_begin,
                        auth_tree_data_block_allocation_blocks_log2,
                    )
                    .next();

                return task::Poll::Ready(Ok(Some(auth_tree::PhysicalAuthTreeDataBlockUpdate {
                    data_block_allocation_blocks_begin: cur_auth_tree_data_block_allocation_blocks_begin,
                    data_block_digest: auth_digest,
                })));
            }

            let fs_instance = fs_instance_sync_state.get_fs_ref();
            let fs_config = &fs_instance.fs_config;
            let update_states = &mut transaction.auth_tree_data_blocks_update_states;
            if let Some(next_pending_free) = this.next_pending_free.take_if(|next_pending_free| {
                usize::from(this.next_update_states_index) == update_states.len()
                    || update_states[this.next_update_states_index].get_target_allocation_blocks_begin()
                        > next_pending_free.0
            }) {
                this.next_auth_tree_data_block_allocation_blocks_begin = next_pending_free.0;
                // Compute the updated Authentication Tree Data Block's allocation bitmap. To
                // save some lookups in the transaction's pending_allocs and
                // pending_frees bitmaps, it is assumed that the two had been
                // normalized and that all entries from pending_allocs have data
                // modifications pending to them as well, meaning there are none in the
                // current Authentication Tree Data Block.
                let fs_sync_state_alloc_bitmap = &fs_instance_sync_state.alloc_bitmap;
                let empty_sparse_alloc_bitmap = alloc_bitmap::SparseAllocBitmapUnion::new(&[]);
                let original_auth_tree_data_block_alloc_bitmap = match fs_sync_state_alloc_bitmap
                    .iter_chunked_at_allocation_block(
                        &empty_sparse_alloc_bitmap,
                        &empty_sparse_alloc_bitmap,
                        this.next_auth_tree_data_block_allocation_blocks_begin,
                        u64::from(auth_tree_data_block_allocation_blocks) as u32,
                    )
                    .next()
                {
                    Some(original_auth_tree_data_block_alloc_bitmap) => original_auth_tree_data_block_alloc_bitmap,
                    None => return task::Poll::Ready(Err(nvfs_err_internal!())),
                };
                // It is assumed that the transaction's pending_frees is normalized, i.e. there
                // are no frees for unallocated Allocation Blocks.
                debug_assert!(next_pending_free.1 & original_auth_tree_data_block_alloc_bitmap == next_pending_free.1);
                let updated_auth_tree_data_block_alloc_bitmap =
                    original_auth_tree_data_block_alloc_bitmap & !next_pending_free.1;
                let redigest_auth_tree_data_block_fut = match RedigestAuthTreeDataBlockFuture::new(
                    fs_config,
                    this.next_auth_tree_data_block_allocation_blocks_begin,
                    updated_auth_tree_data_block_alloc_bitmap,
                ) {
                    Ok(redigest_auth_tree_data_block_fut) => redigest_auth_tree_data_block_fut,
                    Err(e) => return task::Poll::Ready(Err(e)),
                };
                this.redigest_auth_tree_data_block_fut = Some(redigest_auth_tree_data_block_fut);
            } else if usize::from(this.next_update_states_index) < update_states.len() {
                let update_state = &mut update_states[this.next_update_states_index];
                let cur_auth_tree_data_block_allocation_blocks_begin =
                    update_state.get_target_allocation_blocks_begin();
                let auth_digest = match update_state.steal_auth_digest() {
                    Some(auth_digest) => auth_digest,
                    None => return task::Poll::Ready(Err(nvfs_err_internal!())),
                };
                this.next_auth_tree_data_block_allocation_blocks_begin =
                    cur_auth_tree_data_block_allocation_blocks_begin + auth_tree_data_block_allocation_blocks;
                this.next_update_states_index = Self::skip_to_auth_tree_update_state_with_modified_data(
                    update_states,
                    this.next_update_states_index.step(),
                    fs_config.image_header_end,
                    auth_tree_data_block_allocation_blocks,
                );
                if this
                    .next_pending_free
                    .as_ref()
                    .map(|next_pending_free| next_pending_free.0 == cur_auth_tree_data_block_allocation_blocks_begin)
                    .unwrap_or(false)
                {
                    this.next_pending_free = transaction
                        .allocs
                        .pending_frees
                        .block_iter_at(
                            this.next_auth_tree_data_block_allocation_blocks_begin,
                            auth_tree_data_block_allocation_blocks_log2,
                        )
                        .next();
                }
                return task::Poll::Ready(Ok(Some(auth_tree::PhysicalAuthTreeDataBlockUpdate {
                    data_block_allocation_blocks_begin: cur_auth_tree_data_block_allocation_blocks_begin,
                    data_block_digest: auth_digest,
                })));
            } else {
                return task::Poll::Ready(Ok(None));
            }
        }
    }

    fn return_digests_on_error(
        &mut self,
        fs_config: &CocoonFsConfig,
        returned_updates: auth_tree::AuthTreePendingNodesUpdatesIntoDataUpdatesIter,
    ) -> Result<(), NvFsError> {
        let transaction = self.transaction.as_mut().ok_or_else(|| nvfs_err_internal!())?;
        transaction.restore_update_states_auth_digests(
            returned_updates,
            fs_config.image_layout.auth_tree_data_block_allocation_blocks_log2,
            fs_config.image_header_end,
        )
    }

    fn return_digest_on_error(
        &mut self,
        fs_config: &CocoonFsConfig,
        returned_update: auth_tree::PhysicalAuthTreeDataBlockUpdate,
    ) -> Result<(), NvFsError> {
        let transaction = self.transaction.as_mut().ok_or_else(|| nvfs_err_internal!())?;

        let auth_tree_data_block_allocation_blocks_log2 =
            fs_config.image_layout.auth_tree_data_block_allocation_blocks_log2 as u32;
        let auth_tree_data_block_allocation_blocks =
            layout::AllocBlockCount::from(1u64 << auth_tree_data_block_allocation_blocks_log2);
        let image_header_end = fs_config.image_header_end;

        let update_states = &mut transaction.auth_tree_data_blocks_update_states;
        let update_states_index = match update_states
            .lookup_auth_tree_data_block_update_state_index(returned_update.data_block_allocation_blocks_begin)
        {
            Ok(update_states_index) => update_states_index,
            Err(_) => return Ok(()),
        };
        let update_state = &mut update_states[update_states_index];

        // Don't store returned digests for Authentication Tree Data Blocks with no data
        // modifications. Getting such one back means there had been a redigest because
        // of changes in the allocation bitmap.
        if !update_state
            .iter_allocation_blocks()
            .skip(
                u64::from(image_header_end)
                    .saturating_sub(u64::from(update_state.get_target_allocation_blocks_begin()))
                    .min(u64::from(auth_tree_data_block_allocation_blocks)) as usize,
            )
            .any(|allocation_block_update_state| allocation_block_update_state.has_modified_data())
        {
            return Ok(());
        }

        if update_state.get_auth_digest().is_some() {
            return Err(nvfs_err_internal!());
        }
        update_state.set_auth_digest(returned_update.data_block_digest);

        Ok(())
    }
}

impl Transaction {
    /// Restore the [`Transaction`]'s
    /// [`AuthTreeDataBlockUpdateState::auth_digest`]s from an
    /// [`AuthTreePendingNodesUpdatesIntoDataUpdatesIter`](auth_tree::AuthTreePendingNodesUpdatesIntoDataUpdatesIter).
    ///
    /// Invoked by [`TransactionAuthTreeDataBlocksDigestsUpdatesIterator`] to
    /// restore the [`Transaction`]'s
    /// [`AuthTreeDataBlockUpdateState::auth_digest`]s on error.
    ///
    /// # Arguments:
    ///
    /// * `returned_updates` -
    ///   [`AuthTreePendingNodesUpdatesIntoDataUpdatesIter`](auth_tree::AuthTreePendingNodesUpdatesIntoDataUpdatesIter`)
    ///   over the returned digests.
    /// * `auth_tree_data_block_allocation_blocks_log2` - Verbatim value of
    ///   [`ImageLayout::auth_tree_data_block_allocation_blocks_log2`].
    /// * `image_header_end` - [End of the filesystem image header on
    ///   storage](crate::fs::cocoonfs::image_header::MutableImageHeader::physical_location).
    pub(super) fn restore_update_states_auth_digests(
        &mut self,
        returned_updates: auth_tree::AuthTreePendingNodesUpdatesIntoDataUpdatesIter,
        auth_tree_data_block_allocation_blocks_log2: u8,
        image_header_end: layout::PhysicalAllocBlockIndex,
    ) -> Result<(), NvFsError> {
        let auth_tree_data_block_allocation_blocks =
            layout::AllocBlockCount::from(1u64 << (auth_tree_data_block_allocation_blocks_log2 as u32));

        let update_states = &mut self.auth_tree_data_blocks_update_states;
        let mut update_states_index = AuthTreeDataBlocksUpdateStatesIndex::from(0usize);
        for returned_update in returned_updates {
            let returned_update = returned_update?;
            // Skip over all update states for (unmodified) Authentication Tree Data Blocks
            // before the returned update's associated position.
            let update_state = loop {
                if usize::from(update_states_index) == update_states.len() {
                    return Ok(());
                }

                let update_state = &mut update_states[update_states_index];
                if update_state.get_target_allocation_blocks_begin()
                    >= returned_update.data_block_allocation_blocks_begin
                {
                    break update_state;
                }

                // The current update state doesn't receive a digest back. It should not have
                // any data modifications.
                if update_state
                    .iter_allocation_blocks()
                    .skip(
                        u64::from(image_header_end)
                            .saturating_sub(u64::from(update_state.get_target_allocation_blocks_begin()))
                            .min(u64::from(auth_tree_data_block_allocation_blocks)) as usize,
                    )
                    .any(|allocation_block_update_state| allocation_block_update_state.has_modified_data())
                {
                    return Err(nvfs_err_internal!());
                }
                update_states_index = update_states_index.step();
            };
            if update_state.get_target_allocation_blocks_begin() != returned_update.data_block_allocation_blocks_begin {
                continue;
            }

            update_states_index = update_states_index.step();

            // Don't store returned digests for Authentication Tree Data Blocks with no data
            // modifications. Getting such one back means there had been a redigest because
            // of changes in the allocation bitmap.
            if !update_state
                .iter_allocation_blocks()
                .skip(
                    u64::from(image_header_end)
                        .saturating_sub(u64::from(update_state.get_target_allocation_blocks_begin()))
                        .min(u64::from(auth_tree_data_block_allocation_blocks)) as usize,
                )
                .any(|allocation_block_update_state| allocation_block_update_state.has_modified_data())
            {
                continue;
            }

            if update_state.get_auth_digest().is_some() {
                return Err(nvfs_err_internal!());
            }
            update_state.set_auth_digest(returned_update.data_block_digest);
        }

        Ok(())
    }
}

/// Implementation of
/// [`JournalUpdateAuthDigestsScriptIterator`](journal::apply_script::JournalUpdateAuthDigestsScriptIterator)
/// over a [`Transaction`].
///
/// Used for [encoding](journal::apply_script::JournalUpdateAuthDigestsScript::encode) the journal
/// log's [`UpdateAuthDigestsScript`
/// field](journal::log::JournalLogFieldTag::UpdateAuthDigestsScript) at
/// [`Transaction`] commit.
#[derive(Clone)]
pub struct TransactionJournalUpdateAuthDigestsScriptIterator<'a> {
    transaction_update_states: &'a AuthTreeDataBlocksUpdateStates,
    next_update_states_index: AuthTreeDataBlocksUpdateStatesIndex,
    image_header_end: layout::PhysicalAllocBlockIndex,
    transaction_pending_frees_iter: alloc_bitmap::SparseAllocBitmapBlockIterator<'a>,
    next_pending_free: Option<layout::PhysicalAllocBlockIndex>,
    auth_tree_data_block_allocation_blocks_log2: u8,
}

impl<'a> TransactionJournalUpdateAuthDigestsScriptIterator<'a> {
    /// Instantiate a [`TransactionJournalUpdateAuthDigestsScriptIterator`].
    ///
    /// # Arguments:
    ///
    /// * `transaction_update_states` - `mut` reference to
    ///   [`Transaction::auth_tree_data_blocks_update_states`].
    /// * `transaction_pending_frees` - Reference to the
    ///   [`Transaction::allocs`]'
    ///   [`TransactionAllocations::pending_frees`](super::TransactionAllocations::pending_frees).
    /// * `image_header_end` - [End of the filesystem image header on
    ///   storage](crate::fs::cocoonfs::image_header::MutableImageHeader::physical_location).
    /// * `auth_tree_data_block_allocation_blocks_log2` - Verbatim value of
    ///   [`ImageLayout::auth_tree_data_block_allocation_blocks_log2`].
    pub fn new(
        transaction_update_states: &'a AuthTreeDataBlocksUpdateStates,
        transaction_pending_frees: &'a alloc_bitmap::SparseAllocBitmap,
        image_header_end: layout::PhysicalAllocBlockIndex,
        auth_tree_data_block_allocation_blocks_log2: u8,
    ) -> Self {
        let mut transaction_pending_frees_iter =
            transaction_pending_frees.block_iter(auth_tree_data_block_allocation_blocks_log2 as u32);
        let next_pending_free = transaction_pending_frees_iter
            .next()
            .map(|next_pending_free| next_pending_free.0);

        Self {
            transaction_update_states,
            next_update_states_index: AuthTreeDataBlocksUpdateStatesIndex::from(0usize),
            image_header_end,
            transaction_pending_frees_iter,
            next_pending_free,
            auth_tree_data_block_allocation_blocks_log2,
        }
    }
}

impl<'a> journal::apply_script::JournalUpdateAuthDigestsScriptIterator
    for TransactionJournalUpdateAuthDigestsScriptIterator<'a>
{
    fn next(&mut self) -> Result<Option<journal::apply_script::JournalUpdateAuthDigestsScriptEntry>, NvFsError> {
        let update_states = self.transaction_update_states;

        let auth_tree_data_block_allocation_blocks =
            layout::AllocBlockCount::from(1u64 << (self.auth_tree_data_block_allocation_blocks_log2 as u32));

        // Skip over any Authentication Tree Data blocks at the current position whose
        // data is not modified. Note that because it is being assumed that
        // unmodified states had been pruned at this point, this is possible
        // only of the IO Block size is larger than that of an Authentication
        // Tree Data Block.
        while usize::from(self.next_update_states_index) != update_states.len() {
            let cur_auth_tree_data_block_update_state = &update_states[self.next_update_states_index];
            // The Allocation Blocks from the image header are not included in an digest
            // content-wise.
            let cur_auth_tree_data_block_allocation_blocks_begin =
                cur_auth_tree_data_block_update_state.get_target_allocation_blocks_begin();
            let skip_count = if self.image_header_end <= cur_auth_tree_data_block_allocation_blocks_begin {
                0usize
            } else {
                // The number of Allocation Blocks in an Authentication Tree Data Block fits an
                // usize.
                usize::try_from(u64::from(
                    self.image_header_end - cur_auth_tree_data_block_allocation_blocks_begin,
                ))
                .unwrap_or(usize::MAX)
            };
            let cur_auth_tree_data_block_any_modified = cur_auth_tree_data_block_update_state
                .iter_allocation_blocks()
                .skip(skip_count)
                .any(|allocation_block_update_state| allocation_block_update_state.has_modified_data());
            if cur_auth_tree_data_block_any_modified {
                break;
            } else {
                self.next_update_states_index = self.next_update_states_index.step();
            }
        }

        let region_allocation_blocks_begin = match self.next_pending_free.as_ref().copied() {
            Some(next_auth_tree_data_block_with_pending_free_allocation_blocks_begin) => {
                if usize::from(self.next_update_states_index) != update_states.len() {
                    let next_auth_tree_data_block_with_modified_data_allocation_blocks_begin =
                        update_states[self.next_update_states_index].get_target_allocation_blocks_begin();
                    if next_auth_tree_data_block_with_pending_free_allocation_blocks_begin
                        <= next_auth_tree_data_block_with_modified_data_allocation_blocks_begin
                    {
                        self.next_pending_free = self
                            .transaction_pending_frees_iter
                            .next()
                            .map(|next_pending_free| next_pending_free.0);
                        if next_auth_tree_data_block_with_pending_free_allocation_blocks_begin
                            == next_auth_tree_data_block_with_modified_data_allocation_blocks_begin
                        {
                            self.next_update_states_index = self.next_update_states_index.step();
                        }
                        next_auth_tree_data_block_with_pending_free_allocation_blocks_begin
                    } else {
                        self.next_update_states_index = self.next_update_states_index.step();
                        next_auth_tree_data_block_with_modified_data_allocation_blocks_begin
                    }
                } else {
                    self.next_pending_free = self
                        .transaction_pending_frees_iter
                        .next()
                        .map(|next_pending_free| next_pending_free.0);
                    next_auth_tree_data_block_with_pending_free_allocation_blocks_begin
                }
            }
            None => {
                if usize::from(self.next_update_states_index) == update_states.len() {
                    return Ok(None);
                }
                let next_auth_tree_data_block_with_modified_data_allocation_blocks_begin =
                    update_states[self.next_update_states_index].get_target_allocation_blocks_begin();
                self.next_update_states_index = self.next_update_states_index.step();
                next_auth_tree_data_block_with_modified_data_allocation_blocks_begin
            }
        };

        let mut region_allocation_blocks = auth_tree_data_block_allocation_blocks;
        let mut last_auth_tree_data_block_allocation_blocks_begin = region_allocation_blocks_begin;
        while usize::from(self.next_update_states_index) < update_states.len() || self.next_pending_free.is_some() {
            let mut cur_auth_tree_data_block_needs_redigest = false;

            if usize::from(self.next_update_states_index) < update_states.len() {
                let cur_auth_tree_data_block_update_state = &update_states[self.next_update_states_index];
                debug_assert!(
                    cur_auth_tree_data_block_update_state.get_target_allocation_blocks_begin()
                        > last_auth_tree_data_block_allocation_blocks_begin
                );
                if cur_auth_tree_data_block_update_state.get_target_allocation_blocks_begin()
                    - last_auth_tree_data_block_allocation_blocks_begin
                    == auth_tree_data_block_allocation_blocks
                {
                    self.next_update_states_index = self.next_update_states_index.step();
                    let cur_auth_tree_data_block_any_modified = cur_auth_tree_data_block_update_state
                        .iter_allocation_blocks()
                        .any(|allocation_block_update_state| allocation_block_update_state.has_modified_data());
                    cur_auth_tree_data_block_needs_redigest |= cur_auth_tree_data_block_any_modified;
                }
            }

            if let Some(next_auth_tree_data_block_with_pending_free_allocation_blocks_begin) =
                self.next_pending_free.as_ref().copied()
            {
                debug_assert!(
                    next_auth_tree_data_block_with_pending_free_allocation_blocks_begin
                        > last_auth_tree_data_block_allocation_blocks_begin
                );
                if next_auth_tree_data_block_with_pending_free_allocation_blocks_begin
                    - last_auth_tree_data_block_allocation_blocks_begin
                    == auth_tree_data_block_allocation_blocks
                {
                    self.next_pending_free = self
                        .transaction_pending_frees_iter
                        .next()
                        .map(|next_pending_free| next_pending_free.0);
                    cur_auth_tree_data_block_needs_redigest = true;
                }
            }

            if cur_auth_tree_data_block_needs_redigest {
                region_allocation_blocks = region_allocation_blocks + auth_tree_data_block_allocation_blocks;
                last_auth_tree_data_block_allocation_blocks_begin += auth_tree_data_block_allocation_blocks;
            } else {
                break;
            }
        }

        let target_range =
            layout::PhysicalAllocBlockRange::from((region_allocation_blocks_begin, region_allocation_blocks));
        Ok(Some(journal::apply_script::JournalUpdateAuthDigestsScriptEntry::new(
            &target_range,
        )))
    }
}