// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";
package android.snapshot;

option optimize_for = LITE_RUNTIME;

// Next: 4
enum SnapshotState {
    // No snapshot is found.
    NONE = 0;

    // The snapshot has been created and possibly written to. Rollbacks are
    // possible by destroying the snapshot.
    CREATED = 1;

    // Changes are being merged. No rollbacks are possible beyond this point.
    MERGING = 2;

    // Changes have been merged, Future reboots may map the base device
    // directly.
    MERGE_COMPLETED = 3;
}

// Next: 3
enum MergePhase {
    // No merge is in progress.
    NO_MERGE = 0;

    // Shrunk partitions can merge.
    FIRST_PHASE = 1;

    // Grown partitions can merge.
    SECOND_PHASE = 2;
}

// Next: 13
message SnapshotStatus {
    // Name of the snapshot. This is usually the name of the snapshotted
    // logical partition; for example, "system_b".
    string name = 1;

    SnapshotState state = 2;

    // Size of the full (base) device.
    uint64 device_size = 3;

    // Size of the snapshot. This is the sum of lengths of ranges in the base
    // device that needs to be snapshotted during the update.
    // This must be less than or equal to |device_size|.
    // This value is 0 if no snapshot is needed for this device because
    // no changes
    uint64 snapshot_size = 4;

    // Size of the "COW partition". A COW partition is a special logical
    // partition represented in the super partition metadata. This partition and
    // the "COW image" form the "COW device" that supports the snapshot device.
    //
    // When SnapshotManager creates a COW device, it first searches for unused
    // blocks in the super partition, and use those before creating the COW
    // image if the COW partition is not big enough.
    //
    // This value is 0 if no space in super is left for the COW partition.
    // |cow_partition_size + cow_file_size| must not be zero if |snapshot_size|
    // is non-zero.
    uint64 cow_partition_size = 5;

    // Size of the "COW file", or "COW image". A COW file / image is created
    // when the "COW partition" is not big enough to store changes to the
    // snapshot device.
    //
    // This value is 0 if |cow_partition_size| is big enough to hold all changes
    // to the snapshot device.
    uint64 cow_file_size = 6;

    // Sectors allocated for the COW device. Recording this value right after
    // the update and before the merge allows us to infer the progress of the
    // merge process.
    // This is non-zero when |state| == MERGING or MERGE_COMPLETED.
    uint64 sectors_allocated = 7;

    // Metadata sectors allocated for the COW device. Recording this value right
    // before the update and before the merge allows us to infer the progress of
    // the merge process.
    // This is non-zero when |state| == MERGING or MERGE_COMPLETED.
    uint64 metadata_sectors = 8;

    // True if using snapuserd, false otherwise.
    bool using_snapuserd = 9;

    // The old partition size (if none existed, this will be zero).
    uint64 old_partition_size = 10;

    // Compression algorithm (none, gz, or brotli).
    string compression_algorithm = 11;

    // Estimated COW size from OTA manifest.
    uint64 estimated_cow_size = 12;

    // Enable multi-threaded compression
    bool enable_threading = 13;

    // Enable batching for COW writes
    bool batched_writes = 14;
}

// Next: 8
enum UpdateState {
    // No update or merge is in progress.
    None = 0;

    // An update is applying; snapshots may already exist.
    Initiated = 1;

    // An update is pending, but has not been successfully booted yet.
    Unverified = 2;

    // The kernel is merging in the background.
    Merging = 3;

    // Post-merge cleanup steps could not be completed due to a transient
    // error, but the next reboot will finish any pending operations.
    MergeNeedsReboot = 4;

    // Merging is complete, and needs to be acknowledged.
    MergeCompleted = 5;

    // Merging failed due to an unrecoverable error.
    MergeFailed = 6;

    // The update was implicitly cancelled, either by a rollback or a flash
    // operation via fastboot. This state can only be returned by WaitForMerge.
    Cancelled = 7;
};

// Next 14:
//
// To understand the source of each failure, read snapshot.cpp. To handle new
// sources of failure, avoid reusing an existing code; add a new code instead.
enum MergeFailureCode {
    Ok = 0;
    ReadStatus = 1;
    GetTableInfo = 2;
    UnknownTable = 3;
    GetTableParams = 4;
    ActivateNewTable = 5;
    AcquireLock = 6;
    ListSnapshots = 7;
    WriteStatus = 8;
    UnknownTargetType = 9;
    QuerySnapshotStatus = 10;
    ExpectedMergeTarget = 11;
    UnmergedSectorsAfterCompletion = 12;
    UnexpectedMergeState = 13;
    GetCowPathConsistencyCheck = 14;
    OpenCowConsistencyCheck = 15;
    ParseCowConsistencyCheck = 16;
    OpenCowDirectConsistencyCheck = 17;
    MemAlignConsistencyCheck = 18;
    DirectReadConsistencyCheck = 19;
    WrongMergeCountConsistencyCheck = 20;
};

// Next: 8
message SnapshotUpdateStatus {
    UpdateState state = 1;

    // Total number of sectors allocated in the COW files before performing the
    // merge operation.  This field is used to keep track of the total number
    // of sectors modified to monitor and show the progress of the merge during
    // an update.
    uint64 sectors_allocated = 2;

    // Total number of sectors of all the snapshot devices.
    uint64 total_sectors = 3;

    // Sectors allocated for metadata in all the snapshot devices.
    uint64 metadata_sectors = 4;

    // Whether compression/dm-user was used for any snapshots.
    bool using_snapuserd = 5;

    // Merge phase (if state == MERGING).
    MergePhase merge_phase = 6;

    // Merge failure code, filled if state == MergeFailed.
    MergeFailureCode merge_failure_code = 7;

    // Source build fingerprint.
    string source_build_fingerprint = 8;

    // user-space snapshots
    bool userspace_snapshots = 9;

    // io_uring support
    bool io_uring_enabled = 10;
}

// Next: 10
message SnapshotMergeReport {
    // Status of the update after the merge attempts.
    UpdateState state = 1;

    // Number of reboots that occurred after issuing and before completeing the
    // merge of all the snapshot devices.
    int32 resume_count = 2;

    // Total size of all the COW images before the update.
    uint64 cow_file_size = 3;

    // Whether compression/dm-user was used for any snapshots.
    bool compression_enabled = 4;

    // Total size used by COWs, including /data and the super partition.
    uint64 total_cow_size_bytes = 5;

    // Sum of the estimated COW fields in the OTA manifest.
    uint64 estimated_cow_size_bytes = 6;

    // Time from boot to sys.boot_completed, in milliseconds.
    uint32 boot_complete_time_ms = 7;

    // Time from sys.boot_completed to merge start, in milliseconds.
    uint32 boot_complete_to_merge_start_time_ms = 8;

    // Merge failure code, filled if the merge failed at any time (regardless
    // of whether it succeeded at a later time).
    MergeFailureCode merge_failure_code = 9;

    // The source fingerprint at the time the OTA was downloaded.
    string source_build_fingerprint = 10;

    // Whether this update attempt uses userspace snapshots.
    bool userspace_snapshots_used = 11;

    // Whether this update attempt uses XOR compression.
    bool xor_compression_used = 12;

    // Whether this update attempt used io_uring.
    bool iouring_used = 13;
}
