Print this page
expandable RAID-Z
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/zfs/metaslab.c
+++ new/usr/src/uts/common/fs/zfs/metaslab.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #pragma ident "%Z%%M% %I% %E% SMI"
27 27
28 28 #include <sys/zfs_context.h>
29 29 #include <sys/spa_impl.h>
30 30 #include <sys/dmu.h>
31 31 #include <sys/dmu_tx.h>
32 32 #include <sys/space_map.h>
33 33 #include <sys/metaslab_impl.h>
34 34 #include <sys/vdev_impl.h>
35 35 #include <sys/zio.h>
36 36
37 37 uint64_t metaslab_aliquot = 512ULL << 10;
38 38 uint64_t metaslab_gang_bang = SPA_MAXBLOCKSIZE + 1; /* force gang blocks */
39 39
40 40 /*
41 41 * ==========================================================================
42 42 * Metaslab classes
43 43 * ==========================================================================
44 44 */
45 45 metaslab_class_t *
46 46 metaslab_class_create(void)
47 47 {
48 48 metaslab_class_t *mc;
49 49
50 50 mc = kmem_zalloc(sizeof (metaslab_class_t), KM_SLEEP);
51 51
52 52 mc->mc_rotor = NULL;
53 53
54 54 return (mc);
55 55 }
56 56
57 57 void
58 58 metaslab_class_destroy(metaslab_class_t *mc)
59 59 {
60 60 metaslab_group_t *mg;
61 61
62 62 while ((mg = mc->mc_rotor) != NULL) {
63 63 metaslab_class_remove(mc, mg);
64 64 metaslab_group_destroy(mg);
65 65 }
66 66
67 67 kmem_free(mc, sizeof (metaslab_class_t));
68 68 }
69 69
70 70 void
71 71 metaslab_class_add(metaslab_class_t *mc, metaslab_group_t *mg)
72 72 {
73 73 metaslab_group_t *mgprev, *mgnext;
74 74
75 75 ASSERT(mg->mg_class == NULL);
76 76
77 77 if ((mgprev = mc->mc_rotor) == NULL) {
78 78 mg->mg_prev = mg;
79 79 mg->mg_next = mg;
80 80 } else {
81 81 mgnext = mgprev->mg_next;
82 82 mg->mg_prev = mgprev;
83 83 mg->mg_next = mgnext;
84 84 mgprev->mg_next = mg;
85 85 mgnext->mg_prev = mg;
86 86 }
87 87 mc->mc_rotor = mg;
88 88 mg->mg_class = mc;
89 89 }
90 90
91 91 void
92 92 metaslab_class_remove(metaslab_class_t *mc, metaslab_group_t *mg)
93 93 {
94 94 metaslab_group_t *mgprev, *mgnext;
95 95
96 96 ASSERT(mg->mg_class == mc);
97 97
98 98 mgprev = mg->mg_prev;
99 99 mgnext = mg->mg_next;
100 100
101 101 if (mg == mgnext) {
102 102 mc->mc_rotor = NULL;
103 103 } else {
104 104 mc->mc_rotor = mgnext;
105 105 mgprev->mg_next = mgnext;
106 106 mgnext->mg_prev = mgprev;
107 107 }
108 108
109 109 mg->mg_prev = NULL;
110 110 mg->mg_next = NULL;
111 111 mg->mg_class = NULL;
112 112 }
113 113
114 114 /*
115 115 * ==========================================================================
116 116 * Metaslab groups
117 117 * ==========================================================================
118 118 */
119 119 static int
120 120 metaslab_compare(const void *x1, const void *x2)
121 121 {
122 122 const metaslab_t *m1 = x1;
123 123 const metaslab_t *m2 = x2;
124 124
125 125 if (m1->ms_weight < m2->ms_weight)
126 126 return (1);
127 127 if (m1->ms_weight > m2->ms_weight)
128 128 return (-1);
129 129
130 130 /*
131 131 * If the weights are identical, use the offset to force uniqueness.
132 132 */
133 133 if (m1->ms_map.sm_start < m2->ms_map.sm_start)
134 134 return (-1);
135 135 if (m1->ms_map.sm_start > m2->ms_map.sm_start)
136 136 return (1);
137 137
138 138 ASSERT3P(m1, ==, m2);
139 139
140 140 return (0);
141 141 }
142 142
143 143 metaslab_group_t *
144 144 metaslab_group_create(metaslab_class_t *mc, vdev_t *vd)
145 145 {
146 146 metaslab_group_t *mg;
147 147
148 148 mg = kmem_zalloc(sizeof (metaslab_group_t), KM_SLEEP);
149 149 mutex_init(&mg->mg_lock, NULL, MUTEX_DEFAULT, NULL);
150 150 avl_create(&mg->mg_metaslab_tree, metaslab_compare,
151 151 sizeof (metaslab_t), offsetof(struct metaslab, ms_group_node));
152 152 mg->mg_aliquot = metaslab_aliquot * MAX(1, vd->vdev_children);
153 153 mg->mg_vd = vd;
154 154 metaslab_class_add(mc, mg);
155 155
156 156 return (mg);
157 157 }
158 158
159 159 void
160 160 metaslab_group_destroy(metaslab_group_t *mg)
161 161 {
162 162 avl_destroy(&mg->mg_metaslab_tree);
163 163 mutex_destroy(&mg->mg_lock);
164 164 kmem_free(mg, sizeof (metaslab_group_t));
165 165 }
166 166
167 167 static void
168 168 metaslab_group_add(metaslab_group_t *mg, metaslab_t *msp)
169 169 {
170 170 mutex_enter(&mg->mg_lock);
171 171 ASSERT(msp->ms_group == NULL);
172 172 msp->ms_group = mg;
173 173 msp->ms_weight = 0;
174 174 avl_add(&mg->mg_metaslab_tree, msp);
175 175 mutex_exit(&mg->mg_lock);
176 176 }
177 177
178 178 static void
179 179 metaslab_group_remove(metaslab_group_t *mg, metaslab_t *msp)
180 180 {
181 181 mutex_enter(&mg->mg_lock);
182 182 ASSERT(msp->ms_group == mg);
183 183 avl_remove(&mg->mg_metaslab_tree, msp);
184 184 msp->ms_group = NULL;
185 185 mutex_exit(&mg->mg_lock);
186 186 }
187 187
188 188 static void
189 189 metaslab_group_sort(metaslab_group_t *mg, metaslab_t *msp, uint64_t weight)
190 190 {
191 191 /*
192 192 * Although in principle the weight can be any value, in
193 193 * practice we do not use values in the range [1, 510].
194 194 */
195 195 ASSERT(weight >= SPA_MINBLOCKSIZE-1 || weight == 0);
196 196 ASSERT(MUTEX_HELD(&msp->ms_lock));
197 197
198 198 mutex_enter(&mg->mg_lock);
199 199 ASSERT(msp->ms_group == mg);
200 200 avl_remove(&mg->mg_metaslab_tree, msp);
201 201 msp->ms_weight = weight;
202 202 avl_add(&mg->mg_metaslab_tree, msp);
203 203 mutex_exit(&mg->mg_lock);
204 204 }
205 205
206 206 /*
207 207 * ==========================================================================
208 208 * The first-fit block allocator
209 209 * ==========================================================================
210 210 */
211 211 static void
212 212 metaslab_ff_load(space_map_t *sm)
213 213 {
214 214 ASSERT(sm->sm_ppd == NULL);
215 215 sm->sm_ppd = kmem_zalloc(64 * sizeof (uint64_t), KM_SLEEP);
216 216 }
217 217
218 218 static void
219 219 metaslab_ff_unload(space_map_t *sm)
220 220 {
221 221 kmem_free(sm->sm_ppd, 64 * sizeof (uint64_t));
222 222 sm->sm_ppd = NULL;
223 223 }
224 224
225 225 static uint64_t
226 226 metaslab_ff_alloc(space_map_t *sm, uint64_t size)
227 227 {
228 228 avl_tree_t *t = &sm->sm_root;
229 229 uint64_t align = size & -size;
230 230 uint64_t *cursor = (uint64_t *)sm->sm_ppd + highbit(align) - 1;
231 231 space_seg_t *ss, ssearch;
232 232 avl_index_t where;
233 233
234 234 ssearch.ss_start = *cursor;
235 235 ssearch.ss_end = *cursor + size;
236 236
237 237 ss = avl_find(t, &ssearch, &where);
238 238 if (ss == NULL)
239 239 ss = avl_nearest(t, where, AVL_AFTER);
240 240
241 241 while (ss != NULL) {
242 242 uint64_t offset = P2ROUNDUP(ss->ss_start, align);
243 243
244 244 if (offset + size <= ss->ss_end) {
245 245 *cursor = offset + size;
246 246 return (offset);
247 247 }
248 248 ss = AVL_NEXT(t, ss);
249 249 }
250 250
251 251 /*
252 252 * If we know we've searched the whole map (*cursor == 0), give up.
253 253 * Otherwise, reset the cursor to the beginning and try again.
254 254 */
255 255 if (*cursor == 0)
256 256 return (-1ULL);
257 257
258 258 *cursor = 0;
259 259 return (metaslab_ff_alloc(sm, size));
260 260 }
261 261
262 262 /* ARGSUSED */
263 263 static void
264 264 metaslab_ff_claim(space_map_t *sm, uint64_t start, uint64_t size)
265 265 {
266 266 /* No need to update cursor */
267 267 }
268 268
269 269 /* ARGSUSED */
270 270 static void
271 271 metaslab_ff_free(space_map_t *sm, uint64_t start, uint64_t size)
272 272 {
273 273 /* No need to update cursor */
274 274 }
275 275
276 276 static space_map_ops_t metaslab_ff_ops = {
277 277 metaslab_ff_load,
278 278 metaslab_ff_unload,
279 279 metaslab_ff_alloc,
280 280 metaslab_ff_claim,
281 281 metaslab_ff_free
282 282 };
283 283
284 284 /*
285 285 * ==========================================================================
286 286 * Metaslabs
287 287 * ==========================================================================
288 288 */
289 289 metaslab_t *
290 290 metaslab_init(metaslab_group_t *mg, space_map_obj_t *smo,
291 291 uint64_t start, uint64_t size, uint64_t txg)
292 292 {
293 293 vdev_t *vd = mg->mg_vd;
294 294 metaslab_t *msp;
295 295
296 296 msp = kmem_zalloc(sizeof (metaslab_t), KM_SLEEP);
297 297 mutex_init(&msp->ms_lock, NULL, MUTEX_DEFAULT, NULL);
298 298
299 299 msp->ms_smo_syncing = *smo;
300 300
301 301 /*
302 302 * We create the main space map here, but we don't create the
303 303 * allocmaps and freemaps until metaslab_sync_done(). This serves
304 304 * two purposes: it allows metaslab_sync_done() to detect the
305 305 * addition of new space; and for debugging, it ensures that we'd
306 306 * data fault on any attempt to use this metaslab before it's ready.
307 307 */
308 308 space_map_create(&msp->ms_map, start, size,
309 309 vd->vdev_ashift, &msp->ms_lock);
310 310
311 311 metaslab_group_add(mg, msp);
312 312
313 313 /*
314 314 * If we're opening an existing pool (txg == 0) or creating
315 315 * a new one (txg == TXG_INITIAL), all space is available now.
316 316 * If we're adding space to an existing pool, the new space
317 317 * does not become available until after this txg has synced.
318 318 */
319 319 if (txg <= TXG_INITIAL)
320 320 metaslab_sync_done(msp, 0);
321 321
322 322 if (txg != 0) {
323 323 /*
324 324 * The vdev is dirty, but the metaslab isn't -- it just needs
325 325 * to have metaslab_sync_done() invoked from vdev_sync_done().
326 326 * [We could just dirty the metaslab, but that would cause us
327 327 * to allocate a space map object for it, which is wasteful
328 328 * and would mess up the locality logic in metaslab_weight().]
329 329 */
330 330 ASSERT(TXG_CLEAN(txg) == spa_last_synced_txg(vd->vdev_spa));
331 331 vdev_dirty(vd, 0, NULL, txg);
332 332 vdev_dirty(vd, VDD_METASLAB, msp, TXG_CLEAN(txg));
333 333 }
334 334
335 335 return (msp);
336 336 }
337 337
338 338 void
339 339 metaslab_fini(metaslab_t *msp)
340 340 {
341 341 metaslab_group_t *mg = msp->ms_group;
342 342 int t;
343 343
344 344 vdev_space_update(mg->mg_vd, -msp->ms_map.sm_size,
345 345 -msp->ms_smo.smo_alloc, B_TRUE);
346 346
347 347 metaslab_group_remove(mg, msp);
348 348
349 349 mutex_enter(&msp->ms_lock);
350 350
351 351 space_map_unload(&msp->ms_map);
352 352 space_map_destroy(&msp->ms_map);
353 353
354 354 for (t = 0; t < TXG_SIZE; t++) {
355 355 space_map_destroy(&msp->ms_allocmap[t]);
356 356 space_map_destroy(&msp->ms_freemap[t]);
357 357 }
358 358
359 359 mutex_exit(&msp->ms_lock);
360 360 mutex_destroy(&msp->ms_lock);
361 361
362 362 kmem_free(msp, sizeof (metaslab_t));
363 363 }
364 364
365 365 #define METASLAB_WEIGHT_PRIMARY (1ULL << 63)
366 366 #define METASLAB_WEIGHT_SECONDARY (1ULL << 62)
367 367 #define METASLAB_ACTIVE_MASK \
368 368 (METASLAB_WEIGHT_PRIMARY | METASLAB_WEIGHT_SECONDARY)
369 369 #define METASLAB_SMO_BONUS_MULTIPLIER 2
370 370
371 371 static uint64_t
372 372 metaslab_weight(metaslab_t *msp)
373 373 {
374 374 metaslab_group_t *mg = msp->ms_group;
375 375 space_map_t *sm = &msp->ms_map;
376 376 space_map_obj_t *smo = &msp->ms_smo;
377 377 vdev_t *vd = mg->mg_vd;
378 378 uint64_t weight, space;
379 379
380 380 ASSERT(MUTEX_HELD(&msp->ms_lock));
381 381
382 382 /*
383 383 * The baseline weight is the metaslab's free space.
384 384 */
385 385 space = sm->sm_size - smo->smo_alloc;
386 386 weight = space;
387 387
388 388 /*
389 389 * Modern disks have uniform bit density and constant angular velocity.
390 390 * Therefore, the outer recording zones are faster (higher bandwidth)
391 391 * than the inner zones by the ratio of outer to inner track diameter,
392 392 * which is typically around 2:1. We account for this by assigning
393 393 * higher weight to lower metaslabs (multiplier ranging from 2x to 1x).
394 394 * In effect, this means that we'll select the metaslab with the most
395 395 * free bandwidth rather than simply the one with the most free space.
396 396 */
397 397 weight = 2 * weight -
398 398 ((sm->sm_start >> vd->vdev_ms_shift) * weight) / vd->vdev_ms_count;
399 399 ASSERT(weight >= space && weight <= 2 * space);
400 400
401 401 /*
402 402 * For locality, assign higher weight to metaslabs we've used before.
403 403 */
404 404 if (smo->smo_object != 0)
405 405 weight *= METASLAB_SMO_BONUS_MULTIPLIER;
406 406 ASSERT(weight >= space &&
407 407 weight <= 2 * METASLAB_SMO_BONUS_MULTIPLIER * space);
408 408
409 409 /*
410 410 * If this metaslab is one we're actively using, adjust its weight to
411 411 * make it preferable to any inactive metaslab so we'll polish it off.
412 412 */
413 413 weight |= (msp->ms_weight & METASLAB_ACTIVE_MASK);
414 414
415 415 return (weight);
416 416 }
417 417
418 418 static int
419 419 metaslab_activate(metaslab_t *msp, uint64_t activation_weight)
420 420 {
421 421 space_map_t *sm = &msp->ms_map;
422 422
423 423 ASSERT(MUTEX_HELD(&msp->ms_lock));
424 424
425 425 if ((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
426 426 int error = space_map_load(sm, &metaslab_ff_ops,
427 427 SM_FREE, &msp->ms_smo,
428 428 msp->ms_group->mg_vd->vdev_spa->spa_meta_objset);
429 429 if (error) {
430 430 metaslab_group_sort(msp->ms_group, msp, 0);
431 431 return (error);
432 432 }
433 433 metaslab_group_sort(msp->ms_group, msp,
434 434 msp->ms_weight | activation_weight);
435 435 }
436 436 ASSERT(sm->sm_loaded);
437 437 ASSERT(msp->ms_weight & METASLAB_ACTIVE_MASK);
438 438
439 439 return (0);
440 440 }
441 441
442 442 static void
443 443 metaslab_passivate(metaslab_t *msp, uint64_t size)
444 444 {
445 445 /*
446 446 * If size < SPA_MINBLOCKSIZE, then we will not allocate from
447 447 * this metaslab again. In that case, it had better be empty,
448 448 * or we would be leaving space on the table.
449 449 */
450 450 ASSERT(size >= SPA_MINBLOCKSIZE || msp->ms_map.sm_space == 0);
451 451 metaslab_group_sort(msp->ms_group, msp, MIN(msp->ms_weight, size));
452 452 ASSERT((msp->ms_weight & METASLAB_ACTIVE_MASK) == 0);
453 453 }
454 454
455 455 /*
456 456 * Write a metaslab to disk in the context of the specified transaction group.
457 457 */
458 458 void
459 459 metaslab_sync(metaslab_t *msp, uint64_t txg)
460 460 {
461 461 vdev_t *vd = msp->ms_group->mg_vd;
462 462 spa_t *spa = vd->vdev_spa;
463 463 objset_t *mos = spa->spa_meta_objset;
464 464 space_map_t *allocmap = &msp->ms_allocmap[txg & TXG_MASK];
465 465 space_map_t *freemap = &msp->ms_freemap[txg & TXG_MASK];
466 466 space_map_t *freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
467 467 space_map_t *sm = &msp->ms_map;
468 468 space_map_obj_t *smo = &msp->ms_smo_syncing;
469 469 dmu_buf_t *db;
470 470 dmu_tx_t *tx;
471 471 int t;
472 472
473 473 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
474 474
475 475 /*
476 476 * The only state that can actually be changing concurrently with
477 477 * metaslab_sync() is the metaslab's ms_map. No other thread can
478 478 * be modifying this txg's allocmap, freemap, freed_map, or smo.
479 479 * Therefore, we only hold ms_lock to satify space_map ASSERTs.
480 480 * We drop it whenever we call into the DMU, because the DMU
481 481 * can call down to us (e.g. via zio_free()) at any time.
482 482 */
483 483 mutex_enter(&msp->ms_lock);
484 484
485 485 if (smo->smo_object == 0) {
486 486 ASSERT(smo->smo_objsize == 0);
487 487 ASSERT(smo->smo_alloc == 0);
488 488 mutex_exit(&msp->ms_lock);
489 489 smo->smo_object = dmu_object_alloc(mos,
490 490 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
491 491 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
492 492 ASSERT(smo->smo_object != 0);
493 493 dmu_write(mos, vd->vdev_ms_array, sizeof (uint64_t) *
494 494 (sm->sm_start >> vd->vdev_ms_shift),
495 495 sizeof (uint64_t), &smo->smo_object, tx);
496 496 mutex_enter(&msp->ms_lock);
497 497 }
498 498
499 499 space_map_walk(freemap, space_map_add, freed_map);
500 500
501 501 if (sm->sm_loaded && spa_sync_pass(spa) == 1 && smo->smo_objsize >=
502 502 2 * sizeof (uint64_t) * avl_numnodes(&sm->sm_root)) {
503 503 /*
504 504 * The in-core space map representation is twice as compact
505 505 * as the on-disk one, so it's time to condense the latter
506 506 * by generating a pure allocmap from first principles.
507 507 *
508 508 * This metaslab is 100% allocated,
509 509 * minus the content of the in-core map (sm),
510 510 * minus what's been freed this txg (freed_map),
511 511 * minus allocations from txgs in the future
512 512 * (because they haven't been committed yet).
513 513 */
514 514 space_map_vacate(allocmap, NULL, NULL);
515 515 space_map_vacate(freemap, NULL, NULL);
516 516
517 517 space_map_add(allocmap, allocmap->sm_start, allocmap->sm_size);
518 518
519 519 space_map_walk(sm, space_map_remove, allocmap);
520 520 space_map_walk(freed_map, space_map_remove, allocmap);
521 521
522 522 for (t = 1; t < TXG_CONCURRENT_STATES; t++)
523 523 space_map_walk(&msp->ms_allocmap[(txg + t) & TXG_MASK],
524 524 space_map_remove, allocmap);
525 525
526 526 mutex_exit(&msp->ms_lock);
527 527 space_map_truncate(smo, mos, tx);
528 528 mutex_enter(&msp->ms_lock);
529 529 }
530 530
531 531 space_map_sync(allocmap, SM_ALLOC, smo, mos, tx);
532 532 space_map_sync(freemap, SM_FREE, smo, mos, tx);
533 533
534 534 mutex_exit(&msp->ms_lock);
535 535
536 536 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
537 537 dmu_buf_will_dirty(db, tx);
538 538 ASSERT3U(db->db_size, >=, sizeof (*smo));
539 539 bcopy(smo, db->db_data, sizeof (*smo));
540 540 dmu_buf_rele(db, FTAG);
541 541
542 542 dmu_tx_commit(tx);
543 543 }
544 544
545 545 /*
546 546 * Called after a transaction group has completely synced to mark
547 547 * all of the metaslab's free space as usable.
548 548 */
549 549 void
550 550 metaslab_sync_done(metaslab_t *msp, uint64_t txg)
551 551 {
552 552 space_map_obj_t *smo = &msp->ms_smo;
553 553 space_map_obj_t *smosync = &msp->ms_smo_syncing;
554 554 space_map_t *sm = &msp->ms_map;
555 555 space_map_t *freed_map = &msp->ms_freemap[TXG_CLEAN(txg) & TXG_MASK];
556 556 metaslab_group_t *mg = msp->ms_group;
557 557 vdev_t *vd = mg->mg_vd;
558 558 int t;
559 559
560 560 mutex_enter(&msp->ms_lock);
561 561
562 562 /*
563 563 * If this metaslab is just becoming available, initialize its
564 564 * allocmaps and freemaps and add its capacity to the vdev.
565 565 */
566 566 if (freed_map->sm_size == 0) {
567 567 for (t = 0; t < TXG_SIZE; t++) {
568 568 space_map_create(&msp->ms_allocmap[t], sm->sm_start,
569 569 sm->sm_size, sm->sm_shift, sm->sm_lock);
570 570 space_map_create(&msp->ms_freemap[t], sm->sm_start,
571 571 sm->sm_size, sm->sm_shift, sm->sm_lock);
572 572 }
573 573 vdev_space_update(vd, sm->sm_size, 0, B_TRUE);
574 574 }
575 575
576 576 vdev_space_update(vd, 0, smosync->smo_alloc - smo->smo_alloc, B_TRUE);
577 577
578 578 ASSERT(msp->ms_allocmap[txg & TXG_MASK].sm_space == 0);
579 579 ASSERT(msp->ms_freemap[txg & TXG_MASK].sm_space == 0);
580 580
581 581 /*
582 582 * If there's a space_map_load() in progress, wait for it to complete
583 583 * so that we have a consistent view of the in-core space map.
584 584 * Then, add everything we freed in this txg to the map.
585 585 */
586 586 space_map_load_wait(sm);
587 587 space_map_vacate(freed_map, sm->sm_loaded ? space_map_free : NULL, sm);
588 588
589 589 *smo = *smosync;
590 590
591 591 /*
592 592 * If the map is loaded but no longer active, evict it as soon as all
593 593 * future allocations have synced. (If we unloaded it now and then
594 594 * loaded a moment later, the map wouldn't reflect those allocations.)
595 595 */
596 596 if (sm->sm_loaded && (msp->ms_weight & METASLAB_ACTIVE_MASK) == 0) {
597 597 int evictable = 1;
598 598
599 599 for (t = 1; t < TXG_CONCURRENT_STATES; t++)
600 600 if (msp->ms_allocmap[(txg + t) & TXG_MASK].sm_space)
601 601 evictable = 0;
602 602
603 603 if (evictable)
604 604 space_map_unload(sm);
605 605 }
606 606
607 607 metaslab_group_sort(mg, msp, metaslab_weight(msp));
608 608
609 609 mutex_exit(&msp->ms_lock);
610 610 }
611 611
612 612 static uint64_t
613 613 metaslab_distance(metaslab_t *msp, dva_t *dva)
614 614 {
615 615 uint64_t ms_shift = msp->ms_group->mg_vd->vdev_ms_shift;
616 616 uint64_t offset = DVA_GET_OFFSET(dva) >> ms_shift;
617 617 uint64_t start = msp->ms_map.sm_start >> ms_shift;
618 618
619 619 if (msp->ms_group->mg_vd->vdev_id != DVA_GET_VDEV(dva))
620 620 return (1ULL << 63);
621 621
622 622 if (offset < start)
623 623 return ((start - offset) << ms_shift);
624 624 if (offset > start)
625 625 return ((offset - start) << ms_shift);
626 626 return (0);
627 627 }
628 628
629 629 static uint64_t
630 630 metaslab_group_alloc(metaslab_group_t *mg, uint64_t size, uint64_t txg,
631 631 uint64_t min_distance, dva_t *dva, int d)
632 632 {
633 633 metaslab_t *msp = NULL;
634 634 uint64_t offset = -1ULL;
635 635 avl_tree_t *t = &mg->mg_metaslab_tree;
636 636 uint64_t activation_weight;
637 637 uint64_t target_distance;
638 638 int i;
639 639
640 640 activation_weight = METASLAB_WEIGHT_PRIMARY;
641 641 for (i = 0; i < d; i++)
642 642 if (DVA_GET_VDEV(&dva[i]) == mg->mg_vd->vdev_id)
643 643 activation_weight = METASLAB_WEIGHT_SECONDARY;
644 644
645 645 for (;;) {
646 646 mutex_enter(&mg->mg_lock);
647 647 for (msp = avl_first(t); msp; msp = AVL_NEXT(t, msp)) {
648 648 if (msp->ms_weight < size) {
649 649 mutex_exit(&mg->mg_lock);
650 650 return (-1ULL);
651 651 }
652 652
653 653 if (activation_weight == METASLAB_WEIGHT_PRIMARY)
654 654 break;
655 655
656 656 target_distance = min_distance +
657 657 (msp->ms_smo.smo_alloc ? 0 : min_distance >> 1);
658 658
659 659 for (i = 0; i < d; i++)
660 660 if (metaslab_distance(msp, &dva[i]) <
661 661 target_distance)
662 662 break;
663 663 if (i == d)
664 664 break;
665 665 }
666 666 mutex_exit(&mg->mg_lock);
667 667 if (msp == NULL)
668 668 return (-1ULL);
669 669
670 670 mutex_enter(&msp->ms_lock);
671 671
672 672 /*
673 673 * Ensure that the metaslab we have selected is still
674 674 * capable of handling our request. It's possible that
675 675 * another thread may have changed the weight while we
676 676 * were blocked on the metaslab lock.
677 677 */
678 678 if (msp->ms_weight < size) {
679 679 mutex_exit(&msp->ms_lock);
680 680 continue;
681 681 }
682 682
683 683 if ((msp->ms_weight & METASLAB_WEIGHT_SECONDARY) &&
684 684 activation_weight == METASLAB_WEIGHT_PRIMARY) {
685 685 metaslab_passivate(msp,
686 686 msp->ms_weight & ~METASLAB_ACTIVE_MASK);
687 687 mutex_exit(&msp->ms_lock);
688 688 continue;
689 689 }
690 690
691 691 if (metaslab_activate(msp, activation_weight) != 0) {
692 692 mutex_exit(&msp->ms_lock);
693 693 continue;
694 694 }
695 695
696 696 if ((offset = space_map_alloc(&msp->ms_map, size)) != -1ULL)
697 697 break;
698 698
699 699 metaslab_passivate(msp, size - 1);
700 700
701 701 mutex_exit(&msp->ms_lock);
702 702 }
703 703
704 704 if (msp->ms_allocmap[txg & TXG_MASK].sm_space == 0)
705 705 vdev_dirty(mg->mg_vd, VDD_METASLAB, msp, txg);
706 706
707 707 space_map_add(&msp->ms_allocmap[txg & TXG_MASK], offset, size);
708 708
709 709 mutex_exit(&msp->ms_lock);
710 710
711 711 return (offset);
712 712 }
713 713
714 714 /*
715 715 * Allocate a block for the specified i/o.
716 716 */
717 717 static int
718 718 metaslab_alloc_dva(spa_t *spa, metaslab_class_t *mc, uint64_t psize,
719 719 dva_t *dva, int d, dva_t *hintdva, uint64_t txg, boolean_t hintdva_avoid)
720 720 {
721 721 metaslab_group_t *mg, *rotor;
722 722 vdev_t *vd;
723 723 int dshift = 3;
724 724 int all_zero;
725 725 uint64_t offset = -1ULL;
726 726 uint64_t asize;
727 727 uint64_t distance;
728 728
729 729 ASSERT(!DVA_IS_VALID(&dva[d]));
730 730
731 731 /*
732 732 * For testing, make some blocks above a certain size be gang blocks.
733 733 */
734 734 if (psize >= metaslab_gang_bang && (lbolt & 3) == 0)
735 735 return (ENOSPC);
736 736
737 737 /*
738 738 * Start at the rotor and loop through all mgs until we find something.
739 739 * Note that there's no locking on mc_rotor or mc_allocated because
740 740 * nothing actually breaks if we miss a few updates -- we just won't
741 741 * allocate quite as evenly. It all balances out over time.
742 742 *
743 743 * If we are doing ditto or log blocks, try to spread them across
744 744 * consecutive vdevs. If we're forced to reuse a vdev before we've
745 745 * allocated all of our ditto blocks, then try and spread them out on
746 746 * that vdev as much as possible. If it turns out to not be possible,
747 747 * gradually lower our standards until anything becomes acceptable.
748 748 * Also, allocating on consecutive vdevs (as opposed to random vdevs)
749 749 * gives us hope of containing our fault domains to something we're
750 750 * able to reason about. Otherwise, any two top-level vdev failures
751 751 * will guarantee the loss of data. With consecutive allocation,
752 752 * only two adjacent top-level vdev failures will result in data loss.
753 753 *
754 754 * If we are doing gang blocks (hintdva is non-NULL), try to keep
755 755 * ourselves on the same vdev as our gang block header. That
756 756 * way, we can hope for locality in vdev_cache, plus it makes our
757 757 * fault domains something tractable.
758 758 */
759 759 if (hintdva) {
760 760 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&hintdva[d]));
761 761 if (hintdva_avoid)
762 762 mg = vd->vdev_mg->mg_next;
763 763 else
764 764 mg = vd->vdev_mg;
765 765 } else if (d != 0) {
766 766 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[d - 1]));
767 767 mg = vd->vdev_mg->mg_next;
768 768 } else {
769 769 mg = mc->mc_rotor;
770 770 }
771 771
772 772 /*
773 773 * If the hint put us into the wrong class, just follow the rotor.
774 774 */
775 775 if (mg->mg_class != mc)
776 776 mg = mc->mc_rotor;
777 777
778 778 rotor = mg;
779 779 top:
780 780 all_zero = B_TRUE;
781 781 do {
782 782 vd = mg->mg_vd;
783 783 /*
784 784 * Dont allocate from faulted devices
785 785 */
786 786 if (!vdev_writeable(vd))
787 787 goto next;
788 788 /*
789 789 * Avoid writing single-copy data to a failing vdev
790 790 */
791 791 if ((vd->vdev_stat.vs_write_errors > 0 ||
792 792 vd->vdev_state < VDEV_STATE_HEALTHY) &&
793 793 d == 0 && dshift == 3) {
794 794 all_zero = B_FALSE;
795 795 goto next;
796 796 }
797 797
798 798 ASSERT(mg->mg_class == mc);
799 799
800 800 distance = vd->vdev_asize >> dshift;
801 801 if (distance <= (1ULL << vd->vdev_ms_shift))
802 802 distance = 0;
803 803 else
804 804 all_zero = B_FALSE;
805 805
806 806 asize = vdev_psize_to_asize(vd, psize);
807 807 ASSERT(P2PHASE(asize, 1ULL << vd->vdev_ashift) == 0);
808 808
809 809 offset = metaslab_group_alloc(mg, asize, txg, distance, dva, d);
810 810 if (offset != -1ULL) {
811 811 /*
812 812 * If we've just selected this metaslab group,
813 813 * figure out whether the corresponding vdev is
814 814 * over- or under-used relative to the pool,
815 815 * and set an allocation bias to even it out.
816 816 */
817 817 if (mc->mc_allocated == 0) {
818 818 vdev_stat_t *vs = &vd->vdev_stat;
819 819 uint64_t alloc, space;
820 820 int64_t vu, su;
821 821
822 822 alloc = spa_get_alloc(spa);
823 823 space = spa_get_space(spa);
824 824
825 825 /*
826 826 * Determine percent used in units of 0..1024.
827 827 * (This is just to avoid floating point.)
828 828 */
829 829 vu = (vs->vs_alloc << 10) / (vs->vs_space + 1);
830 830 su = (alloc << 10) / (space + 1);
831 831
832 832 /*
833 833 * Bias by at most +/- 25% of the aliquot.
834 834 */
835 835 mg->mg_bias = ((su - vu) *
|
↓ open down ↓ |
835 lines elided |
↑ open up ↑ |
836 836 (int64_t)mg->mg_aliquot) / (1024 * 4);
837 837 }
838 838
839 839 if (atomic_add_64_nv(&mc->mc_allocated, asize) >=
840 840 mg->mg_aliquot + mg->mg_bias) {
841 841 mc->mc_rotor = mg->mg_next;
842 842 mc->mc_allocated = 0;
843 843 }
844 844
845 845 DVA_SET_VDEV(&dva[d], vd->vdev_id);
846 - DVA_SET_OFFSET(&dva[d], offset);
847 - DVA_SET_GANG(&dva[d], 0);
846 + if (vd->vdev_ops->vdev_op_grid != NULL)
847 + DVA_SET_GRID(&dva[d],
848 + vd->vdev_ops->vdev_op_grid(vd);
848 849 DVA_SET_ASIZE(&dva[d], asize);
850 + DVA_SET_GANG(&dva[d], 0);
851 + DVA_SET_OFFSET(&dva[d], offset);
849 852
850 853 return (0);
851 854 }
852 855 next:
853 856 mc->mc_rotor = mg->mg_next;
854 857 mc->mc_allocated = 0;
855 858 } while ((mg = mg->mg_next) != rotor);
856 859
857 860 if (!all_zero) {
858 861 dshift++;
859 862 ASSERT(dshift < 64);
860 863 goto top;
861 864 }
862 865
863 866 bzero(&dva[d], sizeof (dva_t));
864 867
865 868 return (ENOSPC);
866 869 }
867 870
868 871 /*
869 872 * Free the block represented by DVA in the context of the specified
870 873 * transaction group.
871 874 */
872 875 static void
873 876 metaslab_free_dva(spa_t *spa, const dva_t *dva, uint64_t txg, boolean_t now)
874 877 {
875 878 uint64_t vdev = DVA_GET_VDEV(dva);
876 879 uint64_t offset = DVA_GET_OFFSET(dva);
877 880 uint64_t size = DVA_GET_ASIZE(dva);
878 881 vdev_t *vd;
879 882 metaslab_t *msp;
880 883
881 884 ASSERT(DVA_IS_VALID(dva));
882 885
883 886 if (txg > spa_freeze_txg(spa))
884 887 return;
885 888
886 889 if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
887 890 (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) {
888 891 cmn_err(CE_WARN, "metaslab_free_dva(): bad DVA %llu:%llu",
889 892 (u_longlong_t)vdev, (u_longlong_t)offset);
890 893 ASSERT(0);
891 894 return;
892 895 }
893 896
894 897 msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
895 898
896 899 if (DVA_GET_GANG(dva))
897 900 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
898 901
899 902 mutex_enter(&msp->ms_lock);
900 903
901 904 if (now) {
902 905 space_map_remove(&msp->ms_allocmap[txg & TXG_MASK],
903 906 offset, size);
904 907 space_map_free(&msp->ms_map, offset, size);
905 908 } else {
906 909 if (msp->ms_freemap[txg & TXG_MASK].sm_space == 0)
907 910 vdev_dirty(vd, VDD_METASLAB, msp, txg);
908 911 space_map_add(&msp->ms_freemap[txg & TXG_MASK], offset, size);
909 912
910 913 /*
911 914 * verify that this region is actually allocated in
912 915 * either a ms_allocmap or the ms_map
913 916 */
914 917 if (msp->ms_map.sm_loaded) {
915 918 boolean_t allocd = B_FALSE;
916 919 int i;
917 920
918 921 if (!space_map_contains(&msp->ms_map, offset, size)) {
919 922 allocd = B_TRUE;
920 923 } else {
921 924 for (i = 0; i < TXG_CONCURRENT_STATES; i++) {
922 925 space_map_t *sm = &msp->ms_allocmap
923 926 [(txg - i) & TXG_MASK];
924 927 if (space_map_contains(sm,
925 928 offset, size)) {
926 929 allocd = B_TRUE;
927 930 break;
928 931 }
929 932 }
930 933 }
931 934
932 935 if (!allocd) {
933 936 zfs_panic_recover("freeing free segment "
934 937 "(vdev=%llu offset=%llx size=%llx)",
935 938 (longlong_t)vdev, (longlong_t)offset,
936 939 (longlong_t)size);
937 940 }
938 941 }
939 942
940 943
941 944 }
942 945
943 946 mutex_exit(&msp->ms_lock);
944 947 }
945 948
946 949 /*
947 950 * Intent log support: upon opening the pool after a crash, notify the SPA
948 951 * of blocks that the intent log has allocated for immediate write, but
949 952 * which are still considered free by the SPA because the last transaction
950 953 * group didn't commit yet.
951 954 */
952 955 static int
953 956 metaslab_claim_dva(spa_t *spa, const dva_t *dva, uint64_t txg)
954 957 {
955 958 uint64_t vdev = DVA_GET_VDEV(dva);
956 959 uint64_t offset = DVA_GET_OFFSET(dva);
957 960 uint64_t size = DVA_GET_ASIZE(dva);
958 961 vdev_t *vd;
959 962 metaslab_t *msp;
960 963 int error;
961 964
962 965 ASSERT(DVA_IS_VALID(dva));
963 966
964 967 if ((vd = vdev_lookup_top(spa, vdev)) == NULL ||
965 968 (offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count)
966 969 return (ENXIO);
967 970
968 971 msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
969 972
970 973 if (DVA_GET_GANG(dva))
971 974 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
972 975
973 976 mutex_enter(&msp->ms_lock);
974 977
975 978 error = metaslab_activate(msp, METASLAB_WEIGHT_SECONDARY);
976 979 if (error) {
977 980 mutex_exit(&msp->ms_lock);
978 981 return (error);
979 982 }
980 983
981 984 if (msp->ms_allocmap[txg & TXG_MASK].sm_space == 0)
982 985 vdev_dirty(vd, VDD_METASLAB, msp, txg);
983 986
984 987 space_map_claim(&msp->ms_map, offset, size);
985 988 space_map_add(&msp->ms_allocmap[txg & TXG_MASK], offset, size);
986 989
987 990 mutex_exit(&msp->ms_lock);
988 991
989 992 return (0);
990 993 }
991 994
992 995 int
993 996 metaslab_alloc(spa_t *spa, metaslab_class_t *mc, uint64_t psize, blkptr_t *bp,
994 997 int ndvas, uint64_t txg, blkptr_t *hintbp, boolean_t hintbp_avoid)
995 998 {
996 999 dva_t *dva = bp->blk_dva;
997 1000 dva_t *hintdva = hintbp->blk_dva;
998 1001 int d;
999 1002 int error = 0;
1000 1003
1001 1004 if (mc->mc_rotor == NULL) /* no vdevs in this class */
1002 1005 return (ENOSPC);
1003 1006
1004 1007 ASSERT(ndvas > 0 && ndvas <= spa_max_replication(spa));
1005 1008 ASSERT(BP_GET_NDVAS(bp) == 0);
1006 1009 ASSERT(hintbp == NULL || ndvas <= BP_GET_NDVAS(hintbp));
1007 1010
1008 1011 for (d = 0; d < ndvas; d++) {
1009 1012 error = metaslab_alloc_dva(spa, mc, psize, dva, d, hintdva,
1010 1013 txg, hintbp_avoid);
1011 1014 if (error) {
1012 1015 for (d--; d >= 0; d--) {
1013 1016 metaslab_free_dva(spa, &dva[d], txg, B_TRUE);
1014 1017 bzero(&dva[d], sizeof (dva_t));
1015 1018 }
1016 1019 return (error);
1017 1020 }
1018 1021 }
1019 1022 ASSERT(error == 0);
1020 1023 ASSERT(BP_GET_NDVAS(bp) == ndvas);
1021 1024
1022 1025 return (0);
1023 1026 }
1024 1027
1025 1028 void
1026 1029 metaslab_free(spa_t *spa, const blkptr_t *bp, uint64_t txg, boolean_t now)
1027 1030 {
1028 1031 const dva_t *dva = bp->blk_dva;
1029 1032 int ndvas = BP_GET_NDVAS(bp);
1030 1033 int d;
1031 1034
1032 1035 ASSERT(!BP_IS_HOLE(bp));
1033 1036
1034 1037 for (d = 0; d < ndvas; d++)
1035 1038 metaslab_free_dva(spa, &dva[d], txg, now);
1036 1039 }
1037 1040
1038 1041 int
1039 1042 metaslab_claim(spa_t *spa, const blkptr_t *bp, uint64_t txg)
1040 1043 {
1041 1044 const dva_t *dva = bp->blk_dva;
1042 1045 int ndvas = BP_GET_NDVAS(bp);
1043 1046 int d, error;
1044 1047 int last_error = 0;
1045 1048
1046 1049 ASSERT(!BP_IS_HOLE(bp));
1047 1050
1048 1051 for (d = 0; d < ndvas; d++)
1049 1052 if ((error = metaslab_claim_dva(spa, &dva[d], txg)) != 0)
1050 1053 last_error = error;
1051 1054
1052 1055 return (last_error);
1053 1056 }
|
↓ open down ↓ |
195 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX