Print this page
4374 dn_free_ranges should use range_tree_t
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Max Grossman <max.grossman@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com
Reviewed by: Garrett D'Amore <garrett@damore.org>
Reviewed by: Dan McDonald <danmcd@omniti.com>
Approved by: Dan McDonald <danmcd@omniti.com>


   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) 2013 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/dnode.h>
  33 #include <sys/zio.h>
  34 #include <sys/range_tree.h>
  35 
  36 static kmem_cache_t *range_seg_cache;
  37 
  38 void
  39 range_tree_init(void)
  40 {
  41         ASSERT(range_seg_cache == NULL);
  42         range_seg_cache = kmem_cache_create("range_seg_cache",
  43             sizeof (range_seg_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
  44 }
  45 
  46 void
  47 range_tree_fini(void)
  48 {
  49         kmem_cache_destroy(range_seg_cache);
  50         range_seg_cache = NULL;
  51 }
  52 
  53 void
  54 range_tree_stat_verify(range_tree_t *rt)
  55 {
  56         range_seg_t *rs;
  57         uint64_t hist[RANGE_TREE_HISTOGRAM_SIZE] = { 0 };
  58         int i;
  59 
  60         for (rs = avl_first(&rt->rt_root); rs != NULL;
  61             rs = AVL_NEXT(&rt->rt_root, rs)) {
  62                 uint64_t size = rs->rs_end - rs->rs_start;
  63                 int idx = highbit(size) - 1;
  64 
  65                 hist[idx]++;
  66                 ASSERT3U(hist[idx], !=, 0);
  67         }
  68 
  69         for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
  70                 if (hist[i] != rt->rt_histogram[i]) {
  71                         zfs_dbgmsg("i=%d, hist=%p, hist=%llu, rt_hist=%llu",
  72                             i, hist, hist[i], rt->rt_histogram[i]);
  73                 }
  74                 VERIFY3U(hist[i], ==, rt->rt_histogram[i]);
  75         }
  76 }
  77 
  78 static void
  79 range_tree_stat_incr(range_tree_t *rt, range_seg_t *rs)
  80 {
  81         uint64_t size = rs->rs_end - rs->rs_start;
  82         int idx = highbit(size) - 1;
  83 
  84         ASSERT3U(idx, <,
  85             sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram));
  86 
  87         ASSERT(MUTEX_HELD(rt->rt_lock));
  88         rt->rt_histogram[idx]++;
  89         ASSERT3U(rt->rt_histogram[idx], !=, 0);
  90 }
  91 
  92 static void
  93 range_tree_stat_decr(range_tree_t *rt, range_seg_t *rs)
  94 {
  95         uint64_t size = rs->rs_end - rs->rs_start;
  96         int idx = highbit(size) - 1;
  97 
  98         ASSERT3U(idx, <,
  99             sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram));
 100 
 101         ASSERT(MUTEX_HELD(rt->rt_lock));
 102         ASSERT3U(rt->rt_histogram[idx], !=, 0);
 103         rt->rt_histogram[idx]--;
 104 }
 105 
 106 /*
 107  * NOTE: caller is responsible for all locking.
 108  */
 109 static int
 110 range_tree_seg_compare(const void *x1, const void *x2)
 111 {
 112         const range_seg_t *r1 = x1;
 113         const range_seg_t *r2 = x2;
 114 
 115         if (r1->rs_start < r2->rs_start) {
 116                 if (r1->rs_end > r2->rs_start)


 282                 rs->rs_end = start;
 283         } else if (right_over) {
 284                 rs->rs_start = end;
 285         } else {
 286                 avl_remove(&rt->rt_root, rs);
 287                 kmem_cache_free(range_seg_cache, rs);
 288                 rs = NULL;
 289         }
 290 
 291         if (rs != NULL) {
 292                 range_tree_stat_incr(rt, rs);
 293 
 294                 if (rt->rt_ops != NULL)
 295                         rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
 296         }
 297 
 298         rt->rt_space -= size;
 299 }
 300 
 301 static range_seg_t *
 302 range_tree_find(range_tree_t *rt, uint64_t start, uint64_t size,
 303     avl_index_t *wherep)
 304 {
 305         range_seg_t rsearch, *rs;

 306         uint64_t end = start + size;
 307 
 308         ASSERT(MUTEX_HELD(rt->rt_lock));
 309         VERIFY(size != 0);
 310 
 311         rsearch.rs_start = start;
 312         rsearch.rs_end = end;
 313         rs = avl_find(&rt->rt_root, &rsearch, wherep);

 314 
 315         if (rs != NULL && rs->rs_start <= start && rs->rs_end >= end)




 316                 return (rs);
 317         return (NULL);
 318 }
 319 
 320 void
 321 range_tree_verify(range_tree_t *rt, uint64_t off, uint64_t size)
 322 {
 323         range_seg_t *rs;
 324         avl_index_t where;
 325 
 326         mutex_enter(rt->rt_lock);
 327         rs = range_tree_find(rt, off, size, &where);
 328         if (rs != NULL)
 329                 panic("freeing free block; rs=%p", (void *)rs);
 330         mutex_exit(rt->rt_lock);
 331 }
 332 
 333 boolean_t
 334 range_tree_contains(range_tree_t *rt, uint64_t start, uint64_t size)
 335 {
 336         avl_index_t where;

 337 
 338         return (range_tree_find(rt, start, size, &where) != NULL);













 339 }
 340 
 341 void
 342 range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst)
 343 {
 344         range_tree_t *rt;
 345 
 346         ASSERT(MUTEX_HELD((*rtsrc)->rt_lock));
 347         ASSERT0(range_tree_space(*rtdst));
 348         ASSERT0(avl_numnodes(&(*rtdst)->rt_root));
 349 
 350         rt = *rtsrc;
 351         *rtsrc = *rtdst;
 352         *rtdst = rt;
 353 }
 354 
 355 void
 356 range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg)
 357 {
 358         range_seg_t *rs;




   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) 2013, 2014 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/dnode.h>
  33 #include <sys/zio.h>
  34 #include <sys/range_tree.h>
  35 
  36 static kmem_cache_t *range_seg_cache;
  37 
  38 void
  39 range_tree_init(void)
  40 {
  41         ASSERT(range_seg_cache == NULL);
  42         range_seg_cache = kmem_cache_create("range_seg_cache",
  43             sizeof (range_seg_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
  44 }
  45 
  46 void
  47 range_tree_fini(void)
  48 {
  49         kmem_cache_destroy(range_seg_cache);
  50         range_seg_cache = NULL;
  51 }
  52 
  53 void
  54 range_tree_stat_verify(range_tree_t *rt)
  55 {
  56         range_seg_t *rs;
  57         uint64_t hist[RANGE_TREE_HISTOGRAM_SIZE] = { 0 };
  58         int i;
  59 
  60         for (rs = avl_first(&rt->rt_root); rs != NULL;
  61             rs = AVL_NEXT(&rt->rt_root, rs)) {
  62                 uint64_t size = rs->rs_end - rs->rs_start;
  63                 int idx = highbit64(size) - 1;
  64 
  65                 hist[idx]++;
  66                 ASSERT3U(hist[idx], !=, 0);
  67         }
  68 
  69         for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
  70                 if (hist[i] != rt->rt_histogram[i]) {
  71                         zfs_dbgmsg("i=%d, hist=%p, hist=%llu, rt_hist=%llu",
  72                             i, hist, hist[i], rt->rt_histogram[i]);
  73                 }
  74                 VERIFY3U(hist[i], ==, rt->rt_histogram[i]);
  75         }
  76 }
  77 
  78 static void
  79 range_tree_stat_incr(range_tree_t *rt, range_seg_t *rs)
  80 {
  81         uint64_t size = rs->rs_end - rs->rs_start;
  82         int idx = highbit64(size) - 1;
  83 
  84         ASSERT3U(idx, <,
  85             sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram));
  86 
  87         ASSERT(MUTEX_HELD(rt->rt_lock));
  88         rt->rt_histogram[idx]++;
  89         ASSERT3U(rt->rt_histogram[idx], !=, 0);
  90 }
  91 
  92 static void
  93 range_tree_stat_decr(range_tree_t *rt, range_seg_t *rs)
  94 {
  95         uint64_t size = rs->rs_end - rs->rs_start;
  96         int idx = highbit64(size) - 1;
  97 
  98         ASSERT3U(idx, <,
  99             sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram));
 100 
 101         ASSERT(MUTEX_HELD(rt->rt_lock));
 102         ASSERT3U(rt->rt_histogram[idx], !=, 0);
 103         rt->rt_histogram[idx]--;
 104 }
 105 
 106 /*
 107  * NOTE: caller is responsible for all locking.
 108  */
 109 static int
 110 range_tree_seg_compare(const void *x1, const void *x2)
 111 {
 112         const range_seg_t *r1 = x1;
 113         const range_seg_t *r2 = x2;
 114 
 115         if (r1->rs_start < r2->rs_start) {
 116                 if (r1->rs_end > r2->rs_start)


 282                 rs->rs_end = start;
 283         } else if (right_over) {
 284                 rs->rs_start = end;
 285         } else {
 286                 avl_remove(&rt->rt_root, rs);
 287                 kmem_cache_free(range_seg_cache, rs);
 288                 rs = NULL;
 289         }
 290 
 291         if (rs != NULL) {
 292                 range_tree_stat_incr(rt, rs);
 293 
 294                 if (rt->rt_ops != NULL)
 295                         rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
 296         }
 297 
 298         rt->rt_space -= size;
 299 }
 300 
 301 static range_seg_t *
 302 range_tree_find_impl(range_tree_t *rt, uint64_t start, uint64_t size)

 303 {
 304         avl_index_t where;
 305         range_seg_t rsearch;
 306         uint64_t end = start + size;
 307 
 308         ASSERT(MUTEX_HELD(rt->rt_lock));
 309         VERIFY(size != 0);
 310 
 311         rsearch.rs_start = start;
 312         rsearch.rs_end = end;
 313         return (avl_find(&rt->rt_root, &rsearch, &where));
 314 }
 315 
 316 static range_seg_t *
 317 range_tree_find(range_tree_t *rt, uint64_t start, uint64_t size)
 318 {
 319         range_seg_t *rs = range_tree_find_impl(rt, start, size);
 320         if (rs != NULL && rs->rs_start <= start && rs->rs_end >= start + size)
 321                 return (rs);
 322         return (NULL);
 323 }
 324 
 325 void
 326 range_tree_verify(range_tree_t *rt, uint64_t off, uint64_t size)
 327 {
 328         range_seg_t *rs;

 329 
 330         mutex_enter(rt->rt_lock);
 331         rs = range_tree_find(rt, off, size);
 332         if (rs != NULL)
 333                 panic("freeing free block; rs=%p", (void *)rs);
 334         mutex_exit(rt->rt_lock);
 335 }
 336 
 337 boolean_t
 338 range_tree_contains(range_tree_t *rt, uint64_t start, uint64_t size)
 339 {
 340         return (range_tree_find(rt, start, size) != NULL);
 341 }
 342 
 343 /*
 344  * Ensure that this range is not in the tree, regardless of whether
 345  * it is currently in the tree.
 346  */
 347 void
 348 range_tree_clear(range_tree_t *rt, uint64_t start, uint64_t size)
 349 {
 350         range_seg_t *rs;
 351 
 352         while ((rs = range_tree_find_impl(rt, start, size)) != NULL) {
 353                 uint64_t free_start = MAX(rs->rs_start, start);
 354                 uint64_t free_end = MIN(rs->rs_end, start + size);
 355                 range_tree_remove(rt, free_start, free_end - free_start);
 356         }
 357 }
 358 
 359 void
 360 range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst)
 361 {
 362         range_tree_t *rt;
 363 
 364         ASSERT(MUTEX_HELD((*rtsrc)->rt_lock));
 365         ASSERT0(range_tree_space(*rtdst));
 366         ASSERT0(avl_numnodes(&(*rtdst)->rt_root));
 367 
 368         rt = *rtsrc;
 369         *rtsrc = *rtdst;
 370         *rtdst = rt;
 371 }
 372 
 373 void
 374 range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg)
 375 {
 376         range_seg_t *rs;