1 // Copyright (C) 2020 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 namespace android {
18 namespace snapshot {
19 
20 #define DM_USER_REQ_MAP_READ 0
21 #define DM_USER_REQ_MAP_WRITE 1
22 
23 #define DM_USER_RESP_SUCCESS 0
24 #define DM_USER_RESP_ERROR 1
25 #define DM_USER_RESP_UNSUPPORTED 2
26 
27 // Kernel COW header fields
28 static constexpr uint32_t SNAP_MAGIC = 0x70416e53;
29 
30 static constexpr uint32_t SNAPSHOT_DISK_VERSION = 1;
31 
32 static constexpr uint32_t NUM_SNAPSHOT_HDR_CHUNKS = 1;
33 
34 static constexpr uint32_t SNAPSHOT_VALID = 1;
35 
36 /*
37  * The basic unit of block I/O is a sector. It is used in a number of contexts
38  * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9
39  * bytes. Variables of type sector_t represent an offset or size that is a
40  * multiple of 512 bytes. Hence these two constants.
41  */
42 static constexpr uint32_t SECTOR_SHIFT = 9;
43 
44 typedef __u64 sector_t;
45 typedef sector_t chunk_t;
46 
47 static constexpr uint32_t CHUNK_SIZE = 8;
48 static constexpr uint32_t CHUNK_SHIFT = (__builtin_ffs(CHUNK_SIZE) - 1);
49 
50 // This structure represents the kernel COW header.
51 // All the below fields should be in Little Endian format.
52 struct disk_header {
53     uint32_t magic;
54 
55     /*
56      * Is this snapshot valid.  There is no way of recovering
57      * an invalid snapshot.
58      */
59     uint32_t valid;
60 
61     /*
62      * Simple, incrementing version. no backward
63      * compatibility.
64      */
65     uint32_t version;
66 
67     /* In sectors */
68     uint32_t chunk_size;
69 } __packed;
70 
71 // A disk exception is a mapping of old_chunk to new_chunk
72 // old_chunk is the chunk ID of a dm-snapshot device.
73 // new_chunk is the chunk ID of the COW device.
74 struct disk_exception {
75     uint64_t old_chunk;
76     uint64_t new_chunk;
77 } __packed;
78 
79 // Control structures to communicate with dm-user
80 // It comprises of header and a payload
81 struct dm_user_header {
82     __u64 seq;
83     __u64 type;
84     __u64 flags;
85     __u64 sector;
86     __u64 len;
87 } __attribute__((packed));
88 
89 struct dm_user_payload {
90     __u8 buf[];
91 };
92 
93 // Message comprising both header and payload
94 struct dm_user_message {
95     struct dm_user_header header;
96     struct dm_user_payload payload;
97 };
98 
99 }  // namespace snapshot
100 }  // namespace android
101