189b17223SAlexander Motin /*- 23728855aSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 33728855aSPedro F. Giffuni * 489b17223SAlexander Motin * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org> 5e26083caSAlexander Motin * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org> 689b17223SAlexander Motin * All rights reserved. 789b17223SAlexander Motin * 889b17223SAlexander Motin * Redistribution and use in source and binary forms, with or without 989b17223SAlexander Motin * modification, are permitted provided that the following conditions 1089b17223SAlexander Motin * are met: 1189b17223SAlexander Motin * 1. Redistributions of source code must retain the above copyright 1289b17223SAlexander Motin * notice, this list of conditions and the following disclaimer. 1389b17223SAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 1489b17223SAlexander Motin * notice, this list of conditions and the following disclaimer in the 1589b17223SAlexander Motin * documentation and/or other materials provided with the distribution. 1689b17223SAlexander Motin * 1789b17223SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 1889b17223SAlexander Motin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1989b17223SAlexander Motin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2089b17223SAlexander Motin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 2189b17223SAlexander Motin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2289b17223SAlexander Motin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2389b17223SAlexander Motin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2489b17223SAlexander Motin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2589b17223SAlexander Motin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2689b17223SAlexander Motin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2789b17223SAlexander Motin * SUCH DAMAGE. 2889b17223SAlexander Motin */ 2989b17223SAlexander Motin 3089b17223SAlexander Motin #include <sys/cdefs.h> 3189b17223SAlexander Motin __FBSDID("$FreeBSD$"); 3289b17223SAlexander Motin 3389b17223SAlexander Motin #include <sys/param.h> 3489b17223SAlexander Motin #include <sys/bio.h> 3589b17223SAlexander Motin #include <sys/endian.h> 3689b17223SAlexander Motin #include <sys/kernel.h> 3789b17223SAlexander Motin #include <sys/kobj.h> 3889b17223SAlexander Motin #include <sys/limits.h> 3989b17223SAlexander Motin #include <sys/lock.h> 4089b17223SAlexander Motin #include <sys/malloc.h> 4189b17223SAlexander Motin #include <sys/mutex.h> 4289b17223SAlexander Motin #include <sys/systm.h> 4389b17223SAlexander Motin #include <geom/geom.h> 44ac03832eSConrad Meyer #include <geom/geom_dbg.h> 4589b17223SAlexander Motin #include "geom/raid/g_raid.h" 4689b17223SAlexander Motin #include "g_raid_md_if.h" 4789b17223SAlexander Motin 4889b17223SAlexander Motin static MALLOC_DEFINE(M_MD_PROMISE, "md_promise_data", "GEOM_RAID Promise metadata"); 4989b17223SAlexander Motin 5089b17223SAlexander Motin #define PROMISE_MAX_DISKS 8 5189b17223SAlexander Motin #define PROMISE_MAX_SUBDISKS 2 5289b17223SAlexander Motin #define PROMISE_META_OFFSET 14 5389b17223SAlexander Motin 5489b17223SAlexander Motin struct promise_raid_disk { 5589b17223SAlexander Motin uint8_t flags; /* Subdisk status. */ 5689b17223SAlexander Motin #define PROMISE_F_VALID 0x01 5789b17223SAlexander Motin #define PROMISE_F_ONLINE 0x02 5889b17223SAlexander Motin #define PROMISE_F_ASSIGNED 0x04 5989b17223SAlexander Motin #define PROMISE_F_SPARE 0x08 6089b17223SAlexander Motin #define PROMISE_F_DUPLICATE 0x10 6189b17223SAlexander Motin #define PROMISE_F_REDIR 0x20 6289b17223SAlexander Motin #define PROMISE_F_DOWN 0x40 6389b17223SAlexander Motin #define PROMISE_F_READY 0x80 6489b17223SAlexander Motin 6589b17223SAlexander Motin uint8_t number; /* Position in a volume. */ 6689b17223SAlexander Motin uint8_t channel; /* ATA channel number. */ 6789b17223SAlexander Motin uint8_t device; /* ATA device number. */ 6889b17223SAlexander Motin uint64_t id __packed; /* Subdisk ID. */ 6989b17223SAlexander Motin } __packed; 7089b17223SAlexander Motin 7189b17223SAlexander Motin struct promise_raid_conf { 7289b17223SAlexander Motin char promise_id[24]; 7389b17223SAlexander Motin #define PROMISE_MAGIC "Promise Technology, Inc." 7489b17223SAlexander Motin #define FREEBSD_MAGIC "FreeBSD ATA driver RAID " 7589b17223SAlexander Motin 7689b17223SAlexander Motin uint32_t dummy_0; 7789b17223SAlexander Motin uint64_t magic_0; 7889b17223SAlexander Motin #define PROMISE_MAGIC0(x) (((uint64_t)(x.channel) << 48) | \ 7989b17223SAlexander Motin ((uint64_t)(x.device != 0) << 56)) 8089b17223SAlexander Motin uint16_t magic_1; 8189b17223SAlexander Motin uint32_t magic_2; 8289b17223SAlexander Motin uint8_t filler1[470]; 8389b17223SAlexander Motin 8489b17223SAlexander Motin uint32_t integrity; 8589b17223SAlexander Motin #define PROMISE_I_VALID 0x00000080 8689b17223SAlexander Motin 8789b17223SAlexander Motin struct promise_raid_disk disk; /* This subdisk info. */ 8889b17223SAlexander Motin uint32_t disk_offset; /* Subdisk offset. */ 8989b17223SAlexander Motin uint32_t disk_sectors; /* Subdisk size */ 90821a0f63SAlexander Motin uint32_t disk_rebuild; /* Rebuild position. */ 9189b17223SAlexander Motin uint16_t generation; /* Generation number. */ 9289b17223SAlexander Motin uint8_t status; /* Volume status. */ 9389b17223SAlexander Motin #define PROMISE_S_VALID 0x01 9489b17223SAlexander Motin #define PROMISE_S_ONLINE 0x02 9589b17223SAlexander Motin #define PROMISE_S_INITED 0x04 9689b17223SAlexander Motin #define PROMISE_S_READY 0x08 9789b17223SAlexander Motin #define PROMISE_S_DEGRADED 0x10 9889b17223SAlexander Motin #define PROMISE_S_MARKED 0x20 9989b17223SAlexander Motin #define PROMISE_S_MIGRATING 0x40 10089b17223SAlexander Motin #define PROMISE_S_FUNCTIONAL 0x80 10189b17223SAlexander Motin 10289b17223SAlexander Motin uint8_t type; /* Voluem type. */ 10389b17223SAlexander Motin #define PROMISE_T_RAID0 0x00 10489b17223SAlexander Motin #define PROMISE_T_RAID1 0x01 10589b17223SAlexander Motin #define PROMISE_T_RAID3 0x02 10689b17223SAlexander Motin #define PROMISE_T_RAID5 0x04 10789b17223SAlexander Motin #define PROMISE_T_SPAN 0x08 10889b17223SAlexander Motin #define PROMISE_T_JBOD 0x10 10989b17223SAlexander Motin 11089b17223SAlexander Motin uint8_t total_disks; /* Disks in this volume. */ 11189b17223SAlexander Motin uint8_t stripe_shift; /* Strip size. */ 11289b17223SAlexander Motin uint8_t array_width; /* Number of RAID0 stripes. */ 11389b17223SAlexander Motin uint8_t array_number; /* Global volume number. */ 11489b17223SAlexander Motin uint32_t total_sectors; /* Volume size. */ 11589b17223SAlexander Motin uint16_t cylinders; /* Volume geometry: C. */ 11689b17223SAlexander Motin uint8_t heads; /* Volume geometry: H. */ 11789b17223SAlexander Motin uint8_t sectors; /* Volume geometry: S. */ 11889b17223SAlexander Motin uint64_t volume_id __packed; /* Volume ID, */ 11989b17223SAlexander Motin struct promise_raid_disk disks[PROMISE_MAX_DISKS]; 12089b17223SAlexander Motin /* Subdisks in this volume. */ 12189b17223SAlexander Motin char name[32]; /* Volume label. */ 12289b17223SAlexander Motin 12389b17223SAlexander Motin uint32_t filler2[8]; 12489b17223SAlexander Motin uint32_t magic_3; /* Something related to rebuild. */ 12589b17223SAlexander Motin uint64_t rebuild_lba64; /* Per-volume rebuild position. */ 12689b17223SAlexander Motin uint32_t magic_4; 12789b17223SAlexander Motin uint32_t magic_5; 128733a1f3fSAlexander Motin uint32_t total_sectors_high; 129821a0f63SAlexander Motin uint8_t magic_6; 130821a0f63SAlexander Motin uint8_t sector_size; 131821a0f63SAlexander Motin uint16_t magic_7; 132c3ec009aSAlexander Motin uint32_t magic_8[31]; 133c3ec009aSAlexander Motin uint32_t backup_time; 134821a0f63SAlexander Motin uint16_t magic_9; 135821a0f63SAlexander Motin uint32_t disk_offset_high; 136821a0f63SAlexander Motin uint32_t disk_sectors_high; 137821a0f63SAlexander Motin uint32_t disk_rebuild_high; 138821a0f63SAlexander Motin uint16_t magic_10; 139821a0f63SAlexander Motin uint32_t magic_11[3]; 140821a0f63SAlexander Motin uint32_t filler3[284]; 14189b17223SAlexander Motin uint32_t checksum; 14289b17223SAlexander Motin } __packed; 14389b17223SAlexander Motin 14489b17223SAlexander Motin struct g_raid_md_promise_perdisk { 14589b17223SAlexander Motin int pd_updated; 14689b17223SAlexander Motin int pd_subdisks; 14789b17223SAlexander Motin struct promise_raid_conf *pd_meta[PROMISE_MAX_SUBDISKS]; 14889b17223SAlexander Motin }; 14989b17223SAlexander Motin 15089b17223SAlexander Motin struct g_raid_md_promise_pervolume { 15189b17223SAlexander Motin struct promise_raid_conf *pv_meta; 15289b17223SAlexander Motin uint64_t pv_id; 15389b17223SAlexander Motin uint16_t pv_generation; 15489b17223SAlexander Motin int pv_disks_present; 15589b17223SAlexander Motin int pv_started; 15689b17223SAlexander Motin struct callout pv_start_co; /* STARTING state timer. */ 15789b17223SAlexander Motin }; 15889b17223SAlexander Motin 15989b17223SAlexander Motin static g_raid_md_create_t g_raid_md_create_promise; 16089b17223SAlexander Motin static g_raid_md_taste_t g_raid_md_taste_promise; 16189b17223SAlexander Motin static g_raid_md_event_t g_raid_md_event_promise; 16289b17223SAlexander Motin static g_raid_md_volume_event_t g_raid_md_volume_event_promise; 16389b17223SAlexander Motin static g_raid_md_ctl_t g_raid_md_ctl_promise; 16489b17223SAlexander Motin static g_raid_md_write_t g_raid_md_write_promise; 16589b17223SAlexander Motin static g_raid_md_fail_disk_t g_raid_md_fail_disk_promise; 16689b17223SAlexander Motin static g_raid_md_free_disk_t g_raid_md_free_disk_promise; 16789b17223SAlexander Motin static g_raid_md_free_volume_t g_raid_md_free_volume_promise; 16889b17223SAlexander Motin static g_raid_md_free_t g_raid_md_free_promise; 16989b17223SAlexander Motin 17089b17223SAlexander Motin static kobj_method_t g_raid_md_promise_methods[] = { 17189b17223SAlexander Motin KOBJMETHOD(g_raid_md_create, g_raid_md_create_promise), 17289b17223SAlexander Motin KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_promise), 17389b17223SAlexander Motin KOBJMETHOD(g_raid_md_event, g_raid_md_event_promise), 17489b17223SAlexander Motin KOBJMETHOD(g_raid_md_volume_event, g_raid_md_volume_event_promise), 17589b17223SAlexander Motin KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_promise), 17689b17223SAlexander Motin KOBJMETHOD(g_raid_md_write, g_raid_md_write_promise), 17789b17223SAlexander Motin KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_promise), 17889b17223SAlexander Motin KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_promise), 17989b17223SAlexander Motin KOBJMETHOD(g_raid_md_free_volume, g_raid_md_free_volume_promise), 18089b17223SAlexander Motin KOBJMETHOD(g_raid_md_free, g_raid_md_free_promise), 18189b17223SAlexander Motin { 0, 0 } 18289b17223SAlexander Motin }; 18389b17223SAlexander Motin 18489b17223SAlexander Motin static struct g_raid_md_class g_raid_md_promise_class = { 18589b17223SAlexander Motin "Promise", 18689b17223SAlexander Motin g_raid_md_promise_methods, 18789b17223SAlexander Motin sizeof(struct g_raid_md_object), 188c89d2fbeSAlexander Motin .mdc_enable = 1, 18989b17223SAlexander Motin .mdc_priority = 100 19089b17223SAlexander Motin }; 19189b17223SAlexander Motin 19289b17223SAlexander Motin static void 19389b17223SAlexander Motin g_raid_md_promise_print(struct promise_raid_conf *meta) 19489b17223SAlexander Motin { 19589b17223SAlexander Motin int i; 19689b17223SAlexander Motin 19789b17223SAlexander Motin if (g_raid_debug < 1) 19889b17223SAlexander Motin return; 19989b17223SAlexander Motin 20089b17223SAlexander Motin printf("********* ATA Promise Metadata *********\n"); 20189b17223SAlexander Motin printf("promise_id <%.24s>\n", meta->promise_id); 20289b17223SAlexander Motin printf("disk %02x %02x %02x %02x %016jx\n", 20389b17223SAlexander Motin meta->disk.flags, meta->disk.number, meta->disk.channel, 20489b17223SAlexander Motin meta->disk.device, meta->disk.id); 20589b17223SAlexander Motin printf("disk_offset %u\n", meta->disk_offset); 20689b17223SAlexander Motin printf("disk_sectors %u\n", meta->disk_sectors); 207821a0f63SAlexander Motin printf("disk_rebuild %u\n", meta->disk_rebuild); 20889b17223SAlexander Motin printf("generation %u\n", meta->generation); 20989b17223SAlexander Motin printf("status 0x%02x\n", meta->status); 21089b17223SAlexander Motin printf("type %u\n", meta->type); 21189b17223SAlexander Motin printf("total_disks %u\n", meta->total_disks); 21289b17223SAlexander Motin printf("stripe_shift %u\n", meta->stripe_shift); 21389b17223SAlexander Motin printf("array_width %u\n", meta->array_width); 21489b17223SAlexander Motin printf("array_number %u\n", meta->array_number); 21589b17223SAlexander Motin printf("total_sectors %u\n", meta->total_sectors); 21689b17223SAlexander Motin printf("cylinders %u\n", meta->cylinders); 21789b17223SAlexander Motin printf("heads %u\n", meta->heads); 21889b17223SAlexander Motin printf("sectors %u\n", meta->sectors); 21989b17223SAlexander Motin printf("volume_id 0x%016jx\n", meta->volume_id); 22089b17223SAlexander Motin printf("disks:\n"); 22189b17223SAlexander Motin for (i = 0; i < PROMISE_MAX_DISKS; i++ ) { 22289b17223SAlexander Motin printf(" %02x %02x %02x %02x %016jx\n", 22389b17223SAlexander Motin meta->disks[i].flags, meta->disks[i].number, 22489b17223SAlexander Motin meta->disks[i].channel, meta->disks[i].device, 22589b17223SAlexander Motin meta->disks[i].id); 22689b17223SAlexander Motin } 22789b17223SAlexander Motin printf("name <%.32s>\n", meta->name); 22889b17223SAlexander Motin printf("magic_3 0x%08x\n", meta->magic_3); 22989b17223SAlexander Motin printf("rebuild_lba64 %ju\n", meta->rebuild_lba64); 23089b17223SAlexander Motin printf("magic_4 0x%08x\n", meta->magic_4); 23189b17223SAlexander Motin printf("magic_5 0x%08x\n", meta->magic_5); 232733a1f3fSAlexander Motin printf("total_sectors_high 0x%08x\n", meta->total_sectors_high); 233821a0f63SAlexander Motin printf("sector_size %u\n", meta->sector_size); 234c3ec009aSAlexander Motin printf("backup_time %d\n", meta->backup_time); 235821a0f63SAlexander Motin printf("disk_offset_high 0x%08x\n", meta->disk_offset_high); 236821a0f63SAlexander Motin printf("disk_sectors_high 0x%08x\n", meta->disk_sectors_high); 237821a0f63SAlexander Motin printf("disk_rebuild_high 0x%08x\n", meta->disk_rebuild_high); 23889b17223SAlexander Motin printf("=================================================\n"); 23989b17223SAlexander Motin } 24089b17223SAlexander Motin 24189b17223SAlexander Motin static struct promise_raid_conf * 24289b17223SAlexander Motin promise_meta_copy(struct promise_raid_conf *meta) 24389b17223SAlexander Motin { 24489b17223SAlexander Motin struct promise_raid_conf *nmeta; 24589b17223SAlexander Motin 24689b17223SAlexander Motin nmeta = malloc(sizeof(*nmeta), M_MD_PROMISE, M_WAITOK); 24789b17223SAlexander Motin memcpy(nmeta, meta, sizeof(*nmeta)); 24889b17223SAlexander Motin return (nmeta); 24989b17223SAlexander Motin } 25089b17223SAlexander Motin 25189b17223SAlexander Motin static int 25289b17223SAlexander Motin promise_meta_find_disk(struct promise_raid_conf *meta, uint64_t id) 25389b17223SAlexander Motin { 25489b17223SAlexander Motin int pos; 25589b17223SAlexander Motin 25689b17223SAlexander Motin for (pos = 0; pos < meta->total_disks; pos++) { 25789b17223SAlexander Motin if (meta->disks[pos].id == id) 25889b17223SAlexander Motin return (pos); 25989b17223SAlexander Motin } 26089b17223SAlexander Motin return (-1); 26189b17223SAlexander Motin } 26289b17223SAlexander Motin 26389b17223SAlexander Motin static int 26489b17223SAlexander Motin promise_meta_unused_range(struct promise_raid_conf **metaarr, int nsd, 265821a0f63SAlexander Motin off_t sectors, off_t *off, off_t *size) 26689b17223SAlexander Motin { 267821a0f63SAlexander Motin off_t coff, csize, tmp; 26889b17223SAlexander Motin int i, j; 26989b17223SAlexander Motin 27089b17223SAlexander Motin sectors -= 131072; 27189b17223SAlexander Motin *off = 0; 27289b17223SAlexander Motin *size = 0; 27389b17223SAlexander Motin coff = 0; 27489b17223SAlexander Motin csize = sectors; 27589b17223SAlexander Motin i = 0; 27689b17223SAlexander Motin while (1) { 27789b17223SAlexander Motin for (j = 0; j < nsd; j++) { 278821a0f63SAlexander Motin tmp = ((off_t)metaarr[j]->disk_offset_high << 32) + 279821a0f63SAlexander Motin metaarr[j]->disk_offset; 280821a0f63SAlexander Motin if (tmp >= coff) 281821a0f63SAlexander Motin csize = MIN(csize, tmp - coff); 28289b17223SAlexander Motin } 28389b17223SAlexander Motin if (csize > *size) { 28489b17223SAlexander Motin *off = coff; 28589b17223SAlexander Motin *size = csize; 28689b17223SAlexander Motin } 28789b17223SAlexander Motin if (i >= nsd) 28889b17223SAlexander Motin break; 289821a0f63SAlexander Motin coff = ((off_t)metaarr[i]->disk_offset_high << 32) + 290821a0f63SAlexander Motin metaarr[i]->disk_offset + 291821a0f63SAlexander Motin ((off_t)metaarr[i]->disk_sectors_high << 32) + 292821a0f63SAlexander Motin metaarr[i]->disk_sectors; 29389b17223SAlexander Motin csize = sectors - coff; 29489b17223SAlexander Motin i++; 29574b8d63dSPedro F. Giffuni } 29689b17223SAlexander Motin return ((*size > 0) ? 1 : 0); 29789b17223SAlexander Motin } 29889b17223SAlexander Motin 29989b17223SAlexander Motin static int 30089b17223SAlexander Motin promise_meta_translate_disk(struct g_raid_volume *vol, int md_disk_pos) 30189b17223SAlexander Motin { 30289b17223SAlexander Motin int disk_pos, width; 30389b17223SAlexander Motin 30489b17223SAlexander Motin if (md_disk_pos >= 0 && vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) { 30589b17223SAlexander Motin width = vol->v_disks_count / 2; 30689b17223SAlexander Motin disk_pos = (md_disk_pos / width) + 30789b17223SAlexander Motin (md_disk_pos % width) * width; 30889b17223SAlexander Motin } else 30989b17223SAlexander Motin disk_pos = md_disk_pos; 31089b17223SAlexander Motin return (disk_pos); 31189b17223SAlexander Motin } 31289b17223SAlexander Motin 31389b17223SAlexander Motin static void 31489b17223SAlexander Motin promise_meta_get_name(struct promise_raid_conf *meta, char *buf) 31589b17223SAlexander Motin { 31689b17223SAlexander Motin int i; 31789b17223SAlexander Motin 31889b17223SAlexander Motin strncpy(buf, meta->name, 32); 31989b17223SAlexander Motin buf[32] = 0; 32089b17223SAlexander Motin for (i = 31; i >= 0; i--) { 32189b17223SAlexander Motin if (buf[i] > 0x20) 32289b17223SAlexander Motin break; 32389b17223SAlexander Motin buf[i] = 0; 32489b17223SAlexander Motin } 32589b17223SAlexander Motin } 32689b17223SAlexander Motin 32789b17223SAlexander Motin static void 32889b17223SAlexander Motin promise_meta_put_name(struct promise_raid_conf *meta, char *buf) 32989b17223SAlexander Motin { 33089b17223SAlexander Motin 33189b17223SAlexander Motin memset(meta->name, 0x20, 32); 33289b17223SAlexander Motin memcpy(meta->name, buf, MIN(strlen(buf), 32)); 33389b17223SAlexander Motin } 33489b17223SAlexander Motin 33589b17223SAlexander Motin static int 33689b17223SAlexander Motin promise_meta_read(struct g_consumer *cp, struct promise_raid_conf **metaarr) 33789b17223SAlexander Motin { 33889b17223SAlexander Motin struct g_provider *pp; 33989b17223SAlexander Motin struct promise_raid_conf *meta; 34089b17223SAlexander Motin char *buf; 34189b17223SAlexander Motin int error, i, subdisks; 34289b17223SAlexander Motin uint32_t checksum, *ptr; 34389b17223SAlexander Motin 34489b17223SAlexander Motin pp = cp->provider; 34589b17223SAlexander Motin subdisks = 0; 346b28ea2c2SConrad Meyer 347*9e9ba9c7SMark Johnston if (pp->sectorsize * 4 < sizeof(*meta)) 348*9e9ba9c7SMark Johnston return (subdisks); 349cd853791SKonstantin Belousov if (pp->sectorsize * 4 > maxphys) { 350b28ea2c2SConrad Meyer G_RAID_DEBUG(1, "%s: Blocksize is too big.", pp->name); 351b28ea2c2SConrad Meyer return (subdisks); 352b28ea2c2SConrad Meyer } 35389b17223SAlexander Motin next: 35489b17223SAlexander Motin /* Read metadata block. */ 35589b17223SAlexander Motin buf = g_read_data(cp, pp->mediasize - pp->sectorsize * 35689b17223SAlexander Motin (63 - subdisks * PROMISE_META_OFFSET), 35789b17223SAlexander Motin pp->sectorsize * 4, &error); 35889b17223SAlexander Motin if (buf == NULL) { 35989b17223SAlexander Motin G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).", 36089b17223SAlexander Motin pp->name, error); 36189b17223SAlexander Motin return (subdisks); 36289b17223SAlexander Motin } 36389b17223SAlexander Motin meta = (struct promise_raid_conf *)buf; 36489b17223SAlexander Motin 36589b17223SAlexander Motin /* Check if this is an Promise RAID struct */ 36689b17223SAlexander Motin if (strncmp(meta->promise_id, PROMISE_MAGIC, strlen(PROMISE_MAGIC)) && 36789b17223SAlexander Motin strncmp(meta->promise_id, FREEBSD_MAGIC, strlen(FREEBSD_MAGIC))) { 36889b17223SAlexander Motin if (subdisks == 0) 36989b17223SAlexander Motin G_RAID_DEBUG(1, 37089b17223SAlexander Motin "Promise signature check failed on %s", pp->name); 37189b17223SAlexander Motin g_free(buf); 37289b17223SAlexander Motin return (subdisks); 37389b17223SAlexander Motin } 37489b17223SAlexander Motin meta = malloc(sizeof(*meta), M_MD_PROMISE, M_WAITOK); 37589b17223SAlexander Motin memcpy(meta, buf, MIN(sizeof(*meta), pp->sectorsize * 4)); 37689b17223SAlexander Motin g_free(buf); 37789b17223SAlexander Motin 37889b17223SAlexander Motin /* Check metadata checksum. */ 37989b17223SAlexander Motin for (checksum = 0, ptr = (uint32_t *)meta, i = 0; i < 511; i++) 38089b17223SAlexander Motin checksum += *ptr++; 38189b17223SAlexander Motin if (checksum != meta->checksum) { 38289b17223SAlexander Motin G_RAID_DEBUG(1, "Promise checksum check failed on %s", pp->name); 38389b17223SAlexander Motin free(meta, M_MD_PROMISE); 38489b17223SAlexander Motin return (subdisks); 38589b17223SAlexander Motin } 38689b17223SAlexander Motin 38789b17223SAlexander Motin if ((meta->integrity & PROMISE_I_VALID) == 0) { 38889b17223SAlexander Motin G_RAID_DEBUG(1, "Promise metadata is invalid on %s", pp->name); 38989b17223SAlexander Motin free(meta, M_MD_PROMISE); 39089b17223SAlexander Motin return (subdisks); 39189b17223SAlexander Motin } 39289b17223SAlexander Motin 39389b17223SAlexander Motin if (meta->total_disks > PROMISE_MAX_DISKS) { 39489b17223SAlexander Motin G_RAID_DEBUG(1, "Wrong number of disks on %s (%d)", 39589b17223SAlexander Motin pp->name, meta->total_disks); 39689b17223SAlexander Motin free(meta, M_MD_PROMISE); 39789b17223SAlexander Motin return (subdisks); 39889b17223SAlexander Motin } 39989b17223SAlexander Motin 400821a0f63SAlexander Motin /* Remove filler garbage from fields used in newer metadata. */ 401821a0f63SAlexander Motin if (meta->disk_offset_high == 0x8b8c8d8e && 402821a0f63SAlexander Motin meta->disk_sectors_high == 0x8788898a && 403821a0f63SAlexander Motin meta->disk_rebuild_high == 0x83848586) { 404821a0f63SAlexander Motin meta->disk_offset_high = 0; 405821a0f63SAlexander Motin meta->disk_sectors_high = 0; 406821a0f63SAlexander Motin if (meta->disk_rebuild == UINT32_MAX) 407821a0f63SAlexander Motin meta->disk_rebuild_high = UINT32_MAX; 408821a0f63SAlexander Motin else 409821a0f63SAlexander Motin meta->disk_rebuild_high = 0; 410821a0f63SAlexander Motin if (meta->total_sectors_high == 0x15161718) { 411821a0f63SAlexander Motin meta->total_sectors_high = 0; 412c3ec009aSAlexander Motin meta->backup_time = 0; 413821a0f63SAlexander Motin if (meta->rebuild_lba64 == 0x2122232425262728) 414821a0f63SAlexander Motin meta->rebuild_lba64 = UINT64_MAX; 415821a0f63SAlexander Motin } 416821a0f63SAlexander Motin } 417821a0f63SAlexander Motin if (meta->sector_size < 1 || meta->sector_size > 8) 418821a0f63SAlexander Motin meta->sector_size = 1; 419821a0f63SAlexander Motin 42089b17223SAlexander Motin /* Save this part and look for next. */ 42189b17223SAlexander Motin *metaarr = meta; 42289b17223SAlexander Motin metaarr++; 42389b17223SAlexander Motin subdisks++; 42489b17223SAlexander Motin if (subdisks < PROMISE_MAX_SUBDISKS) 42589b17223SAlexander Motin goto next; 42689b17223SAlexander Motin 42789b17223SAlexander Motin return (subdisks); 42889b17223SAlexander Motin } 42989b17223SAlexander Motin 43089b17223SAlexander Motin static int 43189b17223SAlexander Motin promise_meta_write(struct g_consumer *cp, 43289b17223SAlexander Motin struct promise_raid_conf **metaarr, int nsd) 43389b17223SAlexander Motin { 43489b17223SAlexander Motin struct g_provider *pp; 43589b17223SAlexander Motin struct promise_raid_conf *meta; 43689b17223SAlexander Motin char *buf; 437821a0f63SAlexander Motin off_t off, size; 43889b17223SAlexander Motin int error, i, subdisk, fake; 439821a0f63SAlexander Motin uint32_t checksum, *ptr; 44089b17223SAlexander Motin 44189b17223SAlexander Motin pp = cp->provider; 44289b17223SAlexander Motin subdisk = 0; 44389b17223SAlexander Motin fake = 0; 44489b17223SAlexander Motin next: 44589b17223SAlexander Motin buf = malloc(pp->sectorsize * 4, M_MD_PROMISE, M_WAITOK | M_ZERO); 44689b17223SAlexander Motin meta = NULL; 44789b17223SAlexander Motin if (subdisk < nsd) { 44889b17223SAlexander Motin meta = metaarr[subdisk]; 44989b17223SAlexander Motin } else if (!fake && promise_meta_unused_range(metaarr, nsd, 45089b17223SAlexander Motin cp->provider->mediasize / cp->provider->sectorsize, 45189b17223SAlexander Motin &off, &size)) { 45289b17223SAlexander Motin /* Optionally add record for unused space. */ 45389b17223SAlexander Motin meta = (struct promise_raid_conf *)buf; 45463607675SAlexander Motin memcpy(&meta->promise_id[0], PROMISE_MAGIC, 45563607675SAlexander Motin sizeof(PROMISE_MAGIC) - 1); 45689b17223SAlexander Motin meta->dummy_0 = 0x00020000; 45789b17223SAlexander Motin meta->integrity = PROMISE_I_VALID; 45889b17223SAlexander Motin meta->disk.flags = PROMISE_F_ONLINE | PROMISE_F_VALID; 45989b17223SAlexander Motin meta->disk.number = 0xff; 46089b17223SAlexander Motin arc4rand(&meta->disk.id, sizeof(meta->disk.id), 0); 461821a0f63SAlexander Motin meta->disk_offset_high = off >> 32; 462821a0f63SAlexander Motin meta->disk_offset = (uint32_t)off; 463821a0f63SAlexander Motin meta->disk_sectors_high = size >> 32; 464821a0f63SAlexander Motin meta->disk_sectors = (uint32_t)size; 465821a0f63SAlexander Motin meta->disk_rebuild_high = UINT32_MAX; 466821a0f63SAlexander Motin meta->disk_rebuild = UINT32_MAX; 46789b17223SAlexander Motin fake = 1; 46889b17223SAlexander Motin } 46989b17223SAlexander Motin if (meta != NULL) { 47089b17223SAlexander Motin /* Recalculate checksum for case if metadata were changed. */ 47189b17223SAlexander Motin meta->checksum = 0; 47289b17223SAlexander Motin for (checksum = 0, ptr = (uint32_t *)meta, i = 0; i < 511; i++) 47389b17223SAlexander Motin checksum += *ptr++; 47489b17223SAlexander Motin meta->checksum = checksum; 47589b17223SAlexander Motin memcpy(buf, meta, MIN(pp->sectorsize * 4, sizeof(*meta))); 47689b17223SAlexander Motin } 47789b17223SAlexander Motin error = g_write_data(cp, pp->mediasize - pp->sectorsize * 47889b17223SAlexander Motin (63 - subdisk * PROMISE_META_OFFSET), 47989b17223SAlexander Motin buf, pp->sectorsize * 4); 48089b17223SAlexander Motin if (error != 0) { 48189b17223SAlexander Motin G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).", 48289b17223SAlexander Motin pp->name, error); 48389b17223SAlexander Motin } 48489b17223SAlexander Motin free(buf, M_MD_PROMISE); 48589b17223SAlexander Motin 48689b17223SAlexander Motin subdisk++; 48789b17223SAlexander Motin if (subdisk < PROMISE_MAX_SUBDISKS) 48889b17223SAlexander Motin goto next; 48989b17223SAlexander Motin 49089b17223SAlexander Motin return (error); 49189b17223SAlexander Motin } 49289b17223SAlexander Motin 49389b17223SAlexander Motin static int 49489b17223SAlexander Motin promise_meta_erase(struct g_consumer *cp) 49589b17223SAlexander Motin { 49689b17223SAlexander Motin struct g_provider *pp; 49789b17223SAlexander Motin char *buf; 49889b17223SAlexander Motin int error, subdisk; 49989b17223SAlexander Motin 50089b17223SAlexander Motin pp = cp->provider; 50189b17223SAlexander Motin buf = malloc(4 * pp->sectorsize, M_MD_PROMISE, M_WAITOK | M_ZERO); 50289b17223SAlexander Motin for (subdisk = 0; subdisk < PROMISE_MAX_SUBDISKS; subdisk++) { 50389b17223SAlexander Motin error = g_write_data(cp, pp->mediasize - pp->sectorsize * 50489b17223SAlexander Motin (63 - subdisk * PROMISE_META_OFFSET), 50589b17223SAlexander Motin buf, 4 * pp->sectorsize); 50689b17223SAlexander Motin if (error != 0) { 50789b17223SAlexander Motin G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).", 50889b17223SAlexander Motin pp->name, error); 50989b17223SAlexander Motin } 51089b17223SAlexander Motin } 51189b17223SAlexander Motin free(buf, M_MD_PROMISE); 51289b17223SAlexander Motin return (error); 51389b17223SAlexander Motin } 51489b17223SAlexander Motin 51589b17223SAlexander Motin static int 51689b17223SAlexander Motin promise_meta_write_spare(struct g_consumer *cp) 51789b17223SAlexander Motin { 51889b17223SAlexander Motin struct promise_raid_conf *meta; 519821a0f63SAlexander Motin off_t tmp; 52089b17223SAlexander Motin int error; 52189b17223SAlexander Motin 52289b17223SAlexander Motin meta = malloc(sizeof(*meta), M_MD_PROMISE, M_WAITOK | M_ZERO); 52363607675SAlexander Motin memcpy(&meta->promise_id[0], PROMISE_MAGIC, sizeof(PROMISE_MAGIC) - 1); 52489b17223SAlexander Motin meta->dummy_0 = 0x00020000; 52589b17223SAlexander Motin meta->integrity = PROMISE_I_VALID; 52689b17223SAlexander Motin meta->disk.flags = PROMISE_F_SPARE | PROMISE_F_ONLINE | PROMISE_F_VALID; 52789b17223SAlexander Motin meta->disk.number = 0xff; 52889b17223SAlexander Motin arc4rand(&meta->disk.id, sizeof(meta->disk.id), 0); 529821a0f63SAlexander Motin tmp = cp->provider->mediasize / cp->provider->sectorsize - 131072; 530821a0f63SAlexander Motin meta->disk_sectors_high = tmp >> 32; 531821a0f63SAlexander Motin meta->disk_sectors = (uint32_t)tmp; 532821a0f63SAlexander Motin meta->disk_rebuild_high = UINT32_MAX; 533821a0f63SAlexander Motin meta->disk_rebuild = UINT32_MAX; 53489b17223SAlexander Motin error = promise_meta_write(cp, &meta, 1); 53589b17223SAlexander Motin free(meta, M_MD_PROMISE); 53689b17223SAlexander Motin return (error); 53789b17223SAlexander Motin } 53889b17223SAlexander Motin 53989b17223SAlexander Motin static struct g_raid_volume * 54089b17223SAlexander Motin g_raid_md_promise_get_volume(struct g_raid_softc *sc, uint64_t id) 54189b17223SAlexander Motin { 54289b17223SAlexander Motin struct g_raid_volume *vol; 54389b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 54489b17223SAlexander Motin 54589b17223SAlexander Motin TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 54689b17223SAlexander Motin pv = vol->v_md_data; 54789b17223SAlexander Motin if (pv->pv_id == id) 54889b17223SAlexander Motin break; 54989b17223SAlexander Motin } 55089b17223SAlexander Motin return (vol); 55189b17223SAlexander Motin } 55289b17223SAlexander Motin 55389b17223SAlexander Motin static int 55489b17223SAlexander Motin g_raid_md_promise_purge_volumes(struct g_raid_softc *sc) 55589b17223SAlexander Motin { 55689b17223SAlexander Motin struct g_raid_volume *vol, *tvol; 55789b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 55889b17223SAlexander Motin int i, res; 55989b17223SAlexander Motin 56089b17223SAlexander Motin res = 0; 56189b17223SAlexander Motin TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tvol) { 56289b17223SAlexander Motin pv = vol->v_md_data; 56389b17223SAlexander Motin if (!pv->pv_started || vol->v_stopping) 56489b17223SAlexander Motin continue; 56589b17223SAlexander Motin for (i = 0; i < vol->v_disks_count; i++) { 56689b17223SAlexander Motin if (vol->v_subdisks[i].sd_state != G_RAID_SUBDISK_S_NONE) 56789b17223SAlexander Motin break; 56889b17223SAlexander Motin } 56989b17223SAlexander Motin if (i >= vol->v_disks_count) { 57089b17223SAlexander Motin g_raid_destroy_volume(vol); 57189b17223SAlexander Motin res = 1; 57289b17223SAlexander Motin } 57389b17223SAlexander Motin } 57489b17223SAlexander Motin return (res); 57589b17223SAlexander Motin } 57689b17223SAlexander Motin 57789b17223SAlexander Motin static int 57889b17223SAlexander Motin g_raid_md_promise_purge_disks(struct g_raid_softc *sc) 57989b17223SAlexander Motin { 58089b17223SAlexander Motin struct g_raid_disk *disk, *tdisk; 58189b17223SAlexander Motin struct g_raid_volume *vol; 58289b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 58389b17223SAlexander Motin int i, j, res; 58489b17223SAlexander Motin 58589b17223SAlexander Motin res = 0; 58689b17223SAlexander Motin TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) { 58789b17223SAlexander Motin if (disk->d_state == G_RAID_DISK_S_SPARE) 58889b17223SAlexander Motin continue; 58989b17223SAlexander Motin pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data; 59089b17223SAlexander Motin 59189b17223SAlexander Motin /* Scan for deleted volumes. */ 59289b17223SAlexander Motin for (i = 0; i < pd->pd_subdisks; ) { 59389b17223SAlexander Motin vol = g_raid_md_promise_get_volume(sc, 59489b17223SAlexander Motin pd->pd_meta[i]->volume_id); 59589b17223SAlexander Motin if (vol != NULL && !vol->v_stopping) { 59689b17223SAlexander Motin i++; 59789b17223SAlexander Motin continue; 59889b17223SAlexander Motin } 59989b17223SAlexander Motin free(pd->pd_meta[i], M_MD_PROMISE); 60089b17223SAlexander Motin for (j = i; j < pd->pd_subdisks - 1; j++) 60189b17223SAlexander Motin pd->pd_meta[j] = pd->pd_meta[j + 1]; 602c6f0cd57SAlexander Motin pd->pd_meta[pd->pd_subdisks - 1] = NULL; 60389b17223SAlexander Motin pd->pd_subdisks--; 60489b17223SAlexander Motin pd->pd_updated = 1; 60589b17223SAlexander Motin } 60689b17223SAlexander Motin 60789b17223SAlexander Motin /* If there is no metadata left - erase and delete disk. */ 60889b17223SAlexander Motin if (pd->pd_subdisks == 0) { 60989b17223SAlexander Motin promise_meta_erase(disk->d_consumer); 61089b17223SAlexander Motin g_raid_destroy_disk(disk); 61189b17223SAlexander Motin res = 1; 61289b17223SAlexander Motin } 61389b17223SAlexander Motin } 61489b17223SAlexander Motin return (res); 61589b17223SAlexander Motin } 61689b17223SAlexander Motin 61789b17223SAlexander Motin static int 61889b17223SAlexander Motin g_raid_md_promise_supported(int level, int qual, int disks, int force) 61989b17223SAlexander Motin { 62089b17223SAlexander Motin 62189b17223SAlexander Motin if (disks > PROMISE_MAX_DISKS) 62289b17223SAlexander Motin return (0); 62389b17223SAlexander Motin switch (level) { 62489b17223SAlexander Motin case G_RAID_VOLUME_RL_RAID0: 62589b17223SAlexander Motin if (disks < 1) 62689b17223SAlexander Motin return (0); 62789b17223SAlexander Motin if (!force && disks < 2) 62889b17223SAlexander Motin return (0); 62989b17223SAlexander Motin break; 63089b17223SAlexander Motin case G_RAID_VOLUME_RL_RAID1: 63189b17223SAlexander Motin if (disks < 1) 63289b17223SAlexander Motin return (0); 63389b17223SAlexander Motin if (!force && (disks != 2)) 63489b17223SAlexander Motin return (0); 63589b17223SAlexander Motin break; 63689b17223SAlexander Motin case G_RAID_VOLUME_RL_RAID1E: 63789b17223SAlexander Motin if (disks < 2) 63889b17223SAlexander Motin return (0); 63989b17223SAlexander Motin if (disks % 2 != 0) 64089b17223SAlexander Motin return (0); 64189b17223SAlexander Motin if (!force && (disks != 4)) 64289b17223SAlexander Motin return (0); 64389b17223SAlexander Motin break; 64489b17223SAlexander Motin case G_RAID_VOLUME_RL_SINGLE: 64589b17223SAlexander Motin if (disks != 1) 64689b17223SAlexander Motin return (0); 64789b17223SAlexander Motin break; 64889b17223SAlexander Motin case G_RAID_VOLUME_RL_CONCAT: 64989b17223SAlexander Motin if (disks < 2) 65089b17223SAlexander Motin return (0); 65189b17223SAlexander Motin break; 65289b17223SAlexander Motin case G_RAID_VOLUME_RL_RAID5: 65389b17223SAlexander Motin if (disks < 3) 65489b17223SAlexander Motin return (0); 655fc1de960SAlexander Motin if (qual != G_RAID_VOLUME_RLQ_R5LA) 656fc1de960SAlexander Motin return (0); 65789b17223SAlexander Motin break; 65889b17223SAlexander Motin default: 65989b17223SAlexander Motin return (0); 66089b17223SAlexander Motin } 661fc1de960SAlexander Motin if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE) 66289b17223SAlexander Motin return (0); 66389b17223SAlexander Motin return (1); 66489b17223SAlexander Motin } 66589b17223SAlexander Motin 66689b17223SAlexander Motin static int 66789b17223SAlexander Motin g_raid_md_promise_start_disk(struct g_raid_disk *disk, int sdn, 66889b17223SAlexander Motin struct g_raid_volume *vol) 66989b17223SAlexander Motin { 67089b17223SAlexander Motin struct g_raid_softc *sc; 67189b17223SAlexander Motin struct g_raid_subdisk *sd; 67289b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 67389b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 67489b17223SAlexander Motin struct promise_raid_conf *meta; 675821a0f63SAlexander Motin off_t eoff, esize, size; 67689b17223SAlexander Motin int disk_pos, md_disk_pos, i, resurrection = 0; 67789b17223SAlexander Motin 67889b17223SAlexander Motin sc = disk->d_softc; 67989b17223SAlexander Motin pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data; 68089b17223SAlexander Motin 68189b17223SAlexander Motin pv = vol->v_md_data; 68289b17223SAlexander Motin meta = pv->pv_meta; 68389b17223SAlexander Motin 68489b17223SAlexander Motin if (sdn >= 0) { 68528323addSBryan Drewery /* Find disk position in metadata by its serial. */ 68689b17223SAlexander Motin md_disk_pos = promise_meta_find_disk(meta, pd->pd_meta[sdn]->disk.id); 68789b17223SAlexander Motin /* For RAID0+1 we need to translate order. */ 68889b17223SAlexander Motin disk_pos = promise_meta_translate_disk(vol, md_disk_pos); 68989b17223SAlexander Motin } else { 69089b17223SAlexander Motin md_disk_pos = -1; 69189b17223SAlexander Motin disk_pos = -1; 69289b17223SAlexander Motin } 69389b17223SAlexander Motin if (disk_pos < 0) { 69489b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Disk %s is not part of the volume %s", 69589b17223SAlexander Motin g_raid_get_diskname(disk), vol->v_name); 69689b17223SAlexander Motin /* Failed stale disk is useless for us. */ 69789b17223SAlexander Motin if (sdn >= 0 && 69889b17223SAlexander Motin pd->pd_meta[sdn]->disk.flags & PROMISE_F_DOWN) { 69989b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED); 70089b17223SAlexander Motin return (0); 70189b17223SAlexander Motin } 70289b17223SAlexander Motin /* If we were given specific metadata subdisk - erase it. */ 70389b17223SAlexander Motin if (sdn >= 0) { 70489b17223SAlexander Motin free(pd->pd_meta[sdn], M_MD_PROMISE); 70589b17223SAlexander Motin for (i = sdn; i < pd->pd_subdisks - 1; i++) 70689b17223SAlexander Motin pd->pd_meta[i] = pd->pd_meta[i + 1]; 707c6f0cd57SAlexander Motin pd->pd_meta[pd->pd_subdisks - 1] = NULL; 70889b17223SAlexander Motin pd->pd_subdisks--; 70989b17223SAlexander Motin } 71089b17223SAlexander Motin /* If we are in the start process, that's all for now. */ 71189b17223SAlexander Motin if (!pv->pv_started) 71289b17223SAlexander Motin goto nofit; 71389b17223SAlexander Motin /* 71489b17223SAlexander Motin * If we have already started - try to get use of the disk. 71589b17223SAlexander Motin * Try to replace OFFLINE disks first, then FAILED. 71689b17223SAlexander Motin */ 71789b17223SAlexander Motin promise_meta_unused_range(pd->pd_meta, pd->pd_subdisks, 71889b17223SAlexander Motin disk->d_consumer->provider->mediasize / 71989b17223SAlexander Motin disk->d_consumer->provider->sectorsize, 72089b17223SAlexander Motin &eoff, &esize); 72189b17223SAlexander Motin if (esize == 0) { 72289b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "No free space on disk %s", 72389b17223SAlexander Motin g_raid_get_diskname(disk)); 72489b17223SAlexander Motin goto nofit; 72589b17223SAlexander Motin } 72689b17223SAlexander Motin size = INT64_MAX; 72789b17223SAlexander Motin for (i = 0; i < vol->v_disks_count; i++) { 72889b17223SAlexander Motin sd = &vol->v_subdisks[i]; 72989b17223SAlexander Motin if (sd->sd_state != G_RAID_SUBDISK_S_NONE) 73089b17223SAlexander Motin size = sd->sd_size; 73189b17223SAlexander Motin if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED && 73289b17223SAlexander Motin (disk_pos < 0 || 73389b17223SAlexander Motin vol->v_subdisks[i].sd_state < sd->sd_state)) 73489b17223SAlexander Motin disk_pos = i; 73589b17223SAlexander Motin } 73689b17223SAlexander Motin if (disk_pos >= 0 && 73789b17223SAlexander Motin vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT && 73889b17223SAlexander Motin (off_t)esize * 512 < size) { 73989b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Disk %s free space " 74089b17223SAlexander Motin "is too small (%ju < %ju)", 74189b17223SAlexander Motin g_raid_get_diskname(disk), 74289b17223SAlexander Motin (off_t)esize * 512, size); 74389b17223SAlexander Motin disk_pos = -1; 74489b17223SAlexander Motin } 74589b17223SAlexander Motin if (disk_pos >= 0) { 74689b17223SAlexander Motin if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT) 74789b17223SAlexander Motin esize = size / 512; 74889b17223SAlexander Motin /* For RAID0+1 we need to translate order. */ 74989b17223SAlexander Motin md_disk_pos = promise_meta_translate_disk(vol, disk_pos); 75089b17223SAlexander Motin } else { 75189b17223SAlexander Motin nofit: 75289b17223SAlexander Motin if (pd->pd_subdisks == 0) { 75389b17223SAlexander Motin g_raid_change_disk_state(disk, 75489b17223SAlexander Motin G_RAID_DISK_S_SPARE); 75589b17223SAlexander Motin } 75689b17223SAlexander Motin return (0); 75789b17223SAlexander Motin } 75889b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s", 75989b17223SAlexander Motin g_raid_get_diskname(disk), disk_pos, vol->v_name); 76089b17223SAlexander Motin resurrection = 1; 76189b17223SAlexander Motin } 76289b17223SAlexander Motin 76389b17223SAlexander Motin sd = &vol->v_subdisks[disk_pos]; 76489b17223SAlexander Motin 76589b17223SAlexander Motin if (resurrection && sd->sd_disk != NULL) { 76689b17223SAlexander Motin g_raid_change_disk_state(sd->sd_disk, 76789b17223SAlexander Motin G_RAID_DISK_S_STALE_FAILED); 76889b17223SAlexander Motin TAILQ_REMOVE(&sd->sd_disk->d_subdisks, 76989b17223SAlexander Motin sd, sd_next); 77089b17223SAlexander Motin } 77189b17223SAlexander Motin vol->v_subdisks[disk_pos].sd_disk = disk; 77289b17223SAlexander Motin TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 77389b17223SAlexander Motin 77489b17223SAlexander Motin /* Welcome the new disk. */ 77589b17223SAlexander Motin if (resurrection) 77689b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 77789b17223SAlexander Motin else if (meta->disks[md_disk_pos].flags & PROMISE_F_DOWN) 77889b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED); 77989b17223SAlexander Motin else 78089b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 78189b17223SAlexander Motin 78289b17223SAlexander Motin if (resurrection) { 78389b17223SAlexander Motin sd->sd_offset = (off_t)eoff * 512; 78489b17223SAlexander Motin sd->sd_size = (off_t)esize * 512; 78589b17223SAlexander Motin } else { 786821a0f63SAlexander Motin sd->sd_offset = (((off_t)pd->pd_meta[sdn]->disk_offset_high 787821a0f63SAlexander Motin << 32) + pd->pd_meta[sdn]->disk_offset) * 512; 788821a0f63SAlexander Motin sd->sd_size = (((off_t)pd->pd_meta[sdn]->disk_sectors_high 789821a0f63SAlexander Motin << 32) + pd->pd_meta[sdn]->disk_sectors) * 512; 79089b17223SAlexander Motin } 79189b17223SAlexander Motin 79289b17223SAlexander Motin if (resurrection) { 79389b17223SAlexander Motin /* Stale disk, almost same as new. */ 79489b17223SAlexander Motin g_raid_change_subdisk_state(sd, 79589b17223SAlexander Motin G_RAID_SUBDISK_S_NEW); 79689b17223SAlexander Motin } else if (meta->disks[md_disk_pos].flags & PROMISE_F_DOWN) { 79789b17223SAlexander Motin /* Failed disk. */ 79889b17223SAlexander Motin g_raid_change_subdisk_state(sd, 79989b17223SAlexander Motin G_RAID_SUBDISK_S_FAILED); 80089b17223SAlexander Motin } else if (meta->disks[md_disk_pos].flags & PROMISE_F_REDIR) { 80189b17223SAlexander Motin /* Rebuilding disk. */ 80289b17223SAlexander Motin g_raid_change_subdisk_state(sd, 80389b17223SAlexander Motin G_RAID_SUBDISK_S_REBUILD); 80489b17223SAlexander Motin if (pd->pd_meta[sdn]->generation != meta->generation) 80589b17223SAlexander Motin sd->sd_rebuild_pos = 0; 80689b17223SAlexander Motin else { 80789b17223SAlexander Motin sd->sd_rebuild_pos = 808c3ec009aSAlexander Motin (((off_t)pd->pd_meta[sdn]->disk_rebuild_high << 32) + 809821a0f63SAlexander Motin pd->pd_meta[sdn]->disk_rebuild) * 512; 81089b17223SAlexander Motin } 81189b17223SAlexander Motin } else if (!(meta->disks[md_disk_pos].flags & PROMISE_F_ONLINE)) { 81289b17223SAlexander Motin /* Rebuilding disk. */ 81389b17223SAlexander Motin g_raid_change_subdisk_state(sd, 81489b17223SAlexander Motin G_RAID_SUBDISK_S_NEW); 81589b17223SAlexander Motin } else if (pd->pd_meta[sdn]->generation != meta->generation || 81689b17223SAlexander Motin (meta->status & PROMISE_S_MARKED)) { 81789b17223SAlexander Motin /* Stale disk or dirty volume (unclean shutdown). */ 81889b17223SAlexander Motin g_raid_change_subdisk_state(sd, 81989b17223SAlexander Motin G_RAID_SUBDISK_S_STALE); 82089b17223SAlexander Motin } else { 82189b17223SAlexander Motin /* Up to date disk. */ 82289b17223SAlexander Motin g_raid_change_subdisk_state(sd, 82389b17223SAlexander Motin G_RAID_SUBDISK_S_ACTIVE); 82489b17223SAlexander Motin } 82589b17223SAlexander Motin g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 82689b17223SAlexander Motin G_RAID_EVENT_SUBDISK); 82789b17223SAlexander Motin 82889b17223SAlexander Motin return (resurrection); 82989b17223SAlexander Motin } 83089b17223SAlexander Motin 83189b17223SAlexander Motin static void 83289b17223SAlexander Motin g_raid_md_promise_refill(struct g_raid_softc *sc) 83389b17223SAlexander Motin { 83489b17223SAlexander Motin struct g_raid_volume *vol; 83589b17223SAlexander Motin struct g_raid_subdisk *sd; 83689b17223SAlexander Motin struct g_raid_disk *disk; 83789b17223SAlexander Motin struct g_raid_md_object *md; 83889b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 83989b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 84089b17223SAlexander Motin int update, updated, i, bad; 84189b17223SAlexander Motin 84289b17223SAlexander Motin md = sc->sc_md; 84389b17223SAlexander Motin restart: 84489b17223SAlexander Motin updated = 0; 84589b17223SAlexander Motin TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 84689b17223SAlexander Motin pv = vol->v_md_data; 84789b17223SAlexander Motin if (!pv->pv_started || vol->v_stopping) 84889b17223SAlexander Motin continue; 84989b17223SAlexander Motin 85089b17223SAlexander Motin /* Search for subdisk that needs replacement. */ 85189b17223SAlexander Motin bad = 0; 85289b17223SAlexander Motin for (i = 0; i < vol->v_disks_count; i++) { 85389b17223SAlexander Motin sd = &vol->v_subdisks[i]; 85489b17223SAlexander Motin if (sd->sd_state == G_RAID_SUBDISK_S_NONE || 85589b17223SAlexander Motin sd->sd_state == G_RAID_SUBDISK_S_FAILED) 85689b17223SAlexander Motin bad = 1; 85789b17223SAlexander Motin } 85889b17223SAlexander Motin if (!bad) 85989b17223SAlexander Motin continue; 86089b17223SAlexander Motin 86189b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Volume %s is not complete, " 86289b17223SAlexander Motin "trying to refill.", vol->v_name); 86389b17223SAlexander Motin 86489b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 86589b17223SAlexander Motin /* Skip failed. */ 86689b17223SAlexander Motin if (disk->d_state < G_RAID_DISK_S_SPARE) 86789b17223SAlexander Motin continue; 86889b17223SAlexander Motin /* Skip already used by this volume. */ 86989b17223SAlexander Motin for (i = 0; i < vol->v_disks_count; i++) { 87089b17223SAlexander Motin sd = &vol->v_subdisks[i]; 87189b17223SAlexander Motin if (sd->sd_disk == disk) 87289b17223SAlexander Motin break; 87389b17223SAlexander Motin } 87489b17223SAlexander Motin if (i < vol->v_disks_count) 87589b17223SAlexander Motin continue; 87689b17223SAlexander Motin 87789b17223SAlexander Motin /* Try to use disk if it has empty extents. */ 87889b17223SAlexander Motin pd = disk->d_md_data; 87989b17223SAlexander Motin if (pd->pd_subdisks < PROMISE_MAX_SUBDISKS) { 88089b17223SAlexander Motin update = 88189b17223SAlexander Motin g_raid_md_promise_start_disk(disk, -1, vol); 88289b17223SAlexander Motin } else 88389b17223SAlexander Motin update = 0; 88489b17223SAlexander Motin if (update) { 88563607675SAlexander Motin updated = 1; 88689b17223SAlexander Motin g_raid_md_write_promise(md, vol, NULL, disk); 88789b17223SAlexander Motin break; 88889b17223SAlexander Motin } 88989b17223SAlexander Motin } 89089b17223SAlexander Motin } 89189b17223SAlexander Motin if (updated) 89289b17223SAlexander Motin goto restart; 89389b17223SAlexander Motin } 89489b17223SAlexander Motin 89589b17223SAlexander Motin static void 89689b17223SAlexander Motin g_raid_md_promise_start(struct g_raid_volume *vol) 89789b17223SAlexander Motin { 89889b17223SAlexander Motin struct g_raid_softc *sc; 89989b17223SAlexander Motin struct g_raid_subdisk *sd; 90089b17223SAlexander Motin struct g_raid_disk *disk; 90189b17223SAlexander Motin struct g_raid_md_object *md; 90289b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 90389b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 90489b17223SAlexander Motin struct promise_raid_conf *meta; 905b99bce73SPedro F. Giffuni u_int i; 90689b17223SAlexander Motin 90789b17223SAlexander Motin sc = vol->v_softc; 90889b17223SAlexander Motin md = sc->sc_md; 90989b17223SAlexander Motin pv = vol->v_md_data; 91089b17223SAlexander Motin meta = pv->pv_meta; 91189b17223SAlexander Motin 912fc1de960SAlexander Motin vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE; 91389b17223SAlexander Motin if (meta->type == PROMISE_T_RAID0) 91489b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID0; 91589b17223SAlexander Motin else if (meta->type == PROMISE_T_RAID1) { 91689b17223SAlexander Motin if (meta->array_width == 1) 91789b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID1; 91889b17223SAlexander Motin else 91989b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E; 92089b17223SAlexander Motin } else if (meta->type == PROMISE_T_RAID3) 92189b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID3; 922fc1de960SAlexander Motin else if (meta->type == PROMISE_T_RAID5) { 92389b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID5; 924fc1de960SAlexander Motin vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA; 925fc1de960SAlexander Motin } else if (meta->type == PROMISE_T_SPAN) 92689b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT; 92789b17223SAlexander Motin else if (meta->type == PROMISE_T_JBOD) 92889b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE; 92989b17223SAlexander Motin else 93089b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN; 93189b17223SAlexander Motin vol->v_strip_size = 512 << meta->stripe_shift; //ZZZ 93289b17223SAlexander Motin vol->v_disks_count = meta->total_disks; 93389b17223SAlexander Motin vol->v_mediasize = (off_t)meta->total_sectors * 512; //ZZZ 934733a1f3fSAlexander Motin if (meta->total_sectors_high < 256) /* If value looks sane. */ 935821a0f63SAlexander Motin vol->v_mediasize += 936733a1f3fSAlexander Motin ((off_t)meta->total_sectors_high << 32) * 512; //ZZZ 937821a0f63SAlexander Motin vol->v_sectorsize = 512 * meta->sector_size; 93889b17223SAlexander Motin for (i = 0; i < vol->v_disks_count; i++) { 93989b17223SAlexander Motin sd = &vol->v_subdisks[i]; 940821a0f63SAlexander Motin sd->sd_offset = (((off_t)meta->disk_offset_high << 32) + 941821a0f63SAlexander Motin meta->disk_offset) * 512; 942821a0f63SAlexander Motin sd->sd_size = (((off_t)meta->disk_sectors_high << 32) + 943821a0f63SAlexander Motin meta->disk_sectors) * 512; 94489b17223SAlexander Motin } 94589b17223SAlexander Motin g_raid_start_volume(vol); 94689b17223SAlexander Motin 94789b17223SAlexander Motin /* Make all disks found till the moment take their places. */ 94889b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 94989b17223SAlexander Motin pd = disk->d_md_data; 95089b17223SAlexander Motin for (i = 0; i < pd->pd_subdisks; i++) { 95189b17223SAlexander Motin if (pd->pd_meta[i]->volume_id == meta->volume_id) 95289b17223SAlexander Motin g_raid_md_promise_start_disk(disk, i, vol); 95389b17223SAlexander Motin } 95489b17223SAlexander Motin } 95589b17223SAlexander Motin 95689b17223SAlexander Motin pv->pv_started = 1; 95789b17223SAlexander Motin callout_stop(&pv->pv_start_co); 95889b17223SAlexander Motin G_RAID_DEBUG1(0, sc, "Volume started."); 95989b17223SAlexander Motin g_raid_md_write_promise(md, vol, NULL, NULL); 96089b17223SAlexander Motin 96189b17223SAlexander Motin /* Pickup any STALE/SPARE disks to refill array if needed. */ 96289b17223SAlexander Motin g_raid_md_promise_refill(sc); 96389b17223SAlexander Motin 96489b17223SAlexander Motin g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME); 96589b17223SAlexander Motin } 96689b17223SAlexander Motin 96789b17223SAlexander Motin static void 96889b17223SAlexander Motin g_raid_promise_go(void *arg) 96989b17223SAlexander Motin { 97089b17223SAlexander Motin struct g_raid_volume *vol; 97189b17223SAlexander Motin struct g_raid_softc *sc; 97289b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 97389b17223SAlexander Motin 97489b17223SAlexander Motin vol = arg; 97589b17223SAlexander Motin pv = vol->v_md_data; 97689b17223SAlexander Motin sc = vol->v_softc; 97789b17223SAlexander Motin if (!pv->pv_started) { 97889b17223SAlexander Motin G_RAID_DEBUG1(0, sc, "Force volume start due to timeout."); 97989b17223SAlexander Motin g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD, 98089b17223SAlexander Motin G_RAID_EVENT_VOLUME); 98189b17223SAlexander Motin } 98289b17223SAlexander Motin } 98389b17223SAlexander Motin 98489b17223SAlexander Motin static void 98589b17223SAlexander Motin g_raid_md_promise_new_disk(struct g_raid_disk *disk) 98689b17223SAlexander Motin { 98789b17223SAlexander Motin struct g_raid_softc *sc; 98889b17223SAlexander Motin struct g_raid_md_object *md; 98989b17223SAlexander Motin struct promise_raid_conf *pdmeta; 99089b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 99189b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 99289b17223SAlexander Motin struct g_raid_volume *vol; 99389b17223SAlexander Motin int i; 99489b17223SAlexander Motin char buf[33]; 99589b17223SAlexander Motin 99689b17223SAlexander Motin sc = disk->d_softc; 99789b17223SAlexander Motin md = sc->sc_md; 99889b17223SAlexander Motin pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data; 99989b17223SAlexander Motin 100089b17223SAlexander Motin if (pd->pd_subdisks == 0) { 100189b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE); 100289b17223SAlexander Motin g_raid_md_promise_refill(sc); 100389b17223SAlexander Motin return; 100489b17223SAlexander Motin } 100589b17223SAlexander Motin 100689b17223SAlexander Motin for (i = 0; i < pd->pd_subdisks; i++) { 100789b17223SAlexander Motin pdmeta = pd->pd_meta[i]; 100889b17223SAlexander Motin 100989b17223SAlexander Motin /* Look for volume with matching ID. */ 101089b17223SAlexander Motin vol = g_raid_md_promise_get_volume(sc, pdmeta->volume_id); 101189b17223SAlexander Motin if (vol == NULL) { 101289b17223SAlexander Motin promise_meta_get_name(pdmeta, buf); 101389b17223SAlexander Motin vol = g_raid_create_volume(sc, buf, pdmeta->array_number); 101489b17223SAlexander Motin pv = malloc(sizeof(*pv), M_MD_PROMISE, M_WAITOK | M_ZERO); 101589b17223SAlexander Motin pv->pv_id = pdmeta->volume_id; 101689b17223SAlexander Motin vol->v_md_data = pv; 101789b17223SAlexander Motin callout_init(&pv->pv_start_co, 1); 101889b17223SAlexander Motin callout_reset(&pv->pv_start_co, 101989b17223SAlexander Motin g_raid_start_timeout * hz, 102089b17223SAlexander Motin g_raid_promise_go, vol); 102189b17223SAlexander Motin } else 102289b17223SAlexander Motin pv = vol->v_md_data; 102389b17223SAlexander Motin 102489b17223SAlexander Motin /* If we haven't started yet - check metadata freshness. */ 102589b17223SAlexander Motin if (pv->pv_meta == NULL || !pv->pv_started) { 102689b17223SAlexander Motin if (pv->pv_meta == NULL || 102789b17223SAlexander Motin ((int16_t)(pdmeta->generation - pv->pv_generation)) > 0) { 102889b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Newer disk"); 102989b17223SAlexander Motin if (pv->pv_meta != NULL) 103089b17223SAlexander Motin free(pv->pv_meta, M_MD_PROMISE); 103189b17223SAlexander Motin pv->pv_meta = promise_meta_copy(pdmeta); 103289b17223SAlexander Motin pv->pv_generation = pv->pv_meta->generation; 103389b17223SAlexander Motin pv->pv_disks_present = 1; 103489b17223SAlexander Motin } else if (pdmeta->generation == pv->pv_generation) { 103589b17223SAlexander Motin pv->pv_disks_present++; 103689b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)", 103789b17223SAlexander Motin pv->pv_disks_present, 103889b17223SAlexander Motin pv->pv_meta->total_disks); 103989b17223SAlexander Motin } else { 104089b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Older disk"); 104189b17223SAlexander Motin } 104289b17223SAlexander Motin } 104389b17223SAlexander Motin } 104489b17223SAlexander Motin 104589b17223SAlexander Motin for (i = 0; i < pd->pd_subdisks; i++) { 104689b17223SAlexander Motin pdmeta = pd->pd_meta[i]; 104789b17223SAlexander Motin 104889b17223SAlexander Motin /* Look for volume with matching ID. */ 104989b17223SAlexander Motin vol = g_raid_md_promise_get_volume(sc, pdmeta->volume_id); 105089b17223SAlexander Motin if (vol == NULL) 105189b17223SAlexander Motin continue; 105289b17223SAlexander Motin pv = vol->v_md_data; 105389b17223SAlexander Motin 105489b17223SAlexander Motin if (pv->pv_started) { 105589b17223SAlexander Motin if (g_raid_md_promise_start_disk(disk, i, vol)) 105689b17223SAlexander Motin g_raid_md_write_promise(md, vol, NULL, NULL); 105789b17223SAlexander Motin } else { 105889b17223SAlexander Motin /* If we collected all needed disks - start array. */ 105989b17223SAlexander Motin if (pv->pv_disks_present == pv->pv_meta->total_disks) 106089b17223SAlexander Motin g_raid_md_promise_start(vol); 106189b17223SAlexander Motin } 106289b17223SAlexander Motin } 106389b17223SAlexander Motin } 106489b17223SAlexander Motin 106589b17223SAlexander Motin static int 106689b17223SAlexander Motin g_raid_md_create_promise(struct g_raid_md_object *md, struct g_class *mp, 106789b17223SAlexander Motin struct g_geom **gp) 106889b17223SAlexander Motin { 106989b17223SAlexander Motin struct g_geom *geom; 107089b17223SAlexander Motin struct g_raid_softc *sc; 107189b17223SAlexander Motin 107289b17223SAlexander Motin /* Search for existing node. */ 107389b17223SAlexander Motin LIST_FOREACH(geom, &mp->geom, geom) { 107489b17223SAlexander Motin sc = geom->softc; 107589b17223SAlexander Motin if (sc == NULL) 107689b17223SAlexander Motin continue; 107789b17223SAlexander Motin if (sc->sc_stopping != 0) 107889b17223SAlexander Motin continue; 107989b17223SAlexander Motin if (sc->sc_md->mdo_class != md->mdo_class) 108089b17223SAlexander Motin continue; 108189b17223SAlexander Motin break; 108289b17223SAlexander Motin } 108389b17223SAlexander Motin if (geom != NULL) { 108489b17223SAlexander Motin *gp = geom; 108589b17223SAlexander Motin return (G_RAID_MD_TASTE_EXISTING); 108689b17223SAlexander Motin } 108789b17223SAlexander Motin 108889b17223SAlexander Motin /* Create new one if not found. */ 108989b17223SAlexander Motin sc = g_raid_create_node(mp, "Promise", md); 109089b17223SAlexander Motin if (sc == NULL) 109189b17223SAlexander Motin return (G_RAID_MD_TASTE_FAIL); 109289b17223SAlexander Motin md->mdo_softc = sc; 109389b17223SAlexander Motin *gp = sc->sc_geom; 109489b17223SAlexander Motin return (G_RAID_MD_TASTE_NEW); 109589b17223SAlexander Motin } 109689b17223SAlexander Motin 109789b17223SAlexander Motin static int 109889b17223SAlexander Motin g_raid_md_taste_promise(struct g_raid_md_object *md, struct g_class *mp, 109989b17223SAlexander Motin struct g_consumer *cp, struct g_geom **gp) 110089b17223SAlexander Motin { 110189b17223SAlexander Motin struct g_consumer *rcp; 110289b17223SAlexander Motin struct g_provider *pp; 110389b17223SAlexander Motin struct g_raid_softc *sc; 110489b17223SAlexander Motin struct g_raid_disk *disk; 1105151ba793SAlexander Kabaev struct promise_raid_conf *metaarr[4]; 110689b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 110789b17223SAlexander Motin struct g_geom *geom; 1108609a7474SAlexander Motin int i, j, result, len, subdisks; 110989b17223SAlexander Motin char name[16]; 111089b17223SAlexander Motin uint16_t vendor; 111189b17223SAlexander Motin 111289b17223SAlexander Motin G_RAID_DEBUG(1, "Tasting Promise on %s", cp->provider->name); 111389b17223SAlexander Motin pp = cp->provider; 111489b17223SAlexander Motin 111589b17223SAlexander Motin /* Read metadata from device. */ 111689b17223SAlexander Motin g_topology_unlock(); 11170b1b7c2cSAlexander Motin vendor = 0xffff; 11180b1b7c2cSAlexander Motin len = sizeof(vendor); 111989b17223SAlexander Motin if (pp->geom->rank == 1) 112089b17223SAlexander Motin g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor); 112189b17223SAlexander Motin subdisks = promise_meta_read(cp, metaarr); 112289b17223SAlexander Motin g_topology_lock(); 112389b17223SAlexander Motin if (subdisks == 0) { 112489b17223SAlexander Motin if (g_raid_aggressive_spare) { 112589b17223SAlexander Motin if (vendor == 0x105a || vendor == 0x1002) { 112689b17223SAlexander Motin G_RAID_DEBUG(1, 112789b17223SAlexander Motin "No Promise metadata, forcing spare."); 112889b17223SAlexander Motin goto search; 112989b17223SAlexander Motin } else { 113089b17223SAlexander Motin G_RAID_DEBUG(1, 113189b17223SAlexander Motin "Promise/ATI vendor mismatch " 113289b17223SAlexander Motin "0x%04x != 0x105a/0x1002", 113389b17223SAlexander Motin vendor); 113489b17223SAlexander Motin } 113589b17223SAlexander Motin } 113689b17223SAlexander Motin return (G_RAID_MD_TASTE_FAIL); 113789b17223SAlexander Motin } 113889b17223SAlexander Motin 113989b17223SAlexander Motin /* Metadata valid. Print it. */ 114089b17223SAlexander Motin for (i = 0; i < subdisks; i++) 114189b17223SAlexander Motin g_raid_md_promise_print(metaarr[i]); 114289b17223SAlexander Motin 114389b17223SAlexander Motin /* Purge meaningless (empty/spare) records. */ 114489b17223SAlexander Motin for (i = 0; i < subdisks; ) { 114589b17223SAlexander Motin if (metaarr[i]->disk.flags & PROMISE_F_ASSIGNED) { 114689b17223SAlexander Motin i++; 114789b17223SAlexander Motin continue; 114889b17223SAlexander Motin } 114989b17223SAlexander Motin free(metaarr[i], M_MD_PROMISE); 115089b17223SAlexander Motin for (j = i; j < subdisks - 1; j++) 115189b17223SAlexander Motin metaarr[i] = metaarr[j + 1]; 1152c6f0cd57SAlexander Motin metaarr[subdisks - 1] = NULL; 115389b17223SAlexander Motin subdisks--; 115489b17223SAlexander Motin } 115589b17223SAlexander Motin 115689b17223SAlexander Motin search: 115789b17223SAlexander Motin /* Search for matching node. */ 115889b17223SAlexander Motin sc = NULL; 115989b17223SAlexander Motin LIST_FOREACH(geom, &mp->geom, geom) { 116089b17223SAlexander Motin sc = geom->softc; 116189b17223SAlexander Motin if (sc == NULL) 116289b17223SAlexander Motin continue; 116389b17223SAlexander Motin if (sc->sc_stopping != 0) 116489b17223SAlexander Motin continue; 116589b17223SAlexander Motin if (sc->sc_md->mdo_class != md->mdo_class) 116689b17223SAlexander Motin continue; 116789b17223SAlexander Motin break; 116889b17223SAlexander Motin } 116989b17223SAlexander Motin 117089b17223SAlexander Motin /* Found matching node. */ 117189b17223SAlexander Motin if (geom != NULL) { 117289b17223SAlexander Motin G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name); 117389b17223SAlexander Motin result = G_RAID_MD_TASTE_EXISTING; 117489b17223SAlexander Motin 117589b17223SAlexander Motin } else { /* Not found matching node -- create one. */ 117689b17223SAlexander Motin result = G_RAID_MD_TASTE_NEW; 117789b17223SAlexander Motin snprintf(name, sizeof(name), "Promise"); 117889b17223SAlexander Motin sc = g_raid_create_node(mp, name, md); 117989b17223SAlexander Motin md->mdo_softc = sc; 118089b17223SAlexander Motin geom = sc->sc_geom; 118189b17223SAlexander Motin } 118289b17223SAlexander Motin 1183dea1e226SAlexander Motin /* There is no return after this point, so we close passed consumer. */ 1184dea1e226SAlexander Motin g_access(cp, -1, 0, 0); 1185dea1e226SAlexander Motin 118689b17223SAlexander Motin rcp = g_new_consumer(geom); 118740ea77a0SAlexander Motin rcp->flags |= G_CF_DIRECT_RECEIVE; 118889b17223SAlexander Motin g_attach(rcp, pp); 118989b17223SAlexander Motin if (g_access(rcp, 1, 1, 1) != 0) 119089b17223SAlexander Motin ; //goto fail1; 119189b17223SAlexander Motin 119289b17223SAlexander Motin g_topology_unlock(); 119389b17223SAlexander Motin sx_xlock(&sc->sc_lock); 119489b17223SAlexander Motin 119589b17223SAlexander Motin pd = malloc(sizeof(*pd), M_MD_PROMISE, M_WAITOK | M_ZERO); 119689b17223SAlexander Motin pd->pd_subdisks = subdisks; 119789b17223SAlexander Motin for (i = 0; i < subdisks; i++) 119889b17223SAlexander Motin pd->pd_meta[i] = metaarr[i]; 119989b17223SAlexander Motin disk = g_raid_create_disk(sc); 120089b17223SAlexander Motin disk->d_md_data = (void *)pd; 120189b17223SAlexander Motin disk->d_consumer = rcp; 120289b17223SAlexander Motin rcp->private = disk; 120389b17223SAlexander Motin 1204609a7474SAlexander Motin g_raid_get_disk_info(disk); 120589b17223SAlexander Motin 120689b17223SAlexander Motin g_raid_md_promise_new_disk(disk); 120789b17223SAlexander Motin 120889b17223SAlexander Motin sx_xunlock(&sc->sc_lock); 120989b17223SAlexander Motin g_topology_lock(); 121089b17223SAlexander Motin *gp = geom; 121189b17223SAlexander Motin return (result); 121289b17223SAlexander Motin } 121389b17223SAlexander Motin 121489b17223SAlexander Motin static int 121589b17223SAlexander Motin g_raid_md_event_promise(struct g_raid_md_object *md, 121689b17223SAlexander Motin struct g_raid_disk *disk, u_int event) 121789b17223SAlexander Motin { 121889b17223SAlexander Motin struct g_raid_softc *sc; 121989b17223SAlexander Motin 122089b17223SAlexander Motin sc = md->mdo_softc; 122189b17223SAlexander Motin if (disk == NULL) 122289b17223SAlexander Motin return (-1); 122389b17223SAlexander Motin switch (event) { 122489b17223SAlexander Motin case G_RAID_DISK_E_DISCONNECTED: 122589b17223SAlexander Motin /* Delete disk. */ 122689b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 122789b17223SAlexander Motin g_raid_destroy_disk(disk); 122889b17223SAlexander Motin g_raid_md_promise_purge_volumes(sc); 122989b17223SAlexander Motin 123089b17223SAlexander Motin /* Write updated metadata to all disks. */ 123189b17223SAlexander Motin g_raid_md_write_promise(md, NULL, NULL, NULL); 123289b17223SAlexander Motin 123389b17223SAlexander Motin /* Check if anything left. */ 123489b17223SAlexander Motin if (g_raid_ndisks(sc, -1) == 0) 123589b17223SAlexander Motin g_raid_destroy_node(sc, 0); 123689b17223SAlexander Motin else 123789b17223SAlexander Motin g_raid_md_promise_refill(sc); 123889b17223SAlexander Motin return (0); 123989b17223SAlexander Motin } 124089b17223SAlexander Motin return (-2); 124189b17223SAlexander Motin } 124289b17223SAlexander Motin 124389b17223SAlexander Motin static int 124489b17223SAlexander Motin g_raid_md_volume_event_promise(struct g_raid_md_object *md, 124589b17223SAlexander Motin struct g_raid_volume *vol, u_int event) 124689b17223SAlexander Motin { 124789b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 124889b17223SAlexander Motin 124989b17223SAlexander Motin pv = (struct g_raid_md_promise_pervolume *)vol->v_md_data; 125089b17223SAlexander Motin switch (event) { 125189b17223SAlexander Motin case G_RAID_VOLUME_E_STARTMD: 125289b17223SAlexander Motin if (!pv->pv_started) 125389b17223SAlexander Motin g_raid_md_promise_start(vol); 125489b17223SAlexander Motin return (0); 125589b17223SAlexander Motin } 125689b17223SAlexander Motin return (-2); 125789b17223SAlexander Motin } 125889b17223SAlexander Motin 125989b17223SAlexander Motin static int 126089b17223SAlexander Motin g_raid_md_ctl_promise(struct g_raid_md_object *md, 126189b17223SAlexander Motin struct gctl_req *req) 126289b17223SAlexander Motin { 126389b17223SAlexander Motin struct g_raid_softc *sc; 126489b17223SAlexander Motin struct g_raid_volume *vol, *vol1; 126589b17223SAlexander Motin struct g_raid_subdisk *sd; 126689b17223SAlexander Motin struct g_raid_disk *disk, *disks[PROMISE_MAX_DISKS]; 126789b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 126889b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 126989b17223SAlexander Motin struct g_consumer *cp; 127089b17223SAlexander Motin struct g_provider *pp; 127189b17223SAlexander Motin char arg[16]; 12726871a543SAlexander Motin const char *nodename, *verb, *volname, *levelname, *diskname; 127389b17223SAlexander Motin char *tmp; 127489b17223SAlexander Motin int *nargs, *force; 1275821a0f63SAlexander Motin off_t esize, offs[PROMISE_MAX_DISKS], size, sectorsize, strip; 127689b17223SAlexander Motin intmax_t *sizearg, *striparg; 127789b17223SAlexander Motin int numdisks, i, len, level, qual; 127889b17223SAlexander Motin int error; 127989b17223SAlexander Motin 128089b17223SAlexander Motin sc = md->mdo_softc; 128189b17223SAlexander Motin verb = gctl_get_param(req, "verb", NULL); 128289b17223SAlexander Motin nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 128389b17223SAlexander Motin error = 0; 128489b17223SAlexander Motin if (strcmp(verb, "label") == 0) { 128589b17223SAlexander Motin if (*nargs < 4) { 128689b17223SAlexander Motin gctl_error(req, "Invalid number of arguments."); 128789b17223SAlexander Motin return (-1); 128889b17223SAlexander Motin } 128989b17223SAlexander Motin volname = gctl_get_asciiparam(req, "arg1"); 129089b17223SAlexander Motin if (volname == NULL) { 129189b17223SAlexander Motin gctl_error(req, "No volume name."); 129289b17223SAlexander Motin return (-2); 129389b17223SAlexander Motin } 129489b17223SAlexander Motin levelname = gctl_get_asciiparam(req, "arg2"); 129589b17223SAlexander Motin if (levelname == NULL) { 129689b17223SAlexander Motin gctl_error(req, "No RAID level."); 129789b17223SAlexander Motin return (-3); 129889b17223SAlexander Motin } 1299fc1de960SAlexander Motin if (strcasecmp(levelname, "RAID5") == 0) 13007b2a8d78SAlexander Motin levelname = "RAID5-LA"; 130189b17223SAlexander Motin if (g_raid_volume_str2level(levelname, &level, &qual)) { 130289b17223SAlexander Motin gctl_error(req, "Unknown RAID level '%s'.", levelname); 130389b17223SAlexander Motin return (-4); 130489b17223SAlexander Motin } 130589b17223SAlexander Motin numdisks = *nargs - 3; 130689b17223SAlexander Motin force = gctl_get_paraml(req, "force", sizeof(*force)); 130789b17223SAlexander Motin if (!g_raid_md_promise_supported(level, qual, numdisks, 130889b17223SAlexander Motin force ? *force : 0)) { 130989b17223SAlexander Motin gctl_error(req, "Unsupported RAID level " 131089b17223SAlexander Motin "(0x%02x/0x%02x), or number of disks (%d).", 131189b17223SAlexander Motin level, qual, numdisks); 131289b17223SAlexander Motin return (-5); 131389b17223SAlexander Motin } 131489b17223SAlexander Motin 131589b17223SAlexander Motin /* Search for disks, connect them and probe. */ 131689b17223SAlexander Motin size = INT64_MAX; 131789b17223SAlexander Motin sectorsize = 0; 131889b17223SAlexander Motin bzero(disks, sizeof(disks)); 131989b17223SAlexander Motin bzero(offs, sizeof(offs)); 132089b17223SAlexander Motin for (i = 0; i < numdisks; i++) { 132189b17223SAlexander Motin snprintf(arg, sizeof(arg), "arg%d", i + 3); 132289b17223SAlexander Motin diskname = gctl_get_asciiparam(req, arg); 132389b17223SAlexander Motin if (diskname == NULL) { 132489b17223SAlexander Motin gctl_error(req, "No disk name (%s).", arg); 132589b17223SAlexander Motin error = -6; 132689b17223SAlexander Motin break; 132789b17223SAlexander Motin } 132889b17223SAlexander Motin if (strcmp(diskname, "NONE") == 0) 132989b17223SAlexander Motin continue; 133089b17223SAlexander Motin 133189b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 133289b17223SAlexander Motin if (disk->d_consumer != NULL && 133389b17223SAlexander Motin disk->d_consumer->provider != NULL && 133489b17223SAlexander Motin strcmp(disk->d_consumer->provider->name, 133589b17223SAlexander Motin diskname) == 0) 133689b17223SAlexander Motin break; 133789b17223SAlexander Motin } 133889b17223SAlexander Motin if (disk != NULL) { 133989b17223SAlexander Motin if (disk->d_state != G_RAID_DISK_S_ACTIVE) { 134089b17223SAlexander Motin gctl_error(req, "Disk '%s' is in a " 134189b17223SAlexander Motin "wrong state (%s).", diskname, 134289b17223SAlexander Motin g_raid_disk_state2str(disk->d_state)); 134389b17223SAlexander Motin error = -7; 134489b17223SAlexander Motin break; 134589b17223SAlexander Motin } 134689b17223SAlexander Motin pd = disk->d_md_data; 134789b17223SAlexander Motin if (pd->pd_subdisks >= PROMISE_MAX_SUBDISKS) { 134889b17223SAlexander Motin gctl_error(req, "Disk '%s' already " 134989b17223SAlexander Motin "used by %d volumes.", 135089b17223SAlexander Motin diskname, pd->pd_subdisks); 135189b17223SAlexander Motin error = -7; 135289b17223SAlexander Motin break; 135389b17223SAlexander Motin } 135489b17223SAlexander Motin pp = disk->d_consumer->provider; 135589b17223SAlexander Motin disks[i] = disk; 135689b17223SAlexander Motin promise_meta_unused_range(pd->pd_meta, 135789b17223SAlexander Motin pd->pd_subdisks, 135889b17223SAlexander Motin pp->mediasize / pp->sectorsize, 135989b17223SAlexander Motin &offs[i], &esize); 136089b17223SAlexander Motin size = MIN(size, (off_t)esize * pp->sectorsize); 136189b17223SAlexander Motin sectorsize = MAX(sectorsize, pp->sectorsize); 136289b17223SAlexander Motin continue; 136389b17223SAlexander Motin } 136489b17223SAlexander Motin 136589b17223SAlexander Motin g_topology_lock(); 136689b17223SAlexander Motin cp = g_raid_open_consumer(sc, diskname); 136789b17223SAlexander Motin if (cp == NULL) { 136889b17223SAlexander Motin gctl_error(req, "Can't open disk '%s'.", 136989b17223SAlexander Motin diskname); 137089b17223SAlexander Motin g_topology_unlock(); 137189b17223SAlexander Motin error = -8; 137289b17223SAlexander Motin break; 137389b17223SAlexander Motin } 137489b17223SAlexander Motin pp = cp->provider; 137589b17223SAlexander Motin pd = malloc(sizeof(*pd), M_MD_PROMISE, M_WAITOK | M_ZERO); 137689b17223SAlexander Motin disk = g_raid_create_disk(sc); 137789b17223SAlexander Motin disk->d_md_data = (void *)pd; 137889b17223SAlexander Motin disk->d_consumer = cp; 137989b17223SAlexander Motin disks[i] = disk; 138089b17223SAlexander Motin cp->private = disk; 138189b17223SAlexander Motin g_topology_unlock(); 138289b17223SAlexander Motin 1383609a7474SAlexander Motin g_raid_get_disk_info(disk); 138489b17223SAlexander Motin 138589b17223SAlexander Motin /* Reserve some space for metadata. */ 138689b17223SAlexander Motin size = MIN(size, pp->mediasize - 131072llu * pp->sectorsize); 138789b17223SAlexander Motin sectorsize = MAX(sectorsize, pp->sectorsize); 138889b17223SAlexander Motin } 138989b17223SAlexander Motin if (error != 0) { 139089b17223SAlexander Motin for (i = 0; i < numdisks; i++) { 139189b17223SAlexander Motin if (disks[i] != NULL && 139289b17223SAlexander Motin disks[i]->d_state == G_RAID_DISK_S_NONE) 139389b17223SAlexander Motin g_raid_destroy_disk(disks[i]); 139489b17223SAlexander Motin } 139589b17223SAlexander Motin return (error); 139689b17223SAlexander Motin } 139789b17223SAlexander Motin 139814e2cd0aSAlexander Motin if (sectorsize <= 0) { 139914e2cd0aSAlexander Motin gctl_error(req, "Can't get sector size."); 140014e2cd0aSAlexander Motin return (-8); 140114e2cd0aSAlexander Motin } 140214e2cd0aSAlexander Motin 140389b17223SAlexander Motin /* Handle size argument. */ 140489b17223SAlexander Motin len = sizeof(*sizearg); 140589b17223SAlexander Motin sizearg = gctl_get_param(req, "size", &len); 140689b17223SAlexander Motin if (sizearg != NULL && len == sizeof(*sizearg) && 140789b17223SAlexander Motin *sizearg > 0) { 140889b17223SAlexander Motin if (*sizearg > size) { 140989b17223SAlexander Motin gctl_error(req, "Size too big %lld > %lld.", 141089b17223SAlexander Motin (long long)*sizearg, (long long)size); 141189b17223SAlexander Motin return (-9); 141289b17223SAlexander Motin } 141389b17223SAlexander Motin size = *sizearg; 141489b17223SAlexander Motin } 141589b17223SAlexander Motin 141689b17223SAlexander Motin /* Handle strip argument. */ 141789b17223SAlexander Motin strip = 131072; 141889b17223SAlexander Motin len = sizeof(*striparg); 141989b17223SAlexander Motin striparg = gctl_get_param(req, "strip", &len); 142089b17223SAlexander Motin if (striparg != NULL && len == sizeof(*striparg) && 142189b17223SAlexander Motin *striparg > 0) { 142289b17223SAlexander Motin if (*striparg < sectorsize) { 142389b17223SAlexander Motin gctl_error(req, "Strip size too small."); 142489b17223SAlexander Motin return (-10); 142589b17223SAlexander Motin } 142689b17223SAlexander Motin if (*striparg % sectorsize != 0) { 142789b17223SAlexander Motin gctl_error(req, "Incorrect strip size."); 142889b17223SAlexander Motin return (-11); 142989b17223SAlexander Motin } 143089b17223SAlexander Motin strip = *striparg; 143189b17223SAlexander Motin } 143289b17223SAlexander Motin 143389b17223SAlexander Motin /* Round size down to strip or sector. */ 143489b17223SAlexander Motin if (level == G_RAID_VOLUME_RL_RAID1 || 143589b17223SAlexander Motin level == G_RAID_VOLUME_RL_SINGLE || 143689b17223SAlexander Motin level == G_RAID_VOLUME_RL_CONCAT) 143789b17223SAlexander Motin size -= (size % sectorsize); 143889b17223SAlexander Motin else if (level == G_RAID_VOLUME_RL_RAID1E && 143989b17223SAlexander Motin (numdisks & 1) != 0) 144089b17223SAlexander Motin size -= (size % (2 * strip)); 144189b17223SAlexander Motin else 144289b17223SAlexander Motin size -= (size % strip); 144389b17223SAlexander Motin if (size <= 0) { 144489b17223SAlexander Motin gctl_error(req, "Size too small."); 144589b17223SAlexander Motin return (-13); 144689b17223SAlexander Motin } 144789b17223SAlexander Motin 144889b17223SAlexander Motin /* We have all we need, create things: volume, ... */ 144989b17223SAlexander Motin pv = malloc(sizeof(*pv), M_MD_PROMISE, M_WAITOK | M_ZERO); 145089b17223SAlexander Motin arc4rand(&pv->pv_id, sizeof(pv->pv_id), 0); 145189b17223SAlexander Motin pv->pv_generation = 0; 145289b17223SAlexander Motin pv->pv_started = 1; 145389b17223SAlexander Motin vol = g_raid_create_volume(sc, volname, -1); 145489b17223SAlexander Motin vol->v_md_data = pv; 145589b17223SAlexander Motin vol->v_raid_level = level; 1456fc1de960SAlexander Motin vol->v_raid_level_qualifier = qual; 145789b17223SAlexander Motin vol->v_strip_size = strip; 145889b17223SAlexander Motin vol->v_disks_count = numdisks; 145989b17223SAlexander Motin if (level == G_RAID_VOLUME_RL_RAID0 || 146089b17223SAlexander Motin level == G_RAID_VOLUME_RL_CONCAT || 146189b17223SAlexander Motin level == G_RAID_VOLUME_RL_SINGLE) 146289b17223SAlexander Motin vol->v_mediasize = size * numdisks; 146389b17223SAlexander Motin else if (level == G_RAID_VOLUME_RL_RAID1) 146489b17223SAlexander Motin vol->v_mediasize = size; 146589b17223SAlexander Motin else if (level == G_RAID_VOLUME_RL_RAID3 || 146689b17223SAlexander Motin level == G_RAID_VOLUME_RL_RAID5) 146789b17223SAlexander Motin vol->v_mediasize = size * (numdisks - 1); 146889b17223SAlexander Motin else { /* RAID1E */ 146989b17223SAlexander Motin vol->v_mediasize = ((size * numdisks) / strip / 2) * 147089b17223SAlexander Motin strip; 147189b17223SAlexander Motin } 147289b17223SAlexander Motin vol->v_sectorsize = sectorsize; 147389b17223SAlexander Motin g_raid_start_volume(vol); 147489b17223SAlexander Motin 147589b17223SAlexander Motin /* , and subdisks. */ 147689b17223SAlexander Motin for (i = 0; i < numdisks; i++) { 147789b17223SAlexander Motin disk = disks[i]; 147889b17223SAlexander Motin sd = &vol->v_subdisks[i]; 147989b17223SAlexander Motin sd->sd_disk = disk; 148089b17223SAlexander Motin sd->sd_offset = (off_t)offs[i] * 512; 148189b17223SAlexander Motin sd->sd_size = size; 148289b17223SAlexander Motin if (disk == NULL) 148389b17223SAlexander Motin continue; 148489b17223SAlexander Motin TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 148589b17223SAlexander Motin g_raid_change_disk_state(disk, 148689b17223SAlexander Motin G_RAID_DISK_S_ACTIVE); 148789b17223SAlexander Motin g_raid_change_subdisk_state(sd, 148889b17223SAlexander Motin G_RAID_SUBDISK_S_ACTIVE); 148989b17223SAlexander Motin g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 149089b17223SAlexander Motin G_RAID_EVENT_SUBDISK); 149189b17223SAlexander Motin } 149289b17223SAlexander Motin 149389b17223SAlexander Motin /* Write metadata based on created entities. */ 149489b17223SAlexander Motin G_RAID_DEBUG1(0, sc, "Array started."); 149589b17223SAlexander Motin g_raid_md_write_promise(md, vol, NULL, NULL); 149689b17223SAlexander Motin 149789b17223SAlexander Motin /* Pickup any STALE/SPARE disks to refill array if needed. */ 149889b17223SAlexander Motin g_raid_md_promise_refill(sc); 149989b17223SAlexander Motin 150089b17223SAlexander Motin g_raid_event_send(vol, G_RAID_VOLUME_E_START, 150189b17223SAlexander Motin G_RAID_EVENT_VOLUME); 150289b17223SAlexander Motin return (0); 150389b17223SAlexander Motin } 150489b17223SAlexander Motin if (strcmp(verb, "add") == 0) { 150589b17223SAlexander Motin gctl_error(req, "`add` command is not applicable, " 150689b17223SAlexander Motin "use `label` instead."); 150789b17223SAlexander Motin return (-99); 150889b17223SAlexander Motin } 150989b17223SAlexander Motin if (strcmp(verb, "delete") == 0) { 15106871a543SAlexander Motin nodename = gctl_get_asciiparam(req, "arg0"); 15116871a543SAlexander Motin if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0) 15126871a543SAlexander Motin nodename = NULL; 15136871a543SAlexander Motin 151489b17223SAlexander Motin /* Full node destruction. */ 15156871a543SAlexander Motin if (*nargs == 1 && nodename != NULL) { 151689b17223SAlexander Motin /* Check if some volume is still open. */ 151789b17223SAlexander Motin force = gctl_get_paraml(req, "force", sizeof(*force)); 151889b17223SAlexander Motin if (force != NULL && *force == 0 && 151989b17223SAlexander Motin g_raid_nopens(sc) != 0) { 152089b17223SAlexander Motin gctl_error(req, "Some volume is still open."); 152189b17223SAlexander Motin return (-4); 152289b17223SAlexander Motin } 152389b17223SAlexander Motin 152489b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 152589b17223SAlexander Motin if (disk->d_consumer) 152689b17223SAlexander Motin promise_meta_erase(disk->d_consumer); 152789b17223SAlexander Motin } 152889b17223SAlexander Motin g_raid_destroy_node(sc, 0); 152989b17223SAlexander Motin return (0); 153089b17223SAlexander Motin } 153189b17223SAlexander Motin 153289b17223SAlexander Motin /* Destroy specified volume. If it was last - all node. */ 15336871a543SAlexander Motin if (*nargs > 2) { 153489b17223SAlexander Motin gctl_error(req, "Invalid number of arguments."); 153589b17223SAlexander Motin return (-1); 153689b17223SAlexander Motin } 15376871a543SAlexander Motin volname = gctl_get_asciiparam(req, 15386871a543SAlexander Motin nodename != NULL ? "arg1" : "arg0"); 153989b17223SAlexander Motin if (volname == NULL) { 154089b17223SAlexander Motin gctl_error(req, "No volume name."); 154189b17223SAlexander Motin return (-2); 154289b17223SAlexander Motin } 154389b17223SAlexander Motin 154489b17223SAlexander Motin /* Search for volume. */ 154589b17223SAlexander Motin TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 154689b17223SAlexander Motin if (strcmp(vol->v_name, volname) == 0) 154789b17223SAlexander Motin break; 15486871a543SAlexander Motin pp = vol->v_provider; 15496871a543SAlexander Motin if (pp == NULL) 15506871a543SAlexander Motin continue; 15516871a543SAlexander Motin if (strcmp(pp->name, volname) == 0) 15526871a543SAlexander Motin break; 15536871a543SAlexander Motin if (strncmp(pp->name, "raid/", 5) == 0 && 15546871a543SAlexander Motin strcmp(pp->name + 5, volname) == 0) 15556871a543SAlexander Motin break; 155689b17223SAlexander Motin } 155789b17223SAlexander Motin if (vol == NULL) { 155889b17223SAlexander Motin i = strtol(volname, &tmp, 10); 155989b17223SAlexander Motin if (verb != volname && tmp[0] == 0) { 156089b17223SAlexander Motin TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 156189b17223SAlexander Motin if (vol->v_global_id == i) 156289b17223SAlexander Motin break; 156389b17223SAlexander Motin } 156489b17223SAlexander Motin } 156589b17223SAlexander Motin } 156689b17223SAlexander Motin if (vol == NULL) { 156789b17223SAlexander Motin gctl_error(req, "Volume '%s' not found.", volname); 156889b17223SAlexander Motin return (-3); 156989b17223SAlexander Motin } 157089b17223SAlexander Motin 157189b17223SAlexander Motin /* Check if volume is still open. */ 157289b17223SAlexander Motin force = gctl_get_paraml(req, "force", sizeof(*force)); 157389b17223SAlexander Motin if (force != NULL && *force == 0 && 157489b17223SAlexander Motin vol->v_provider_open != 0) { 157589b17223SAlexander Motin gctl_error(req, "Volume is still open."); 157689b17223SAlexander Motin return (-4); 157789b17223SAlexander Motin } 157889b17223SAlexander Motin 157989b17223SAlexander Motin /* Destroy volume and potentially node. */ 158089b17223SAlexander Motin i = 0; 158189b17223SAlexander Motin TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next) 158289b17223SAlexander Motin i++; 158389b17223SAlexander Motin if (i >= 2) { 158489b17223SAlexander Motin g_raid_destroy_volume(vol); 158589b17223SAlexander Motin g_raid_md_promise_purge_disks(sc); 158689b17223SAlexander Motin g_raid_md_write_promise(md, NULL, NULL, NULL); 158789b17223SAlexander Motin } else { 158889b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 158989b17223SAlexander Motin if (disk->d_consumer) 159089b17223SAlexander Motin promise_meta_erase(disk->d_consumer); 159189b17223SAlexander Motin } 159289b17223SAlexander Motin g_raid_destroy_node(sc, 0); 159389b17223SAlexander Motin } 159489b17223SAlexander Motin return (0); 159589b17223SAlexander Motin } 159689b17223SAlexander Motin if (strcmp(verb, "remove") == 0 || 159789b17223SAlexander Motin strcmp(verb, "fail") == 0) { 159889b17223SAlexander Motin if (*nargs < 2) { 159989b17223SAlexander Motin gctl_error(req, "Invalid number of arguments."); 160089b17223SAlexander Motin return (-1); 160189b17223SAlexander Motin } 160289b17223SAlexander Motin for (i = 1; i < *nargs; i++) { 160389b17223SAlexander Motin snprintf(arg, sizeof(arg), "arg%d", i); 160489b17223SAlexander Motin diskname = gctl_get_asciiparam(req, arg); 160589b17223SAlexander Motin if (diskname == NULL) { 160689b17223SAlexander Motin gctl_error(req, "No disk name (%s).", arg); 160789b17223SAlexander Motin error = -2; 160889b17223SAlexander Motin break; 160989b17223SAlexander Motin } 16108510f61aSXin LI if (strncmp(diskname, _PATH_DEV, 5) == 0) 161189b17223SAlexander Motin diskname += 5; 161289b17223SAlexander Motin 161389b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 161489b17223SAlexander Motin if (disk->d_consumer != NULL && 161589b17223SAlexander Motin disk->d_consumer->provider != NULL && 161689b17223SAlexander Motin strcmp(disk->d_consumer->provider->name, 161789b17223SAlexander Motin diskname) == 0) 161889b17223SAlexander Motin break; 161989b17223SAlexander Motin } 162089b17223SAlexander Motin if (disk == NULL) { 162189b17223SAlexander Motin gctl_error(req, "Disk '%s' not found.", 162289b17223SAlexander Motin diskname); 162389b17223SAlexander Motin error = -3; 162489b17223SAlexander Motin break; 162589b17223SAlexander Motin } 162689b17223SAlexander Motin 162789b17223SAlexander Motin if (strcmp(verb, "fail") == 0) { 162889b17223SAlexander Motin g_raid_md_fail_disk_promise(md, NULL, disk); 162989b17223SAlexander Motin continue; 163089b17223SAlexander Motin } 163189b17223SAlexander Motin 163289b17223SAlexander Motin /* Erase metadata on deleting disk and destroy it. */ 163389b17223SAlexander Motin promise_meta_erase(disk->d_consumer); 163489b17223SAlexander Motin g_raid_destroy_disk(disk); 163589b17223SAlexander Motin } 163689b17223SAlexander Motin g_raid_md_promise_purge_volumes(sc); 163789b17223SAlexander Motin 163889b17223SAlexander Motin /* Write updated metadata to remaining disks. */ 163989b17223SAlexander Motin g_raid_md_write_promise(md, NULL, NULL, NULL); 164089b17223SAlexander Motin 164189b17223SAlexander Motin /* Check if anything left. */ 164289b17223SAlexander Motin if (g_raid_ndisks(sc, -1) == 0) 164389b17223SAlexander Motin g_raid_destroy_node(sc, 0); 164489b17223SAlexander Motin else 164589b17223SAlexander Motin g_raid_md_promise_refill(sc); 164689b17223SAlexander Motin return (error); 164789b17223SAlexander Motin } 164889b17223SAlexander Motin if (strcmp(verb, "insert") == 0) { 164989b17223SAlexander Motin if (*nargs < 2) { 165089b17223SAlexander Motin gctl_error(req, "Invalid number of arguments."); 165189b17223SAlexander Motin return (-1); 165289b17223SAlexander Motin } 165389b17223SAlexander Motin for (i = 1; i < *nargs; i++) { 165489b17223SAlexander Motin /* Get disk name. */ 165589b17223SAlexander Motin snprintf(arg, sizeof(arg), "arg%d", i); 165689b17223SAlexander Motin diskname = gctl_get_asciiparam(req, arg); 165789b17223SAlexander Motin if (diskname == NULL) { 165889b17223SAlexander Motin gctl_error(req, "No disk name (%s).", arg); 165989b17223SAlexander Motin error = -3; 166089b17223SAlexander Motin break; 166189b17223SAlexander Motin } 166289b17223SAlexander Motin 166389b17223SAlexander Motin /* Try to find provider with specified name. */ 166489b17223SAlexander Motin g_topology_lock(); 166589b17223SAlexander Motin cp = g_raid_open_consumer(sc, diskname); 166689b17223SAlexander Motin if (cp == NULL) { 166789b17223SAlexander Motin gctl_error(req, "Can't open disk '%s'.", 166889b17223SAlexander Motin diskname); 166989b17223SAlexander Motin g_topology_unlock(); 167089b17223SAlexander Motin error = -4; 167189b17223SAlexander Motin break; 167289b17223SAlexander Motin } 1673733a1f3fSAlexander Motin pp = cp->provider; 167489b17223SAlexander Motin g_topology_unlock(); 167589b17223SAlexander Motin 167689b17223SAlexander Motin pd = malloc(sizeof(*pd), M_MD_PROMISE, M_WAITOK | M_ZERO); 167789b17223SAlexander Motin 167889b17223SAlexander Motin disk = g_raid_create_disk(sc); 167989b17223SAlexander Motin disk->d_consumer = cp; 168089b17223SAlexander Motin disk->d_md_data = (void *)pd; 168189b17223SAlexander Motin cp->private = disk; 168289b17223SAlexander Motin 1683609a7474SAlexander Motin g_raid_get_disk_info(disk); 168489b17223SAlexander Motin 168589b17223SAlexander Motin /* Welcome the "new" disk. */ 168689b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE); 168789b17223SAlexander Motin promise_meta_write_spare(cp); 168889b17223SAlexander Motin g_raid_md_promise_refill(sc); 168989b17223SAlexander Motin } 169089b17223SAlexander Motin return (error); 169189b17223SAlexander Motin } 169289b17223SAlexander Motin return (-100); 169389b17223SAlexander Motin } 169489b17223SAlexander Motin 169589b17223SAlexander Motin static int 169689b17223SAlexander Motin g_raid_md_write_promise(struct g_raid_md_object *md, struct g_raid_volume *tvol, 169789b17223SAlexander Motin struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 169889b17223SAlexander Motin { 169989b17223SAlexander Motin struct g_raid_softc *sc; 170089b17223SAlexander Motin struct g_raid_volume *vol; 170189b17223SAlexander Motin struct g_raid_subdisk *sd; 170289b17223SAlexander Motin struct g_raid_disk *disk; 170389b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 170489b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 170589b17223SAlexander Motin struct promise_raid_conf *meta; 170689b17223SAlexander Motin off_t rebuild_lba64; 170789b17223SAlexander Motin int i, j, pos, rebuild; 170889b17223SAlexander Motin 170989b17223SAlexander Motin sc = md->mdo_softc; 171089b17223SAlexander Motin 171189b17223SAlexander Motin if (sc->sc_stopping == G_RAID_DESTROY_HARD) 171289b17223SAlexander Motin return (0); 171389b17223SAlexander Motin 171489b17223SAlexander Motin /* Generate new per-volume metadata for affected volumes. */ 171589b17223SAlexander Motin TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) { 171689b17223SAlexander Motin if (vol->v_stopping) 171789b17223SAlexander Motin continue; 171889b17223SAlexander Motin 171989b17223SAlexander Motin /* Skip volumes not related to specified targets. */ 172089b17223SAlexander Motin if (tvol != NULL && vol != tvol) 172189b17223SAlexander Motin continue; 172289b17223SAlexander Motin if (tsd != NULL && vol != tsd->sd_volume) 172389b17223SAlexander Motin continue; 172489b17223SAlexander Motin if (tdisk != NULL) { 172589b17223SAlexander Motin for (i = 0; i < vol->v_disks_count; i++) { 172689b17223SAlexander Motin if (vol->v_subdisks[i].sd_disk == tdisk) 172789b17223SAlexander Motin break; 172889b17223SAlexander Motin } 172989b17223SAlexander Motin if (i >= vol->v_disks_count) 173089b17223SAlexander Motin continue; 173189b17223SAlexander Motin } 173289b17223SAlexander Motin 173389b17223SAlexander Motin pv = (struct g_raid_md_promise_pervolume *)vol->v_md_data; 173489b17223SAlexander Motin pv->pv_generation++; 173589b17223SAlexander Motin 173689b17223SAlexander Motin meta = malloc(sizeof(*meta), M_MD_PROMISE, M_WAITOK | M_ZERO); 173789b17223SAlexander Motin if (pv->pv_meta != NULL) 173889b17223SAlexander Motin memcpy(meta, pv->pv_meta, sizeof(*meta)); 173963607675SAlexander Motin memcpy(meta->promise_id, PROMISE_MAGIC, 174063607675SAlexander Motin sizeof(PROMISE_MAGIC) - 1); 174189b17223SAlexander Motin meta->dummy_0 = 0x00020000; 174289b17223SAlexander Motin meta->integrity = PROMISE_I_VALID; 174389b17223SAlexander Motin 174489b17223SAlexander Motin meta->generation = pv->pv_generation; 174589b17223SAlexander Motin meta->status = PROMISE_S_VALID | PROMISE_S_ONLINE | 174689b17223SAlexander Motin PROMISE_S_INITED | PROMISE_S_READY; 174789b17223SAlexander Motin if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED) 174889b17223SAlexander Motin meta->status |= PROMISE_S_DEGRADED; 174989b17223SAlexander Motin if (vol->v_dirty) 175089b17223SAlexander Motin meta->status |= PROMISE_S_MARKED; /* XXX: INVENTED! */ 175189b17223SAlexander Motin if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0 || 175289b17223SAlexander Motin vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE) 175389b17223SAlexander Motin meta->type = PROMISE_T_RAID0; 175489b17223SAlexander Motin else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 || 175589b17223SAlexander Motin vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) 175689b17223SAlexander Motin meta->type = PROMISE_T_RAID1; 175789b17223SAlexander Motin else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) 175889b17223SAlexander Motin meta->type = PROMISE_T_RAID3; 175989b17223SAlexander Motin else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5) 176089b17223SAlexander Motin meta->type = PROMISE_T_RAID5; 176189b17223SAlexander Motin else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) 176289b17223SAlexander Motin meta->type = PROMISE_T_SPAN; 176389b17223SAlexander Motin else 176489b17223SAlexander Motin meta->type = PROMISE_T_JBOD; 176589b17223SAlexander Motin meta->total_disks = vol->v_disks_count; 176689b17223SAlexander Motin meta->stripe_shift = ffs(vol->v_strip_size / 1024); 176789b17223SAlexander Motin meta->array_width = vol->v_disks_count; 176889b17223SAlexander Motin if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 || 176989b17223SAlexander Motin vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) 177089b17223SAlexander Motin meta->array_width /= 2; 177189b17223SAlexander Motin meta->array_number = vol->v_global_id; 1772821a0f63SAlexander Motin meta->total_sectors = vol->v_mediasize / 512; 1773821a0f63SAlexander Motin meta->total_sectors_high = (vol->v_mediasize / 512) >> 32; 1774821a0f63SAlexander Motin meta->sector_size = vol->v_sectorsize / 512; 177589b17223SAlexander Motin meta->cylinders = meta->total_sectors / (255 * 63) - 1; 177689b17223SAlexander Motin meta->heads = 254; 177789b17223SAlexander Motin meta->sectors = 63; 177889b17223SAlexander Motin meta->volume_id = pv->pv_id; 177989b17223SAlexander Motin rebuild_lba64 = UINT64_MAX; 178089b17223SAlexander Motin rebuild = 0; 178189b17223SAlexander Motin for (i = 0; i < vol->v_disks_count; i++) { 178289b17223SAlexander Motin sd = &vol->v_subdisks[i]; 178389b17223SAlexander Motin /* For RAID0+1 we need to translate order. */ 178489b17223SAlexander Motin pos = promise_meta_translate_disk(vol, i); 178589b17223SAlexander Motin meta->disks[pos].flags = PROMISE_F_VALID | 178689b17223SAlexander Motin PROMISE_F_ASSIGNED; 178789b17223SAlexander Motin if (sd->sd_state == G_RAID_SUBDISK_S_NONE) { 178889b17223SAlexander Motin meta->disks[pos].flags |= 0; 178989b17223SAlexander Motin } else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED) { 179089b17223SAlexander Motin meta->disks[pos].flags |= 179189b17223SAlexander Motin PROMISE_F_DOWN | PROMISE_F_REDIR; 179289b17223SAlexander Motin } else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD) { 179389b17223SAlexander Motin meta->disks[pos].flags |= 179489b17223SAlexander Motin PROMISE_F_ONLINE | PROMISE_F_REDIR; 179589b17223SAlexander Motin if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD) { 179689b17223SAlexander Motin rebuild_lba64 = MIN(rebuild_lba64, 179789b17223SAlexander Motin sd->sd_rebuild_pos / 512); 179889b17223SAlexander Motin } else 179989b17223SAlexander Motin rebuild_lba64 = 0; 180089b17223SAlexander Motin rebuild = 1; 180189b17223SAlexander Motin } else { 180289b17223SAlexander Motin meta->disks[pos].flags |= PROMISE_F_ONLINE; 180389b17223SAlexander Motin if (sd->sd_state < G_RAID_SUBDISK_S_ACTIVE) { 180489b17223SAlexander Motin meta->status |= PROMISE_S_MARKED; 180589b17223SAlexander Motin if (sd->sd_state == G_RAID_SUBDISK_S_RESYNC) { 180689b17223SAlexander Motin rebuild_lba64 = MIN(rebuild_lba64, 180789b17223SAlexander Motin sd->sd_rebuild_pos / 512); 180889b17223SAlexander Motin } else 180989b17223SAlexander Motin rebuild_lba64 = 0; 181089b17223SAlexander Motin } 181189b17223SAlexander Motin } 181289b17223SAlexander Motin if (pv->pv_meta != NULL) { 181389b17223SAlexander Motin meta->disks[pos].id = pv->pv_meta->disks[pos].id; 181489b17223SAlexander Motin } else { 181589b17223SAlexander Motin meta->disks[pos].number = i * 2; 181689b17223SAlexander Motin arc4rand(&meta->disks[pos].id, 181789b17223SAlexander Motin sizeof(meta->disks[pos].id), 0); 181889b17223SAlexander Motin } 181989b17223SAlexander Motin } 182089b17223SAlexander Motin promise_meta_put_name(meta, vol->v_name); 182189b17223SAlexander Motin 182289b17223SAlexander Motin /* Try to mimic AMD BIOS rebuild/resync behavior. */ 182389b17223SAlexander Motin if (rebuild_lba64 != UINT64_MAX) { 182489b17223SAlexander Motin if (rebuild) 182589b17223SAlexander Motin meta->magic_3 = 0x03040010UL; /* Rebuild? */ 182689b17223SAlexander Motin else 182789b17223SAlexander Motin meta->magic_3 = 0x03040008UL; /* Resync? */ 182889b17223SAlexander Motin /* Translate from per-disk to per-volume LBA. */ 182989b17223SAlexander Motin if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 || 183089b17223SAlexander Motin vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) { 183189b17223SAlexander Motin rebuild_lba64 *= meta->array_width; 183289b17223SAlexander Motin } else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 || 183389b17223SAlexander Motin vol->v_raid_level == G_RAID_VOLUME_RL_RAID5) { 183489b17223SAlexander Motin rebuild_lba64 *= meta->array_width - 1; 183589b17223SAlexander Motin } else 183689b17223SAlexander Motin rebuild_lba64 = 0; 183789b17223SAlexander Motin } else 183889b17223SAlexander Motin meta->magic_3 = 0x03000000UL; 183989b17223SAlexander Motin meta->rebuild_lba64 = rebuild_lba64; 184089b17223SAlexander Motin meta->magic_4 = 0x04010101UL; 184189b17223SAlexander Motin 184289b17223SAlexander Motin /* Replace per-volume metadata with new. */ 184389b17223SAlexander Motin if (pv->pv_meta != NULL) 184489b17223SAlexander Motin free(pv->pv_meta, M_MD_PROMISE); 184589b17223SAlexander Motin pv->pv_meta = meta; 184689b17223SAlexander Motin 184789b17223SAlexander Motin /* Copy new metadata to the disks, adding or replacing old. */ 184889b17223SAlexander Motin for (i = 0; i < vol->v_disks_count; i++) { 184989b17223SAlexander Motin sd = &vol->v_subdisks[i]; 185089b17223SAlexander Motin disk = sd->sd_disk; 185189b17223SAlexander Motin if (disk == NULL) 185289b17223SAlexander Motin continue; 185389b17223SAlexander Motin /* For RAID0+1 we need to translate order. */ 185489b17223SAlexander Motin pos = promise_meta_translate_disk(vol, i); 185589b17223SAlexander Motin pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data; 185689b17223SAlexander Motin for (j = 0; j < pd->pd_subdisks; j++) { 185789b17223SAlexander Motin if (pd->pd_meta[j]->volume_id == meta->volume_id) 185889b17223SAlexander Motin break; 185989b17223SAlexander Motin } 186089b17223SAlexander Motin if (j == pd->pd_subdisks) 186189b17223SAlexander Motin pd->pd_subdisks++; 186289b17223SAlexander Motin if (pd->pd_meta[j] != NULL) 186389b17223SAlexander Motin free(pd->pd_meta[j], M_MD_PROMISE); 186489b17223SAlexander Motin pd->pd_meta[j] = promise_meta_copy(meta); 186589b17223SAlexander Motin pd->pd_meta[j]->disk = meta->disks[pos]; 186689b17223SAlexander Motin pd->pd_meta[j]->disk.number = pos; 1867821a0f63SAlexander Motin pd->pd_meta[j]->disk_offset_high = 1868821a0f63SAlexander Motin (sd->sd_offset / 512) >> 32; 186989b17223SAlexander Motin pd->pd_meta[j]->disk_offset = sd->sd_offset / 512; 1870821a0f63SAlexander Motin pd->pd_meta[j]->disk_sectors_high = 1871821a0f63SAlexander Motin (sd->sd_size / 512) >> 32; 187289b17223SAlexander Motin pd->pd_meta[j]->disk_sectors = sd->sd_size / 512; 187389b17223SAlexander Motin if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD) { 1874821a0f63SAlexander Motin pd->pd_meta[j]->disk_rebuild_high = 1875821a0f63SAlexander Motin (sd->sd_rebuild_pos / 512) >> 32; 1876821a0f63SAlexander Motin pd->pd_meta[j]->disk_rebuild = 187789b17223SAlexander Motin sd->sd_rebuild_pos / 512; 1878821a0f63SAlexander Motin } else if (sd->sd_state < G_RAID_SUBDISK_S_REBUILD) { 1879821a0f63SAlexander Motin pd->pd_meta[j]->disk_rebuild_high = 0; 1880821a0f63SAlexander Motin pd->pd_meta[j]->disk_rebuild = 0; 1881821a0f63SAlexander Motin } else { 1882821a0f63SAlexander Motin pd->pd_meta[j]->disk_rebuild_high = UINT32_MAX; 1883821a0f63SAlexander Motin pd->pd_meta[j]->disk_rebuild = UINT32_MAX; 1884821a0f63SAlexander Motin } 188589b17223SAlexander Motin pd->pd_updated = 1; 188689b17223SAlexander Motin } 188789b17223SAlexander Motin } 188889b17223SAlexander Motin 188989b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 189089b17223SAlexander Motin pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data; 189189b17223SAlexander Motin if (disk->d_state != G_RAID_DISK_S_ACTIVE) 189289b17223SAlexander Motin continue; 189389b17223SAlexander Motin if (!pd->pd_updated) 189489b17223SAlexander Motin continue; 189589b17223SAlexander Motin G_RAID_DEBUG(1, "Writing Promise metadata to %s", 189689b17223SAlexander Motin g_raid_get_diskname(disk)); 189789b17223SAlexander Motin for (i = 0; i < pd->pd_subdisks; i++) 189889b17223SAlexander Motin g_raid_md_promise_print(pd->pd_meta[i]); 189989b17223SAlexander Motin promise_meta_write(disk->d_consumer, 190089b17223SAlexander Motin pd->pd_meta, pd->pd_subdisks); 190189b17223SAlexander Motin pd->pd_updated = 0; 190289b17223SAlexander Motin } 190389b17223SAlexander Motin 190489b17223SAlexander Motin return (0); 190589b17223SAlexander Motin } 190689b17223SAlexander Motin 190789b17223SAlexander Motin static int 190889b17223SAlexander Motin g_raid_md_fail_disk_promise(struct g_raid_md_object *md, 190989b17223SAlexander Motin struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 191089b17223SAlexander Motin { 191189b17223SAlexander Motin struct g_raid_softc *sc; 191289b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 191389b17223SAlexander Motin struct g_raid_subdisk *sd; 191489b17223SAlexander Motin int i, pos; 191589b17223SAlexander Motin 191689b17223SAlexander Motin sc = md->mdo_softc; 191789b17223SAlexander Motin pd = (struct g_raid_md_promise_perdisk *)tdisk->d_md_data; 191889b17223SAlexander Motin 191989b17223SAlexander Motin /* We can't fail disk that is not a part of array now. */ 192089b17223SAlexander Motin if (tdisk->d_state != G_RAID_DISK_S_ACTIVE) 192189b17223SAlexander Motin return (-1); 192289b17223SAlexander Motin 192389b17223SAlexander Motin /* 192489b17223SAlexander Motin * Mark disk as failed in metadata and try to write that metadata 192589b17223SAlexander Motin * to the disk itself to prevent it's later resurrection as STALE. 192689b17223SAlexander Motin */ 192789b17223SAlexander Motin if (pd->pd_subdisks > 0 && tdisk->d_consumer != NULL) 192889b17223SAlexander Motin G_RAID_DEBUG(1, "Writing Promise metadata to %s", 192989b17223SAlexander Motin g_raid_get_diskname(tdisk)); 193089b17223SAlexander Motin for (i = 0; i < pd->pd_subdisks; i++) { 193189b17223SAlexander Motin pd->pd_meta[i]->disk.flags |= 193289b17223SAlexander Motin PROMISE_F_DOWN | PROMISE_F_REDIR; 193389b17223SAlexander Motin pos = pd->pd_meta[i]->disk.number; 193489b17223SAlexander Motin if (pos >= 0 && pos < PROMISE_MAX_DISKS) { 193589b17223SAlexander Motin pd->pd_meta[i]->disks[pos].flags |= 193689b17223SAlexander Motin PROMISE_F_DOWN | PROMISE_F_REDIR; 193789b17223SAlexander Motin } 193889b17223SAlexander Motin g_raid_md_promise_print(pd->pd_meta[i]); 193989b17223SAlexander Motin } 194089b17223SAlexander Motin if (tdisk->d_consumer != NULL) 194189b17223SAlexander Motin promise_meta_write(tdisk->d_consumer, 194289b17223SAlexander Motin pd->pd_meta, pd->pd_subdisks); 194389b17223SAlexander Motin 194489b17223SAlexander Motin /* Change states. */ 194589b17223SAlexander Motin g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED); 194689b17223SAlexander Motin TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) { 194789b17223SAlexander Motin g_raid_change_subdisk_state(sd, 194889b17223SAlexander Motin G_RAID_SUBDISK_S_FAILED); 194989b17223SAlexander Motin g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED, 195089b17223SAlexander Motin G_RAID_EVENT_SUBDISK); 195189b17223SAlexander Motin } 195289b17223SAlexander Motin 195389b17223SAlexander Motin /* Write updated metadata to remaining disks. */ 195489b17223SAlexander Motin g_raid_md_write_promise(md, NULL, NULL, tdisk); 195589b17223SAlexander Motin 195689b17223SAlexander Motin g_raid_md_promise_refill(sc); 195789b17223SAlexander Motin return (0); 195889b17223SAlexander Motin } 195989b17223SAlexander Motin 196089b17223SAlexander Motin static int 196189b17223SAlexander Motin g_raid_md_free_disk_promise(struct g_raid_md_object *md, 196289b17223SAlexander Motin struct g_raid_disk *disk) 196389b17223SAlexander Motin { 196489b17223SAlexander Motin struct g_raid_md_promise_perdisk *pd; 196589b17223SAlexander Motin int i; 196689b17223SAlexander Motin 196789b17223SAlexander Motin pd = (struct g_raid_md_promise_perdisk *)disk->d_md_data; 196889b17223SAlexander Motin for (i = 0; i < pd->pd_subdisks; i++) { 196989b17223SAlexander Motin if (pd->pd_meta[i] != NULL) { 197089b17223SAlexander Motin free(pd->pd_meta[i], M_MD_PROMISE); 197189b17223SAlexander Motin pd->pd_meta[i] = NULL; 197289b17223SAlexander Motin } 197389b17223SAlexander Motin } 197489b17223SAlexander Motin free(pd, M_MD_PROMISE); 197589b17223SAlexander Motin disk->d_md_data = NULL; 197689b17223SAlexander Motin return (0); 197789b17223SAlexander Motin } 197889b17223SAlexander Motin 197989b17223SAlexander Motin static int 198089b17223SAlexander Motin g_raid_md_free_volume_promise(struct g_raid_md_object *md, 198189b17223SAlexander Motin struct g_raid_volume *vol) 198289b17223SAlexander Motin { 198389b17223SAlexander Motin struct g_raid_md_promise_pervolume *pv; 198489b17223SAlexander Motin 198589b17223SAlexander Motin pv = (struct g_raid_md_promise_pervolume *)vol->v_md_data; 198689b17223SAlexander Motin if (pv && pv->pv_meta != NULL) { 198789b17223SAlexander Motin free(pv->pv_meta, M_MD_PROMISE); 198889b17223SAlexander Motin pv->pv_meta = NULL; 198989b17223SAlexander Motin } 199089b17223SAlexander Motin if (pv && !pv->pv_started) { 199189b17223SAlexander Motin pv->pv_started = 1; 199289b17223SAlexander Motin callout_stop(&pv->pv_start_co); 199389b17223SAlexander Motin } 1994eb3b1cd0SAlexander Motin free(pv, M_MD_PROMISE); 1995eb3b1cd0SAlexander Motin vol->v_md_data = NULL; 199689b17223SAlexander Motin return (0); 199789b17223SAlexander Motin } 199889b17223SAlexander Motin 199989b17223SAlexander Motin static int 200089b17223SAlexander Motin g_raid_md_free_promise(struct g_raid_md_object *md) 200189b17223SAlexander Motin { 200289b17223SAlexander Motin 200389b17223SAlexander Motin return (0); 200489b17223SAlexander Motin } 200589b17223SAlexander Motin 2006c89d2fbeSAlexander Motin G_RAID_MD_DECLARE(promise, "Promise"); 2007