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