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