xref: /aosp_15_r20/system/core/fs_mgr/libsnapshot/snapuserd/include/snapuserd/block_server.h (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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 <stddef.h>
18 #include <stdint.h>
19 
20 #include <memory>
21 
22 namespace android {
23 namespace snapshot {
24 
25 // These interfaces model the block device driver component of snapuserd (eg,
26 // dm-user).
27 
28 // An open connection to a userspace block device control
29 class IBlockServer {
30   public:
31     class Delegate {
32       public:
~Delegate()33         virtual ~Delegate() {}
34 
35         // Respond to a request for reading a contiguous run of sectors. This
36         // call should be followed by calls to GetResponseBuffer/CommitBuffer
37         // until the |size| is fulfilled.
38         //
39         // If false is returned, an error will be automatically reported unless
40         // SendError was called.
41         virtual bool RequestSectors(uint64_t sector, uint64_t size) = 0;
42     };
43 
~IBlockServer()44     virtual ~IBlockServer() {}
45 
46     // Process I/O requests. This can block the worker thread until either a
47     // request is available or the underlying connection has been destroyed.
48     //
49     // True indicates that one or more requests was processed. False indicates
50     // an unrecoverable condition and processing should stop.
51     virtual bool ProcessRequests() = 0;
52 
53     // Return a buffer for fulfilling a RequestSectors request. This buffer
54     // is valid until calling SendBufferedIo. This cannot be called outside
55     // of RequestSectors().
56     //
57     // "to_write" must be <= "size". If it is < size, the excess bytes are
58     // available for writing, but will not be send via SendBufferedIo, and
59     // may be reallocated in the next call to GetResponseBuffer.
60     //
61     // All buffers returned are invalidated after SendBufferedIo or returning
62     // control from RequestSectors.
63     virtual void* GetResponseBuffer(size_t size, size_t to_write) = 0;
64 
65     // Send all outstanding buffers to the driver, in order. This should
66     // be called at least once in response to RequestSectors. This returns
67     // ownership of any buffers returned by GetResponseBuffer.
68     //
69     // If false is returned, an error is automatically reported to the driver.
70     virtual bool SendBufferedIo() = 0;
71 
GetResponseBuffer(size_t size)72     void* GetResponseBuffer(size_t size) { return GetResponseBuffer(size, size); }
73 };
74 
75 class IBlockServerOpener {
76   public:
77     virtual ~IBlockServerOpener() = default;
78 
79     // Open a connection to the service. This is called on the daemon thread.
80     //
81     // buffer_size is the maximum amount of buffered I/O to use.
82     virtual std::unique_ptr<IBlockServer> Open(IBlockServer::Delegate* delegate,
83                                                size_t buffer_size) = 0;
84 };
85 
86 class IBlockServerFactory {
87   public:
~IBlockServerFactory()88     virtual ~IBlockServerFactory() {}
89 
90     // Return a new IBlockServerOpener given a unique device name.
91     virtual std::shared_ptr<IBlockServerOpener> CreateOpener(const std::string& misc_name) = 0;
92 };
93 
94 }  // namespace snapshot
95 }  // namespace android
96