1 // Copyright (C) 2023 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #include <memory> 18 #include <queue> 19 #include <string> 20 #include <thread> 21 #include <vector> 22 23 #include <android-base/unique_fd.h> 24 #include <snapuserd/block_server.h> 25 26 namespace android { 27 namespace snapshot { 28 29 class SnapshotHandler; 30 31 class HandlerThread { 32 public: 33 explicit HandlerThread(std::shared_ptr<SnapshotHandler> snapuserd); 34 35 void FreeResources(); snapuserd()36 const std::shared_ptr<SnapshotHandler>& snapuserd() const { return snapuserd_; } thread()37 std::thread& thread() { return thread_; } 38 misc_name()39 const std::string& misc_name() const { return misc_name_; } ThreadTerminated()40 bool ThreadTerminated() { return thread_terminated_; } SetThreadTerminated()41 void SetThreadTerminated() { thread_terminated_ = true; } 42 43 private: 44 std::thread thread_; 45 std::shared_ptr<SnapshotHandler> snapuserd_; 46 std::string misc_name_; 47 bool thread_terminated_ = false; 48 }; 49 50 class ISnapshotHandlerManager { 51 public: ~ISnapshotHandlerManager()52 virtual ~ISnapshotHandlerManager() {} 53 54 // Add a new snapshot handler but do not start serving requests yet. 55 virtual std::shared_ptr<HandlerThread> AddHandler( 56 const std::string& misc_name, const std::string& cow_device_path, 57 const std::string& backing_device, const std::string& base_path_merge, 58 std::shared_ptr<IBlockServerOpener> opener, int num_worker_threads, bool use_iouring, 59 bool o_direct, uint32_t cow_op_merge_size) = 0; 60 61 // Start serving requests on a snapshot handler. 62 virtual bool StartHandler(const std::string& misc_name) = 0; 63 64 // Stop serving requests on a snapshot handler and remove it. 65 virtual bool DeleteHandler(const std::string& misc_name) = 0; 66 67 // Begin merging blocks on the given snapshot handler. 68 virtual bool InitiateMerge(const std::string& misc_name) = 0; 69 70 // Return a string containing a status code indicating the merge status 71 // on the handler. Returns empty on error. 72 virtual std::string GetMergeStatus(const std::string& misc_name) = 0; 73 74 // Wait until all handlers have terminated. 75 virtual void JoinAllThreads() = 0; 76 77 // Stop any in-progress merge threads. 78 virtual void TerminateMergeThreads() = 0; 79 80 // Returns the merge progress across all merging snapshot handlers. 81 virtual double GetMergePercentage() = 0; 82 83 // Returns whether all snapshots have verified. 84 virtual bool GetVerificationStatus() = 0; 85 86 // Disable partition verification 87 virtual void DisableVerification() = 0; 88 }; 89 90 class SnapshotHandlerManager final : public ISnapshotHandlerManager { 91 public: 92 SnapshotHandlerManager(); 93 std::shared_ptr<HandlerThread> AddHandler(const std::string& misc_name, 94 const std::string& cow_device_path, 95 const std::string& backing_device, 96 const std::string& base_path_merge, 97 std::shared_ptr<IBlockServerOpener> opener, 98 int num_worker_threads, bool use_iouring, 99 bool o_direct, uint32_t cow_op_merge_size) override; 100 bool StartHandler(const std::string& misc_name) override; 101 bool DeleteHandler(const std::string& misc_name) override; 102 bool InitiateMerge(const std::string& misc_name) override; 103 std::string GetMergeStatus(const std::string& misc_name) override; 104 void JoinAllThreads() override; 105 void TerminateMergeThreads() override; 106 double GetMergePercentage() override; 107 bool GetVerificationStatus() override; DisableVerification()108 void DisableVerification() override { perform_verification_ = false; } 109 110 private: 111 bool StartHandler(const std::shared_ptr<HandlerThread>& handler); 112 void RunThread(std::shared_ptr<HandlerThread> handler); 113 bool StartMerge(std::lock_guard<std::mutex>* proof_of_lock, 114 const std::shared_ptr<HandlerThread>& handler); 115 void MonitorMerge(); 116 void WakeupMonitorMergeThread(); 117 bool RemoveAndJoinHandler(const std::string& misc_name); 118 119 // Find a HandlerThread within a lock. 120 using HandlerList = std::vector<std::shared_ptr<HandlerThread>>; 121 HandlerList::iterator FindHandler(std::lock_guard<std::mutex>* proof_of_lock, 122 const std::string& misc_name); 123 124 std::mutex lock_; 125 HandlerList dm_users_; 126 127 bool stop_monitor_merge_thread_ = false; 128 int active_merge_threads_ = 0; 129 std::thread merge_monitor_; 130 int num_partitions_merge_complete_ = 0; 131 std::queue<std::shared_ptr<HandlerThread>> merge_handlers_; 132 android::base::unique_fd monitor_merge_event_fd_; 133 bool perform_verification_ = true; 134 }; 135 136 } // namespace snapshot 137 } // namespace android 138