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