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 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d)) 51 52 // This structure represents the kernel COW header. 53 // All the below fields should be in Little Endian format. 54 struct disk_header { 55 uint32_t magic; 56 57 /* 58 * Is this snapshot valid. There is no way of recovering 59 * an invalid snapshot. 60 */ 61 uint32_t valid; 62 63 /* 64 * Simple, incrementing version. no backward 65 * compatibility. 66 */ 67 uint32_t version; 68 69 /* In sectors */ 70 uint32_t chunk_size; 71 } __packed; 72 73 // A disk exception is a mapping of old_chunk to new_chunk 74 // old_chunk is the chunk ID of a dm-snapshot device. 75 // new_chunk is the chunk ID of the COW device. 76 struct disk_exception { 77 uint64_t old_chunk; 78 uint64_t new_chunk; 79 } __packed; 80 81 // Control structures to communicate with dm-user 82 // It comprises of header and a payload 83 struct dm_user_header { 84 __u64 seq; 85 __u64 type; 86 __u64 flags; 87 __u64 sector; 88 __u64 len; 89 } __attribute__((packed)); 90 91 struct dm_user_payload { 92 __u8 buf[]; 93 }; 94 95 // Message comprising both header and payload 96 struct dm_user_message { 97 struct dm_user_header header; 98 struct dm_user_payload payload; 99 }; 100 101 } // namespace snapshot 102 } // namespace android 103