Skip to content

Commit

Permalink
Merge "Revert "Shove type into source_info"" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelvin Zhang authored and Gerrit Code Review committed Oct 11, 2023
2 parents da3caea + b6153ad commit d6fe303
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 135 deletions.
25 changes: 7 additions & 18 deletions fs_mgr/libsnapshot/include/libsnapshot/cow_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,16 @@ struct CowOperationV2 {

// The on disk format of cow (currently == CowOperation)
struct CowOperationV3 {
// The operation code (see the constants and structures below).
uint8_t type;

// If this operation reads from the data section of the COW, this contains
// the length.
uint16_t data_length;

// The block of data in the new image that this operation modifies.
uint32_t new_block;

// source_info with have the following layout
// |---4 bits ---| ---12 bits---| --- 48 bits ---|
// |--- type --- | -- unused -- | --- source --- |
//
// The value of |source| depends on the operation code.
//
// CopyOp: a 32-bit block location in the source image.
Expand All @@ -180,6 +179,8 @@ struct CowOperationV3 {
// ZeroOp: unused
// LabelOp: a 64-bit opaque identifier.
//
// For ops other than Label:
// Bits 47-62 are reserved and must be zero.
// A block is compressed if it’s data is < block_sz
uint64_t source_info;
} __attribute__((packed));
Expand Down Expand Up @@ -211,21 +212,9 @@ static constexpr uint8_t kCowReadAheadNotStarted = 0;
static constexpr uint8_t kCowReadAheadInProgress = 1;
static constexpr uint8_t kCowReadAheadDone = 2;

// this is a mask to grab the last 48 bits of a 64 bit integer
static constexpr uint64_t kCowOpSourceInfoDataMask = (0x1ULL << 48) - 1;
// this is a mask to grab the first 4 bits of a 64 bit integer
static constexpr uint64_t kCowOpSourceInfoTypeBit = 60;
static constexpr uint64_t kCowOpSourceInfoTypeNumBits = 4;
static constexpr uint64_t kCowOpSourceInfoTypeMask = (1 << kCowOpSourceInfoTypeNumBits) - 1;

static constexpr void SetCowOpSourceInfoType(CowOperation* op, const uint8_t type) {
op->source_info |= uint64_t(type) << kCowOpSourceInfoTypeBit;
}
static constexpr uint64_t GetCowOpSourceInfoType(const CowOperation& op) {
return (op.source_info >> kCowOpSourceInfoTypeBit) & kCowOpSourceInfoTypeMask;
}
static constexpr uint64_t kCowOpSourceInfoDataMask = (1ULL << 48) - 1;

static constexpr uint64_t GetCowOpSourceInfoData(const CowOperation& op) {
static inline uint64_t GetCowOpSourceInfoData(const CowOperation& op) {
return op.source_info & kCowOpSourceInfoDataMask;
}

Expand Down
17 changes: 7 additions & 10 deletions fs_mgr/libsnapshot/libsnapshot_cow/cow_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,16 @@ std::ostream& operator<<(std::ostream& os, CowOperationV2 const& op) {

std::ostream& operator<<(std::ostream& os, CowOperation const& op) {
os << "CowOperation(";
EmitCowTypeString(os, GetCowOpSourceInfoType(op));
if (GetCowOpSourceInfoType(op) == kCowReplaceOp || GetCowOpSourceInfoType(op) == kCowXorOp ||
GetCowOpSourceInfoType(op) == kCowSequenceOp) {
EmitCowTypeString(os, op.type);
if (op.type == kCowReplaceOp || op.type == kCowXorOp || op.type == kCowSequenceOp) {
os << ", data_length:" << op.data_length;
}
if (GetCowOpSourceInfoType(op) != kCowClusterOp &&
GetCowOpSourceInfoType(op) != kCowSequenceOp && GetCowOpSourceInfoType(op) != kCowLabelOp) {
if (op.type != kCowClusterOp && op.type != kCowSequenceOp && op.type != kCowLabelOp) {
os << ", new_block:" << op.new_block;
}
if (GetCowOpSourceInfoType(op) == kCowXorOp || GetCowOpSourceInfoType(op) == kCowReplaceOp ||
GetCowOpSourceInfoType(op) == kCowCopyOp) {
if (op.type == kCowXorOp || op.type == kCowReplaceOp || op.type == kCowCopyOp) {
os << ", source:" << (op.source_info & kCowOpSourceInfoDataMask);
} else if (GetCowOpSourceInfoType(op) == kCowClusterOp) {
} else if (op.type == kCowClusterOp) {
os << ", cluster_data:" << (op.source_info & kCowOpSourceInfoDataMask);
} else {
os << ", label:0x" << android::base::StringPrintf("%" PRIx64, op.source_info);
Expand Down Expand Up @@ -123,7 +120,7 @@ int64_t GetNextDataOffset(const CowOperationV2& op, uint32_t cluster_ops) {
}

bool IsMetadataOp(const CowOperation& op) {
switch (GetCowOpSourceInfoType(op)) {
switch (op.type) {
case kCowLabelOp:
case kCowClusterOp:
case kCowFooterOp:
Expand All @@ -135,7 +132,7 @@ bool IsMetadataOp(const CowOperation& op) {
}

bool IsOrderedOp(const CowOperation& op) {
switch (GetCowOpSourceInfoType(op)) {
switch (op.type) {
case kCowCopyOp:
case kCowXorOp:
return true;
Expand Down
15 changes: 7 additions & 8 deletions fs_mgr/libsnapshot/libsnapshot_cow/cow_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include <android-base/file.h>
#include <android-base/logging.h>
#include <libsnapshot/cow_format.h>
#include <libsnapshot/cow_reader.h>
#include <zlib.h>

Expand Down Expand Up @@ -140,7 +139,7 @@ bool CowReader::Parse(android::base::borrowed_fd fd, std::optional<uint64_t> lab
const auto& v2_op = parser.ops()->at(i);

auto& new_op = ops_->at(i);
SetCowOpSourceInfoType(&new_op, v2_op.type);
new_op.type = v2_op.type;
new_op.data_length = v2_op.data_length;

if (v2_op.new_block > std::numeric_limits<uint32_t>::max()) {
Expand All @@ -150,7 +149,7 @@ bool CowReader::Parse(android::base::borrowed_fd fd, std::optional<uint64_t> lab
new_op.new_block = v2_op.new_block;

uint64_t source_info = v2_op.source;
if (GetCowOpSourceInfoType(new_op) != kCowLabelOp) {
if (new_op.type != kCowLabelOp) {
source_info &= kCowOpSourceInfoDataMask;
if (source_info != v2_op.source) {
LOG(ERROR) << "Out-of-range source value in COW op: " << v2_op;
Expand All @@ -167,7 +166,7 @@ bool CowReader::Parse(android::base::borrowed_fd fd, std::optional<uint64_t> lab
return false;
}
}
new_op.source_info |= source_info;
new_op.source_info = source_info;
}

// If we're resuming a write, we're not ready to merge
Expand Down Expand Up @@ -290,7 +289,7 @@ bool CowReader::PrepMergeOps() {
for (size_t i = 0; i < ops_->size(); i++) {
auto& current_op = ops_->data()[i];

if (GetCowOpSourceInfoType(current_op) == kCowSequenceOp) {
if (current_op.type == kCowSequenceOp) {
size_t seq_len = current_op.data_length / sizeof(uint32_t);

merge_op_blocks->resize(merge_op_blocks->size() + seq_len);
Expand Down Expand Up @@ -602,7 +601,7 @@ std::unique_ptr<ICowOpIter> CowReader::GetMergeOpIter(bool ignore_progress) {
}

bool CowReader::GetRawBytes(const CowOperation* op, void* buffer, size_t len, size_t* read) {
switch (GetCowOpSourceInfoType(*op)) {
switch (op->type) {
case kCowSequenceOp:
case kCowReplaceOp:
case kCowXorOp:
Expand Down Expand Up @@ -695,7 +694,7 @@ ssize_t CowReader::ReadData(const CowOperation* op, void* buffer, size_t buffer_
}

uint64_t offset;
if (GetCowOpSourceInfoType(*op) == kCowXorOp) {
if (op->type == kCowXorOp) {
offset = data_loc_->at(op->new_block);
} else {
offset = GetCowOpSourceInfoData(*op);
Expand All @@ -712,7 +711,7 @@ ssize_t CowReader::ReadData(const CowOperation* op, void* buffer, size_t buffer_
}

bool CowReader::GetSourceOffset(const CowOperation* op, uint64_t* source_offset) {
switch (GetCowOpSourceInfoType(*op)) {
switch (op->type) {
case kCowCopyOp:
*source_offset = GetCowOpSourceInfoData(*op) * header_.block_size;
return true;
Expand Down
16 changes: 7 additions & 9 deletions fs_mgr/libsnapshot/libsnapshot_cow/inspect_cow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <gflags/gflags.h>
#include <libsnapshot/cow_format.h>
#include <libsnapshot/cow_reader.h>
#include "parser_v2.h"

Expand Down Expand Up @@ -199,7 +198,7 @@ static bool Inspect(const std::string& path) {

if (!FLAGS_silent && FLAGS_show_ops) std::cout << *op << "\n";

if ((FLAGS_decompress || extract_to >= 0) && GetCowOpSourceInfoType(*op) == kCowReplaceOp) {
if ((FLAGS_decompress || extract_to >= 0) && op->type == kCowReplaceOp) {
if (reader.ReadData(op, buffer.data(), buffer.size()) < 0) {
std::cerr << "Failed to decompress for :" << *op << "\n";
success = false;
Expand All @@ -213,13 +212,12 @@ static bool Inspect(const std::string& path) {
return false;
}
}
} else if (extract_to >= 0 && !IsMetadataOp(*op) &&
GetCowOpSourceInfoType(*op) != kCowZeroOp) {
} else if (extract_to >= 0 && !IsMetadataOp(*op) && op->type != kCowZeroOp) {
PLOG(ERROR) << "Cannot extract op yet: " << *op;
return false;
}

if (GetCowOpSourceInfoType(*op) == kCowSequenceOp && FLAGS_show_merge_sequence) {
if (op->type == kCowSequenceOp && FLAGS_show_merge_sequence) {
size_t read;
std::vector<uint32_t> merge_op_blocks;
size_t seq_len = op->data_length / sizeof(uint32_t);
Expand All @@ -237,13 +235,13 @@ static bool Inspect(const std::string& path) {
}
}

if (GetCowOpSourceInfoType(*op) == kCowCopyOp) {
if (op->type == kCowCopyOp) {
copy_ops++;
} else if (GetCowOpSourceInfoType(*op) == kCowReplaceOp) {
} else if (op->type == kCowReplaceOp) {
replace_ops++;
} else if (GetCowOpSourceInfoType(*op) == kCowZeroOp) {
} else if (op->type == kCowZeroOp) {
zero_ops++;
} else if (GetCowOpSourceInfoType(*op) == kCowXorOp) {
} else if (op->type == kCowXorOp) {
xor_ops++;
}

Expand Down
11 changes: 5 additions & 6 deletions fs_mgr/libsnapshot/libsnapshot_cow/snapshot_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ ssize_t CompressedSnapshotReader::ReadBlock(uint64_t chunk, size_t start_offset,
op = ops_[chunk];
}

if (!op || GetCowOpSourceInfoType(*op) == kCowCopyOp) {
if (!op || op->type == kCowCopyOp) {
borrowed_fd fd = GetSourceFd();
if (fd < 0) {
// GetSourceFd sets errno.
Expand All @@ -169,15 +169,15 @@ ssize_t CompressedSnapshotReader::ReadBlock(uint64_t chunk, size_t start_offset,
// ReadFullyAtOffset sets errno.
return -1;
}
} else if (GetCowOpSourceInfoType(*op) == kCowZeroOp) {
} else if (op->type == kCowZeroOp) {
memset(buffer, 0, bytes_to_read);
} else if (GetCowOpSourceInfoType(*op) == kCowReplaceOp) {
} else if (op->type == kCowReplaceOp) {
if (cow_->ReadData(op, buffer, bytes_to_read, start_offset) < bytes_to_read) {
LOG(ERROR) << "CompressedSnapshotReader failed to read replace op";
errno = EIO;
return -1;
}
} else if (GetCowOpSourceInfoType(*op) == kCowXorOp) {
} else if (op->type == kCowXorOp) {
borrowed_fd fd = GetSourceFd();
if (fd < 0) {
// GetSourceFd sets errno.
Expand Down Expand Up @@ -208,8 +208,7 @@ ssize_t CompressedSnapshotReader::ReadBlock(uint64_t chunk, size_t start_offset,
((char*)buffer)[i] ^= data[i];
}
} else {
LOG(ERROR) << "CompressedSnapshotReader unknown op type: "
<< uint32_t(GetCowOpSourceInfoType(*op));
LOG(ERROR) << "CompressedSnapshotReader unknown op type: " << uint32_t(op->type);
errno = EINVAL;
return -1;
}
Expand Down
Loading

0 comments on commit d6fe303

Please sign in to comment.