Skip to content

Commit 3aab337

Browse files
dvandercorpGerrit Code Review
authored andcommitted
Merge "libsnapshot: Use the compression algorithm specified in the DeltaArchiveManifest."
2 parents e886702 + cbc204b commit 3aab337

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

fs_mgr/libsnapshot/android/snapshot/snapshot.proto

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ enum MergePhase {
4646
SECOND_PHASE = 2;
4747
}
4848

49-
// Next: 11
49+
// Next: 12
5050
message SnapshotStatus {
5151
// Name of the snapshot. This is usually the name of the snapshotted
5252
// logical partition; for example, "system_b".
@@ -102,6 +102,9 @@ message SnapshotStatus {
102102

103103
// The old partition size (if none existed, this will be zero).
104104
uint64 old_partition_size = 10;
105+
106+
// Compression algorithm (none, gz, or brotli).
107+
string compression_algorithm = 11;
105108
}
106109

107110
// Next: 8

fs_mgr/libsnapshot/partition_cow_creator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct PartitionCowCreator {
5858
std::vector<ChromeOSExtent> extra_extents = {};
5959
// True if compression is enabled.
6060
bool compression_enabled = false;
61+
std::string compression_algorithm;
6162

6263
struct Return {
6364
SnapshotStatus snapshot_status;

fs_mgr/libsnapshot/snapshot.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ bool SnapshotManager::CreateSnapshot(LockedFile* lock, PartitionCowCreator* cow_
359359
status->set_sectors_allocated(0);
360360
status->set_metadata_sectors(0);
361361
status->set_compression_enabled(cow_creator->compression_enabled);
362+
status->set_compression_algorithm(cow_creator->compression_algorithm);
362363

363364
if (!WriteSnapshotStatus(lock, *status)) {
364365
PLOG(ERROR) << "Could not write snapshot status: " << status->name();
@@ -2660,9 +2661,20 @@ Return SnapshotManager::CreateUpdateSnapshots(const DeltaArchiveManifest& manife
26602661
// these devices.
26612662
AutoDeviceList created_devices;
26622663

2663-
bool use_compression = IsCompressionEnabled() &&
2664-
manifest.dynamic_partition_metadata().vabc_enabled() &&
2665-
!device_->IsRecovery();
2664+
const auto& dap_metadata = manifest.dynamic_partition_metadata();
2665+
bool use_compression =
2666+
IsCompressionEnabled() && dap_metadata.vabc_enabled() && !device_->IsRecovery();
2667+
2668+
std::string compression_algorithm;
2669+
if (use_compression) {
2670+
compression_algorithm = dap_metadata.vabc_compression_param();
2671+
if (compression_algorithm.empty()) {
2672+
// Older OTAs don't set an explicit compression type, so default to gz.
2673+
compression_algorithm = "gz";
2674+
}
2675+
} else {
2676+
compression_algorithm = "none";
2677+
}
26662678

26672679
PartitionCowCreator cow_creator{
26682680
.target_metadata = target_metadata.get(),
@@ -2673,6 +2685,7 @@ Return SnapshotManager::CreateUpdateSnapshots(const DeltaArchiveManifest& manife
26732685
.update = nullptr,
26742686
.extra_extents = {},
26752687
.compression_enabled = use_compression,
2688+
.compression_algorithm = compression_algorithm,
26762689
};
26772690

26782691
auto ret = CreateUpdateSnapshotsInternal(lock.get(), manifest, &cow_creator, &created_devices,
@@ -2917,7 +2930,7 @@ Return SnapshotManager::InitializeUpdateSnapshots(
29172930
return Return::Error();
29182931
}
29192932

2920-
CowWriter writer(CowOptions{});
2933+
CowWriter writer(CowOptions{.compression = it->second.compression_algorithm()});
29212934
if (!writer.Initialize(fd) || !writer.Finalize()) {
29222935
LOG(ERROR) << "Could not initialize COW device for " << target_partition->name();
29232936
return Return::Error();
@@ -3024,7 +3037,7 @@ std::unique_ptr<ISnapshotWriter> SnapshotManager::OpenCompressedSnapshotWriter(
30243037
CHECK(lock);
30253038

30263039
CowOptions cow_options;
3027-
cow_options.compression = "gz";
3040+
cow_options.compression = status.compression_algorithm();
30283041
cow_options.max_blocks = {status.device_size() / cow_options.block_size};
30293042

30303043
// Currently we don't support partial snapshots, since partition_cow_creator
@@ -3163,6 +3176,7 @@ bool SnapshotManager::Dump(std::ostream& os) {
31633176
ss << " cow file size (bytes): " << status.cow_file_size() << std::endl;
31643177
ss << " allocated sectors: " << status.sectors_allocated() << std::endl;
31653178
ss << " metadata sectors: " << status.metadata_sectors() << std::endl;
3179+
ss << " compression: " << status.compression_algorithm() << std::endl;
31663180
}
31673181
os << ss.rdbuf();
31683182
return ok;

fs_mgr/libsnapshot/snapshot_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ TEST_F(SnapshotTest, CreateSnapshot) {
423423

424424
PartitionCowCreator cow_creator;
425425
cow_creator.compression_enabled = IsCompressionEnabled();
426+
if (cow_creator.compression_enabled) {
427+
cow_creator.compression_algorithm = "gz";
428+
} else {
429+
cow_creator.compression_algorithm = "none";
430+
}
426431

427432
static const uint64_t kDeviceSize = 1024 * 1024;
428433
SnapshotStatus status;
@@ -446,6 +451,7 @@ TEST_F(SnapshotTest, CreateSnapshot) {
446451
ASSERT_EQ(status.device_size(), kDeviceSize);
447452
ASSERT_EQ(status.snapshot_size(), kDeviceSize);
448453
ASSERT_EQ(status.compression_enabled(), cow_creator.compression_enabled);
454+
ASSERT_EQ(status.compression_algorithm(), cow_creator.compression_algorithm);
449455
}
450456

451457
ASSERT_TRUE(sm->UnmapSnapshot(lock_.get(), "test-snapshot"));
@@ -576,6 +582,11 @@ TEST_F(SnapshotTest, FirstStageMountAndMerge) {
576582
SnapshotStatus status;
577583
ASSERT_TRUE(init->ReadSnapshotStatus(lock_.get(), "test_partition_b", &status));
578584
ASSERT_EQ(status.state(), SnapshotState::CREATED);
585+
if (IsCompressionEnabled()) {
586+
ASSERT_EQ(status.compression_algorithm(), "gz");
587+
} else {
588+
ASSERT_EQ(status.compression_algorithm(), "none");
589+
}
579590

580591
DeviceMapper::TargetInfo target;
581592
ASSERT_TRUE(init->IsSnapshotDevice("test_partition_b", &target));

fs_mgr/libsnapshot/update_engine/update_metadata.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ message DynamicPartitionGroup {
7474
message DynamicPartitionMetadata {
7575
repeated DynamicPartitionGroup groups = 1;
7676
optional bool vabc_enabled = 3;
77+
optional string vabc_compression_param = 4;
7778
}
7879

7980
message DeltaArchiveManifest {

0 commit comments

Comments
 (0)