Home | History | Annotate | Line # | Download | only in raidframe
rf_paritymap.c revision 1.4.2.2
      1  1.4.2.2  yamt /* $NetBSD: rf_paritymap.c,v 1.4.2.2 2010/03/11 15:04:01 yamt Exp $ */
      2  1.4.2.2  yamt 
      3  1.4.2.2  yamt /*-
      4  1.4.2.2  yamt  * Copyright (c) 2009 Jed Davis.
      5  1.4.2.2  yamt  * All rights reserved.
      6  1.4.2.2  yamt  *
      7  1.4.2.2  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.4.2.2  yamt  * modification, are permitted provided that the following conditions
      9  1.4.2.2  yamt  * are met:
     10  1.4.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.4.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.4.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.4.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.4.2.2  yamt  *
     16  1.4.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.4.2.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.4.2.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.4.2.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.4.2.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.4.2.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.4.2.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.4.2.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.4.2.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.4.2.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.4.2.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     27  1.4.2.2  yamt  */
     28  1.4.2.2  yamt 
     29  1.4.2.2  yamt #include <sys/cdefs.h>
     30  1.4.2.2  yamt __KERNEL_RCSID(0, "$NetBSD: rf_paritymap.c,v 1.4.2.2 2010/03/11 15:04:01 yamt Exp $");
     31  1.4.2.2  yamt 
     32  1.4.2.2  yamt #include <sys/param.h>
     33  1.4.2.2  yamt #include <sys/callout.h>
     34  1.4.2.2  yamt #include <sys/kmem.h>
     35  1.4.2.2  yamt #include <sys/mutex.h>
     36  1.4.2.2  yamt #include <sys/rwlock.h>
     37  1.4.2.2  yamt #include <sys/systm.h>
     38  1.4.2.2  yamt #include <sys/types.h>
     39  1.4.2.2  yamt 
     40  1.4.2.2  yamt #include <dev/raidframe/rf_paritymap.h>
     41  1.4.2.2  yamt #include <dev/raidframe/rf_stripelocks.h>
     42  1.4.2.2  yamt #include <dev/raidframe/rf_layout.h>
     43  1.4.2.2  yamt #include <dev/raidframe/rf_raid.h>
     44  1.4.2.2  yamt #include <dev/raidframe/rf_parityscan.h>
     45  1.4.2.2  yamt #include <dev/raidframe/rf_kintf.h>
     46  1.4.2.2  yamt 
     47  1.4.2.2  yamt /* Important parameters: */
     48  1.4.2.2  yamt #define REGION_MINSIZE (25ULL << 20)
     49  1.4.2.2  yamt #define DFL_TICKMS      40000
     50  1.4.2.2  yamt #define DFL_COOLDOWN    8     /* 7-8 intervals of 40s = 5min +/- 20s */
     51  1.4.2.2  yamt 
     52  1.4.2.2  yamt /* Internal-use flag bits. */
     53  1.4.2.2  yamt #define TICKING 1
     54  1.4.2.2  yamt #define TICKED 2
     55  1.4.2.2  yamt 
     56  1.4.2.2  yamt /* Prototypes! */
     57  1.4.2.2  yamt static void rf_paritymap_write_locked(struct rf_paritymap *);
     58  1.4.2.2  yamt static void rf_paritymap_tick(void *);
     59  1.4.2.2  yamt static u_int rf_paritymap_nreg(RF_Raid_t *);
     60  1.4.2.2  yamt 
     61  1.4.2.2  yamt /* Extract the current status of the parity map. */
     62  1.4.2.2  yamt void
     63  1.4.2.2  yamt rf_paritymap_status(struct rf_paritymap *pm, struct rf_pmstat *ps)
     64  1.4.2.2  yamt {
     65  1.4.2.2  yamt 	memset(ps, 0, sizeof(*ps));
     66  1.4.2.2  yamt 	if (pm == NULL)
     67  1.4.2.2  yamt 		ps->enabled = 0;
     68  1.4.2.2  yamt 	else {
     69  1.4.2.2  yamt 		ps->enabled = 1;
     70  1.4.2.2  yamt 		ps->region_size = pm->region_size;
     71  1.4.2.2  yamt 		mutex_enter(&pm->lock);
     72  1.4.2.2  yamt 		memcpy(&ps->params, &pm->params, sizeof(ps->params));
     73  1.4.2.2  yamt 		memcpy(ps->dirty, pm->disk_now, sizeof(ps->dirty));
     74  1.4.2.2  yamt 		memcpy(&ps->ctrs, &pm->ctrs, sizeof(ps->ctrs));
     75  1.4.2.2  yamt 		mutex_exit(&pm->lock);
     76  1.4.2.2  yamt 	}
     77  1.4.2.2  yamt }
     78  1.4.2.2  yamt 
     79  1.4.2.2  yamt /*
     80  1.4.2.2  yamt  * Test whether parity in a given sector is suspected of being inconsistent
     81  1.4.2.2  yamt  * on disk (assuming that any pending I/O to it is allowed to complete).
     82  1.4.2.2  yamt  * This may be of interest to future work on parity scrubbing.
     83  1.4.2.2  yamt  */
     84  1.4.2.2  yamt int
     85  1.4.2.2  yamt rf_paritymap_test(struct rf_paritymap *pm, daddr_t sector)
     86  1.4.2.2  yamt {
     87  1.4.2.2  yamt 	unsigned region = sector / pm->region_size;
     88  1.4.2.2  yamt 	int retval;
     89  1.4.2.2  yamt 
     90  1.4.2.2  yamt 	mutex_enter(&pm->lock);
     91  1.4.2.2  yamt 	retval = isset(pm->disk_boot->bits, region) ? 1 : 0;
     92  1.4.2.2  yamt 	mutex_exit(&pm->lock);
     93  1.4.2.2  yamt 	return retval;
     94  1.4.2.2  yamt }
     95  1.4.2.2  yamt 
     96  1.4.2.2  yamt /* To be called before a write to the RAID is submitted. */
     97  1.4.2.2  yamt void
     98  1.4.2.2  yamt rf_paritymap_begin(struct rf_paritymap *pm, daddr_t offset, daddr_t size)
     99  1.4.2.2  yamt {
    100  1.4.2.2  yamt 	unsigned i, b, e;
    101  1.4.2.2  yamt 
    102  1.4.2.2  yamt 	b = offset / pm->region_size;
    103  1.4.2.2  yamt 	e = (offset + size - 1) / pm->region_size;
    104  1.4.2.2  yamt 
    105  1.4.2.2  yamt 	for (i = b; i <= e; i++)
    106  1.4.2.2  yamt 		rf_paritymap_begin_region(pm, i);
    107  1.4.2.2  yamt }
    108  1.4.2.2  yamt 
    109  1.4.2.2  yamt /* To be called after a write to the RAID completes. */
    110  1.4.2.2  yamt void
    111  1.4.2.2  yamt rf_paritymap_end(struct rf_paritymap *pm, daddr_t offset, daddr_t size)
    112  1.4.2.2  yamt {
    113  1.4.2.2  yamt 	unsigned i, b, e;
    114  1.4.2.2  yamt 
    115  1.4.2.2  yamt 	b = offset / pm->region_size;
    116  1.4.2.2  yamt 	e = (offset + size - 1) / pm->region_size;
    117  1.4.2.2  yamt 
    118  1.4.2.2  yamt 	for (i = b; i <= e; i++)
    119  1.4.2.2  yamt 		rf_paritymap_end_region(pm, i);
    120  1.4.2.2  yamt }
    121  1.4.2.2  yamt 
    122  1.4.2.2  yamt void
    123  1.4.2.2  yamt rf_paritymap_begin_region(struct rf_paritymap *pm, unsigned region)
    124  1.4.2.2  yamt {
    125  1.4.2.2  yamt 	int needs_write;
    126  1.4.2.2  yamt 
    127  1.4.2.2  yamt 	KASSERT(region < RF_PARITYMAP_NREG);
    128  1.4.2.2  yamt 	pm->ctrs.nwrite++;
    129  1.4.2.2  yamt 
    130  1.4.2.2  yamt 	/* If it was being kept warm, deal with that. */
    131  1.4.2.2  yamt 	mutex_enter(&pm->lock);
    132  1.4.2.2  yamt 	if (pm->current->state[region] < 0)
    133  1.4.2.2  yamt 		pm->current->state[region] = 0;
    134  1.4.2.2  yamt 
    135  1.4.2.2  yamt 	/* This shouldn't happen unless RAIDOUTSTANDING is set too high. */
    136  1.4.2.2  yamt 	KASSERT(pm->current->state[region] < 127);
    137  1.4.2.2  yamt 	pm->current->state[region]++;
    138  1.4.2.2  yamt 
    139  1.4.2.2  yamt 	needs_write = isclr(pm->disk_now->bits, region);
    140  1.4.2.2  yamt 
    141  1.4.2.2  yamt 	if (needs_write) {
    142  1.4.2.2  yamt 		KASSERT(pm->current->state[region] == 1);
    143  1.4.2.2  yamt 		rf_paritymap_write_locked(pm);
    144  1.4.2.2  yamt 	}
    145  1.4.2.2  yamt 
    146  1.4.2.2  yamt 	mutex_exit(&pm->lock);
    147  1.4.2.2  yamt }
    148  1.4.2.2  yamt 
    149  1.4.2.2  yamt void
    150  1.4.2.2  yamt rf_paritymap_end_region(struct rf_paritymap *pm, unsigned region)
    151  1.4.2.2  yamt {
    152  1.4.2.2  yamt 	KASSERT(region < RF_PARITYMAP_NREG);
    153  1.4.2.2  yamt 
    154  1.4.2.2  yamt 	mutex_enter(&pm->lock);
    155  1.4.2.2  yamt 	KASSERT(pm->current->state[region] > 0);
    156  1.4.2.2  yamt 	--pm->current->state[region];
    157  1.4.2.2  yamt 
    158  1.4.2.2  yamt 	if (pm->current->state[region] <= 0) {
    159  1.4.2.2  yamt 		pm->current->state[region] = -pm->params.cooldown;
    160  1.4.2.2  yamt 		KASSERT(pm->current->state[region] <= 0);
    161  1.4.2.2  yamt 		mutex_enter(&pm->lk_flags);
    162  1.4.2.2  yamt 		if (!(pm->flags & TICKING)) {
    163  1.4.2.2  yamt 			pm->flags |= TICKING;
    164  1.4.2.2  yamt 			mutex_exit(&pm->lk_flags);
    165  1.4.2.2  yamt 			callout_schedule(&pm->ticker,
    166  1.4.2.2  yamt 			    mstohz(pm->params.tickms));
    167  1.4.2.2  yamt 		} else
    168  1.4.2.2  yamt 			mutex_exit(&pm->lk_flags);
    169  1.4.2.2  yamt 	}
    170  1.4.2.2  yamt 	mutex_exit(&pm->lock);
    171  1.4.2.2  yamt }
    172  1.4.2.2  yamt 
    173  1.4.2.2  yamt /*
    174  1.4.2.2  yamt  * Updates the parity map to account for any changes in current activity
    175  1.4.2.2  yamt  * and/or an ongoing parity scan, then writes it to disk with appropriate
    176  1.4.2.2  yamt  * synchronization.
    177  1.4.2.2  yamt  */
    178  1.4.2.2  yamt void
    179  1.4.2.2  yamt rf_paritymap_write(struct rf_paritymap *pm)
    180  1.4.2.2  yamt {
    181  1.4.2.2  yamt 	mutex_enter(&pm->lock);
    182  1.4.2.2  yamt 	rf_paritymap_write_locked(pm);
    183  1.4.2.2  yamt 	mutex_exit(&pm->lock);
    184  1.4.2.2  yamt }
    185  1.4.2.2  yamt 
    186  1.4.2.2  yamt /* As above, but to be used when pm->lock is already held. */
    187  1.4.2.2  yamt static void
    188  1.4.2.2  yamt rf_paritymap_write_locked(struct rf_paritymap *pm)
    189  1.4.2.2  yamt {
    190  1.4.2.2  yamt 	char w, w0;
    191  1.4.2.2  yamt 	int i, j, setting, clearing;
    192  1.4.2.2  yamt 
    193  1.4.2.2  yamt 	setting = clearing = 0;
    194  1.4.2.2  yamt 	for (i = 0; i < RF_PARITYMAP_NBYTE; i++) {
    195  1.4.2.2  yamt 		w0 = pm->disk_now->bits[i];
    196  1.4.2.2  yamt 		w = pm->disk_boot->bits[i];
    197  1.4.2.2  yamt 
    198  1.4.2.2  yamt 		for (j = 0; j < NBBY; j++)
    199  1.4.2.2  yamt 			if (pm->current->state[i * NBBY + j] != 0)
    200  1.4.2.2  yamt 				w |= 1 << j;
    201  1.4.2.2  yamt 
    202  1.4.2.2  yamt 		if (w & ~w0)
    203  1.4.2.2  yamt 			setting = 1;
    204  1.4.2.2  yamt 		if (w0 & ~w)
    205  1.4.2.2  yamt 			clearing = 1;
    206  1.4.2.2  yamt 
    207  1.4.2.2  yamt 		pm->disk_now->bits[i] = w;
    208  1.4.2.2  yamt 	}
    209  1.4.2.2  yamt 	pm->ctrs.ncachesync += setting + clearing;
    210  1.4.2.2  yamt 	pm->ctrs.nclearing += clearing;
    211  1.4.2.2  yamt 
    212  1.4.2.2  yamt 	/*
    213  1.4.2.2  yamt 	 * If bits are being set in the parity map, then a sync is
    214  1.4.2.2  yamt 	 * required afterwards, so that the regions are marked dirty
    215  1.4.2.2  yamt 	 * on disk before any writes to them take place.  If bits are
    216  1.4.2.2  yamt 	 * being cleared, then a sync is required before the write, so
    217  1.4.2.2  yamt 	 * that any writes to those regions are processed before the
    218  1.4.2.2  yamt 	 * region is marked clean.  (Synchronization is somewhat
    219  1.4.2.2  yamt 	 * overkill; a write ordering barrier would suffice, but we
    220  1.4.2.2  yamt 	 * currently have no way to express that directly.)
    221  1.4.2.2  yamt 	 */
    222  1.4.2.2  yamt 	if (clearing)
    223  1.4.2.2  yamt 		rf_sync_component_caches(pm->raid);
    224  1.4.2.2  yamt 	rf_paritymap_kern_write(pm->raid, pm->disk_now);
    225  1.4.2.2  yamt 	if (setting)
    226  1.4.2.2  yamt 		rf_sync_component_caches(pm->raid);
    227  1.4.2.2  yamt }
    228  1.4.2.2  yamt 
    229  1.4.2.2  yamt /* Mark all parity as being in need of rewrite. */
    230  1.4.2.2  yamt void
    231  1.4.2.2  yamt rf_paritymap_invalidate(struct rf_paritymap *pm)
    232  1.4.2.2  yamt {
    233  1.4.2.2  yamt 	mutex_enter(&pm->lock);
    234  1.4.2.2  yamt 	memset(pm->disk_boot, ~(unsigned char)0,
    235  1.4.2.2  yamt 	    sizeof(struct rf_paritymap_ondisk));
    236  1.4.2.2  yamt 	mutex_exit(&pm->lock);
    237  1.4.2.2  yamt }
    238  1.4.2.2  yamt 
    239  1.4.2.2  yamt /* Mark all parity as being correct. */
    240  1.4.2.2  yamt void
    241  1.4.2.2  yamt rf_paritymap_forceclean(struct rf_paritymap *pm)
    242  1.4.2.2  yamt {
    243  1.4.2.2  yamt 	mutex_enter(&pm->lock);
    244  1.4.2.2  yamt 	memset(pm->disk_boot, (unsigned char)0,
    245  1.4.2.2  yamt 	    sizeof(struct rf_paritymap_ondisk));
    246  1.4.2.2  yamt 	mutex_exit(&pm->lock);
    247  1.4.2.2  yamt }
    248  1.4.2.2  yamt 
    249  1.4.2.2  yamt /*
    250  1.4.2.2  yamt  * The cooldown callout routine just defers its work to a thread; it can't do
    251  1.4.2.2  yamt  * the parity map write itself as it would block, and although mutex-induced
    252  1.4.2.2  yamt  * blocking is permitted it seems wise to avoid tying up the softint.
    253  1.4.2.2  yamt  */
    254  1.4.2.2  yamt static void
    255  1.4.2.2  yamt rf_paritymap_tick(void *arg)
    256  1.4.2.2  yamt {
    257  1.4.2.2  yamt 	struct rf_paritymap *pm = arg;
    258  1.4.2.2  yamt 
    259  1.4.2.2  yamt 	mutex_enter(&pm->lk_flags);
    260  1.4.2.2  yamt 	pm->flags |= TICKED;
    261  1.4.2.2  yamt 	mutex_exit(&pm->lk_flags);
    262  1.4.2.2  yamt 	wakeup(&(pm->raid->iodone)); /* XXX */
    263  1.4.2.2  yamt }
    264  1.4.2.2  yamt 
    265  1.4.2.2  yamt /*
    266  1.4.2.2  yamt  * This is where the parity cooling work (and rearming the callout if needed)
    267  1.4.2.2  yamt  * is done; the raidio thread calls it when woken up, as by the above.
    268  1.4.2.2  yamt  */
    269  1.4.2.2  yamt void
    270  1.4.2.2  yamt rf_paritymap_checkwork(struct rf_paritymap *pm)
    271  1.4.2.2  yamt {
    272  1.4.2.2  yamt 	int i, zerop, progressp;
    273  1.4.2.2  yamt 
    274  1.4.2.2  yamt 	mutex_enter(&pm->lk_flags);
    275  1.4.2.2  yamt 	if (pm->flags & TICKED) {
    276  1.4.2.2  yamt 		zerop = progressp = 0;
    277  1.4.2.2  yamt 
    278  1.4.2.2  yamt 		pm->flags &= ~TICKED;
    279  1.4.2.2  yamt 		mutex_exit(&pm->lk_flags);
    280  1.4.2.2  yamt 
    281  1.4.2.2  yamt 		mutex_enter(&pm->lock);
    282  1.4.2.2  yamt 		for (i = 0; i < RF_PARITYMAP_NREG; i++) {
    283  1.4.2.2  yamt 			if (pm->current->state[i] < 0) {
    284  1.4.2.2  yamt 				progressp = 1;
    285  1.4.2.2  yamt 				pm->current->state[i]++;
    286  1.4.2.2  yamt 				if (pm->current->state[i] == 0)
    287  1.4.2.2  yamt 					zerop = 1;
    288  1.4.2.2  yamt 			}
    289  1.4.2.2  yamt 		}
    290  1.4.2.2  yamt 
    291  1.4.2.2  yamt 		if (progressp)
    292  1.4.2.2  yamt 			callout_schedule(&pm->ticker,
    293  1.4.2.2  yamt 			    mstohz(pm->params.tickms));
    294  1.4.2.2  yamt 		else {
    295  1.4.2.2  yamt 			mutex_enter(&pm->lk_flags);
    296  1.4.2.2  yamt 			pm->flags &= ~TICKING;
    297  1.4.2.2  yamt 			mutex_exit(&pm->lk_flags);
    298  1.4.2.2  yamt 		}
    299  1.4.2.2  yamt 
    300  1.4.2.2  yamt 		if (zerop)
    301  1.4.2.2  yamt 			rf_paritymap_write_locked(pm);
    302  1.4.2.2  yamt 		mutex_exit(&pm->lock);
    303  1.4.2.2  yamt 	} else
    304  1.4.2.2  yamt 		mutex_exit(&pm->lk_flags);
    305  1.4.2.2  yamt }
    306  1.4.2.2  yamt 
    307  1.4.2.2  yamt /*
    308  1.4.2.2  yamt  * Set parity map parameters; used both to alter parameters on the fly and to
    309  1.4.2.2  yamt  * establish their initial values.  Note that setting a parameter to 0 means
    310  1.4.2.2  yamt  * to leave the previous setting unchanged, and that if this is done for the
    311  1.4.2.2  yamt  * initial setting of "regions", then a default value will be computed based
    312  1.4.2.2  yamt  * on the RAID component size.
    313  1.4.2.2  yamt  */
    314  1.4.2.2  yamt int
    315  1.4.2.2  yamt rf_paritymap_set_params(struct rf_paritymap *pm,
    316  1.4.2.2  yamt     const struct rf_pmparams *params, int todisk)
    317  1.4.2.2  yamt {
    318  1.4.2.2  yamt 	int cooldown, tickms;
    319  1.4.2.2  yamt 	u_int regions;
    320  1.4.2.2  yamt 	RF_RowCol_t col;
    321  1.4.2.2  yamt 	RF_ComponentLabel_t *clabel;
    322  1.4.2.2  yamt 	RF_Raid_t *raidPtr;
    323  1.4.2.2  yamt 
    324  1.4.2.2  yamt 	cooldown = params->cooldown != 0
    325  1.4.2.2  yamt 	    ? params->cooldown : pm->params.cooldown;
    326  1.4.2.2  yamt 	tickms = params->tickms != 0
    327  1.4.2.2  yamt 	    ? params->tickms : pm->params.tickms;
    328  1.4.2.2  yamt 	regions = params->regions != 0
    329  1.4.2.2  yamt 	    ? params->regions : pm->params.regions;
    330  1.4.2.2  yamt 
    331  1.4.2.2  yamt 	if (cooldown < 1 || cooldown > 128) {
    332  1.4.2.2  yamt 		printf("raid%d: cooldown %d out of range\n", pm->raid->raidid,
    333  1.4.2.2  yamt 		    cooldown);
    334  1.4.2.2  yamt 		return (-1);
    335  1.4.2.2  yamt 	}
    336  1.4.2.2  yamt 	if (tickms < 10) {
    337  1.4.2.2  yamt 		printf("raid%d: tick time %dms out of range\n",
    338  1.4.2.2  yamt 		    pm->raid->raidid, tickms);
    339  1.4.2.2  yamt 		return (-1);
    340  1.4.2.2  yamt 	}
    341  1.4.2.2  yamt 	if (regions == 0) {
    342  1.4.2.2  yamt 		regions = rf_paritymap_nreg(pm->raid);
    343  1.4.2.2  yamt 	} else if (regions > RF_PARITYMAP_NREG) {
    344  1.4.2.2  yamt 		printf("raid%d: region count %u too large (more than %u)\n",
    345  1.4.2.2  yamt 		    pm->raid->raidid, regions, RF_PARITYMAP_NREG);
    346  1.4.2.2  yamt 		return (-1);
    347  1.4.2.2  yamt 	}
    348  1.4.2.2  yamt 
    349  1.4.2.2  yamt 	/* XXX any currently warm parity will be used with the new tickms! */
    350  1.4.2.2  yamt 	pm->params.cooldown = cooldown;
    351  1.4.2.2  yamt 	pm->params.tickms = tickms;
    352  1.4.2.2  yamt 	/* Apply the initial region count, but do not change it after that. */
    353  1.4.2.2  yamt 	if (pm->params.regions == 0)
    354  1.4.2.2  yamt 		pm->params.regions = regions;
    355  1.4.2.2  yamt 
    356  1.4.2.2  yamt 	/* So that the newly set parameters can be tested: */
    357  1.4.2.2  yamt 	pm->ctrs.nwrite = pm->ctrs.ncachesync = pm->ctrs.nclearing = 0;
    358  1.4.2.2  yamt 
    359  1.4.2.2  yamt 	if (todisk) {
    360  1.4.2.2  yamt 		raidPtr = pm->raid;
    361  1.4.2.2  yamt 		for (col = 0; col < raidPtr->numCol; col++) {
    362  1.4.2.2  yamt 			if (RF_DEAD_DISK(raidPtr->Disks[col].status))
    363  1.4.2.2  yamt 				continue;
    364  1.4.2.2  yamt 
    365  1.4.2.2  yamt 			clabel = raidget_component_label(raidPtr, col);
    366  1.4.2.2  yamt 			clabel->parity_map_ntick = cooldown;
    367  1.4.2.2  yamt 			clabel->parity_map_tickms = tickms;
    368  1.4.2.2  yamt 			clabel->parity_map_regions = regions;
    369  1.4.2.2  yamt 
    370  1.4.2.2  yamt 			/* Don't touch the disk if it's been spared */
    371  1.4.2.2  yamt 			if (clabel->status == rf_ds_spared)
    372  1.4.2.2  yamt 				continue;
    373  1.4.2.2  yamt 
    374  1.4.2.2  yamt 			raidflush_component_label(raidPtr, col);
    375  1.4.2.2  yamt 		}
    376  1.4.2.2  yamt 
    377  1.4.2.2  yamt 		/* handle the spares too... */
    378  1.4.2.2  yamt 		for (col = 0; col < raidPtr->numSpare; col++) {
    379  1.4.2.2  yamt 			if (raidPtr->Disks[raidPtr->numCol+col].status == rf_ds_used_spare) {
    380  1.4.2.2  yamt 				clabel = raidget_component_label(raidPtr, raidPtr->numCol+col);
    381  1.4.2.2  yamt 				clabel->parity_map_ntick = cooldown;
    382  1.4.2.2  yamt 				clabel->parity_map_tickms = tickms;
    383  1.4.2.2  yamt 				clabel->parity_map_regions = regions;
    384  1.4.2.2  yamt 				raidflush_component_label(raidPtr, raidPtr->numCol+col);
    385  1.4.2.2  yamt 			}
    386  1.4.2.2  yamt 		}
    387  1.4.2.2  yamt 	}
    388  1.4.2.2  yamt 	return 0;
    389  1.4.2.2  yamt }
    390  1.4.2.2  yamt 
    391  1.4.2.2  yamt /*
    392  1.4.2.2  yamt  * The number of regions may not be as many as can fit into the map, because
    393  1.4.2.2  yamt  * when regions are too small, the overhead of setting parity map bits
    394  1.4.2.2  yamt  * becomes significant in comparison to the actual I/O, while the
    395  1.4.2.2  yamt  * corresponding gains in parity verification time become negligible.  Thus,
    396  1.4.2.2  yamt  * a minimum region size (defined above) is imposed.
    397  1.4.2.2  yamt  *
    398  1.4.2.2  yamt  * Note that, if the number of regions is less than the maximum, then some of
    399  1.4.2.2  yamt  * the regions will be "fictional", corresponding to no actual disk; some
    400  1.4.2.2  yamt  * parts of the code may process them as normal, but they can not ever be
    401  1.4.2.2  yamt  * written to.
    402  1.4.2.2  yamt  */
    403  1.4.2.2  yamt static u_int
    404  1.4.2.2  yamt rf_paritymap_nreg(RF_Raid_t *raid)
    405  1.4.2.2  yamt {
    406  1.4.2.2  yamt 	daddr_t bytes_per_disk, nreg;
    407  1.4.2.2  yamt 
    408  1.4.2.2  yamt 	bytes_per_disk = raid->sectorsPerDisk << raid->logBytesPerSector;
    409  1.4.2.2  yamt 	nreg = bytes_per_disk / REGION_MINSIZE;
    410  1.4.2.2  yamt 	if (nreg > RF_PARITYMAP_NREG)
    411  1.4.2.2  yamt 		nreg = RF_PARITYMAP_NREG;
    412  1.4.2.2  yamt 
    413  1.4.2.2  yamt 	return (u_int)nreg;
    414  1.4.2.2  yamt }
    415  1.4.2.2  yamt 
    416  1.4.2.2  yamt /*
    417  1.4.2.2  yamt  * Initialize a parity map given specific parameters.  This neither reads nor
    418  1.4.2.2  yamt  * writes the parity map config in the component labels; for that, see below.
    419  1.4.2.2  yamt  */
    420  1.4.2.2  yamt int
    421  1.4.2.2  yamt rf_paritymap_init(struct rf_paritymap *pm, RF_Raid_t *raid,
    422  1.4.2.2  yamt     const struct rf_pmparams *params)
    423  1.4.2.2  yamt {
    424  1.4.2.2  yamt 	daddr_t rstripes;
    425  1.4.2.2  yamt 	struct rf_pmparams safe;
    426  1.4.2.2  yamt 
    427  1.4.2.2  yamt 	pm->raid = raid;
    428  1.4.2.2  yamt 	pm->params.regions = 0;
    429  1.4.2.2  yamt 	if (0 != rf_paritymap_set_params(pm, params, 0)) {
    430  1.4.2.2  yamt 		/*
    431  1.4.2.2  yamt 		 * If the parameters are out-of-range, then bring the
    432  1.4.2.2  yamt 		 * parity map up with something reasonable, so that
    433  1.4.2.2  yamt 		 * the admin can at least go and fix it (or ignore it
    434  1.4.2.2  yamt 		 * entirely).
    435  1.4.2.2  yamt 		 */
    436  1.4.2.2  yamt 		safe.cooldown = DFL_COOLDOWN;
    437  1.4.2.2  yamt 		safe.tickms = DFL_TICKMS;
    438  1.4.2.2  yamt 		safe.regions = 0;
    439  1.4.2.2  yamt 
    440  1.4.2.2  yamt 		if (0 != rf_paritymap_set_params(pm, &safe, 0))
    441  1.4.2.2  yamt 			return (-1);
    442  1.4.2.2  yamt 	}
    443  1.4.2.2  yamt 
    444  1.4.2.2  yamt 	rstripes = howmany(raid->Layout.numStripe, pm->params.regions);
    445  1.4.2.2  yamt 	pm->region_size = rstripes * raid->Layout.dataSectorsPerStripe;
    446  1.4.2.2  yamt 
    447  1.4.2.2  yamt 	callout_init(&pm->ticker, CALLOUT_MPSAFE);
    448  1.4.2.2  yamt 	callout_setfunc(&pm->ticker, rf_paritymap_tick, pm);
    449  1.4.2.2  yamt 	pm->flags = 0;
    450  1.4.2.2  yamt 
    451  1.4.2.2  yamt 	pm->disk_boot = kmem_alloc(sizeof(struct rf_paritymap_ondisk),
    452  1.4.2.2  yamt 	    KM_SLEEP);
    453  1.4.2.2  yamt 	pm->disk_now = kmem_alloc(sizeof(struct rf_paritymap_ondisk),
    454  1.4.2.2  yamt 	    KM_SLEEP);
    455  1.4.2.2  yamt 	pm->current = kmem_zalloc(sizeof(struct rf_paritymap_current),
    456  1.4.2.2  yamt 	    KM_SLEEP);
    457  1.4.2.2  yamt 
    458  1.4.2.2  yamt 	rf_paritymap_kern_read(pm->raid, pm->disk_boot);
    459  1.4.2.2  yamt 	memcpy(pm->disk_now, pm->disk_boot, sizeof(*pm->disk_now));
    460  1.4.2.2  yamt 
    461  1.4.2.2  yamt 	mutex_init(&pm->lock, MUTEX_DEFAULT, IPL_NONE);
    462  1.4.2.2  yamt 	mutex_init(&pm->lk_flags, MUTEX_DEFAULT, IPL_SOFTCLOCK);
    463  1.4.2.2  yamt 
    464  1.4.2.2  yamt 	return 0;
    465  1.4.2.2  yamt }
    466  1.4.2.2  yamt 
    467  1.4.2.2  yamt /*
    468  1.4.2.2  yamt  * Destroys a parity map; unless "force" is set, also cleans parity for any
    469  1.4.2.2  yamt  * regions which were still in cooldown (but are not dirty on disk).
    470  1.4.2.2  yamt  */
    471  1.4.2.2  yamt void
    472  1.4.2.2  yamt rf_paritymap_destroy(struct rf_paritymap *pm, int force)
    473  1.4.2.2  yamt {
    474  1.4.2.2  yamt 	int i;
    475  1.4.2.2  yamt 
    476  1.4.2.2  yamt 	callout_halt(&pm->ticker, NULL); /* XXX stop? halt? */
    477  1.4.2.2  yamt 	callout_destroy(&pm->ticker);
    478  1.4.2.2  yamt 
    479  1.4.2.2  yamt 	if (!force) {
    480  1.4.2.2  yamt 		for (i = 0; i < RF_PARITYMAP_NREG; i++) {
    481  1.4.2.2  yamt 			/* XXX check for > 0 ? */
    482  1.4.2.2  yamt 			if (pm->current->state[i] < 0)
    483  1.4.2.2  yamt 				pm->current->state[i] = 0;
    484  1.4.2.2  yamt 		}
    485  1.4.2.2  yamt 
    486  1.4.2.2  yamt 		rf_paritymap_write_locked(pm);
    487  1.4.2.2  yamt 	}
    488  1.4.2.2  yamt 
    489  1.4.2.2  yamt 	mutex_destroy(&pm->lock);
    490  1.4.2.2  yamt 	mutex_destroy(&pm->lk_flags);
    491  1.4.2.2  yamt 
    492  1.4.2.2  yamt 	kmem_free(pm->disk_boot, sizeof(struct rf_paritymap_ondisk));
    493  1.4.2.2  yamt 	kmem_free(pm->disk_now, sizeof(struct rf_paritymap_ondisk));
    494  1.4.2.2  yamt 	kmem_free(pm->current, sizeof(struct rf_paritymap_current));
    495  1.4.2.2  yamt }
    496  1.4.2.2  yamt 
    497  1.4.2.2  yamt /*
    498  1.4.2.2  yamt  * Rewrite parity, taking parity map into account; this is the equivalent of
    499  1.4.2.2  yamt  * the old rf_RewriteParity, and is likewise to be called from a suitable
    500  1.4.2.2  yamt  * thread and shouldn't have multiple copies running in parallel and so on.
    501  1.4.2.2  yamt  *
    502  1.4.2.2  yamt  * Note that the fictional regions are "cleaned" in one shot, so that very
    503  1.4.2.2  yamt  * small RAIDs (useful for testing) will not experience potentially severe
    504  1.4.2.2  yamt  * regressions in rewrite time.
    505  1.4.2.2  yamt  */
    506  1.4.2.2  yamt int
    507  1.4.2.2  yamt rf_paritymap_rewrite(struct rf_paritymap *pm)
    508  1.4.2.2  yamt {
    509  1.4.2.2  yamt 	int i, ret_val = 0;
    510  1.4.2.2  yamt 	daddr_t reg_b, reg_e;
    511  1.4.2.2  yamt 
    512  1.4.2.2  yamt 	/* Process only the actual regions. */
    513  1.4.2.2  yamt 	for (i = 0; i < pm->params.regions; i++) {
    514  1.4.2.2  yamt 		mutex_enter(&pm->lock);
    515  1.4.2.2  yamt 		if (isset(pm->disk_boot->bits, i)) {
    516  1.4.2.2  yamt 			mutex_exit(&pm->lock);
    517  1.4.2.2  yamt 
    518  1.4.2.2  yamt 			reg_b = i * pm->region_size;
    519  1.4.2.2  yamt 			reg_e = reg_b + pm->region_size;
    520  1.4.2.2  yamt 			if (reg_e > pm->raid->totalSectors)
    521  1.4.2.2  yamt 				reg_e = pm->raid->totalSectors;
    522  1.4.2.2  yamt 
    523  1.4.2.2  yamt 			if (rf_RewriteParityRange(pm->raid, reg_b,
    524  1.4.2.2  yamt 			    reg_e - reg_b)) {
    525  1.4.2.2  yamt 				ret_val = 1;
    526  1.4.2.2  yamt 				if (pm->raid->waitShutdown)
    527  1.4.2.2  yamt 					return ret_val;
    528  1.4.2.2  yamt 			} else {
    529  1.4.2.2  yamt 				mutex_enter(&pm->lock);
    530  1.4.2.2  yamt 				clrbit(pm->disk_boot->bits, i);
    531  1.4.2.2  yamt 				rf_paritymap_write_locked(pm);
    532  1.4.2.2  yamt 				mutex_exit(&pm->lock);
    533  1.4.2.2  yamt 			}
    534  1.4.2.2  yamt 		} else {
    535  1.4.2.2  yamt 			mutex_exit(&pm->lock);
    536  1.4.2.2  yamt 		}
    537  1.4.2.2  yamt 	}
    538  1.4.2.2  yamt 
    539  1.4.2.2  yamt 	/* Now, clear the fictional regions, if any. */
    540  1.4.2.2  yamt 	rf_paritymap_forceclean(pm);
    541  1.4.2.2  yamt 	rf_paritymap_write(pm);
    542  1.4.2.2  yamt 
    543  1.4.2.2  yamt 	return ret_val;
    544  1.4.2.2  yamt }
    545  1.4.2.2  yamt 
    546  1.4.2.2  yamt /*
    547  1.4.2.2  yamt  * How to merge the on-disk parity maps when reading them in from the
    548  1.4.2.2  yamt  * various components; returns whether they differ.  In the case that
    549  1.4.2.2  yamt  * they do differ, sets *dst to the union of *dst and *src.
    550  1.4.2.2  yamt  *
    551  1.4.2.2  yamt  * In theory, it should be safe to take the intersection (or just pick
    552  1.4.2.2  yamt  * a single component arbitrarily), but the paranoid approach costs
    553  1.4.2.2  yamt  * little.
    554  1.4.2.2  yamt  *
    555  1.4.2.2  yamt  * Appropriate locking, if any, is the responsibility of the caller.
    556  1.4.2.2  yamt  */
    557  1.4.2.2  yamt int
    558  1.4.2.2  yamt rf_paritymap_merge(struct rf_paritymap_ondisk *dst,
    559  1.4.2.2  yamt     struct rf_paritymap_ondisk *src)
    560  1.4.2.2  yamt {
    561  1.4.2.2  yamt 	int i, discrep = 0;
    562  1.4.2.2  yamt 
    563  1.4.2.2  yamt 	for (i = 0; i < RF_PARITYMAP_NBYTE; i++) {
    564  1.4.2.2  yamt 		if (dst->bits[i] != src->bits[i])
    565  1.4.2.2  yamt 			discrep = 1;
    566  1.4.2.2  yamt 		dst->bits[i] |= src->bits[i];
    567  1.4.2.2  yamt 	}
    568  1.4.2.2  yamt 
    569  1.4.2.2  yamt 	return discrep;
    570  1.4.2.2  yamt }
    571  1.4.2.2  yamt 
    572  1.4.2.2  yamt /*
    573  1.4.2.2  yamt  * Detach a parity map from its RAID.  This is not meant to be applied except
    574  1.4.2.2  yamt  * when unconfiguring the RAID after all I/O has been resolved, as otherwise
    575  1.4.2.2  yamt  * an out-of-date parity map could be treated as current.
    576  1.4.2.2  yamt  */
    577  1.4.2.2  yamt void
    578  1.4.2.2  yamt rf_paritymap_detach(RF_Raid_t *raidPtr)
    579  1.4.2.2  yamt {
    580  1.4.2.2  yamt 	if (raidPtr->parity_map == NULL)
    581  1.4.2.2  yamt 		return;
    582  1.4.2.2  yamt 
    583  1.4.2.2  yamt 	simple_lock(&(raidPtr->iodone_lock));
    584  1.4.2.2  yamt 	struct rf_paritymap *pm = raidPtr->parity_map;
    585  1.4.2.2  yamt 	raidPtr->parity_map = NULL;
    586  1.4.2.2  yamt 	simple_unlock(&(raidPtr->iodone_lock));
    587  1.4.2.2  yamt 	/* XXXjld is that enough locking?  Or too much? */
    588  1.4.2.2  yamt 	rf_paritymap_destroy(pm, 0);
    589  1.4.2.2  yamt 	kmem_free(pm, sizeof(*pm));
    590  1.4.2.2  yamt }
    591  1.4.2.2  yamt 
    592  1.4.2.2  yamt /*
    593  1.4.2.2  yamt  * Attach a parity map to a RAID set if appropriate.  Includes
    594  1.4.2.2  yamt  * configure-time processing of parity-map fields of component label.
    595  1.4.2.2  yamt  */
    596  1.4.2.2  yamt void
    597  1.4.2.2  yamt rf_paritymap_attach(RF_Raid_t *raidPtr, int force)
    598  1.4.2.2  yamt {
    599  1.4.2.2  yamt 	RF_RowCol_t col;
    600  1.4.2.2  yamt 	int pm_use, pm_zap;
    601  1.4.2.2  yamt 	int g_tickms, g_ntick, g_regions;
    602  1.4.2.2  yamt 	int good;
    603  1.4.2.2  yamt 	RF_ComponentLabel_t *clabel;
    604  1.4.2.2  yamt 	u_int flags, regions;
    605  1.4.2.2  yamt 	struct rf_pmparams params;
    606  1.4.2.2  yamt 
    607  1.4.2.2  yamt 	if (raidPtr->Layout.map->faultsTolerated == 0) {
    608  1.4.2.2  yamt 		/* There isn't any parity. */
    609  1.4.2.2  yamt 		return;
    610  1.4.2.2  yamt 	}
    611  1.4.2.2  yamt 
    612  1.4.2.2  yamt 	pm_use = 1;
    613  1.4.2.2  yamt 	pm_zap = 0;
    614  1.4.2.2  yamt 	g_tickms = DFL_TICKMS;
    615  1.4.2.2  yamt 	g_ntick = DFL_COOLDOWN;
    616  1.4.2.2  yamt 	g_regions = 0;
    617  1.4.2.2  yamt 
    618  1.4.2.2  yamt 	/*
    619  1.4.2.2  yamt 	 * Collect opinions on the set config.  If this is the initial
    620  1.4.2.2  yamt 	 * config (raidctl -C), treat all labels as invalid, since
    621  1.4.2.2  yamt 	 * there may be random data present.
    622  1.4.2.2  yamt 	 */
    623  1.4.2.2  yamt 	if (!force) {
    624  1.4.2.2  yamt 		for (col = 0; col < raidPtr->numCol; col++) {
    625  1.4.2.2  yamt 			if (RF_DEAD_DISK(raidPtr->Disks[col].status))
    626  1.4.2.2  yamt 				continue;
    627  1.4.2.2  yamt 			clabel = raidget_component_label(raidPtr, col);
    628  1.4.2.2  yamt 			flags = clabel->parity_map_flags;
    629  1.4.2.2  yamt 			/* Check for use by non-parity-map kernel. */
    630  1.4.2.2  yamt 			if (clabel->parity_map_modcount
    631  1.4.2.2  yamt 			    != clabel->mod_counter) {
    632  1.4.2.2  yamt 				flags &= ~RF_PMLABEL_WASUSED;
    633  1.4.2.2  yamt 			}
    634  1.4.2.2  yamt 
    635  1.4.2.2  yamt 			if (flags & RF_PMLABEL_VALID) {
    636  1.4.2.2  yamt 				g_tickms = clabel->parity_map_tickms;
    637  1.4.2.2  yamt 				g_ntick = clabel->parity_map_ntick;
    638  1.4.2.2  yamt 				regions = clabel->parity_map_regions;
    639  1.4.2.2  yamt 				if (g_regions == 0)
    640  1.4.2.2  yamt 					g_regions = regions;
    641  1.4.2.2  yamt 				else if (g_regions != regions) {
    642  1.4.2.2  yamt 					pm_zap = 1; /* important! */
    643  1.4.2.2  yamt 				}
    644  1.4.2.2  yamt 
    645  1.4.2.2  yamt 				if (flags & RF_PMLABEL_DISABLE) {
    646  1.4.2.2  yamt 					pm_use = 0;
    647  1.4.2.2  yamt 				}
    648  1.4.2.2  yamt 				if (!(flags & RF_PMLABEL_WASUSED)) {
    649  1.4.2.2  yamt 					pm_zap = 1;
    650  1.4.2.2  yamt 				}
    651  1.4.2.2  yamt 			} else {
    652  1.4.2.2  yamt 				pm_zap = 1;
    653  1.4.2.2  yamt 			}
    654  1.4.2.2  yamt 		}
    655  1.4.2.2  yamt 	} else {
    656  1.4.2.2  yamt 		pm_zap = 1;
    657  1.4.2.2  yamt 	}
    658  1.4.2.2  yamt 
    659  1.4.2.2  yamt 	/* Finally, create and attach the parity map. */
    660  1.4.2.2  yamt 	if (pm_use) {
    661  1.4.2.2  yamt 		params.cooldown = g_ntick;
    662  1.4.2.2  yamt 		params.tickms = g_tickms;
    663  1.4.2.2  yamt 		params.regions = g_regions;
    664  1.4.2.2  yamt 
    665  1.4.2.2  yamt 		raidPtr->parity_map = kmem_alloc(sizeof(struct rf_paritymap),
    666  1.4.2.2  yamt 		    KM_SLEEP);
    667  1.4.2.2  yamt 		if (0 != rf_paritymap_init(raidPtr->parity_map, raidPtr,
    668  1.4.2.2  yamt 			&params)) {
    669  1.4.2.2  yamt 			/* It failed; do without. */
    670  1.4.2.2  yamt 			kmem_free(raidPtr->parity_map,
    671  1.4.2.2  yamt 			    sizeof(struct rf_paritymap));
    672  1.4.2.2  yamt 			raidPtr->parity_map = NULL;
    673  1.4.2.2  yamt 			return;
    674  1.4.2.2  yamt 		}
    675  1.4.2.2  yamt 
    676  1.4.2.2  yamt 		if (g_regions == 0)
    677  1.4.2.2  yamt 			/* Pick up the autoconfigured region count. */
    678  1.4.2.2  yamt 			g_regions = raidPtr->parity_map->params.regions;
    679  1.4.2.2  yamt 
    680  1.4.2.2  yamt 		if (pm_zap) {
    681  1.4.2.2  yamt 			good = raidPtr->parity_good && !force;
    682  1.4.2.2  yamt 
    683  1.4.2.2  yamt 			if (good)
    684  1.4.2.2  yamt 				rf_paritymap_forceclean(raidPtr->parity_map);
    685  1.4.2.2  yamt 			else
    686  1.4.2.2  yamt 				rf_paritymap_invalidate(raidPtr->parity_map);
    687  1.4.2.2  yamt 			/* This needs to be on disk before WASUSED is set. */
    688  1.4.2.2  yamt 			rf_paritymap_write(raidPtr->parity_map);
    689  1.4.2.2  yamt 		}
    690  1.4.2.2  yamt 	}
    691  1.4.2.2  yamt 
    692  1.4.2.2  yamt 	/* Alter labels in-core to reflect the current view of things. */
    693  1.4.2.2  yamt 	for (col = 0; col < raidPtr->numCol; col++) {
    694  1.4.2.2  yamt 		if (RF_DEAD_DISK(raidPtr->Disks[col].status))
    695  1.4.2.2  yamt 			continue;
    696  1.4.2.2  yamt 		clabel = raidget_component_label(raidPtr, col);
    697  1.4.2.2  yamt 
    698  1.4.2.2  yamt 		if (pm_use)
    699  1.4.2.2  yamt 			flags = RF_PMLABEL_VALID | RF_PMLABEL_WASUSED;
    700  1.4.2.2  yamt 		else
    701  1.4.2.2  yamt 			flags = RF_PMLABEL_VALID | RF_PMLABEL_DISABLE;
    702  1.4.2.2  yamt 
    703  1.4.2.2  yamt 		clabel->parity_map_flags = flags;
    704  1.4.2.2  yamt 		clabel->parity_map_tickms = g_tickms;
    705  1.4.2.2  yamt 		clabel->parity_map_ntick = g_ntick;
    706  1.4.2.2  yamt 		clabel->parity_map_regions = g_regions;
    707  1.4.2.2  yamt 		raidflush_component_label(raidPtr, col);
    708  1.4.2.2  yamt 	}
    709  1.4.2.2  yamt 	/* Note that we're just in 'attach' here, and there won't
    710  1.4.2.2  yamt 	   be any spare disks at this point. */
    711  1.4.2.2  yamt }
    712  1.4.2.2  yamt 
    713  1.4.2.2  yamt /*
    714  1.4.2.2  yamt  * For initializing the parity-map fields of a component label, both on
    715  1.4.2.2  yamt  * initial creation and on reconstruct/copyback/etc.  */
    716  1.4.2.2  yamt void
    717  1.4.2.2  yamt rf_paritymap_init_label(struct rf_paritymap *pm, RF_ComponentLabel_t *clabel)
    718  1.4.2.2  yamt {
    719  1.4.2.2  yamt 	if (pm != NULL) {
    720  1.4.2.2  yamt 		clabel->parity_map_flags =
    721  1.4.2.2  yamt 		    RF_PMLABEL_VALID | RF_PMLABEL_WASUSED;
    722  1.4.2.2  yamt 		clabel->parity_map_tickms = pm->params.tickms;
    723  1.4.2.2  yamt 		clabel->parity_map_ntick = pm->params.cooldown;
    724  1.4.2.2  yamt 		/*
    725  1.4.2.2  yamt 		 * XXXjld: If the number of regions is changed on disk, and
    726  1.4.2.2  yamt 		 * then a new component is labeled before the next configure,
    727  1.4.2.2  yamt 		 * then it will get the old value and they will conflict on
    728  1.4.2.2  yamt 		 * the next boot (and the default will be used instead).
    729  1.4.2.2  yamt 		 */
    730  1.4.2.2  yamt 		clabel->parity_map_regions = pm->params.regions;
    731  1.4.2.2  yamt 	} else {
    732  1.4.2.2  yamt 		/*
    733  1.4.2.2  yamt 		 * XXXjld: if the map is disabled, and all the components are
    734  1.4.2.2  yamt 		 * replaced without an intervening unconfigure/reconfigure,
    735  1.4.2.2  yamt 		 * then it will become enabled on the next unconfig/reconfig.
    736  1.4.2.2  yamt 		 */
    737  1.4.2.2  yamt 	}
    738  1.4.2.2  yamt }
    739  1.4.2.2  yamt 
    740  1.4.2.2  yamt 
    741  1.4.2.2  yamt /* Will the parity map be disabled next time? */
    742  1.4.2.2  yamt int
    743  1.4.2.2  yamt rf_paritymap_get_disable(RF_Raid_t *raidPtr)
    744  1.4.2.2  yamt {
    745  1.4.2.2  yamt 	RF_ComponentLabel_t *clabel;
    746  1.4.2.2  yamt 	RF_RowCol_t col;
    747  1.4.2.2  yamt 	int dis;
    748  1.4.2.2  yamt 
    749  1.4.2.2  yamt 	dis = 0;
    750  1.4.2.2  yamt 	for (col = 0; col < raidPtr->numCol; col++) {
    751  1.4.2.2  yamt 		if (RF_DEAD_DISK(raidPtr->Disks[col].status))
    752  1.4.2.2  yamt 			continue;
    753  1.4.2.2  yamt 		clabel = raidget_component_label(raidPtr, col);
    754  1.4.2.2  yamt 		if (clabel->parity_map_flags & RF_PMLABEL_DISABLE)
    755  1.4.2.2  yamt 			dis = 1;
    756  1.4.2.2  yamt 	}
    757  1.4.2.2  yamt         for (col = 0; col < raidPtr->numSpare; col++) {
    758  1.4.2.2  yamt 		if (raidPtr->Disks[raidPtr->numCol+col].status != rf_ds_used_spare)
    759  1.4.2.2  yamt                         continue;
    760  1.4.2.2  yamt                 clabel = raidget_component_label(raidPtr, raidPtr->numCol+col);
    761  1.4.2.2  yamt                 if (clabel->parity_map_flags & RF_PMLABEL_DISABLE)
    762  1.4.2.2  yamt                         dis = 1;
    763  1.4.2.2  yamt         }
    764  1.4.2.2  yamt 
    765  1.4.2.2  yamt 	return dis;
    766  1.4.2.2  yamt }
    767  1.4.2.2  yamt 
    768  1.4.2.2  yamt /* Set whether the parity map will be disabled next time. */
    769  1.4.2.2  yamt void
    770  1.4.2.2  yamt rf_paritymap_set_disable(RF_Raid_t *raidPtr, int dis)
    771  1.4.2.2  yamt {
    772  1.4.2.2  yamt 	RF_ComponentLabel_t *clabel;
    773  1.4.2.2  yamt 	RF_RowCol_t col;
    774  1.4.2.2  yamt 
    775  1.4.2.2  yamt 	for (col = 0; col < raidPtr->numCol; col++) {
    776  1.4.2.2  yamt 		if (RF_DEAD_DISK(raidPtr->Disks[col].status))
    777  1.4.2.2  yamt 			continue;
    778  1.4.2.2  yamt 		clabel = raidget_component_label(raidPtr, col);
    779  1.4.2.2  yamt 		if (dis)
    780  1.4.2.2  yamt 			clabel->parity_map_flags |= RF_PMLABEL_DISABLE;
    781  1.4.2.2  yamt 		else
    782  1.4.2.2  yamt 			clabel->parity_map_flags &= ~RF_PMLABEL_DISABLE;
    783  1.4.2.2  yamt 		raidflush_component_label(raidPtr, col);
    784  1.4.2.2  yamt 	}
    785  1.4.2.2  yamt 
    786  1.4.2.2  yamt 	/* update any used spares as well */
    787  1.4.2.2  yamt 	for (col = 0; col < raidPtr->numSpare; col++) {
    788  1.4.2.2  yamt 		if (raidPtr->Disks[raidPtr->numCol+col].status != rf_ds_used_spare)
    789  1.4.2.2  yamt 			continue;
    790  1.4.2.2  yamt 
    791  1.4.2.2  yamt 		clabel = raidget_component_label(raidPtr, raidPtr->numCol+col);
    792  1.4.2.2  yamt 		if (dis)
    793  1.4.2.2  yamt 			clabel->parity_map_flags |= RF_PMLABEL_DISABLE;
    794  1.4.2.2  yamt 		else
    795  1.4.2.2  yamt 			clabel->parity_map_flags &= ~RF_PMLABEL_DISABLE;
    796  1.4.2.2  yamt 		raidflush_component_label(raidPtr, raidPtr->numCol+col);
    797  1.4.2.2  yamt 	}
    798  1.4.2.2  yamt }
    799