g_mirror.h revision 135872
1/*- 2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/geom/mirror/g_mirror.h 135872 2004-09-28 07:33:37Z pjd $ 27 */ 28 29#ifndef _G_MIRROR_H_ 30#define _G_MIRROR_H_ 31 32#include <sys/endian.h> 33#include <sys/md5.h> 34 35#define G_MIRROR_CLASS_NAME "MIRROR" 36 37#define G_MIRROR_MAGIC "GEOM::MIRROR" 38#define G_MIRROR_VERSION 1 39 40#define G_MIRROR_BALANCE_NONE 0 41#define G_MIRROR_BALANCE_ROUND_ROBIN 1 42#define G_MIRROR_BALANCE_LOAD 2 43#define G_MIRROR_BALANCE_SPLIT 3 44#define G_MIRROR_BALANCE_PREFER 4 45#define G_MIRROR_BALANCE_MIN G_MIRROR_BALANCE_NONE 46#define G_MIRROR_BALANCE_MAX G_MIRROR_BALANCE_PREFER 47 48#define G_MIRROR_DISK_FLAG_DIRTY 0x0000000000000001ULL 49#define G_MIRROR_DISK_FLAG_SYNCHRONIZING 0x0000000000000002ULL 50#define G_MIRROR_DISK_FLAG_FORCE_SYNC 0x0000000000000004ULL 51#define G_MIRROR_DISK_FLAG_INACTIVE 0x0000000000000008ULL 52#define G_MIRROR_DISK_FLAG_HARDCODED 0x0000000000000010ULL 53#define G_MIRROR_DISK_FLAG_MASK (G_MIRROR_DISK_FLAG_DIRTY | \ 54 G_MIRROR_DISK_FLAG_SYNCHRONIZING | \ 55 G_MIRROR_DISK_FLAG_FORCE_SYNC | \ 56 G_MIRROR_DISK_FLAG_INACTIVE) 57 58#define G_MIRROR_DEVICE_FLAG_NOAUTOSYNC 0x0000000000000001ULL 59#define G_MIRROR_DEVICE_FLAG_MASK (G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) 60 61#ifdef _KERNEL 62extern u_int g_mirror_debug; 63 64#define G_MIRROR_DEBUG(lvl, ...) do { \ 65 if (g_mirror_debug >= (lvl)) { \ 66 printf("GEOM_MIRROR"); \ 67 if (g_mirror_debug > 0) \ 68 printf("[%u]", lvl); \ 69 printf(": "); \ 70 printf(__VA_ARGS__); \ 71 printf("\n"); \ 72 } \ 73} while (0) 74#define G_MIRROR_LOGREQ(lvl, bp, ...) do { \ 75 if (g_mirror_debug >= (lvl)) { \ 76 printf("GEOM_MIRROR"); \ 77 if (g_mirror_debug > 0) \ 78 printf("[%u]", lvl); \ 79 printf(": "); \ 80 printf(__VA_ARGS__); \ 81 printf(" "); \ 82 g_print_bio(bp); \ 83 printf("\n"); \ 84 } \ 85} while (0) 86 87#define G_MIRROR_BIO_FLAG_REGULAR 0x01 88#define G_MIRROR_BIO_FLAG_SYNC 0x02 89 90/* 91 * Informations needed for synchronization. 92 */ 93struct g_mirror_disk_sync { 94 struct g_consumer *ds_consumer; /* Consumer connected to our mirror. */ 95 off_t ds_offset; /* Offset of next request to send. */ 96 off_t ds_offset_done; /* Offset of already synchronized 97 region. */ 98 off_t ds_resync; /* Resynchronize from this offset. */ 99 u_int ds_syncid; /* Disk's synchronization ID. */ 100 u_char *ds_data; 101}; 102 103/* 104 * Informations needed for synchronization. 105 */ 106struct g_mirror_device_sync { 107 struct g_geom *ds_geom; /* Synchronization geom. */ 108 u_int ds_ndisks; /* Number of disks in SYNCHRONIZING 109 state. */ 110}; 111 112#define G_MIRROR_DISK_STATE_NONE 0 113#define G_MIRROR_DISK_STATE_NEW 1 114#define G_MIRROR_DISK_STATE_ACTIVE 2 115#define G_MIRROR_DISK_STATE_STALE 3 116#define G_MIRROR_DISK_STATE_SYNCHRONIZING 4 117#define G_MIRROR_DISK_STATE_DISCONNECTED 5 118#define G_MIRROR_DISK_STATE_DESTROY 6 119struct g_mirror_disk { 120 uint32_t d_id; /* Disk ID. */ 121 struct g_consumer *d_consumer; /* Consumer. */ 122 struct g_mirror_softc *d_softc; /* Back-pointer to softc. */ 123 int d_state; /* Disk state. */ 124 u_int d_priority; /* Disk priority. */ 125 struct bintime d_delay; /* Disk delay. */ 126 struct bintime d_last_used; /* When disk was last used. */ 127 uint64_t d_flags; /* Additional flags. */ 128 struct g_mirror_disk_sync d_sync;/* Sync information. */ 129 LIST_ENTRY(g_mirror_disk) d_next; 130}; 131#define d_name d_consumer->provider->name 132 133#define G_MIRROR_EVENT_DONTWAIT 0x1 134#define G_MIRROR_EVENT_WAIT 0x2 135#define G_MIRROR_EVENT_DEVICE 0x4 136#define G_MIRROR_EVENT_DONE 0x8 137struct g_mirror_event { 138 struct g_mirror_disk *e_disk; 139 int e_state; 140 int e_flags; 141 int e_error; 142 TAILQ_ENTRY(g_mirror_event) e_next; 143}; 144 145#define G_MIRROR_DEVICE_FLAG_DESTROY 0x0100000000000000ULL 146#define G_MIRROR_DEVICE_FLAG_WAIT 0x0200000000000000ULL 147 148#define G_MIRROR_DEVICE_STATE_STARTING 0 149#define G_MIRROR_DEVICE_STATE_RUNNING 1 150 151#define G_MIRROR_BUMP_ON_FIRST_WRITE 1 152#define G_MIRROR_BUMP_IMMEDIATELY 2 153struct g_mirror_softc { 154 u_int sc_state; /* Device state. */ 155 uint32_t sc_slice; /* Slice size. */ 156 uint8_t sc_balance; /* Balance algorithm. */ 157 uint64_t sc_mediasize; /* Device size. */ 158 uint32_t sc_sectorsize; /* Sector size. */ 159 uint64_t sc_flags; /* Additional flags. */ 160 161 struct g_geom *sc_geom; 162 struct g_provider *sc_provider; 163 164 uint32_t sc_id; /* Mirror unique ID. */ 165 166 struct bio_queue_head sc_queue; 167 struct mtx sc_queue_mtx; 168 struct proc *sc_worker; 169 170 LIST_HEAD(, g_mirror_disk) sc_disks; 171 u_int sc_ndisks; /* Number of disks. */ 172 struct g_mirror_disk *sc_hint; 173 174 u_int sc_syncid; /* Synchronization ID. */ 175 int sc_bump_syncid; 176 struct g_mirror_device_sync sc_sync; 177 178 TAILQ_HEAD(, g_mirror_event) sc_events; 179 struct mtx sc_events_mtx; 180 181 struct callout sc_callout; 182}; 183#define sc_name sc_geom->name 184 185u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state); 186int g_mirror_destroy(struct g_mirror_softc *sc, boolean_t force); 187int g_mirror_event_send(void *arg, int state, int flags); 188struct g_mirror_metadata; 189void g_mirror_fill_metadata(struct g_mirror_softc *sc, 190 struct g_mirror_disk *disk, struct g_mirror_metadata *md); 191void g_mirror_update_metadata(struct g_mirror_disk *disk); 192 193g_ctl_req_t g_mirror_config; 194#endif /* _KERNEL */ 195 196struct g_mirror_metadata { 197 char md_magic[16]; /* Magic value. */ 198 uint32_t md_version; /* Version number. */ 199 char md_name[16]; /* Mirror name. */ 200 uint32_t md_mid; /* Mirror unique ID. */ 201 uint32_t md_did; /* Disk unique ID. */ 202 uint8_t md_all; /* Number of disks in mirror. */ 203 uint32_t md_syncid; /* Synchronization ID. */ 204 uint8_t md_priority; /* Disk priority. */ 205 uint32_t md_slice; /* Slice size. */ 206 uint8_t md_balance; /* Balance type. */ 207 uint64_t md_mediasize; /* Size of the smallest 208 disk in mirror. */ 209 uint32_t md_sectorsize; /* Sector size. */ 210 uint64_t md_sync_offset; /* Synchronized offset. */ 211 uint64_t md_mflags; /* Additional mirror flags. */ 212 uint64_t md_dflags; /* Additional disk flags. */ 213 char md_provider[16]; /* Hardcoded provider. */ 214 u_char md_hash[16]; /* MD5 hash. */ 215}; 216static __inline void 217mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data) 218{ 219 MD5_CTX ctx; 220 221 bcopy(md->md_magic, data, 16); 222 le32enc(data + 16, md->md_version); 223 bcopy(md->md_name, data + 20, 16); 224 le32enc(data + 36, md->md_mid); 225 le32enc(data + 40, md->md_did); 226 *(data + 44) = md->md_all; 227 le32enc(data + 45, md->md_syncid); 228 *(data + 49) = md->md_priority; 229 le32enc(data + 50, md->md_slice); 230 *(data + 54) = md->md_balance; 231 le64enc(data + 55, md->md_mediasize); 232 le32enc(data + 63, md->md_sectorsize); 233 le64enc(data + 67, md->md_sync_offset); 234 le64enc(data + 75, md->md_mflags); 235 le64enc(data + 83, md->md_dflags); 236 bcopy(md->md_provider, data + 91, 16); 237 MD5Init(&ctx); 238 MD5Update(&ctx, data, 107); 239 MD5Final(md->md_hash, &ctx); 240 bcopy(md->md_hash, data + 107, 16); 241} 242static __inline int 243mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md) 244{ 245 MD5_CTX ctx; 246 247 bcopy(data, md->md_magic, 16); 248 md->md_version = le32dec(data + 16); 249 bcopy(data + 20, md->md_name, 16); 250 md->md_mid = le32dec(data + 36); 251 md->md_did = le32dec(data + 40); 252 md->md_all = *(data + 44); 253 md->md_syncid = le32dec(data + 45); 254 md->md_priority = *(data + 49); 255 md->md_slice = le32dec(data + 50); 256 md->md_balance = *(data + 54); 257 md->md_mediasize = le64dec(data + 55); 258 md->md_sectorsize = le32dec(data + 63); 259 md->md_sync_offset = le64dec(data + 67); 260 md->md_mflags = le64dec(data + 75); 261 md->md_dflags = le64dec(data + 83); 262 bcopy(data + 91, md->md_provider, 16); 263 bcopy(data + 107, md->md_hash, 16); 264 MD5Init(&ctx); 265 MD5Update(&ctx, data, 107); 266 MD5Final(md->md_hash, &ctx); 267 if (bcmp(md->md_hash, data + 107, 16) != 0) 268 return (EINVAL); 269 return (0); 270} 271 272static __inline const char * 273balance_name(u_int balance) 274{ 275 static const char *algorithms[] = { 276 [G_MIRROR_BALANCE_NONE] = "none", 277 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin", 278 [G_MIRROR_BALANCE_LOAD] = "load", 279 [G_MIRROR_BALANCE_SPLIT] = "split", 280 [G_MIRROR_BALANCE_PREFER] = "prefer", 281 [G_MIRROR_BALANCE_MAX + 1] = "unknown" 282 }; 283 284 if (balance > G_MIRROR_BALANCE_MAX) 285 balance = G_MIRROR_BALANCE_MAX + 1; 286 287 return (algorithms[balance]); 288} 289 290static __inline int 291balance_id(const char *name) 292{ 293 static const char *algorithms[] = { 294 [G_MIRROR_BALANCE_NONE] = "none", 295 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin", 296 [G_MIRROR_BALANCE_LOAD] = "load", 297 [G_MIRROR_BALANCE_SPLIT] = "split", 298 [G_MIRROR_BALANCE_PREFER] = "prefer" 299 }; 300 int n; 301 302 for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) { 303 if (strcmp(name, algorithms[n]) == 0) 304 return (n); 305 } 306 return (-1); 307} 308 309static __inline void 310mirror_metadata_dump(const struct g_mirror_metadata *md) 311{ 312 static const char hex[] = "0123456789abcdef"; 313 char hash[16 * 2 + 1]; 314 u_int i; 315 316 printf(" magic: %s\n", md->md_magic); 317 printf(" version: %u\n", (u_int)md->md_version); 318 printf(" name: %s\n", md->md_name); 319 printf(" mid: %u\n", (u_int)md->md_mid); 320 printf(" did: %u\n", (u_int)md->md_did); 321 printf(" all: %u\n", (u_int)md->md_all); 322 printf(" syncid: %u\n", (u_int)md->md_syncid); 323 printf(" priority: %u\n", (u_int)md->md_priority); 324 printf(" slice: %u\n", (u_int)md->md_slice); 325 printf(" balance: %s\n", balance_name((u_int)md->md_balance)); 326 printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize); 327 printf("sectorsize: %u\n", (u_int)md->md_sectorsize); 328 printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset); 329 printf(" mflags:"); 330 if (md->md_mflags == 0) 331 printf(" NONE"); 332 else { 333 if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) 334 printf(" NOAUTOSYNC"); 335 } 336 printf("\n"); 337 printf(" dflags:"); 338 if (md->md_dflags == 0) 339 printf(" NONE"); 340 else { 341 if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0) 342 printf(" DIRTY"); 343 if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) 344 printf(" SYNCHRONIZING"); 345 if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) 346 printf(" FORCE_SYNC"); 347 if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) 348 printf(" INACTIVE"); 349 } 350 printf("\n"); 351 printf("hcprovider: %s\n", md->md_provider); 352 bzero(hash, sizeof(hash)); 353 for (i = 0; i < 16; i++) { 354 hash[i * 2] = hex[md->md_hash[i] >> 4]; 355 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f]; 356 } 357 printf(" MD5 hash: %s\n", hash); 358} 359#endif /* !_G_MIRROR_H_ */ 360