Deleted Added
full compact
space_map.c (208372) space_map.c (209962)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE

--- 5 unchanged lines hidden (view full) ---

14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE

--- 5 unchanged lines hidden (view full) ---

14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#include <sys/zfs_context.h>
27#include <sys/spa.h>
28#include <sys/dmu.h>
29#include <sys/zio.h>
30#include <sys/space_map.h>

--- 80 unchanged lines hidden (view full) ---

111 ss_before = avl_nearest(&sm->sm_root, where, AVL_BEFORE);
112 ss_after = avl_nearest(&sm->sm_root, where, AVL_AFTER);
113
114 merge_before = (ss_before != NULL && ss_before->ss_end == start);
115 merge_after = (ss_after != NULL && ss_after->ss_start == end);
116
117 if (merge_before && merge_after) {
118 avl_remove(&sm->sm_root, ss_before);
23 * Use is subject to license terms.
24 */
25
26#include <sys/zfs_context.h>
27#include <sys/spa.h>
28#include <sys/dmu.h>
29#include <sys/zio.h>
30#include <sys/space_map.h>

--- 80 unchanged lines hidden (view full) ---

111 ss_before = avl_nearest(&sm->sm_root, where, AVL_BEFORE);
112 ss_after = avl_nearest(&sm->sm_root, where, AVL_AFTER);
113
114 merge_before = (ss_before != NULL && ss_before->ss_end == start);
115 merge_after = (ss_after != NULL && ss_after->ss_start == end);
116
117 if (merge_before && merge_after) {
118 avl_remove(&sm->sm_root, ss_before);
119 if (sm->sm_pp_root) {
120 avl_remove(sm->sm_pp_root, ss_before);
121 avl_remove(sm->sm_pp_root, ss_after);
122 }
119 ss_after->ss_start = ss_before->ss_start;
120 kmem_free(ss_before, sizeof (*ss_before));
123 ss_after->ss_start = ss_before->ss_start;
124 kmem_free(ss_before, sizeof (*ss_before));
125 ss = ss_after;
121 } else if (merge_before) {
122 ss_before->ss_end = end;
126 } else if (merge_before) {
127 ss_before->ss_end = end;
128 if (sm->sm_pp_root)
129 avl_remove(sm->sm_pp_root, ss_before);
130 ss = ss_before;
123 } else if (merge_after) {
124 ss_after->ss_start = start;
131 } else if (merge_after) {
132 ss_after->ss_start = start;
133 if (sm->sm_pp_root)
134 avl_remove(sm->sm_pp_root, ss_after);
135 ss = ss_after;
125 } else {
126 ss = kmem_alloc(sizeof (*ss), KM_SLEEP);
127 ss->ss_start = start;
128 ss->ss_end = end;
129 avl_insert(&sm->sm_root, ss, where);
130 }
131
136 } else {
137 ss = kmem_alloc(sizeof (*ss), KM_SLEEP);
138 ss->ss_start = start;
139 ss->ss_end = end;
140 avl_insert(&sm->sm_root, ss, where);
141 }
142
143 if (sm->sm_pp_root)
144 avl_add(sm->sm_pp_root, ss);
145
132 sm->sm_space += size;
133}
134
135void
136space_map_remove(space_map_t *sm, uint64_t start, uint64_t size)
137{
138 avl_index_t where;
139 space_seg_t ssearch, *ss, *newseg;

--- 18 unchanged lines hidden (view full) ---

158 }
159 VERIFY3U(ss->ss_start, <=, start);
160 VERIFY3U(ss->ss_end, >=, end);
161 VERIFY(sm->sm_space - size <= sm->sm_size);
162
163 left_over = (ss->ss_start != start);
164 right_over = (ss->ss_end != end);
165
146 sm->sm_space += size;
147}
148
149void
150space_map_remove(space_map_t *sm, uint64_t start, uint64_t size)
151{
152 avl_index_t where;
153 space_seg_t ssearch, *ss, *newseg;

--- 18 unchanged lines hidden (view full) ---

172 }
173 VERIFY3U(ss->ss_start, <=, start);
174 VERIFY3U(ss->ss_end, >=, end);
175 VERIFY(sm->sm_space - size <= sm->sm_size);
176
177 left_over = (ss->ss_start != start);
178 right_over = (ss->ss_end != end);
179
180 if (sm->sm_pp_root)
181 avl_remove(sm->sm_pp_root, ss);
182
166 if (left_over && right_over) {
167 newseg = kmem_alloc(sizeof (*newseg), KM_SLEEP);
168 newseg->ss_start = end;
169 newseg->ss_end = ss->ss_end;
170 ss->ss_end = start;
171 avl_insert_here(&sm->sm_root, newseg, ss, AVL_AFTER);
183 if (left_over && right_over) {
184 newseg = kmem_alloc(sizeof (*newseg), KM_SLEEP);
185 newseg->ss_start = end;
186 newseg->ss_end = ss->ss_end;
187 ss->ss_end = start;
188 avl_insert_here(&sm->sm_root, newseg, ss, AVL_AFTER);
189 if (sm->sm_pp_root)
190 avl_add(sm->sm_pp_root, newseg);
172 } else if (left_over) {
173 ss->ss_end = start;
174 } else if (right_over) {
175 ss->ss_start = end;
176 } else {
177 avl_remove(&sm->sm_root, ss);
178 kmem_free(ss, sizeof (*ss));
191 } else if (left_over) {
192 ss->ss_end = start;
193 } else if (right_over) {
194 ss->ss_start = end;
195 } else {
196 avl_remove(&sm->sm_root, ss);
197 kmem_free(ss, sizeof (*ss));
198 ss = NULL;
179 }
180
199 }
200
201 if (sm->sm_pp_root && ss != NULL)
202 avl_add(sm->sm_pp_root, ss);
203
181 sm->sm_space -= size;
182}
183
204 sm->sm_space -= size;
205}
206
184int
207boolean_t
185space_map_contains(space_map_t *sm, uint64_t start, uint64_t size)
186{
187 avl_index_t where;
188 space_seg_t ssearch, *ss;
189 uint64_t end = start + size;
190
191 ASSERT(MUTEX_HELD(sm->sm_lock));
192 VERIFY(size != 0);

--- 23 unchanged lines hidden (view full) ---

216 sm->sm_space = 0;
217}
218
219void
220space_map_walk(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
221{
222 space_seg_t *ss;
223
208space_map_contains(space_map_t *sm, uint64_t start, uint64_t size)
209{
210 avl_index_t where;
211 space_seg_t ssearch, *ss;
212 uint64_t end = start + size;
213
214 ASSERT(MUTEX_HELD(sm->sm_lock));
215 VERIFY(size != 0);

--- 23 unchanged lines hidden (view full) ---

239 sm->sm_space = 0;
240}
241
242void
243space_map_walk(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
244{
245 space_seg_t *ss;
246
247 ASSERT(MUTEX_HELD(sm->sm_lock));
248
224 for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
225 func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
226}
227
249 for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
250 func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
251}
252
228void
229space_map_excise(space_map_t *sm, uint64_t start, uint64_t size)
230{
231 avl_tree_t *t = &sm->sm_root;
232 avl_index_t where;
233 space_seg_t *ss, search;
234 uint64_t end = start + size;
235 uint64_t rm_start, rm_end;
236
237 ASSERT(MUTEX_HELD(sm->sm_lock));
238
239 search.ss_start = start;
240 search.ss_end = start;
241
242 for (;;) {
243 ss = avl_find(t, &search, &where);
244
245 if (ss == NULL)
246 ss = avl_nearest(t, where, AVL_AFTER);
247
248 if (ss == NULL || ss->ss_start >= end)
249 break;
250
251 rm_start = MAX(ss->ss_start, start);
252 rm_end = MIN(ss->ss_end, end);
253
254 space_map_remove(sm, rm_start, rm_end - rm_start);
255 }
256}
257
258/*
253/*
259 * Replace smd with the union of smd and sms.
260 */
261void
262space_map_union(space_map_t *smd, space_map_t *sms)
263{
264 avl_tree_t *t = &sms->sm_root;
265 space_seg_t *ss;
266
267 ASSERT(MUTEX_HELD(smd->sm_lock));
268
269 /*
270 * For each source segment, remove any intersections with the
271 * destination, then add the source segment to the destination.
272 */
273 for (ss = avl_first(t); ss != NULL; ss = AVL_NEXT(t, ss)) {
274 space_map_excise(smd, ss->ss_start, ss->ss_end - ss->ss_start);
275 space_map_add(smd, ss->ss_start, ss->ss_end - ss->ss_start);
276 }
277}
278
279/*
280 * Wait for any in-progress space_map_load() to complete.
281 */
282void
283space_map_load_wait(space_map_t *sm)
284{
285 ASSERT(MUTEX_HELD(sm->sm_lock));
286
287 while (sm->sm_loading)

--- 44 unchanged lines hidden (view full) ---

332 size = MIN(end - offset, bufsize);
333 VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
334 VERIFY(size != 0);
335
336 dprintf("object=%llu offset=%llx size=%llx\n",
337 smo->smo_object, offset, size);
338
339 mutex_exit(sm->sm_lock);
254 * Wait for any in-progress space_map_load() to complete.
255 */
256void
257space_map_load_wait(space_map_t *sm)
258{
259 ASSERT(MUTEX_HELD(sm->sm_lock));
260
261 while (sm->sm_loading)

--- 44 unchanged lines hidden (view full) ---

306 size = MIN(end - offset, bufsize);
307 VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
308 VERIFY(size != 0);
309
310 dprintf("object=%llu offset=%llx size=%llx\n",
311 smo->smo_object, offset, size);
312
313 mutex_exit(sm->sm_lock);
340 error = dmu_read(os, smo->smo_object, offset, size, entry_map);
314 error = dmu_read(os, smo->smo_object, offset, size, entry_map,
315 DMU_READ_PREFETCH);
341 mutex_enter(sm->sm_lock);
342 if (error != 0)
343 break;
344
345 entry_map_end = entry_map + (size / sizeof (uint64_t));
346 for (entry = entry_map; entry < entry_map_end; entry++) {
347 uint64_t e = *entry;
348

--- 37 unchanged lines hidden (view full) ---

386
387 sm->sm_loaded = B_FALSE;
388 sm->sm_ops = NULL;
389
390 space_map_vacate(sm, NULL, NULL);
391}
392
393uint64_t
316 mutex_enter(sm->sm_lock);
317 if (error != 0)
318 break;
319
320 entry_map_end = entry_map + (size / sizeof (uint64_t));
321 for (entry = entry_map; entry < entry_map_end; entry++) {
322 uint64_t e = *entry;
323

--- 37 unchanged lines hidden (view full) ---

361
362 sm->sm_loaded = B_FALSE;
363 sm->sm_ops = NULL;
364
365 space_map_vacate(sm, NULL, NULL);
366}
367
368uint64_t
369space_map_maxsize(space_map_t *sm)
370{
371 if (sm->sm_loaded && sm->sm_ops != NULL)
372 return (sm->sm_ops->smop_max(sm));
373 else
374 return (-1ULL);
375}
376
377uint64_t
394space_map_alloc(space_map_t *sm, uint64_t size)
395{
396 uint64_t start;
397
398 start = sm->sm_ops->smop_alloc(sm, size);
399 if (start != -1ULL)
400 space_map_remove(sm, start, size);
401 return (start);

--- 98 unchanged lines hidden (view full) ---

500void
501space_map_truncate(space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
502{
503 VERIFY(dmu_free_range(os, smo->smo_object, 0, -1ULL, tx) == 0);
504
505 smo->smo_objsize = 0;
506 smo->smo_alloc = 0;
507}
378space_map_alloc(space_map_t *sm, uint64_t size)
379{
380 uint64_t start;
381
382 start = sm->sm_ops->smop_alloc(sm, size);
383 if (start != -1ULL)
384 space_map_remove(sm, start, size);
385 return (start);

--- 98 unchanged lines hidden (view full) ---

484void
485space_map_truncate(space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
486{
487 VERIFY(dmu_free_range(os, smo->smo_object, 0, -1ULL, tx) == 0);
488
489 smo->smo_objsize = 0;
490 smo->smo_alloc = 0;
491}
492
493/*
494 * Space map reference trees.
495 *
496 * A space map is a collection of integers. Every integer is either
497 * in the map, or it's not. A space map reference tree generalizes
498 * the idea: it allows its members to have arbitrary reference counts,
499 * as opposed to the implicit reference count of 0 or 1 in a space map.
500 * This representation comes in handy when computing the union or
501 * intersection of multiple space maps. For example, the union of
502 * N space maps is the subset of the reference tree with refcnt >= 1.
503 * The intersection of N space maps is the subset with refcnt >= N.
504 *
505 * [It's very much like a Fourier transform. Unions and intersections
506 * are hard to perform in the 'space map domain', so we convert the maps
507 * into the 'reference count domain', where it's trivial, then invert.]
508 *
509 * vdev_dtl_reassess() uses computations of this form to determine
510 * DTL_MISSING and DTL_OUTAGE for interior vdevs -- e.g. a RAID-Z vdev
511 * has an outage wherever refcnt >= vdev_nparity + 1, and a mirror vdev
512 * has an outage wherever refcnt >= vdev_children.
513 */
514static int
515space_map_ref_compare(const void *x1, const void *x2)
516{
517 const space_ref_t *sr1 = x1;
518 const space_ref_t *sr2 = x2;
519
520 if (sr1->sr_offset < sr2->sr_offset)
521 return (-1);
522 if (sr1->sr_offset > sr2->sr_offset)
523 return (1);
524
525 if (sr1 < sr2)
526 return (-1);
527 if (sr1 > sr2)
528 return (1);
529
530 return (0);
531}
532
533void
534space_map_ref_create(avl_tree_t *t)
535{
536 avl_create(t, space_map_ref_compare,
537 sizeof (space_ref_t), offsetof(space_ref_t, sr_node));
538}
539
540void
541space_map_ref_destroy(avl_tree_t *t)
542{
543 space_ref_t *sr;
544 void *cookie = NULL;
545
546 while ((sr = avl_destroy_nodes(t, &cookie)) != NULL)
547 kmem_free(sr, sizeof (*sr));
548
549 avl_destroy(t);
550}
551
552static void
553space_map_ref_add_node(avl_tree_t *t, uint64_t offset, int64_t refcnt)
554{
555 space_ref_t *sr;
556
557 sr = kmem_alloc(sizeof (*sr), KM_SLEEP);
558 sr->sr_offset = offset;
559 sr->sr_refcnt = refcnt;
560
561 avl_add(t, sr);
562}
563
564void
565space_map_ref_add_seg(avl_tree_t *t, uint64_t start, uint64_t end,
566 int64_t refcnt)
567{
568 space_map_ref_add_node(t, start, refcnt);
569 space_map_ref_add_node(t, end, -refcnt);
570}
571
572/*
573 * Convert (or add) a space map into a reference tree.
574 */
575void
576space_map_ref_add_map(avl_tree_t *t, space_map_t *sm, int64_t refcnt)
577{
578 space_seg_t *ss;
579
580 ASSERT(MUTEX_HELD(sm->sm_lock));
581
582 for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
583 space_map_ref_add_seg(t, ss->ss_start, ss->ss_end, refcnt);
584}
585
586/*
587 * Convert a reference tree into a space map. The space map will contain
588 * all members of the reference tree for which refcnt >= minref.
589 */
590void
591space_map_ref_generate_map(avl_tree_t *t, space_map_t *sm, int64_t minref)
592{
593 uint64_t start = -1ULL;
594 int64_t refcnt = 0;
595 space_ref_t *sr;
596
597 ASSERT(MUTEX_HELD(sm->sm_lock));
598
599 space_map_vacate(sm, NULL, NULL);
600
601 for (sr = avl_first(t); sr != NULL; sr = AVL_NEXT(t, sr)) {
602 refcnt += sr->sr_refcnt;
603 if (refcnt >= minref) {
604 if (start == -1ULL) {
605 start = sr->sr_offset;
606 }
607 } else {
608 if (start != -1ULL) {
609 uint64_t end = sr->sr_offset;
610 ASSERT(start <= end);
611 if (end > start)
612 space_map_add(sm, start, end - start);
613 start = -1ULL;
614 }
615 }
616 }
617 ASSERT(refcnt == 0);
618 ASSERT(start == -1ULL);
619}