xref: /titanic_50/usr/src/uts/common/fs/zfs/dsl_synctask.c (revision f841f6ad96ea6675d6c6b35c749eaac601799fdf)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/dmu.h>
29 #include <sys/dmu_tx.h>
30 #include <sys/dsl_pool.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dsl_synctask.h>
33 
34 #define	DST_AVG_BLKSHIFT 14
35 
36 /* ARGSUSED */
37 static int
38 dsl_null_checkfunc(void *arg1, void *arg2, dmu_tx_t *tx)
39 {
40 	return (0);
41 }
42 
43 dsl_sync_task_group_t *
44 dsl_sync_task_group_create(dsl_pool_t *dp)
45 {
46 	dsl_sync_task_group_t *dstg;
47 
48 	dstg = kmem_zalloc(sizeof (dsl_sync_task_group_t), KM_SLEEP);
49 	list_create(&dstg->dstg_tasks, sizeof (dsl_sync_task_t),
50 	    offsetof(dsl_sync_task_t, dst_node));
51 	dstg->dstg_pool = dp;
52 
53 	return (dstg);
54 }
55 
56 void
57 dsl_sync_task_create(dsl_sync_task_group_t *dstg,
58     dsl_checkfunc_t *checkfunc, dsl_syncfunc_t *syncfunc,
59     void *arg1, void *arg2, int blocks_modified)
60 {
61 	dsl_sync_task_t *dst;
62 
63 	if (checkfunc == NULL)
64 		checkfunc = dsl_null_checkfunc;
65 	dst = kmem_zalloc(sizeof (dsl_sync_task_t), KM_SLEEP);
66 	dst->dst_checkfunc = checkfunc;
67 	dst->dst_syncfunc = syncfunc;
68 	dst->dst_arg1 = arg1;
69 	dst->dst_arg2 = arg2;
70 	list_insert_tail(&dstg->dstg_tasks, dst);
71 
72 	dstg->dstg_space += blocks_modified << DST_AVG_BLKSHIFT;
73 }
74 
75 int
76 dsl_sync_task_group_wait(dsl_sync_task_group_t *dstg)
77 {
78 	dmu_tx_t *tx;
79 	uint64_t txg;
80 	dsl_sync_task_t *dst;
81 
82 top:
83 	tx = dmu_tx_create_dd(dstg->dstg_pool->dp_mos_dir);
84 	VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
85 
86 	txg = dmu_tx_get_txg(tx);
87 
88 	/* Do a preliminary error check. */
89 	dstg->dstg_err = 0;
90 	rw_enter(&dstg->dstg_pool->dp_config_rwlock, RW_READER);
91 	for (dst = list_head(&dstg->dstg_tasks); dst;
92 	    dst = list_next(&dstg->dstg_tasks, dst)) {
93 #ifdef ZFS_DEBUG
94 		/*
95 		 * Only check half the time, otherwise, the sync-context
96 		 * check will almost never fail.
97 		 */
98 		if (spa_get_random(2) == 0)
99 			continue;
100 #endif
101 		dst->dst_err =
102 		    dst->dst_checkfunc(dst->dst_arg1, dst->dst_arg2, tx);
103 		if (dst->dst_err)
104 			dstg->dstg_err = dst->dst_err;
105 	}
106 	rw_exit(&dstg->dstg_pool->dp_config_rwlock);
107 
108 	if (dstg->dstg_err) {
109 		dmu_tx_commit(tx);
110 		return (dstg->dstg_err);
111 	}
112 
113 	VERIFY(0 == txg_list_add(&dstg->dstg_pool->dp_sync_tasks, dstg, txg));
114 
115 	dmu_tx_commit(tx);
116 
117 	txg_wait_synced(dstg->dstg_pool, txg);
118 
119 	if (dstg->dstg_err == EAGAIN)
120 		goto top;
121 
122 	return (dstg->dstg_err);
123 }
124 
125 void
126 dsl_sync_task_group_destroy(dsl_sync_task_group_t *dstg)
127 {
128 	dsl_sync_task_t *dst;
129 
130 	while (dst = list_head(&dstg->dstg_tasks)) {
131 		list_remove(&dstg->dstg_tasks, dst);
132 		kmem_free(dst, sizeof (dsl_sync_task_t));
133 	}
134 	kmem_free(dstg, sizeof (dsl_sync_task_group_t));
135 }
136 
137 void
138 dsl_sync_task_group_sync(dsl_sync_task_group_t *dstg, dmu_tx_t *tx)
139 {
140 	dsl_sync_task_t *dst;
141 	void *tr_cookie;
142 
143 	ASSERT3U(dstg->dstg_err, ==, 0);
144 
145 	/*
146 	 * Check for sufficient space.
147 	 */
148 	dstg->dstg_err = dsl_dir_tempreserve_space(dstg->dstg_pool->dp_mos_dir,
149 	    dstg->dstg_space, dstg->dstg_space * 3, 0, &tr_cookie, tx);
150 	/* don't bother trying again */
151 	if (dstg->dstg_err == ERESTART)
152 		dstg->dstg_err = ENOSPC;
153 	if (dstg->dstg_err)
154 		return;
155 
156 	/*
157 	 * Check for errors by calling checkfuncs.
158 	 */
159 	rw_enter(&dstg->dstg_pool->dp_config_rwlock, RW_WRITER);
160 	for (dst = list_head(&dstg->dstg_tasks); dst;
161 	    dst = list_next(&dstg->dstg_tasks, dst)) {
162 		dst->dst_err =
163 		    dst->dst_checkfunc(dst->dst_arg1, dst->dst_arg2, tx);
164 		if (dst->dst_err)
165 			dstg->dstg_err = dst->dst_err;
166 	}
167 
168 	if (dstg->dstg_err == 0) {
169 		/*
170 		 * Execute sync tasks.
171 		 */
172 		for (dst = list_head(&dstg->dstg_tasks); dst;
173 		    dst = list_next(&dstg->dstg_tasks, dst)) {
174 			dst->dst_syncfunc(dst->dst_arg1, dst->dst_arg2, tx);
175 		}
176 	}
177 	rw_exit(&dstg->dstg_pool->dp_config_rwlock);
178 
179 	dsl_dir_tempreserve_clear(tr_cookie, tx);
180 }
181 
182 int
183 dsl_sync_task_do(dsl_pool_t *dp,
184     dsl_checkfunc_t *checkfunc, dsl_syncfunc_t *syncfunc,
185     void *arg1, void *arg2, int blocks_modified)
186 {
187 	dsl_sync_task_group_t *dstg;
188 	int err;
189 
190 	dstg = dsl_sync_task_group_create(dp);
191 	dsl_sync_task_create(dstg, checkfunc, syncfunc,
192 	    arg1, arg2, blocks_modified);
193 	err = dsl_sync_task_group_wait(dstg);
194 	dsl_sync_task_group_destroy(dstg);
195 	return (err);
196 }
197