/*- * Copyright (c) 2004 Pawel Jakub Dawidek * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _G_MIRROR_H_ #define _G_MIRROR_H_ #include #define G_MIRROR_CLASS_NAME "MIRROR" #define G_MIRROR_MAGIC "GEOM::MIRROR" #define G_MIRROR_VERSION 0 #define G_MIRROR_BALANCE_NONE 0 #define G_MIRROR_BALANCE_ROUND_ROBIN 1 #define G_MIRROR_BALANCE_SMART 2 #define G_MIRROR_BALANCE_MIN G_MIRROR_BALANCE_NONE #define G_MIRROR_BALANCE_MAX G_MIRROR_BALANCE_SMART #define G_MIRROR_DISK_FLAG_SYNCHRONIZING 0x0000000000000001ULL #define G_MIRROR_DISK_FLAG_INACTIVE 0x0000000000000002ULL #define G_MIRROR_DISK_FLAG_MASK 0x00000000ffffffffULL #define G_MIRROR_DEVICE_FLAG_MASK 0xffffffff00000000ULL #define G_MIRROR_OPT_BALANCE 0x01 #define G_MIRROR_OPT_SLICE 0x02 #define G_MIRROR_OPT_QUARANTINE 0x04 #define G_MIRROR_OPT_SUSPECTS 0x08 #ifdef _KERNEL #define G_MIRROR_TYPE_MANUAL 0 #define G_MIRROR_TYPE_AUTOMATIC 1 #define G_MIRROR_DEBUG(lvl, ...) do { \ if (g_mirror_debug >= (lvl)) { \ printf("GEOM_MIRROR"); \ if (g_mirror_debug > 0) \ printf("[%u]", lvl); \ printf(": "); \ printf(__VA_ARGS__); \ printf("\n"); \ } \ } while (0) #define G_MIRROR_LOGREQ(bp, ...) do { \ if (g_mirror_debug >= 2) { \ printf("GEOM_MIRROR[2]: "); \ printf(__VA_ARGS__); \ printf(" "); \ g_print_bio(bp); \ printf("\n"); \ } \ } while (0) /* * Informations needed for synchronization. */ struct g_mirror_disk_sync { struct g_consumer *ds_consumer; off_t ds_offset; off_t ds_offset_done; uint16_t ds_syncid; }; /* * Informations needed for synchronization. */ struct g_mirror_device_sync { struct g_geom *ds_geom; size_t ds_block; int ds_bits; uma_zone_t ds_zone; }; #define G_MIRROR_DISK_STATE_NONE 0 #define G_MIRROR_DISK_STATE_NEW 1 #define G_MIRROR_DISK_STATE_ACTIVE 2 #define G_MIRROR_DISK_STATE_SYNCHRONIZING 3 #define G_MIRROR_DISK_STATE_DISCONNECTED 4 #define G_MIRROR_DISK_STATE_DESTROY 5 struct g_mirror_disk { uint32_t d_id; /* Disk ID. */ struct g_consumer *d_consumer; /* Consumer. */ uint32_t d_count; /* Number of pending requests. */ struct g_mirror_softc *d_softc; /* Back-pointer to softc. */ struct proc *d_worker; int d_state; /* Disk state. */ uint64_t d_flags; /* Additional flags. */ struct g_mirror_disk_sync d_sync;/* Sync information. */ LIST_ENTRY(g_mirror_disk) d_next; }; #define d_name d_consumer->provider->name #define G_MIRROR_EVENT_DONTWAIT 0x1 #define G_MIRROR_EVENT_WAIT 0x2 #define G_MIRROR_EVENT_DEVICE 0x4 #define G_MIRROR_EVENT_DONE 0x8 struct g_mirror_event { struct g_mirror_disk *e_disk; int e_state; int e_flags; int e_error; TAILQ_ENTRY(g_mirror_event) e_next; }; #define G_MIRROR_DEVICE_FLAG_DESTROY 0x0001 #define G_MIRROR_DEVICE_FLAG_WAIT 0x0002 #define G_MIRROR_DEVICE_STATE_STARTING 0 #define G_MIRROR_DEVICE_STATE_RUNNING 1 struct g_mirror_softc { u_int sc_state; /* Device state. */ uint32_t sc_slice; /* Slice size. */ uint8_t sc_balance; /* Balance algorithm. */ uint64_t sc_mediasize; /* Device size. */ uint32_t sc_sectorsize; /* Sector size. */ uint64_t sc_flags; /* Additional flags. */ struct g_geom *sc_geom; struct g_provider *sc_provider; char sc_name[16]; /* Mirror name. */ uint32_t sc_id; /* Mirror unique ID. */ struct bio_queue_head sc_queue; struct mtx sc_queue_mtx; struct proc *sc_worker; LIST_HEAD(, g_mirror_disk) sc_disks; u_int sc_ndisks; /* Number of disks. */ struct g_mirror_disk *sc_hint; uint16_t sc_syncid; /* Synchronization ID. */ int sc_bump_syncid; /* Bump syncid on first write. */ struct g_mirror_device_sync sc_sync; TAILQ_HEAD(, g_mirror_event) sc_events; struct mtx sc_events_mtx; struct callout sc_callout; }; /* XXX: Temporary. */ extern int g_mirror_stop; u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state); int g_mirror_destroy(struct g_mirror_softc *sc, boolean_t force); int g_mirror_event_send(void *arg, int state, int flags); struct g_mirror_metadata; void g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk, struct g_mirror_metadata *md); void g_mirror_update_metadata(struct g_mirror_disk *disk); g_ctl_req_t g_mirror_config; #endif /* _KERNEL */ struct g_mirror_metadata { char md_magic[16]; /* Magic value. */ uint32_t md_version; /* Version number. */ char md_name[16]; /* Mirror name. */ uint32_t md_mid; /* Mirror unique ID. */ uint32_t md_did; /* Disk unique ID. */ uint16_t md_all; /* Number of disks in mirror. */ uint16_t md_syncid; /* Synchronization ID. */ uint32_t md_slice; /* Slice size. */ uint8_t md_balance; /* Balance type. */ uint64_t md_mediasize; /* Size of the smallest disk in mirror. */ uint32_t md_sectorsize; /* Sector size. */ uint64_t md_sync_offset; /* Synchronized offset. */ uint64_t md_flags; /* Additional flags. */ }; static __inline void mirror_metadata_encode(const struct g_mirror_metadata *md, u_char *data) { bcopy(md->md_magic, data, 16); le32enc(data + 16, md->md_version); bcopy(md->md_name, data + 20, 16); le32enc(data + 36, md->md_mid); le32enc(data + 40, md->md_did); le16enc(data + 44, md->md_all); le16enc(data + 46, md->md_syncid); le32enc(data + 48, md->md_slice); *(data + 52) = md->md_balance; le64enc(data + 53, md->md_mediasize); le32enc(data + 61, md->md_sectorsize); le64enc(data + 65, md->md_sync_offset); le64enc(data + 73, md->md_flags); } static __inline void mirror_metadata_decode(const u_char *data, struct g_mirror_metadata *md) { bcopy(data, md->md_magic, 16); md->md_version = le32dec(data + 16); bcopy(data + 20, md->md_name, 16); md->md_mid = le32dec(data + 36); md->md_did = le32dec(data + 40); md->md_all = le16dec(data + 44); md->md_syncid = le16dec(data + 46); md->md_slice = le32dec(data + 48); md->md_balance = *(data + 52); md->md_mediasize = le64dec(data + 53); md->md_sectorsize = le64dec(data + 61); md->md_sync_offset = le64dec(data + 65); md->md_flags = le64dec(data + 73); } static __inline const char * balance_name(u_int balance) { static const char *algorithms[] = { [G_MIRROR_BALANCE_NONE] = "none", [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin", [G_MIRROR_BALANCE_SMART] = "smart", [G_MIRROR_BALANCE_MAX + 1] = "unknown" }; if (balance > G_MIRROR_BALANCE_MAX) balance = G_MIRROR_BALANCE_MAX + 1; return (algorithms[balance]); } static __inline int balance_id(const char *name) { static const char *algorithms[] = { [G_MIRROR_BALANCE_NONE] = "none", [G_MIRROR_BALANCE_ROUND_ROBIN] = "round-robin", [G_MIRROR_BALANCE_SMART] = "smart" }; int n; for (n = G_MIRROR_BALANCE_MIN; n <= G_MIRROR_BALANCE_MAX; n++) { if (strcmp(name, algorithms[n]) == 0) return (n); } return (-1); } #endif /* !_G_MIRROR_H_ */