1789Sahrens /*
2789Sahrens * CDDL HEADER START
3789Sahrens *
4789Sahrens * The contents of this file are subject to the terms of the
51544Seschrock * Common Development and Distribution License (the "License").
61544Seschrock * You may not use this file except in compliance with the License.
7789Sahrens *
8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens * or http://www.opensolaris.org/os/licensing.
10789Sahrens * See the License for the specific language governing permissions
11789Sahrens * and limitations under the License.
12789Sahrens *
13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens *
19789Sahrens * CDDL HEADER END
20789Sahrens */
21789Sahrens /*
2212285SJeff.Bonwick@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens */
24789Sahrens
25789Sahrens #include <sys/bplist.h>
26789Sahrens #include <sys/zfs_context.h>
27789Sahrens
2810922SJeff.Bonwick@Sun.COM
2910922SJeff.Bonwick@Sun.COM void
bplist_create(bplist_t * bpl)30*12470SMatthew.Ahrens@Sun.COM bplist_create(bplist_t *bpl)
31789Sahrens {
32*12470SMatthew.Ahrens@Sun.COM mutex_init(&bpl->bpl_lock, NULL, MUTEX_DEFAULT, NULL);
33*12470SMatthew.Ahrens@Sun.COM list_create(&bpl->bpl_list, sizeof (bplist_entry_t),
34*12470SMatthew.Ahrens@Sun.COM offsetof(bplist_entry_t, bpe_node));
35789Sahrens }
36789Sahrens
37789Sahrens void
bplist_destroy(bplist_t * bpl)38*12470SMatthew.Ahrens@Sun.COM bplist_destroy(bplist_t *bpl)
39789Sahrens {
40*12470SMatthew.Ahrens@Sun.COM list_destroy(&bpl->bpl_list);
41*12470SMatthew.Ahrens@Sun.COM mutex_destroy(&bpl->bpl_lock);
42789Sahrens }
43789Sahrens
44789Sahrens void
bplist_append(bplist_t * bpl,const blkptr_t * bp)45*12470SMatthew.Ahrens@Sun.COM bplist_append(bplist_t *bpl, const blkptr_t *bp)
46789Sahrens {
47*12470SMatthew.Ahrens@Sun.COM bplist_entry_t *bpe = kmem_alloc(sizeof (*bpe), KM_SLEEP);
48789Sahrens
49*12470SMatthew.Ahrens@Sun.COM mutex_enter(&bpl->bpl_lock);
50*12470SMatthew.Ahrens@Sun.COM bpe->bpe_blk = *bp;
51*12470SMatthew.Ahrens@Sun.COM list_insert_tail(&bpl->bpl_list, bpe);
52789Sahrens mutex_exit(&bpl->bpl_lock);
53789Sahrens }
54789Sahrens
55*12470SMatthew.Ahrens@Sun.COM void
bplist_iterate(bplist_t * bpl,bplist_itor_t * func,void * arg,dmu_tx_t * tx)56*12470SMatthew.Ahrens@Sun.COM bplist_iterate(bplist_t *bpl, bplist_itor_t *func, void *arg, dmu_tx_t *tx)
571544Seschrock {
58*12470SMatthew.Ahrens@Sun.COM bplist_entry_t *bpe;
59789Sahrens
60789Sahrens mutex_enter(&bpl->bpl_lock);
61*12470SMatthew.Ahrens@Sun.COM while (bpe = list_head(&bpl->bpl_list)) {
62*12470SMatthew.Ahrens@Sun.COM list_remove(&bpl->bpl_list, bpe);
631544Seschrock mutex_exit(&bpl->bpl_lock);
64*12470SMatthew.Ahrens@Sun.COM func(arg, &bpe->bpe_blk, tx);
65*12470SMatthew.Ahrens@Sun.COM kmem_free(bpe, sizeof (*bpe));
66*12470SMatthew.Ahrens@Sun.COM mutex_enter(&bpl->bpl_lock);
672082Seschrock }
68789Sahrens mutex_exit(&bpl->bpl_lock);
69789Sahrens }
70