g_raid3.h revision 133808
1149871Sscottl/*- 2149871Sscottl * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3136849Sscottl * All rights reserved. 4136849Sscottl * 5136849Sscottl * Redistribution and use in source and binary forms, with or without 6136849Sscottl * modification, are permitted provided that the following conditions 7136849Sscottl * are met: 8136849Sscottl * 1. Redistributions of source code must retain the above copyright 9136849Sscottl * notice, this list of conditions and the following disclaimer. 10136849Sscottl * 2. Redistributions in binary form must reproduce the above copyright 11136849Sscottl * notice, this list of conditions and the following disclaimer in the 12136849Sscottl * documentation and/or other materials provided with the distribution. 13136849Sscottl * 14136849Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15136849Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16136849Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17136849Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18136849Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19136849Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20136849Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21136849Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22136849Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23136849Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24136849Sscottl * SUCH DAMAGE. 25136849Sscottl * 26136849Sscottl * $FreeBSD: head/sys/geom/raid3/g_raid3.h 133808 2004-08-16 06:23:14Z pjd $ 27136849Sscottl */ 28136849Sscottl 29136849Sscottl#ifndef _G_RAID3_H_ 30149871Sscottl#define _G_RAID3_H_ 31149871Sscottl 32136849Sscottl#include <sys/endian.h> 33149871Sscottl#include <sys/md5.h> 34149871Sscottl 35136849Sscottl#define G_RAID3_CLASS_NAME "RAID3" 36149871Sscottl 37149871Sscottl#define G_RAID3_MAGIC "GEOM::RAID3" 38149871Sscottl#define G_RAID3_VERSION 0 39136849Sscottl 40149871Sscottl#define G_RAID3_DISK_FLAG_DIRTY 0x0000000000000001ULL 41149871Sscottl#define G_RAID3_DISK_FLAG_SYNCHRONIZING 0x0000000000000002ULL 42149871Sscottl#define G_RAID3_DISK_FLAG_FORCE_SYNC 0x0000000000000004ULL 43149871Sscottl#define G_RAID3_DISK_FLAG_HARDCODED 0x0000000000000008ULL 44149871Sscottl#define G_RAID3_DISK_FLAG_MASK (G_RAID3_DISK_FLAG_DIRTY | \ 45149871Sscottl G_RAID3_DISK_FLAG_SYNCHRONIZING | \ 46149871Sscottl G_RAID3_DISK_FLAG_FORCE_SYNC) 47149871Sscottl 48149871Sscottl#define G_RAID3_DEVICE_FLAG_NOAUTOSYNC 0x0000000000000001ULL 49149871Sscottl#define G_RAID3_DEVICE_FLAG_MASK (G_RAID3_DEVICE_FLAG_NOAUTOSYNC) 50149871Sscottl 51149871Sscottl#ifdef _KERNEL 52136849Sscottlextern u_int g_raid3_debug; 53136849Sscottl 54136849Sscottl#define G_RAID3_DEBUG(lvl, ...) do { \ 55136849Sscottl if (g_raid3_debug >= (lvl)) { \ 56136849Sscottl printf("GEOM_RAID3"); \ 57149871Sscottl if (g_raid3_debug > 0) \ 58136849Sscottl printf("[%u]", lvl); \ 59136849Sscottl printf(": "); \ 60149871Sscottl printf(__VA_ARGS__); \ 61149871Sscottl printf("\n"); \ 62136849Sscottl } \ 63136849Sscottl} while (0) 64136849Sscottl#define G_RAID3_LOGREQ(lvl, bp, ...) do { \ 65136849Sscottl if (g_raid3_debug >= (lvl)) { \ 66149871Sscottl printf("GEOM_RAID3"); \ 67149871Sscottl if (g_raid3_debug > 0) \ 68136849Sscottl printf("[%u]", lvl); \ 69136849Sscottl printf(": "); \ 70136849Sscottl printf(__VA_ARGS__); \ 71136849Sscottl printf(" "); \ 72136849Sscottl g_print_bio(bp); \ 73149871Sscottl printf("\n"); \ 74136849Sscottl } \ 75136849Sscottl} while (0) 76149871Sscottl 77149871Sscottl#define G_RAID3_MAX_IO_SIZE (DFLTPHYS * 2) 78136849Sscottl 79149871Sscottl#define G_RAID3_BIO_CFLAG_REGULAR 0x01 80136849Sscottl#define G_RAID3_BIO_CFLAG_SYNC 0x02 81136849Sscottl#define G_RAID3_BIO_CFLAG_PARITY 0x04 82149871Sscottl#define G_RAID3_BIO_CFLAG_NODISK 0x08 83149871Sscottl#define G_RAID3_BIO_CFLAG_REGSYNC 0x10 84136849Sscottl 85149871Sscottl#define G_RAID3_BIO_PFLAG_DEGRADED 0x01 86136849Sscottl#define G_RAID3_BIO_PFLAG_NOPARITY 0x02 87136849Sscottl 88149871Sscottl/* 89149871Sscottl * Informations needed for synchronization. 90136849Sscottl */ 91149871Sscottlstruct g_raid3_disk_sync { 92136849Sscottl struct g_consumer *ds_consumer; /* Consumer connected to our device. */ 93136849Sscottl off_t ds_offset; /* Offset of next request to send. */ 94136849Sscottl off_t ds_offset_done; /* Offset of already synchronized 95136849Sscottl region. */ 96136849Sscottl u_int ds_syncid; /* Disk's synchronization ID. */ 97136849Sscottl u_char *ds_data; 98136849Sscottl}; 99136849Sscottl 100136849Sscottl/* 101136849Sscottl * Informations needed for synchronization. 102136849Sscottl */ 103136849Sscottlstruct g_raid3_device_sync { 104136849Sscottl struct g_geom *ds_geom; /* Synchronization geom. */ 105136849Sscottl}; 106149871Sscottl 107149871Sscottl#define G_RAID3_DISK_STATE_NODISK 0 108136849Sscottl#define G_RAID3_DISK_STATE_NONE 1 109136849Sscottl#define G_RAID3_DISK_STATE_NEW 2 110136849Sscottl#define G_RAID3_DISK_STATE_ACTIVE 3 111136849Sscottl#define G_RAID3_DISK_STATE_STALE 4 112149871Sscottl#define G_RAID3_DISK_STATE_SYNCHRONIZING 5 113149871Sscottl#define G_RAID3_DISK_STATE_DISCONNECTED 6 114136849Sscottl#define G_RAID3_DISK_STATE_DESTROY 7 115136849Sscottlstruct g_raid3_disk { 116136849Sscottl u_int d_no; /* Disk number. */ 117136849Sscottl struct g_consumer *d_consumer; /* Consumer. */ 118149871Sscottl struct g_raid3_softc *d_softc; /* Back-pointer to softc. */ 119149871Sscottl int d_state; /* Disk state. */ 120136849Sscottl uint64_t d_flags; /* Additional flags. */ 121136849Sscottl struct g_raid3_disk_sync d_sync; /* Sync information. */ 122136849Sscottl LIST_ENTRY(g_raid3_disk) d_next; 123136849Sscottl}; 124149871Sscottl#define d_name d_consumer->provider->name 125149871Sscottl 126136849Sscottl#define G_RAID3_EVENT_DONTWAIT 0x1 127136849Sscottl#define G_RAID3_EVENT_WAIT 0x2 128136849Sscottl#define G_RAID3_EVENT_DEVICE 0x4 129#define G_RAID3_EVENT_DONE 0x8 130struct g_raid3_event { 131 struct g_raid3_disk *e_disk; 132 int e_state; 133 int e_flags; 134 int e_error; 135 TAILQ_ENTRY(g_raid3_event) e_next; 136}; 137 138#define G_RAID3_DEVICE_FLAG_DESTROY 0x0100000000000000ULL 139#define G_RAID3_DEVICE_FLAG_WAIT 0x0200000000000000ULL 140 141#define G_RAID3_DEVICE_STATE_STARTING 0 142#define G_RAID3_DEVICE_STATE_DEGRADED 1 143#define G_RAID3_DEVICE_STATE_COMPLETE 2 144 145#define G_RAID3_BUMP_ON_FIRST_WRITE 1 146#define G_RAID3_BUMP_IMMEDIATELY 2 147 148struct g_raid3_softc { 149 u_int sc_state; /* Device state. */ 150 uint64_t sc_mediasize; /* Device size. */ 151 uint32_t sc_sectorsize; /* Sector size. */ 152 uint64_t sc_flags; /* Additional flags. */ 153 154 struct g_geom *sc_geom; 155 struct g_provider *sc_provider; 156 157 uint32_t sc_id; /* Device unique ID. */ 158 159 struct bio_queue_head sc_queue; 160 struct mtx sc_queue_mtx; 161 struct proc *sc_worker; 162 163 struct g_raid3_disk *sc_disks; 164 u_int sc_ndisks; /* Number of disks. */ 165 struct g_raid3_disk *sc_syncdisk; 166 167 uma_zone_t sc_zone_64k; 168 uma_zone_t sc_zone_16k; 169 uma_zone_t sc_zone_4k; 170 171 u_int sc_syncid; /* Synchronization ID. */ 172 int sc_bump_syncid; 173 struct g_raid3_device_sync sc_sync; 174 175 TAILQ_HEAD(, g_raid3_event) sc_events; 176 struct mtx sc_events_mtx; 177 178 struct callout sc_callout; 179}; 180#define sc_name sc_geom->name 181 182const char *g_raid3_get_diskname(struct g_raid3_disk *disk); 183u_int g_raid3_ndisks(struct g_raid3_softc *sc, int state); 184int g_raid3_destroy(struct g_raid3_softc *sc, boolean_t force); 185int g_raid3_event_send(void *arg, int state, int flags); 186struct g_raid3_metadata; 187void g_raid3_fill_metadata(struct g_raid3_disk *disk, 188 struct g_raid3_metadata *md); 189int g_raid3_clear_metadata(struct g_raid3_disk *disk); 190void g_raid3_update_metadata(struct g_raid3_disk *disk); 191 192g_ctl_req_t g_raid3_config; 193#endif /* _KERNEL */ 194 195struct g_raid3_metadata { 196 char md_magic[16]; /* Magic value. */ 197 uint32_t md_version; /* Version number. */ 198 char md_name[16]; /* Device name. */ 199 uint32_t md_id; /* Device unique ID. */ 200 uint16_t md_no; /* Component number. */ 201 uint16_t md_all; /* Number of disks in device. */ 202 uint32_t md_syncid; /* Synchronization ID. */ 203 uint64_t md_mediasize; /* Size of whole device. */ 204 uint32_t md_sectorsize; /* Sector size. */ 205 uint64_t md_sync_offset; /* Synchronized offset. */ 206 uint64_t md_mflags; /* Additional device flags. */ 207 uint64_t md_dflags; /* Additional disk flags. */ 208 char md_provider[16]; /* Hardcoded provider. */ 209 u_char md_hash[16]; /* MD5 hash. */ 210}; 211static __inline void 212raid3_metadata_encode(struct g_raid3_metadata *md, u_char *data) 213{ 214 MD5_CTX ctx; 215 216 bcopy(md->md_magic, data, 16); 217 le32enc(data + 16, md->md_version); 218 bcopy(md->md_name, data + 20, 16); 219 le32enc(data + 36, md->md_id); 220 le16enc(data + 40, md->md_no); 221 le16enc(data + 42, md->md_all); 222 le32enc(data + 44, md->md_syncid); 223 le64enc(data + 48, md->md_mediasize); 224 le32enc(data + 56, md->md_sectorsize); 225 le64enc(data + 60, md->md_sync_offset); 226 le64enc(data + 68, md->md_mflags); 227 le64enc(data + 76, md->md_dflags); 228 bcopy(md->md_provider, data + 84, 16); 229 MD5Init(&ctx); 230 MD5Update(&ctx, data, 100); 231 MD5Final(md->md_hash, &ctx); 232 bcopy(md->md_hash, data + 100, 16); 233} 234static __inline int 235raid3_metadata_decode(const u_char *data, struct g_raid3_metadata *md) 236{ 237 MD5_CTX ctx; 238 239 bcopy(data, md->md_magic, 16); 240 md->md_version = le32dec(data + 16); 241 bcopy(data + 20, md->md_name, 16); 242 md->md_id = le32dec(data + 36); 243 md->md_no = le16dec(data + 40); 244 md->md_all = le16dec(data + 42); 245 md->md_syncid = le32dec(data + 44); 246 md->md_mediasize = le64dec(data + 48); 247 md->md_sectorsize = le32dec(data + 56); 248 md->md_sync_offset = le64dec(data + 60); 249 md->md_mflags = le64dec(data + 68); 250 md->md_dflags = le64dec(data + 76); 251 bcopy(data + 84, md->md_provider, 16); 252 bcopy(data + 100, md->md_hash, 16); 253 MD5Init(&ctx); 254 MD5Update(&ctx, data, 100); 255 MD5Final(md->md_hash, &ctx); 256 if (bcmp(md->md_hash, data + 100, 16) != 0) 257 return (EINVAL); 258 return (0); 259} 260 261static __inline void 262raid3_metadata_dump(const struct g_raid3_metadata *md) 263{ 264 static const char hex[] = "0123456789abcdef"; 265 char hash[16 * 2 + 1]; 266 u_int i; 267 268 printf(" magic: %s\n", md->md_magic); 269 printf(" version: %u\n", (u_int)md->md_version); 270 printf(" name: %s\n", md->md_name); 271 printf(" id: %u\n", (u_int)md->md_id); 272 printf(" no: %u\n", (u_int)md->md_no); 273 printf(" all: %u\n", (u_int)md->md_all); 274 printf(" syncid: %u\n", (u_int)md->md_syncid); 275 printf(" mediasize: %jd\n", (intmax_t)md->md_mediasize); 276 printf("sectorsize: %u\n", (u_int)md->md_sectorsize); 277 printf("syncoffset: %jd\n", (intmax_t)md->md_sync_offset); 278 printf(" mflags:"); 279 if (md->md_mflags == 0) 280 printf(" NONE"); 281 else { 282 if ((md->md_mflags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0) 283 printf(" NOAUTOSYNC"); 284 } 285 printf("\n"); 286 printf(" dflags:"); 287 if (md->md_dflags == 0) 288 printf(" NONE"); 289 else { 290 if ((md->md_dflags & G_RAID3_DISK_FLAG_DIRTY) != 0) 291 printf(" DIRTY"); 292 if ((md->md_dflags & G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0) 293 printf(" SYNCHRONIZING"); 294 if ((md->md_dflags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) 295 printf(" FORCE_SYNC"); 296 } 297 printf("\n"); 298 printf("hcprovider: %s\n", md->md_provider); 299 bzero(hash, sizeof(hash)); 300 for (i = 0; i < 16; i++) { 301 hash[i * 2] = hex[md->md_hash[i] >> 4]; 302 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f]; 303 } 304 printf(" MD5 hash: %s\n", hash); 305} 306#endif /* !_G_RAID3_H_ */ 307