]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/space_map.c
MFV r247575:
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / space_map.c
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
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2012 by Delphix. All rights reserved.
27  */
28
29 #include <sys/zfs_context.h>
30 #include <sys/spa.h>
31 #include <sys/dmu.h>
32 #include <sys/zio.h>
33 #include <sys/space_map.h>
34
35 SYSCTL_DECL(_vfs_zfs);
36 static int space_map_last_hope;
37 TUNABLE_INT("vfs.zfs.space_map_last_hope", &space_map_last_hope);
38 SYSCTL_INT(_vfs_zfs, OID_AUTO, space_map_last_hope, CTLFLAG_RDTUN,
39     &space_map_last_hope, 0,
40     "If kernel panic in space_map code on pool import, import the pool in readonly mode and backup all your data before trying this option.");
41
42 static kmem_cache_t *space_seg_cache;
43
44 void
45 space_map_init(void)
46 {
47         ASSERT(space_seg_cache == NULL);
48         space_seg_cache = kmem_cache_create("space_seg_cache",
49             sizeof (space_seg_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
50 }
51
52 void
53 space_map_fini(void)
54 {
55         kmem_cache_destroy(space_seg_cache);
56         space_seg_cache = NULL;
57 }
58
59 /*
60  * Space map routines.
61  * NOTE: caller is responsible for all locking.
62  */
63 static int
64 space_map_seg_compare(const void *x1, const void *x2)
65 {
66         const space_seg_t *s1 = x1;
67         const space_seg_t *s2 = x2;
68
69         if (s1->ss_start < s2->ss_start) {
70                 if (s1->ss_end > s2->ss_start)
71                         return (0);
72                 return (-1);
73         }
74         if (s1->ss_start > s2->ss_start) {
75                 if (s1->ss_start < s2->ss_end)
76                         return (0);
77                 return (1);
78         }
79         return (0);
80 }
81
82 void
83 space_map_create(space_map_t *sm, uint64_t start, uint64_t size, uint8_t shift,
84         kmutex_t *lp)
85 {
86         bzero(sm, sizeof (*sm));
87
88         cv_init(&sm->sm_load_cv, NULL, CV_DEFAULT, NULL);
89
90         avl_create(&sm->sm_root, space_map_seg_compare,
91             sizeof (space_seg_t), offsetof(struct space_seg, ss_node));
92
93         sm->sm_start = start;
94         sm->sm_size = size;
95         sm->sm_shift = shift;
96         sm->sm_lock = lp;
97 }
98
99 void
100 space_map_destroy(space_map_t *sm)
101 {
102         ASSERT(!sm->sm_loaded && !sm->sm_loading);
103         VERIFY0(sm->sm_space);
104         avl_destroy(&sm->sm_root);
105         cv_destroy(&sm->sm_load_cv);
106 }
107
108 void
109 space_map_add(space_map_t *sm, uint64_t start, uint64_t size)
110 {
111         avl_index_t where;
112         space_seg_t ssearch, *ss_before, *ss_after, *ss;
113         uint64_t end = start + size;
114         int merge_before, merge_after;
115
116         ASSERT(MUTEX_HELD(sm->sm_lock));
117         VERIFY(!sm->sm_condensing);
118         VERIFY(size != 0);
119         VERIFY3U(start, >=, sm->sm_start);
120         VERIFY3U(end, <=, sm->sm_start + sm->sm_size);
121         VERIFY(sm->sm_space + size <= sm->sm_size);
122         VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
123         VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
124 again:
125         ssearch.ss_start = start;
126         ssearch.ss_end = end;
127         ss = avl_find(&sm->sm_root, &ssearch, &where);
128
129         if (ss != NULL && ss->ss_start <= start && ss->ss_end >= end) {
130                 zfs_panic_recover("zfs: allocating allocated segment"
131                     "(offset=%llu size=%llu)\n",
132                     (longlong_t)start, (longlong_t)size);
133                 return;
134         }
135         if (ss != NULL && space_map_last_hope) {
136                 uint64_t sstart, ssize;
137
138                 if (ss->ss_start > start)
139                         sstart = ss->ss_start;
140                 else
141                         sstart = start;
142                 if (ss->ss_end > end)
143                         ssize = end - sstart;
144                 else
145                         ssize = ss->ss_end - sstart;
146                 ZFS_LOG(0,
147                     "Removing colliding space_map range (start=%ju end=%ju). Good luck!",
148                     (uintmax_t)sstart, (uintmax_t)(sstart + ssize));
149                 space_map_remove(sm, sstart, ssize);
150                 goto again;
151         }
152
153         /* Make sure we don't overlap with either of our neighbors */
154         VERIFY(ss == NULL);
155
156         ss_before = avl_nearest(&sm->sm_root, where, AVL_BEFORE);
157         ss_after = avl_nearest(&sm->sm_root, where, AVL_AFTER);
158
159         merge_before = (ss_before != NULL && ss_before->ss_end == start);
160         merge_after = (ss_after != NULL && ss_after->ss_start == end);
161
162         if (merge_before && merge_after) {
163                 avl_remove(&sm->sm_root, ss_before);
164                 if (sm->sm_pp_root) {
165                         avl_remove(sm->sm_pp_root, ss_before);
166                         avl_remove(sm->sm_pp_root, ss_after);
167                 }
168                 ss_after->ss_start = ss_before->ss_start;
169                 kmem_cache_free(space_seg_cache, ss_before);
170                 ss = ss_after;
171         } else if (merge_before) {
172                 ss_before->ss_end = end;
173                 if (sm->sm_pp_root)
174                         avl_remove(sm->sm_pp_root, ss_before);
175                 ss = ss_before;
176         } else if (merge_after) {
177                 ss_after->ss_start = start;
178                 if (sm->sm_pp_root)
179                         avl_remove(sm->sm_pp_root, ss_after);
180                 ss = ss_after;
181         } else {
182                 ss = kmem_cache_alloc(space_seg_cache, KM_SLEEP);
183                 ss->ss_start = start;
184                 ss->ss_end = end;
185                 avl_insert(&sm->sm_root, ss, where);
186         }
187
188         if (sm->sm_pp_root)
189                 avl_add(sm->sm_pp_root, ss);
190
191         sm->sm_space += size;
192 }
193
194 void
195 space_map_remove(space_map_t *sm, uint64_t start, uint64_t size)
196 {
197         space_seg_t ssearch, *ss, *newseg;
198         uint64_t end = start + size;
199         int left_over, right_over;
200
201         ASSERT(MUTEX_HELD(sm->sm_lock));
202         VERIFY(!sm->sm_condensing);
203         VERIFY(size != 0);
204         VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
205         VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
206
207         ssearch.ss_start = start;
208         ssearch.ss_end = end;
209         ss = avl_find(&sm->sm_root, &ssearch, NULL);
210
211         /* Make sure we completely overlap with someone */
212         if (ss == NULL) {
213                 zfs_panic_recover("zfs: freeing free segment "
214                     "(offset=%llu size=%llu)",
215                     (longlong_t)start, (longlong_t)size);
216                 return;
217         }
218         VERIFY3U(ss->ss_start, <=, start);
219         VERIFY3U(ss->ss_end, >=, end);
220         VERIFY(sm->sm_space - size < sm->sm_size);
221
222         left_over = (ss->ss_start != start);
223         right_over = (ss->ss_end != end);
224
225         if (sm->sm_pp_root)
226                 avl_remove(sm->sm_pp_root, ss);
227
228         if (left_over && right_over) {
229                 newseg = kmem_cache_alloc(space_seg_cache, KM_SLEEP);
230                 newseg->ss_start = end;
231                 newseg->ss_end = ss->ss_end;
232                 ss->ss_end = start;
233                 avl_insert_here(&sm->sm_root, newseg, ss, AVL_AFTER);
234                 if (sm->sm_pp_root)
235                         avl_add(sm->sm_pp_root, newseg);
236         } else if (left_over) {
237                 ss->ss_end = start;
238         } else if (right_over) {
239                 ss->ss_start = end;
240         } else {
241                 avl_remove(&sm->sm_root, ss);
242                 kmem_cache_free(space_seg_cache, ss);
243                 ss = NULL;
244         }
245
246         if (sm->sm_pp_root && ss != NULL)
247                 avl_add(sm->sm_pp_root, ss);
248
249         sm->sm_space -= size;
250 }
251
252 boolean_t
253 space_map_contains(space_map_t *sm, uint64_t start, uint64_t size)
254 {
255         avl_index_t where;
256         space_seg_t ssearch, *ss;
257         uint64_t end = start + size;
258
259         ASSERT(MUTEX_HELD(sm->sm_lock));
260         VERIFY(size != 0);
261         VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
262         VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
263
264         ssearch.ss_start = start;
265         ssearch.ss_end = end;
266         ss = avl_find(&sm->sm_root, &ssearch, &where);
267
268         return (ss != NULL && ss->ss_start <= start && ss->ss_end >= end);
269 }
270
271 void
272 space_map_swap(space_map_t **msrc, space_map_t **mdst)
273 {
274         space_map_t *sm;
275
276         ASSERT(MUTEX_HELD((*msrc)->sm_lock));
277         ASSERT0((*mdst)->sm_space);
278         ASSERT0(avl_numnodes(&(*mdst)->sm_root));
279
280         sm = *msrc;
281         *msrc = *mdst;
282         *mdst = sm;
283 }
284
285 void
286 space_map_vacate(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
287 {
288         space_seg_t *ss;
289         void *cookie = NULL;
290
291         ASSERT(MUTEX_HELD(sm->sm_lock));
292
293         while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
294                 if (func != NULL)
295                         func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
296                 kmem_cache_free(space_seg_cache, ss);
297         }
298         sm->sm_space = 0;
299 }
300
301 void
302 space_map_walk(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
303 {
304         space_seg_t *ss;
305
306         ASSERT(MUTEX_HELD(sm->sm_lock));
307
308         for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
309                 func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
310 }
311
312 /*
313  * Wait for any in-progress space_map_load() to complete.
314  */
315 void
316 space_map_load_wait(space_map_t *sm)
317 {
318         ASSERT(MUTEX_HELD(sm->sm_lock));
319
320         while (sm->sm_loading) {
321                 ASSERT(!sm->sm_loaded);
322                 cv_wait(&sm->sm_load_cv, sm->sm_lock);
323         }
324 }
325
326 /*
327  * Note: space_map_load() will drop sm_lock across dmu_read() calls.
328  * The caller must be OK with this.
329  */
330 int
331 space_map_load(space_map_t *sm, space_map_ops_t *ops, uint8_t maptype,
332         space_map_obj_t *smo, objset_t *os)
333 {
334         uint64_t *entry, *entry_map, *entry_map_end;
335         uint64_t bufsize, size, offset, end, space;
336         uint64_t mapstart = sm->sm_start;
337         int error = 0;
338
339         ASSERT(MUTEX_HELD(sm->sm_lock));
340         ASSERT(!sm->sm_loaded);
341         ASSERT(!sm->sm_loading);
342
343         sm->sm_loading = B_TRUE;
344         end = smo->smo_objsize;
345         space = smo->smo_alloc;
346
347         ASSERT(sm->sm_ops == NULL);
348         VERIFY0(sm->sm_space);
349
350         if (maptype == SM_FREE) {
351                 space_map_add(sm, sm->sm_start, sm->sm_size);
352                 space = sm->sm_size - space;
353         }
354
355         bufsize = 1ULL << SPACE_MAP_BLOCKSHIFT;
356         entry_map = zio_buf_alloc(bufsize);
357
358         mutex_exit(sm->sm_lock);
359         if (end > bufsize)
360                 dmu_prefetch(os, smo->smo_object, bufsize, end - bufsize);
361         mutex_enter(sm->sm_lock);
362
363         for (offset = 0; offset < end; offset += bufsize) {
364                 size = MIN(end - offset, bufsize);
365                 VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
366                 VERIFY(size != 0);
367
368                 dprintf("object=%llu  offset=%llx  size=%llx\n",
369                     smo->smo_object, offset, size);
370
371                 mutex_exit(sm->sm_lock);
372                 error = dmu_read(os, smo->smo_object, offset, size, entry_map,
373                     DMU_READ_PREFETCH);
374                 mutex_enter(sm->sm_lock);
375                 if (error != 0)
376                         break;
377
378                 entry_map_end = entry_map + (size / sizeof (uint64_t));
379                 for (entry = entry_map; entry < entry_map_end; entry++) {
380                         uint64_t e = *entry;
381
382                         if (SM_DEBUG_DECODE(e))         /* Skip debug entries */
383                                 continue;
384
385                         (SM_TYPE_DECODE(e) == maptype ?
386                             space_map_add : space_map_remove)(sm,
387                             (SM_OFFSET_DECODE(e) << sm->sm_shift) + mapstart,
388                             SM_RUN_DECODE(e) << sm->sm_shift);
389                 }
390         }
391
392         if (error == 0) {
393                 VERIFY3U(sm->sm_space, ==, space);
394
395                 sm->sm_loaded = B_TRUE;
396                 sm->sm_ops = ops;
397                 if (ops != NULL)
398                         ops->smop_load(sm);
399         } else {
400                 space_map_vacate(sm, NULL, NULL);
401         }
402
403         zio_buf_free(entry_map, bufsize);
404
405         sm->sm_loading = B_FALSE;
406
407         cv_broadcast(&sm->sm_load_cv);
408
409         return (error);
410 }
411
412 void
413 space_map_unload(space_map_t *sm)
414 {
415         ASSERT(MUTEX_HELD(sm->sm_lock));
416
417         if (sm->sm_loaded && sm->sm_ops != NULL)
418                 sm->sm_ops->smop_unload(sm);
419
420         sm->sm_loaded = B_FALSE;
421         sm->sm_ops = NULL;
422
423         space_map_vacate(sm, NULL, NULL);
424 }
425
426 uint64_t
427 space_map_maxsize(space_map_t *sm)
428 {
429         ASSERT(sm->sm_ops != NULL);
430         return (sm->sm_ops->smop_max(sm));
431 }
432
433 uint64_t
434 space_map_alloc(space_map_t *sm, uint64_t size)
435 {
436         uint64_t start;
437
438         start = sm->sm_ops->smop_alloc(sm, size);
439         if (start != -1ULL)
440                 space_map_remove(sm, start, size);
441         return (start);
442 }
443
444 void
445 space_map_claim(space_map_t *sm, uint64_t start, uint64_t size)
446 {
447         sm->sm_ops->smop_claim(sm, start, size);
448         space_map_remove(sm, start, size);
449 }
450
451 void
452 space_map_free(space_map_t *sm, uint64_t start, uint64_t size)
453 {
454         space_map_add(sm, start, size);
455         sm->sm_ops->smop_free(sm, start, size);
456 }
457
458 /*
459  * Note: space_map_sync() will drop sm_lock across dmu_write() calls.
460  */
461 void
462 space_map_sync(space_map_t *sm, uint8_t maptype,
463         space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
464 {
465         spa_t *spa = dmu_objset_spa(os);
466         avl_tree_t *t = &sm->sm_root;
467         space_seg_t *ss;
468         uint64_t bufsize, start, size, run_len, total, sm_space, nodes;
469         uint64_t *entry, *entry_map, *entry_map_end;
470
471         ASSERT(MUTEX_HELD(sm->sm_lock));
472
473         if (sm->sm_space == 0)
474                 return;
475
476         dprintf("object %4llu, txg %llu, pass %d, %c, count %lu, space %llx\n",
477             smo->smo_object, dmu_tx_get_txg(tx), spa_sync_pass(spa),
478             maptype == SM_ALLOC ? 'A' : 'F', avl_numnodes(&sm->sm_root),
479             sm->sm_space);
480
481         if (maptype == SM_ALLOC)
482                 smo->smo_alloc += sm->sm_space;
483         else
484                 smo->smo_alloc -= sm->sm_space;
485
486         bufsize = (8 + avl_numnodes(&sm->sm_root)) * sizeof (uint64_t);
487         bufsize = MIN(bufsize, 1ULL << SPACE_MAP_BLOCKSHIFT);
488         entry_map = zio_buf_alloc(bufsize);
489         entry_map_end = entry_map + (bufsize / sizeof (uint64_t));
490         entry = entry_map;
491
492         *entry++ = SM_DEBUG_ENCODE(1) |
493             SM_DEBUG_ACTION_ENCODE(maptype) |
494             SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa)) |
495             SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
496
497         total = 0;
498         nodes = avl_numnodes(&sm->sm_root);
499         sm_space = sm->sm_space;
500         for (ss = avl_first(t); ss != NULL; ss = AVL_NEXT(t, ss)) {
501                 size = ss->ss_end - ss->ss_start;
502                 start = (ss->ss_start - sm->sm_start) >> sm->sm_shift;
503
504                 total += size;
505                 size >>= sm->sm_shift;
506
507                 while (size) {
508                         run_len = MIN(size, SM_RUN_MAX);
509
510                         if (entry == entry_map_end) {
511                                 mutex_exit(sm->sm_lock);
512                                 dmu_write(os, smo->smo_object, smo->smo_objsize,
513                                     bufsize, entry_map, tx);
514                                 mutex_enter(sm->sm_lock);
515                                 smo->smo_objsize += bufsize;
516                                 entry = entry_map;
517                         }
518
519                         *entry++ = SM_OFFSET_ENCODE(start) |
520                             SM_TYPE_ENCODE(maptype) |
521                             SM_RUN_ENCODE(run_len);
522
523                         start += run_len;
524                         size -= run_len;
525                 }
526         }
527
528         if (entry != entry_map) {
529                 size = (entry - entry_map) * sizeof (uint64_t);
530                 mutex_exit(sm->sm_lock);
531                 dmu_write(os, smo->smo_object, smo->smo_objsize,
532                     size, entry_map, tx);
533                 mutex_enter(sm->sm_lock);
534                 smo->smo_objsize += size;
535         }
536
537         /*
538          * Ensure that the space_map's accounting wasn't changed
539          * while we were in the middle of writing it out.
540          */
541         VERIFY3U(nodes, ==, avl_numnodes(&sm->sm_root));
542         VERIFY3U(sm->sm_space, ==, sm_space);
543         VERIFY3U(sm->sm_space, ==, total);
544
545         zio_buf_free(entry_map, bufsize);
546 }
547
548 void
549 space_map_truncate(space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
550 {
551         VERIFY(dmu_free_range(os, smo->smo_object, 0, -1ULL, tx) == 0);
552
553         smo->smo_objsize = 0;
554         smo->smo_alloc = 0;
555 }
556
557 /*
558  * Space map reference trees.
559  *
560  * A space map is a collection of integers.  Every integer is either
561  * in the map, or it's not.  A space map reference tree generalizes
562  * the idea: it allows its members to have arbitrary reference counts,
563  * as opposed to the implicit reference count of 0 or 1 in a space map.
564  * This representation comes in handy when computing the union or
565  * intersection of multiple space maps.  For example, the union of
566  * N space maps is the subset of the reference tree with refcnt >= 1.
567  * The intersection of N space maps is the subset with refcnt >= N.
568  *
569  * [It's very much like a Fourier transform.  Unions and intersections
570  * are hard to perform in the 'space map domain', so we convert the maps
571  * into the 'reference count domain', where it's trivial, then invert.]
572  *
573  * vdev_dtl_reassess() uses computations of this form to determine
574  * DTL_MISSING and DTL_OUTAGE for interior vdevs -- e.g. a RAID-Z vdev
575  * has an outage wherever refcnt >= vdev_nparity + 1, and a mirror vdev
576  * has an outage wherever refcnt >= vdev_children.
577  */
578 static int
579 space_map_ref_compare(const void *x1, const void *x2)
580 {
581         const space_ref_t *sr1 = x1;
582         const space_ref_t *sr2 = x2;
583
584         if (sr1->sr_offset < sr2->sr_offset)
585                 return (-1);
586         if (sr1->sr_offset > sr2->sr_offset)
587                 return (1);
588
589         if (sr1 < sr2)
590                 return (-1);
591         if (sr1 > sr2)
592                 return (1);
593
594         return (0);
595 }
596
597 void
598 space_map_ref_create(avl_tree_t *t)
599 {
600         avl_create(t, space_map_ref_compare,
601             sizeof (space_ref_t), offsetof(space_ref_t, sr_node));
602 }
603
604 void
605 space_map_ref_destroy(avl_tree_t *t)
606 {
607         space_ref_t *sr;
608         void *cookie = NULL;
609
610         while ((sr = avl_destroy_nodes(t, &cookie)) != NULL)
611                 kmem_free(sr, sizeof (*sr));
612
613         avl_destroy(t);
614 }
615
616 static void
617 space_map_ref_add_node(avl_tree_t *t, uint64_t offset, int64_t refcnt)
618 {
619         space_ref_t *sr;
620
621         sr = kmem_alloc(sizeof (*sr), KM_SLEEP);
622         sr->sr_offset = offset;
623         sr->sr_refcnt = refcnt;
624
625         avl_add(t, sr);
626 }
627
628 void
629 space_map_ref_add_seg(avl_tree_t *t, uint64_t start, uint64_t end,
630         int64_t refcnt)
631 {
632         space_map_ref_add_node(t, start, refcnt);
633         space_map_ref_add_node(t, end, -refcnt);
634 }
635
636 /*
637  * Convert (or add) a space map into a reference tree.
638  */
639 void
640 space_map_ref_add_map(avl_tree_t *t, space_map_t *sm, int64_t refcnt)
641 {
642         space_seg_t *ss;
643
644         ASSERT(MUTEX_HELD(sm->sm_lock));
645
646         for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
647                 space_map_ref_add_seg(t, ss->ss_start, ss->ss_end, refcnt);
648 }
649
650 /*
651  * Convert a reference tree into a space map.  The space map will contain
652  * all members of the reference tree for which refcnt >= minref.
653  */
654 void
655 space_map_ref_generate_map(avl_tree_t *t, space_map_t *sm, int64_t minref)
656 {
657         uint64_t start = -1ULL;
658         int64_t refcnt = 0;
659         space_ref_t *sr;
660
661         ASSERT(MUTEX_HELD(sm->sm_lock));
662
663         space_map_vacate(sm, NULL, NULL);
664
665         for (sr = avl_first(t); sr != NULL; sr = AVL_NEXT(t, sr)) {
666                 refcnt += sr->sr_refcnt;
667                 if (refcnt >= minref) {
668                         if (start == -1ULL) {
669                                 start = sr->sr_offset;
670                         }
671                 } else {
672                         if (start != -1ULL) {
673                                 uint64_t end = sr->sr_offset;
674                                 ASSERT(start <= end);
675                                 if (end > start)
676                                         space_map_add(sm, start, end - start);
677                                 start = -1ULL;
678                         }
679                 }
680         }
681         ASSERT(refcnt == 0);
682         ASSERT(start == -1ULL);
683 }