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