g_mirror.h revision 133142
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 133142 2004-08-04 21:35:05Z 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_MASK (G_MIRROR_DISK_FLAG_DIRTY | \ 53 G_MIRROR_DISK_FLAG_SYNCHRONIZING | \ 54 G_MIRROR_DISK_FLAG_FORCE_SYNC | \ 55 G_MIRROR_DISK_FLAG_INACTIVE) 56 57#define G_MIRROR_DEVICE_FLAG_NOAUTOSYNC 0x0000000000000001ULL 58#define G_MIRROR_DEVICE_FLAG_MASK (G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) 59 60#ifdef _KERNEL 61extern u_int g_mirror_debug; 62 63#define G_MIRROR_DEBUG(lvl, ...) do { \ 64 if (g_mirror_debug >= (lvl)) { \ 65 printf("GEOM_MIRROR"); \ 66 if (g_mirror_debug > 0) \ 67 printf("[%u]", lvl); \ 68 printf(": "); \ 69 printf(__VA_ARGS__); \ 70 printf("\n"); \ 71 } \ 72} while (0) 73#define G_MIRROR_LOGREQ(lvl, bp, ...) do { \ 74 if (g_mirror_debug >= (lvl)) { \ 75 printf("GEOM_MIRROR"); \ 76 if (g_mirror_debug > 0) \ 77 printf("[%u]", lvl); \ 78 printf(": "); \ 79 printf(__VA_ARGS__); \ 80 printf(" "); \ 81 g_print_bio(bp); \ 82 printf("\n"); \ 83 } \ 84} while (0) 85 86#define G_MIRROR_BIO_FLAG_REGULAR 0x01 87#define G_MIRROR_BIO_FLAG_SYNC 0x02 88 89/* 90 * Informations needed for synchronization. 91 */ 92struct g_mirror_disk_sync { 93 struct g_consumer *ds_consumer; /* Consumer connected to our mirror. */ 94 off_t ds_offset; /* Offset of next request to send. */ 95 off_t ds_offset_done; /* Offset of already synchronized 96 region. */ 97 u_int ds_syncid; /* Disk's synchronization ID. */ 98}; 99 100/* 101 * Informations needed for synchronization. 102 */ 103struct g_mirror_device_sync { 104 struct g_geom *ds_geom; /* Synchronization geom. */ 105 size_t ds_block; /* Synchronization request size. */ 106 u_int ds_ndisks; /* Number of disks in SYNCHRONIZING 107 state. */ 108 uma_zone_t ds_zone; /* UMA zone for synchronization 109 blocks. */ 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 u_char md_hash[16]; /* MD5 hash. */ 214}; 215static __inline void 216mirror_metadata_encode(struct g_mirror_metadata *md, u_char *data) 217{ 218 MD5_CTX ctx; 219 220 bcopy(md->md_magic, data, 16); 221 le32enc(data + 16, md->md_version); 222 bcopy(md->md_name, data + 20, 16); 223 le32enc(data + 36, md->md_mid); 224 le32enc(data + 40, md->md_did); 225 *(data + 44) = md->md_all; 226 le32enc(data + 45, md->md_syncid); 227 *(data + 49) = md->md_priority; 228 le32enc(data + 50, md->md_slice); 229 *(data + 54) = md->md_balance; 230 le64enc(data + 55, md->md_mediasize); 231 le32enc(data + 63, md->md_sectorsize); 232 le64enc(data + 67, md->md_sync_offset); 233 le64enc(data + 75, md->md_mflags); 234 le64enc(data + 83, md->md_dflags); 235 MD5Init(&ctx); 236 MD5Update(&ctx, data, 91); 237 MD5Final(md->md_hash, &ctx); 238 bcopy(md->md_hash, data + 91, 16); 239} 240static __inline int 241mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md) 242{ 243 MD5_CTX ctx; 244 245 bcopy(data, md->md_magic, 16); 246 md->md_version = le32dec(data + 16); 247 bcopy(data + 20, md->md_name, 16); 248 md->md_mid = le32dec(data + 36); 249 md->md_did = le32dec(data + 40); 250 md->md_all = *(data + 44); 251 md->md_syncid = le32dec(data + 45); 252 md->md_priority = *(data + 49); 253 md->md_slice = le32dec(data + 50); 254 md->md_balance = *(data + 54); 255 md->md_mediasize = le64dec(data + 55); 256 md->md_sectorsize = le32dec(data + 63); 257 md->md_sync_offset = le64dec(data + 67); 258 md->md_mflags = le64dec(data + 75); 259 md->md_dflags = le64dec(data + 83); 260 bcopy(data + 91, md->md_hash, 16); 261 MD5Init(&ctx); 262 MD5Update(&ctx, data, 91); 263 MD5Final(md->md_hash, &ctx); 264 if (bcmp(md->md_hash, data + 91, 16) != 0) 265 return (EINVAL); 266 return (0); 267} 268 269static __inline const char * 270balance_name(u_int balance) 271{ 272 static const char *algorithms[] = { 273 [G_MIRROR_BALANCE_NONE] = "none", 274 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin", 275 [G_MIRROR_BALANCE_LOAD] = "load", 276 [G_MIRROR_BALANCE_SPLIT] = "split", 277 [G_MIRROR_BALANCE_PREFER] = "prefer", 278 [G_MIRROR_BALANCE_MAX + 1] = "unknown" 279 }; 280 281 if (balance > G_MIRROR_BALANCE_MAX) 282 balance = G_MIRROR_BALANCE_MAX + 1; 283 284 return (algorithms[balance]); 285} 286 287static __inline int 288balance_id(const char *name) 289{ 290 static const char *algorithms[] = { 291 [G_MIRROR_BALANCE_NONE] = "none", 292 [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin", 293 [G_MIRROR_BALANCE_LOAD] = "load", 294 [G_MIRROR_BALANCE_SPLIT] = "split", 295 [G_MIRROR_BALANCE_PREFER] = "prefer" 296 }; 297 int n; 298 299 for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) { 300 if (strcmp(name, algorithms[n]) == 0) 301 return (n); 302 } 303 return (-1); 304} 305 306static __inline void 307mirror_metadata_dump(const struct g_mirror_metadata *md) 308{ 309 static const char hex[] = "0123456789abcdef"; 310 char hash[16 * 2 + 1]; 311 u_int i; 312 313 printf(" magic: %s\n", md->md_magic); 314 printf(" version: %u\n", (u_int)md->md_version); 315 printf(" name: %s\n", md->md_name); 316 printf(" mid: %u\n", (u_int)md->md_mid); 317 printf(" did: %u\n", (u_int)md->md_did); 318 printf(" all: %u\n", (u_int)md->md_all); 319 printf(" syncid: %u\n", (u_int)md->md_syncid); 320 printf(" priority: %u\n", (u_int)md->md_priority); 321 printf(" slice: %u\n", (u_int)md->md_slice); 322 printf(" balance: %s\n", balance_name((u_int)md->md_balance)); 323 printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize); 324 printf("sectorsize: %u\n", (u_int)md->md_sectorsize); 325 printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset); 326 printf(" mflags:"); 327 if (md->md_mflags == 0) 328 printf(" NONE"); 329 else { 330 if ((md->md_mflags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) 331 printf(" NOAUTOSYNC"); 332 } 333 printf("\n"); 334 printf(" dflags:"); 335 if (md->md_mflags == 0) 336 printf(" NONE"); 337 else { 338 if ((md->md_dflags & G_MIRROR_DISK_FLAG_DIRTY) != 0) 339 printf(" DIRTY"); 340 if ((md->md_dflags & G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) 341 printf(" SYNCHRONIZING"); 342 if ((md->md_dflags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) 343 printf(" FORCE_SYNC"); 344 if ((md->md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) 345 printf(" INACTIVE"); 346 } 347 printf("\n"); 348 bzero(hash, sizeof(hash)); 349 for (i = 0; i < 16; i++) { 350 hash[i * 2] = hex[md->md_hash[i] >> 4]; 351 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f]; 352 } 353 printf(" MD5 hash: %s\n", hash); 354} 355#endif /* !_G_MIRROR_H_ */ 356