]>
git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/space_map.c
ab7cb7a9ad14087a09df3d34b084751ef3dfbc97
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright (c) 2012 by Delphix. All rights reserved.
29 #include <sys/zfs_context.h>
33 #include <sys/space_map.h>
35 static kmem_cache_t
*space_seg_cache
;
40 ASSERT(space_seg_cache
== NULL
);
41 space_seg_cache
= kmem_cache_create("space_seg_cache",
42 sizeof (space_seg_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
48 kmem_cache_destroy(space_seg_cache
);
49 space_seg_cache
= NULL
;
54 * NOTE: caller is responsible for all locking.
57 space_map_seg_compare(const void *x1
, const void *x2
)
59 const space_seg_t
*s1
= x1
;
60 const space_seg_t
*s2
= x2
;
62 if (s1
->ss_start
< s2
->ss_start
) {
63 if (s1
->ss_end
> s2
->ss_start
)
67 if (s1
->ss_start
> s2
->ss_start
) {
68 if (s1
->ss_start
< s2
->ss_end
)
76 space_map_create(space_map_t
*sm
, uint64_t start
, uint64_t size
, uint8_t shift
,
79 bzero(sm
, sizeof (*sm
));
81 cv_init(&sm
->sm_load_cv
, NULL
, CV_DEFAULT
, NULL
);
83 avl_create(&sm
->sm_root
, space_map_seg_compare
,
84 sizeof (space_seg_t
), offsetof(struct space_seg
, ss_node
));
93 space_map_destroy(space_map_t
*sm
)
95 ASSERT(!sm
->sm_loaded
&& !sm
->sm_loading
);
96 VERIFY0(sm
->sm_space
);
97 avl_destroy(&sm
->sm_root
);
98 cv_destroy(&sm
->sm_load_cv
);
102 space_map_add(space_map_t
*sm
, uint64_t start
, uint64_t size
)
105 space_seg_t ssearch
, *ss_before
, *ss_after
, *ss
;
106 uint64_t end
= start
+ size
;
107 int merge_before
, merge_after
;
109 ASSERT(MUTEX_HELD(sm
->sm_lock
));
111 VERIFY3U(start
, >=, sm
->sm_start
);
112 VERIFY3U(end
, <=, sm
->sm_start
+ sm
->sm_size
);
113 VERIFY(sm
->sm_space
+ size
<= sm
->sm_size
);
114 VERIFY(P2PHASE(start
, 1ULL << sm
->sm_shift
) == 0);
115 VERIFY(P2PHASE(size
, 1ULL << sm
->sm_shift
) == 0);
117 ssearch
.ss_start
= start
;
118 ssearch
.ss_end
= end
;
119 ss
= avl_find(&sm
->sm_root
, &ssearch
, &where
);
121 if (ss
!= NULL
&& ss
->ss_start
<= start
&& ss
->ss_end
>= end
) {
122 zfs_panic_recover("zfs: allocating allocated segment"
123 "(offset=%llu size=%llu)\n",
124 (longlong_t
)start
, (longlong_t
)size
);
128 /* Make sure we don't overlap with either of our neighbors */
131 ss_before
= avl_nearest(&sm
->sm_root
, where
, AVL_BEFORE
);
132 ss_after
= avl_nearest(&sm
->sm_root
, where
, AVL_AFTER
);
134 merge_before
= (ss_before
!= NULL
&& ss_before
->ss_end
== start
);
135 merge_after
= (ss_after
!= NULL
&& ss_after
->ss_start
== end
);
137 if (merge_before
&& merge_after
) {
138 avl_remove(&sm
->sm_root
, ss_before
);
139 if (sm
->sm_pp_root
) {
140 avl_remove(sm
->sm_pp_root
, ss_before
);
141 avl_remove(sm
->sm_pp_root
, ss_after
);
143 ss_after
->ss_start
= ss_before
->ss_start
;
144 kmem_cache_free(space_seg_cache
, ss_before
);
146 } else if (merge_before
) {
147 ss_before
->ss_end
= end
;
149 avl_remove(sm
->sm_pp_root
, ss_before
);
151 } else if (merge_after
) {
152 ss_after
->ss_start
= start
;
154 avl_remove(sm
->sm_pp_root
, ss_after
);
157 ss
= kmem_cache_alloc(space_seg_cache
, KM_PUSHPAGE
);
158 ss
->ss_start
= start
;
160 avl_insert(&sm
->sm_root
, ss
, where
);
164 avl_add(sm
->sm_pp_root
, ss
);
166 sm
->sm_space
+= size
;
170 space_map_remove(space_map_t
*sm
, uint64_t start
, uint64_t size
)
173 space_seg_t ssearch
, *ss
, *newseg
;
174 uint64_t end
= start
+ size
;
175 int left_over
, right_over
;
177 ASSERT(MUTEX_HELD(sm
->sm_lock
));
179 VERIFY(P2PHASE(start
, 1ULL << sm
->sm_shift
) == 0);
180 VERIFY(P2PHASE(size
, 1ULL << sm
->sm_shift
) == 0);
182 ssearch
.ss_start
= start
;
183 ssearch
.ss_end
= end
;
184 ss
= avl_find(&sm
->sm_root
, &ssearch
, &where
);
186 /* Make sure we completely overlap with someone */
188 zfs_panic_recover("zfs: freeing free segment "
189 "(offset=%llu size=%llu)",
190 (longlong_t
)start
, (longlong_t
)size
);
193 VERIFY3U(ss
->ss_start
, <=, start
);
194 VERIFY3U(ss
->ss_end
, >=, end
);
195 VERIFY(sm
->sm_space
- size
<= sm
->sm_size
);
197 left_over
= (ss
->ss_start
!= start
);
198 right_over
= (ss
->ss_end
!= end
);
201 avl_remove(sm
->sm_pp_root
, ss
);
203 if (left_over
&& right_over
) {
204 newseg
= kmem_cache_alloc(space_seg_cache
, KM_PUSHPAGE
);
205 newseg
->ss_start
= end
;
206 newseg
->ss_end
= ss
->ss_end
;
208 avl_insert_here(&sm
->sm_root
, newseg
, ss
, AVL_AFTER
);
210 avl_add(sm
->sm_pp_root
, newseg
);
211 } else if (left_over
) {
213 } else if (right_over
) {
216 avl_remove(&sm
->sm_root
, ss
);
217 kmem_cache_free(space_seg_cache
, ss
);
221 if (sm
->sm_pp_root
&& ss
!= NULL
)
222 avl_add(sm
->sm_pp_root
, ss
);
224 sm
->sm_space
-= size
;
228 space_map_contains(space_map_t
*sm
, uint64_t start
, uint64_t size
)
231 space_seg_t ssearch
, *ss
;
232 uint64_t end
= start
+ size
;
234 ASSERT(MUTEX_HELD(sm
->sm_lock
));
236 VERIFY(P2PHASE(start
, 1ULL << sm
->sm_shift
) == 0);
237 VERIFY(P2PHASE(size
, 1ULL << sm
->sm_shift
) == 0);
239 ssearch
.ss_start
= start
;
240 ssearch
.ss_end
= end
;
241 ss
= avl_find(&sm
->sm_root
, &ssearch
, &where
);
243 return (ss
!= NULL
&& ss
->ss_start
<= start
&& ss
->ss_end
>= end
);
247 space_map_vacate(space_map_t
*sm
, space_map_func_t
*func
, space_map_t
*mdest
)
252 ASSERT(MUTEX_HELD(sm
->sm_lock
));
254 while ((ss
= avl_destroy_nodes(&sm
->sm_root
, &cookie
)) != NULL
) {
256 func(mdest
, ss
->ss_start
, ss
->ss_end
- ss
->ss_start
);
257 kmem_cache_free(space_seg_cache
, ss
);
263 space_map_walk(space_map_t
*sm
, space_map_func_t
*func
, space_map_t
*mdest
)
267 ASSERT(MUTEX_HELD(sm
->sm_lock
));
269 for (ss
= avl_first(&sm
->sm_root
); ss
; ss
= AVL_NEXT(&sm
->sm_root
, ss
))
270 func(mdest
, ss
->ss_start
, ss
->ss_end
- ss
->ss_start
);
274 * Wait for any in-progress space_map_load() to complete.
277 space_map_load_wait(space_map_t
*sm
)
279 ASSERT(MUTEX_HELD(sm
->sm_lock
));
281 while (sm
->sm_loading
) {
282 ASSERT(!sm
->sm_loaded
);
283 cv_wait(&sm
->sm_load_cv
, sm
->sm_lock
);
288 * Note: space_map_load() will drop sm_lock across dmu_read() calls.
289 * The caller must be OK with this.
292 space_map_load(space_map_t
*sm
, space_map_ops_t
*ops
, uint8_t maptype
,
293 space_map_obj_t
*smo
, objset_t
*os
)
295 uint64_t *entry
, *entry_map
, *entry_map_end
;
296 uint64_t bufsize
, size
, offset
, end
, space
;
297 uint64_t mapstart
= sm
->sm_start
;
300 ASSERT(MUTEX_HELD(sm
->sm_lock
));
301 ASSERT(!sm
->sm_loaded
);
302 ASSERT(!sm
->sm_loading
);
304 sm
->sm_loading
= B_TRUE
;
305 end
= smo
->smo_objsize
;
306 space
= smo
->smo_alloc
;
308 ASSERT(sm
->sm_ops
== NULL
);
309 VERIFY0(sm
->sm_space
);
311 if (maptype
== SM_FREE
) {
312 space_map_add(sm
, sm
->sm_start
, sm
->sm_size
);
313 space
= sm
->sm_size
- space
;
316 bufsize
= 1ULL << SPACE_MAP_BLOCKSHIFT
;
317 entry_map
= zio_buf_alloc(bufsize
);
319 mutex_exit(sm
->sm_lock
);
321 dmu_prefetch(os
, smo
->smo_object
, bufsize
, end
- bufsize
);
322 mutex_enter(sm
->sm_lock
);
324 for (offset
= 0; offset
< end
; offset
+= bufsize
) {
325 size
= MIN(end
- offset
, bufsize
);
326 VERIFY(P2PHASE(size
, sizeof (uint64_t)) == 0);
329 dprintf("object=%llu offset=%llx size=%llx\n",
330 smo
->smo_object
, offset
, size
);
332 mutex_exit(sm
->sm_lock
);
333 error
= dmu_read(os
, smo
->smo_object
, offset
, size
, entry_map
,
335 mutex_enter(sm
->sm_lock
);
339 entry_map_end
= entry_map
+ (size
/ sizeof (uint64_t));
340 for (entry
= entry_map
; entry
< entry_map_end
; entry
++) {
343 if (SM_DEBUG_DECODE(e
)) /* Skip debug entries */
346 (SM_TYPE_DECODE(e
) == maptype
?
347 space_map_add
: space_map_remove
)(sm
,
348 (SM_OFFSET_DECODE(e
) << sm
->sm_shift
) + mapstart
,
349 SM_RUN_DECODE(e
) << sm
->sm_shift
);
354 VERIFY3U(sm
->sm_space
, ==, space
);
356 sm
->sm_loaded
= B_TRUE
;
361 space_map_vacate(sm
, NULL
, NULL
);
364 zio_buf_free(entry_map
, bufsize
);
366 sm
->sm_loading
= B_FALSE
;
368 cv_broadcast(&sm
->sm_load_cv
);
374 space_map_unload(space_map_t
*sm
)
376 ASSERT(MUTEX_HELD(sm
->sm_lock
));
378 if (sm
->sm_loaded
&& sm
->sm_ops
!= NULL
)
379 sm
->sm_ops
->smop_unload(sm
);
381 sm
->sm_loaded
= B_FALSE
;
384 space_map_vacate(sm
, NULL
, NULL
);
388 space_map_maxsize(space_map_t
*sm
)
390 ASSERT(sm
->sm_ops
!= NULL
);
391 return (sm
->sm_ops
->smop_max(sm
));
395 space_map_alloc(space_map_t
*sm
, uint64_t size
)
399 start
= sm
->sm_ops
->smop_alloc(sm
, size
);
401 space_map_remove(sm
, start
, size
);
406 space_map_claim(space_map_t
*sm
, uint64_t start
, uint64_t size
)
408 sm
->sm_ops
->smop_claim(sm
, start
, size
);
409 space_map_remove(sm
, start
, size
);
413 space_map_free(space_map_t
*sm
, uint64_t start
, uint64_t size
)
415 space_map_add(sm
, start
, size
);
416 sm
->sm_ops
->smop_free(sm
, start
, size
);
420 * Note: space_map_sync() will drop sm_lock across dmu_write() calls.
423 space_map_sync(space_map_t
*sm
, uint8_t maptype
,
424 space_map_obj_t
*smo
, objset_t
*os
, dmu_tx_t
*tx
)
426 spa_t
*spa
= dmu_objset_spa(os
);
429 uint64_t bufsize
, start
, size
, run_len
, delta
, sm_space
;
430 uint64_t *entry
, *entry_map
, *entry_map_end
;
432 ASSERT(MUTEX_HELD(sm
->sm_lock
));
434 if (sm
->sm_space
== 0)
437 dprintf("object %4llu, txg %llu, pass %d, %c, count %lu, space %llx\n",
438 smo
->smo_object
, dmu_tx_get_txg(tx
), spa_sync_pass(spa
),
439 maptype
== SM_ALLOC
? 'A' : 'F', avl_numnodes(&sm
->sm_root
),
442 if (maptype
== SM_ALLOC
)
443 smo
->smo_alloc
+= sm
->sm_space
;
445 smo
->smo_alloc
-= sm
->sm_space
;
447 bufsize
= (8 + avl_numnodes(&sm
->sm_root
)) * sizeof (uint64_t);
448 bufsize
= MIN(bufsize
, 1ULL << SPACE_MAP_BLOCKSHIFT
);
449 entry_map
= zio_buf_alloc(bufsize
);
450 entry_map_end
= entry_map
+ (bufsize
/ sizeof (uint64_t));
453 *entry
++ = SM_DEBUG_ENCODE(1) |
454 SM_DEBUG_ACTION_ENCODE(maptype
) |
455 SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa
)) |
456 SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx
));
459 sm_space
= sm
->sm_space
;
460 while ((ss
= avl_destroy_nodes(&sm
->sm_root
, &cookie
)) != NULL
) {
461 size
= ss
->ss_end
- ss
->ss_start
;
462 start
= (ss
->ss_start
- sm
->sm_start
) >> sm
->sm_shift
;
465 size
>>= sm
->sm_shift
;
468 run_len
= MIN(size
, SM_RUN_MAX
);
470 if (entry
== entry_map_end
) {
471 mutex_exit(sm
->sm_lock
);
472 dmu_write(os
, smo
->smo_object
, smo
->smo_objsize
,
473 bufsize
, entry_map
, tx
);
474 mutex_enter(sm
->sm_lock
);
475 smo
->smo_objsize
+= bufsize
;
479 *entry
++ = SM_OFFSET_ENCODE(start
) |
480 SM_TYPE_ENCODE(maptype
) |
481 SM_RUN_ENCODE(run_len
);
486 kmem_cache_free(space_seg_cache
, ss
);
489 if (entry
!= entry_map
) {
490 size
= (entry
- entry_map
) * sizeof (uint64_t);
491 mutex_exit(sm
->sm_lock
);
492 dmu_write(os
, smo
->smo_object
, smo
->smo_objsize
,
493 size
, entry_map
, tx
);
494 mutex_enter(sm
->sm_lock
);
495 smo
->smo_objsize
+= size
;
499 * Ensure that the space_map's accounting wasn't changed
500 * while we were in the middle of writing it out.
502 VERIFY3U(sm
->sm_space
, ==, sm_space
);
504 zio_buf_free(entry_map
, bufsize
);
506 sm
->sm_space
-= delta
;
507 VERIFY0(sm
->sm_space
);
511 space_map_truncate(space_map_obj_t
*smo
, objset_t
*os
, dmu_tx_t
*tx
)
513 VERIFY(dmu_free_range(os
, smo
->smo_object
, 0, -1ULL, tx
) == 0);
515 smo
->smo_objsize
= 0;
520 * Space map reference trees.
522 * A space map is a collection of integers. Every integer is either
523 * in the map, or it's not. A space map reference tree generalizes
524 * the idea: it allows its members to have arbitrary reference counts,
525 * as opposed to the implicit reference count of 0 or 1 in a space map.
526 * This representation comes in handy when computing the union or
527 * intersection of multiple space maps. For example, the union of
528 * N space maps is the subset of the reference tree with refcnt >= 1.
529 * The intersection of N space maps is the subset with refcnt >= N.
531 * [It's very much like a Fourier transform. Unions and intersections
532 * are hard to perform in the 'space map domain', so we convert the maps
533 * into the 'reference count domain', where it's trivial, then invert.]
535 * vdev_dtl_reassess() uses computations of this form to determine
536 * DTL_MISSING and DTL_OUTAGE for interior vdevs -- e.g. a RAID-Z vdev
537 * has an outage wherever refcnt >= vdev_nparity + 1, and a mirror vdev
538 * has an outage wherever refcnt >= vdev_children.
541 space_map_ref_compare(const void *x1
, const void *x2
)
543 const space_ref_t
*sr1
= x1
;
544 const space_ref_t
*sr2
= x2
;
546 if (sr1
->sr_offset
< sr2
->sr_offset
)
548 if (sr1
->sr_offset
> sr2
->sr_offset
)
560 space_map_ref_create(avl_tree_t
*t
)
562 avl_create(t
, space_map_ref_compare
,
563 sizeof (space_ref_t
), offsetof(space_ref_t
, sr_node
));
567 space_map_ref_destroy(avl_tree_t
*t
)
572 while ((sr
= avl_destroy_nodes(t
, &cookie
)) != NULL
)
573 kmem_free(sr
, sizeof (*sr
));
579 space_map_ref_add_node(avl_tree_t
*t
, uint64_t offset
, int64_t refcnt
)
583 sr
= kmem_alloc(sizeof (*sr
), KM_PUSHPAGE
);
584 sr
->sr_offset
= offset
;
585 sr
->sr_refcnt
= refcnt
;
591 space_map_ref_add_seg(avl_tree_t
*t
, uint64_t start
, uint64_t end
,
594 space_map_ref_add_node(t
, start
, refcnt
);
595 space_map_ref_add_node(t
, end
, -refcnt
);
599 * Convert (or add) a space map into a reference tree.
602 space_map_ref_add_map(avl_tree_t
*t
, space_map_t
*sm
, int64_t refcnt
)
606 ASSERT(MUTEX_HELD(sm
->sm_lock
));
608 for (ss
= avl_first(&sm
->sm_root
); ss
; ss
= AVL_NEXT(&sm
->sm_root
, ss
))
609 space_map_ref_add_seg(t
, ss
->ss_start
, ss
->ss_end
, refcnt
);
613 * Convert a reference tree into a space map. The space map will contain
614 * all members of the reference tree for which refcnt >= minref.
617 space_map_ref_generate_map(avl_tree_t
*t
, space_map_t
*sm
, int64_t minref
)
619 uint64_t start
= -1ULL;
623 ASSERT(MUTEX_HELD(sm
->sm_lock
));
625 space_map_vacate(sm
, NULL
, NULL
);
627 for (sr
= avl_first(t
); sr
!= NULL
; sr
= AVL_NEXT(t
, sr
)) {
628 refcnt
+= sr
->sr_refcnt
;
629 if (refcnt
>= minref
) {
630 if (start
== -1ULL) {
631 start
= sr
->sr_offset
;
634 if (start
!= -1ULL) {
635 uint64_t end
= sr
->sr_offset
;
636 ASSERT(start
<= end
);
638 space_map_add(sm
, start
, end
- start
);
644 ASSERT(start
== -1ULL);