1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 25 */ 26 27 #include <sys/bplist.h> 28 #include <sys/zfs_context.h> 29 30 31 void 32 bplist_create(bplist_t *bpl) 33 { 34 mutex_init(&bpl->bpl_lock, NULL, MUTEX_DEFAULT, NULL); 35 list_create(&bpl->bpl_list, sizeof (bplist_entry_t), 36 offsetof(bplist_entry_t, bpe_node)); 37 } 38 39 void 40 bplist_destroy(bplist_t *bpl) 41 { 42 list_destroy(&bpl->bpl_list); 43 mutex_destroy(&bpl->bpl_lock); 44 } 45 46 void 47 bplist_append(bplist_t *bpl, const blkptr_t *bp) 48 { 49 bplist_entry_t *bpe = kmem_alloc(sizeof (*bpe), KM_SLEEP); 50 51 mutex_enter(&bpl->bpl_lock); 52 bpe->bpe_blk = *bp; 53 list_insert_tail(&bpl->bpl_list, bpe); 54 mutex_exit(&bpl->bpl_lock); 55 } 56 57 /* 58 * To aid debugging, we keep the most recently removed entry. This way if 59 * we are in the callback, we can easily locate the entry. 60 */ 61 static bplist_entry_t *bplist_iterate_last_removed; 62 63 void 64 bplist_iterate(bplist_t *bpl, bplist_itor_t *func, void *arg, dmu_tx_t *tx) 65 { 66 bplist_entry_t *bpe; 67 68 mutex_enter(&bpl->bpl_lock); 69 while ((bpe = list_remove_head(&bpl->bpl_list))) { 70 bplist_iterate_last_removed = bpe; 71 mutex_exit(&bpl->bpl_lock); 72 func(arg, &bpe->bpe_blk, tx); 73 kmem_free(bpe, sizeof (*bpe)); 74 mutex_enter(&bpl->bpl_lock); 75 } 76 mutex_exit(&bpl->bpl_lock); 77 } 78 79 void 80 bplist_clear(bplist_t *bpl) 81 { 82 bplist_entry_t *bpe; 83 84 mutex_enter(&bpl->bpl_lock); 85 while ((bpe = list_remove_head(&bpl->bpl_list))) 86 kmem_free(bpe, sizeof (*bpe)); 87 mutex_exit(&bpl->bpl_lock); 88 } 89