1 //
2 // Copyright (C) 2020 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <libsnapshot/snapshot_writer.h>
18 
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <payload_consumer/file_descriptor.h>
22 #include "snapshot_reader.h"
23 
24 namespace android {
25 namespace snapshot {
26 
27 using android::base::borrowed_fd;
28 using android::base::unique_fd;
29 using chromeos_update_engine::FileDescriptor;
30 
ISnapshotWriter(const CowOptions & options)31 ISnapshotWriter::ISnapshotWriter(const CowOptions& options) : ICowWriter(options) {}
32 
SetSourceDevice(const std::string & source_device)33 void ISnapshotWriter::SetSourceDevice(const std::string& source_device) {
34     source_device_ = {source_device};
35 }
36 
GetSourceFd()37 borrowed_fd ISnapshotWriter::GetSourceFd() {
38     if (!source_device_) {
39         LOG(ERROR) << "Attempted to read from source device but none was set";
40         return borrowed_fd{-1};
41     }
42 
43     if (source_fd_ < 0) {
44         source_fd_.reset(open(source_device_->c_str(), O_RDONLY | O_CLOEXEC));
45         if (source_fd_ < 0) {
46             PLOG(ERROR) << "open " << *source_device_;
47             return borrowed_fd{-1};
48         }
49     }
50     return source_fd_;
51 }
52 
CompressedSnapshotWriter(const CowOptions & options)53 CompressedSnapshotWriter::CompressedSnapshotWriter(const CowOptions& options)
54     : ISnapshotWriter(options) {}
55 
SetCowDevice(android::base::unique_fd && cow_device)56 bool CompressedSnapshotWriter::SetCowDevice(android::base::unique_fd&& cow_device) {
57     cow_device_ = std::move(cow_device);
58     cow_ = std::make_unique<CowWriter>(options_);
59     return true;
60 }
61 
Finalize()62 bool CompressedSnapshotWriter::Finalize() {
63     return cow_->Finalize();
64 }
65 
GetCowSize()66 uint64_t CompressedSnapshotWriter::GetCowSize() {
67     return cow_->GetCowSize();
68 }
69 
OpenReader()70 std::unique_ptr<FileDescriptor> CompressedSnapshotWriter::OpenReader() {
71     unique_fd cow_fd(dup(cow_device_.get()));
72     if (cow_fd < 0) {
73         PLOG(ERROR) << "dup COW device";
74         return nullptr;
75     }
76 
77     auto cow = std::make_unique<CowReader>();
78     if (!cow->Parse(std::move(cow_fd))) {
79         LOG(ERROR) << "Unable to read COW";
80         return nullptr;
81     }
82 
83     auto reader = std::make_unique<CompressedSnapshotReader>();
84     if (!reader->SetCow(std::move(cow))) {
85         LOG(ERROR) << "Unable to initialize COW reader";
86         return nullptr;
87     }
88     if (source_device_) {
89         reader->SetSourceDevice(*source_device_);
90     }
91 
92     const auto& cow_options = options();
93     if (cow_options.max_blocks) {
94         reader->SetBlockDeviceSize(*cow_options.max_blocks * cow_options.block_size);
95     }
96 
97     return reader;
98 }
99 
EmitCopy(uint64_t new_block,uint64_t old_block)100 bool CompressedSnapshotWriter::EmitCopy(uint64_t new_block, uint64_t old_block) {
101     return cow_->AddCopy(new_block, old_block);
102 }
103 
EmitRawBlocks(uint64_t new_block_start,const void * data,size_t size)104 bool CompressedSnapshotWriter::EmitRawBlocks(uint64_t new_block_start, const void* data,
105                                              size_t size) {
106     return cow_->AddRawBlocks(new_block_start, data, size);
107 }
108 
EmitZeroBlocks(uint64_t new_block_start,uint64_t num_blocks)109 bool CompressedSnapshotWriter::EmitZeroBlocks(uint64_t new_block_start, uint64_t num_blocks) {
110     return cow_->AddZeroBlocks(new_block_start, num_blocks);
111 }
112 
EmitLabel(uint64_t label)113 bool CompressedSnapshotWriter::EmitLabel(uint64_t label) {
114     return cow_->AddLabel(label);
115 }
116 
Initialize()117 bool CompressedSnapshotWriter::Initialize() {
118     return cow_->Initialize(cow_device_);
119 }
120 
InitializeAppend(uint64_t label)121 bool CompressedSnapshotWriter::InitializeAppend(uint64_t label) {
122     return cow_->InitializeAppend(cow_device_, label);
123 }
124 
OnlineKernelSnapshotWriter(const CowOptions & options)125 OnlineKernelSnapshotWriter::OnlineKernelSnapshotWriter(const CowOptions& options)
126     : ISnapshotWriter(options) {}
127 
SetSnapshotDevice(android::base::unique_fd && snapshot_fd,uint64_t cow_size)128 void OnlineKernelSnapshotWriter::SetSnapshotDevice(android::base::unique_fd&& snapshot_fd,
129                                                    uint64_t cow_size) {
130     snapshot_fd_ = std::move(snapshot_fd);
131     cow_size_ = cow_size;
132 }
133 
Finalize()134 bool OnlineKernelSnapshotWriter::Finalize() {
135     if (fsync(snapshot_fd_.get()) < 0) {
136         PLOG(ERROR) << "fsync";
137         return false;
138     }
139     return true;
140 }
141 
EmitRawBlocks(uint64_t new_block_start,const void * data,size_t size)142 bool OnlineKernelSnapshotWriter::EmitRawBlocks(uint64_t new_block_start, const void* data,
143                                                size_t size) {
144     uint64_t offset = new_block_start * options_.block_size;
145     if (lseek(snapshot_fd_.get(), offset, SEEK_SET) < 0) {
146         PLOG(ERROR) << "EmitRawBlocks lseek to offset " << offset;
147         return false;
148     }
149     if (!android::base::WriteFully(snapshot_fd_, data, size)) {
150         PLOG(ERROR) << "EmitRawBlocks write";
151         return false;
152     }
153     return true;
154 }
155 
EmitZeroBlocks(uint64_t new_block_start,uint64_t num_blocks)156 bool OnlineKernelSnapshotWriter::EmitZeroBlocks(uint64_t new_block_start, uint64_t num_blocks) {
157     std::string zeroes(options_.block_size, 0);
158     for (uint64_t i = 0; i < num_blocks; i++) {
159         if (!EmitRawBlocks(new_block_start + i, zeroes.data(), zeroes.size())) {
160             return false;
161         }
162     }
163     return true;
164 }
165 
EmitCopy(uint64_t new_block,uint64_t old_block)166 bool OnlineKernelSnapshotWriter::EmitCopy(uint64_t new_block, uint64_t old_block) {
167     auto source_fd = GetSourceFd();
168     if (source_fd < 0) {
169         return false;
170     }
171 
172     std::string buffer(options_.block_size, 0);
173     uint64_t offset = old_block * options_.block_size;
174     if (!android::base::ReadFullyAtOffset(source_fd, buffer.data(), buffer.size(), offset)) {
175         PLOG(ERROR) << "EmitCopy read";
176         return false;
177     }
178     return EmitRawBlocks(new_block, buffer.data(), buffer.size());
179 }
180 
EmitLabel(uint64_t)181 bool OnlineKernelSnapshotWriter::EmitLabel(uint64_t) {
182     // Not Needed
183     return true;
184 }
185 
OpenReader()186 std::unique_ptr<FileDescriptor> OnlineKernelSnapshotWriter::OpenReader() {
187     unique_fd fd(dup(snapshot_fd_.get()));
188     if (fd < 0) {
189         PLOG(ERROR) << "dup2 failed in OpenReader";
190         return nullptr;
191     }
192     return std::make_unique<ReadFdFileDescriptor>(std::move(fd));
193 }
194 
195 }  // namespace snapshot
196 }  // namespace android
197