xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_errlog.c (revision eb633035c80613ec93d62f90482837adaaf21a0a)
1ea8dc4b6Seschrock /*
2ea8dc4b6Seschrock  * CDDL HEADER START
3ea8dc4b6Seschrock  *
4ea8dc4b6Seschrock  * 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.
7ea8dc4b6Seschrock  *
8ea8dc4b6Seschrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ea8dc4b6Seschrock  * or http://www.opensolaris.org/os/licensing.
10ea8dc4b6Seschrock  * See the License for the specific language governing permissions
11ea8dc4b6Seschrock  * and limitations under the License.
12ea8dc4b6Seschrock  *
13ea8dc4b6Seschrock  * When distributing Covered Code, include this CDDL HEADER in each
14ea8dc4b6Seschrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ea8dc4b6Seschrock  * If applicable, add the following below this CDDL HEADER, with the
16ea8dc4b6Seschrock  * fields enclosed by brackets "[]" replaced with your own identifying
17ea8dc4b6Seschrock  * information: Portions Copyright [yyyy] [name of copyright owner]
18ea8dc4b6Seschrock  *
19ea8dc4b6Seschrock  * CDDL HEADER END
20ea8dc4b6Seschrock  */
21ea8dc4b6Seschrock /*
223f9d6ad7SLin Ling  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
237802d7bfSMatthew Ahrens  * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
24ea8dc4b6Seschrock  */
25ea8dc4b6Seschrock 
26ea8dc4b6Seschrock /*
27ea8dc4b6Seschrock  * Routines to manage the on-disk persistent error log.
28ea8dc4b6Seschrock  *
29ea8dc4b6Seschrock  * Each pool stores a log of all logical data errors seen during normal
30ea8dc4b6Seschrock  * operation.  This is actually the union of two distinct logs: the last log,
31ea8dc4b6Seschrock  * and the current log.  All errors seen are logged to the current log.  When a
32ea8dc4b6Seschrock  * scrub completes, the current log becomes the last log, the last log is thrown
33ea8dc4b6Seschrock  * out, and the current log is reinitialized.  This way, if an error is somehow
34ea8dc4b6Seschrock  * corrected, a new scrub will show that that it no longer exists, and will be
35ea8dc4b6Seschrock  * deleted from the log when the scrub completes.
36ea8dc4b6Seschrock  *
37ea8dc4b6Seschrock  * The log is stored using a ZAP object whose key is a string form of the
387802d7bfSMatthew Ahrens  * zbookmark_phys tuple (objset, object, level, blkid), and whose contents is an
39ea8dc4b6Seschrock  * optional 'objset:object' human-readable string describing the data.  When an
40ea8dc4b6Seschrock  * error is first logged, this string will be empty, indicating that no name is
41ea8dc4b6Seschrock  * known.  This prevents us from having to issue a potentially large amount of
42ea8dc4b6Seschrock  * I/O to discover the object name during an error path.  Instead, we do the
43ea8dc4b6Seschrock  * calculation when the data is requested, storing the result so future queries
44ea8dc4b6Seschrock  * will be faster.
45ea8dc4b6Seschrock  *
46ea8dc4b6Seschrock  * This log is then shipped into an nvlist where the key is the dataset name and
47ea8dc4b6Seschrock  * the value is the object name.  Userland is then responsible for uniquifying
48ea8dc4b6Seschrock  * this list and displaying it to the user.
49ea8dc4b6Seschrock  */
50ea8dc4b6Seschrock 
51ea8dc4b6Seschrock #include <sys/dmu_tx.h>
52ea8dc4b6Seschrock #include <sys/spa.h>
53ea8dc4b6Seschrock #include <sys/spa_impl.h>
54ea8dc4b6Seschrock #include <sys/zap.h>
55ea8dc4b6Seschrock #include <sys/zio.h>
56ea8dc4b6Seschrock 
57ea8dc4b6Seschrock 
58ea8dc4b6Seschrock /*
59ea8dc4b6Seschrock  * Convert a bookmark to a string.
60ea8dc4b6Seschrock  */
61ea8dc4b6Seschrock static void
627802d7bfSMatthew Ahrens bookmark_to_name(zbookmark_phys_t *zb, char *buf, size_t len)
63ea8dc4b6Seschrock {
64ea8dc4b6Seschrock 	(void) snprintf(buf, len, "%llx:%llx:%llx:%llx",
65ea8dc4b6Seschrock 	    (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
66ea8dc4b6Seschrock 	    (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid);
67ea8dc4b6Seschrock }
68ea8dc4b6Seschrock 
69ea8dc4b6Seschrock /*
70ea8dc4b6Seschrock  * Convert a string to a bookmark
71ea8dc4b6Seschrock  */
725ad82045Snd #ifdef _KERNEL
73ea8dc4b6Seschrock static void
747802d7bfSMatthew Ahrens name_to_bookmark(char *buf, zbookmark_phys_t *zb)
75ea8dc4b6Seschrock {
764585130bSYuri Pankov 	zb->zb_objset = zfs_strtonum(buf, &buf);
77ea8dc4b6Seschrock 	ASSERT(*buf == ':');
784585130bSYuri Pankov 	zb->zb_object = zfs_strtonum(buf + 1, &buf);
79ea8dc4b6Seschrock 	ASSERT(*buf == ':');
804585130bSYuri Pankov 	zb->zb_level = (int)zfs_strtonum(buf + 1, &buf);
81ea8dc4b6Seschrock 	ASSERT(*buf == ':');
824585130bSYuri Pankov 	zb->zb_blkid = zfs_strtonum(buf + 1, &buf);
83ea8dc4b6Seschrock 	ASSERT(*buf == '\0');
84ea8dc4b6Seschrock }
855ad82045Snd #endif
86ea8dc4b6Seschrock 
87ea8dc4b6Seschrock /*
88ea8dc4b6Seschrock  * Log an uncorrectable error to the persistent error log.  We add it to the
89ea8dc4b6Seschrock  * spa's list of pending errors.  The changes are actually synced out to disk
90ea8dc4b6Seschrock  * during spa_errlog_sync().
91ea8dc4b6Seschrock  */
92ea8dc4b6Seschrock void
93*eb633035STom Caputi spa_log_error(spa_t *spa, const zbookmark_phys_t *zb)
94ea8dc4b6Seschrock {
95ea8dc4b6Seschrock 	spa_error_entry_t search;
96ea8dc4b6Seschrock 	spa_error_entry_t *new;
97ea8dc4b6Seschrock 	avl_tree_t *tree;
98ea8dc4b6Seschrock 	avl_index_t where;
99ea8dc4b6Seschrock 
100ea8dc4b6Seschrock 	/*
101ea8dc4b6Seschrock 	 * If we are trying to import a pool, ignore any errors, as we won't be
102ea8dc4b6Seschrock 	 * writing to the pool any time soon.
103ea8dc4b6Seschrock 	 */
104b16da2e2SGeorge Wilson 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
105ea8dc4b6Seschrock 		return;
106ea8dc4b6Seschrock 
107ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
108ea8dc4b6Seschrock 
109ea8dc4b6Seschrock 	/*
110ea8dc4b6Seschrock 	 * If we have had a request to rotate the log, log it to the next list
111ea8dc4b6Seschrock 	 * instead of the current one.
112ea8dc4b6Seschrock 	 */
113ea8dc4b6Seschrock 	if (spa->spa_scrub_active || spa->spa_scrub_finished)
114ea8dc4b6Seschrock 		tree = &spa->spa_errlist_scrub;
115ea8dc4b6Seschrock 	else
116ea8dc4b6Seschrock 		tree = &spa->spa_errlist_last;
117ea8dc4b6Seschrock 
118ea8dc4b6Seschrock 	search.se_bookmark = *zb;
119ea8dc4b6Seschrock 	if (avl_find(tree, &search, &where) != NULL) {
120ea8dc4b6Seschrock 		mutex_exit(&spa->spa_errlist_lock);
121ea8dc4b6Seschrock 		return;
122ea8dc4b6Seschrock 	}
123ea8dc4b6Seschrock 
124ea8dc4b6Seschrock 	new = kmem_zalloc(sizeof (spa_error_entry_t), KM_SLEEP);
125ea8dc4b6Seschrock 	new->se_bookmark = *zb;
126ea8dc4b6Seschrock 	avl_insert(tree, new, where);
127ea8dc4b6Seschrock 
128ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
129ea8dc4b6Seschrock }
130ea8dc4b6Seschrock 
131ea8dc4b6Seschrock /*
132ea8dc4b6Seschrock  * Return the number of errors currently in the error log.  This is actually the
133ea8dc4b6Seschrock  * sum of both the last log and the current log, since we don't know the union
134ea8dc4b6Seschrock  * of these logs until we reach userland.
135ea8dc4b6Seschrock  */
136ea8dc4b6Seschrock uint64_t
137ea8dc4b6Seschrock spa_get_errlog_size(spa_t *spa)
138ea8dc4b6Seschrock {
139ea8dc4b6Seschrock 	uint64_t total = 0, count;
140ea8dc4b6Seschrock 
141ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlog_lock);
142ea8dc4b6Seschrock 	if (spa->spa_errlog_scrub != 0 &&
143ea8dc4b6Seschrock 	    zap_count(spa->spa_meta_objset, spa->spa_errlog_scrub,
144ea8dc4b6Seschrock 	    &count) == 0)
145ea8dc4b6Seschrock 		total += count;
146ea8dc4b6Seschrock 
147ea8dc4b6Seschrock 	if (spa->spa_errlog_last != 0 && !spa->spa_scrub_finished &&
148ea8dc4b6Seschrock 	    zap_count(spa->spa_meta_objset, spa->spa_errlog_last,
149ea8dc4b6Seschrock 	    &count) == 0)
150ea8dc4b6Seschrock 		total += count;
151ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlog_lock);
152ea8dc4b6Seschrock 
153ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
154ea8dc4b6Seschrock 	total += avl_numnodes(&spa->spa_errlist_last);
155ea8dc4b6Seschrock 	total += avl_numnodes(&spa->spa_errlist_scrub);
156ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
157ea8dc4b6Seschrock 
158ea8dc4b6Seschrock 	return (total);
159ea8dc4b6Seschrock }
160ea8dc4b6Seschrock 
161ea8dc4b6Seschrock #ifdef _KERNEL
162ea8dc4b6Seschrock static int
163ea8dc4b6Seschrock process_error_log(spa_t *spa, uint64_t obj, void *addr, size_t *count)
164ea8dc4b6Seschrock {
165ea8dc4b6Seschrock 	zap_cursor_t zc;
166ea8dc4b6Seschrock 	zap_attribute_t za;
1677802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
168ea8dc4b6Seschrock 
169ea8dc4b6Seschrock 	if (obj == 0)
170ea8dc4b6Seschrock 		return (0);
171ea8dc4b6Seschrock 
172ea8dc4b6Seschrock 	for (zap_cursor_init(&zc, spa->spa_meta_objset, obj);
173ea8dc4b6Seschrock 	    zap_cursor_retrieve(&zc, &za) == 0;
174ea8dc4b6Seschrock 	    zap_cursor_advance(&zc)) {
175ea8dc4b6Seschrock 
176ea8dc4b6Seschrock 		if (*count == 0) {
177ea8dc4b6Seschrock 			zap_cursor_fini(&zc);
178be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
179ea8dc4b6Seschrock 		}
180ea8dc4b6Seschrock 
181ea8dc4b6Seschrock 		name_to_bookmark(za.za_name, &zb);
182ea8dc4b6Seschrock 
183ea8dc4b6Seschrock 		if (copyout(&zb, (char *)addr +
1847802d7bfSMatthew Ahrens 		    (*count - 1) * sizeof (zbookmark_phys_t),
1857802d7bfSMatthew Ahrens 		    sizeof (zbookmark_phys_t)) != 0) {
186b287be1bSWill Andrews 			zap_cursor_fini(&zc);
187be6fd75aSMatthew Ahrens 			return (SET_ERROR(EFAULT));
188b287be1bSWill Andrews 		}
189ea8dc4b6Seschrock 
190ea8dc4b6Seschrock 		*count -= 1;
191ea8dc4b6Seschrock 	}
192ea8dc4b6Seschrock 
193ea8dc4b6Seschrock 	zap_cursor_fini(&zc);
194ea8dc4b6Seschrock 
195ea8dc4b6Seschrock 	return (0);
196ea8dc4b6Seschrock }
197ea8dc4b6Seschrock 
198ea8dc4b6Seschrock static int
199ea8dc4b6Seschrock process_error_list(avl_tree_t *list, void *addr, size_t *count)
200ea8dc4b6Seschrock {
201ea8dc4b6Seschrock 	spa_error_entry_t *se;
202ea8dc4b6Seschrock 
203ea8dc4b6Seschrock 	for (se = avl_first(list); se != NULL; se = AVL_NEXT(list, se)) {
204ea8dc4b6Seschrock 
205ea8dc4b6Seschrock 		if (*count == 0)
206be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
207ea8dc4b6Seschrock 
208ea8dc4b6Seschrock 		if (copyout(&se->se_bookmark, (char *)addr +
2097802d7bfSMatthew Ahrens 		    (*count - 1) * sizeof (zbookmark_phys_t),
2107802d7bfSMatthew Ahrens 		    sizeof (zbookmark_phys_t)) != 0)
211be6fd75aSMatthew Ahrens 			return (SET_ERROR(EFAULT));
212ea8dc4b6Seschrock 
213ea8dc4b6Seschrock 		*count -= 1;
214ea8dc4b6Seschrock 	}
215ea8dc4b6Seschrock 
216ea8dc4b6Seschrock 	return (0);
217ea8dc4b6Seschrock }
218ea8dc4b6Seschrock #endif
219ea8dc4b6Seschrock 
220ea8dc4b6Seschrock /*
221ea8dc4b6Seschrock  * Copy all known errors to userland as an array of bookmarks.  This is
222ea8dc4b6Seschrock  * actually a union of the on-disk last log and current log, as well as any
223ea8dc4b6Seschrock  * pending error requests.
224ea8dc4b6Seschrock  *
225ea8dc4b6Seschrock  * Because the act of reading the on-disk log could cause errors to be
226ea8dc4b6Seschrock  * generated, we have two separate locks: one for the error log and one for the
227ea8dc4b6Seschrock  * in-core error lists.  We only need the error list lock to log and error, so
228ea8dc4b6Seschrock  * we grab the error log lock while we read the on-disk logs, and only pick up
229ea8dc4b6Seschrock  * the error list lock when we are finished.
230ea8dc4b6Seschrock  */
231ea8dc4b6Seschrock int
232ea8dc4b6Seschrock spa_get_errlog(spa_t *spa, void *uaddr, size_t *count)
233ea8dc4b6Seschrock {
234ea8dc4b6Seschrock 	int ret = 0;
235ea8dc4b6Seschrock 
236ea8dc4b6Seschrock #ifdef _KERNEL
237ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlog_lock);
238ea8dc4b6Seschrock 
239ea8dc4b6Seschrock 	ret = process_error_log(spa, spa->spa_errlog_scrub, uaddr, count);
240ea8dc4b6Seschrock 
241ea8dc4b6Seschrock 	if (!ret && !spa->spa_scrub_finished)
242ea8dc4b6Seschrock 		ret = process_error_log(spa, spa->spa_errlog_last, uaddr,
243ea8dc4b6Seschrock 		    count);
244ea8dc4b6Seschrock 
245ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
246ea8dc4b6Seschrock 	if (!ret)
247ea8dc4b6Seschrock 		ret = process_error_list(&spa->spa_errlist_scrub, uaddr,
248ea8dc4b6Seschrock 		    count);
249ea8dc4b6Seschrock 	if (!ret)
250ea8dc4b6Seschrock 		ret = process_error_list(&spa->spa_errlist_last, uaddr,
251ea8dc4b6Seschrock 		    count);
252ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
253ea8dc4b6Seschrock 
254ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlog_lock);
255ea8dc4b6Seschrock #endif
256ea8dc4b6Seschrock 
257ea8dc4b6Seschrock 	return (ret);
258ea8dc4b6Seschrock }
259ea8dc4b6Seschrock 
260ea8dc4b6Seschrock /*
261ea8dc4b6Seschrock  * Called when a scrub completes.  This simply set a bit which tells which AVL
262ea8dc4b6Seschrock  * tree to add new errors.  spa_errlog_sync() is responsible for actually
263ea8dc4b6Seschrock  * syncing the changes to the underlying objects.
264ea8dc4b6Seschrock  */
265ea8dc4b6Seschrock void
266ea8dc4b6Seschrock spa_errlog_rotate(spa_t *spa)
267ea8dc4b6Seschrock {
268ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
269ea8dc4b6Seschrock 	spa->spa_scrub_finished = B_TRUE;
270ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
271ea8dc4b6Seschrock }
272ea8dc4b6Seschrock 
273ea8dc4b6Seschrock /*
274ea8dc4b6Seschrock  * Discard any pending errors from the spa_t.  Called when unloading a faulted
275ea8dc4b6Seschrock  * pool, as the errors encountered during the open cannot be synced to disk.
276ea8dc4b6Seschrock  */
277ea8dc4b6Seschrock void
278ea8dc4b6Seschrock spa_errlog_drain(spa_t *spa)
279ea8dc4b6Seschrock {
280ea8dc4b6Seschrock 	spa_error_entry_t *se;
281ea8dc4b6Seschrock 	void *cookie;
282ea8dc4b6Seschrock 
283ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
284ea8dc4b6Seschrock 
285ea8dc4b6Seschrock 	cookie = NULL;
286ea8dc4b6Seschrock 	while ((se = avl_destroy_nodes(&spa->spa_errlist_last,
287ea8dc4b6Seschrock 	    &cookie)) != NULL)
288ea8dc4b6Seschrock 		kmem_free(se, sizeof (spa_error_entry_t));
289ea8dc4b6Seschrock 	cookie = NULL;
290ea8dc4b6Seschrock 	while ((se = avl_destroy_nodes(&spa->spa_errlist_scrub,
291ea8dc4b6Seschrock 	    &cookie)) != NULL)
292ea8dc4b6Seschrock 		kmem_free(se, sizeof (spa_error_entry_t));
293ea8dc4b6Seschrock 
294ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
295ea8dc4b6Seschrock }
296ea8dc4b6Seschrock 
297ea8dc4b6Seschrock /*
298ea8dc4b6Seschrock  * Process a list of errors into the current on-disk log.
299ea8dc4b6Seschrock  */
300ea8dc4b6Seschrock static void
301ea8dc4b6Seschrock sync_error_list(spa_t *spa, avl_tree_t *t, uint64_t *obj, dmu_tx_t *tx)
302ea8dc4b6Seschrock {
303ea8dc4b6Seschrock 	spa_error_entry_t *se;
304ea8dc4b6Seschrock 	char buf[64];
305ea8dc4b6Seschrock 	void *cookie;
306ea8dc4b6Seschrock 
307ea8dc4b6Seschrock 	if (avl_numnodes(t) != 0) {
308ea8dc4b6Seschrock 		/* create log if necessary */
309ea8dc4b6Seschrock 		if (*obj == 0)
310ea8dc4b6Seschrock 			*obj = zap_create(spa->spa_meta_objset,
311ea8dc4b6Seschrock 			    DMU_OT_ERROR_LOG, DMU_OT_NONE,
312ea8dc4b6Seschrock 			    0, tx);
313ea8dc4b6Seschrock 
314ea8dc4b6Seschrock 		/* add errors to the current log */
315ea8dc4b6Seschrock 		for (se = avl_first(t); se != NULL; se = AVL_NEXT(t, se)) {
316ea8dc4b6Seschrock 			char *name = se->se_name ? se->se_name : "";
317ea8dc4b6Seschrock 
318ea8dc4b6Seschrock 			bookmark_to_name(&se->se_bookmark, buf, sizeof (buf));
319ea8dc4b6Seschrock 
320ea8dc4b6Seschrock 			(void) zap_update(spa->spa_meta_objset,
321ea8dc4b6Seschrock 			    *obj, buf, 1, strlen(name) + 1, name, tx);
322ea8dc4b6Seschrock 		}
323ea8dc4b6Seschrock 
324ea8dc4b6Seschrock 		/* purge the error list */
325ea8dc4b6Seschrock 		cookie = NULL;
326ea8dc4b6Seschrock 		while ((se = avl_destroy_nodes(t, &cookie)) != NULL)
327ea8dc4b6Seschrock 			kmem_free(se, sizeof (spa_error_entry_t));
328ea8dc4b6Seschrock 	}
329ea8dc4b6Seschrock }
330ea8dc4b6Seschrock 
331ea8dc4b6Seschrock /*
332ea8dc4b6Seschrock  * Sync the error log out to disk.  This is a little tricky because the act of
333ea8dc4b6Seschrock  * writing the error log requires the spa_errlist_lock.  So, we need to lock the
334ea8dc4b6Seschrock  * error lists, take a copy of the lists, and then reinitialize them.  Then, we
335ea8dc4b6Seschrock  * drop the error list lock and take the error log lock, at which point we
336ea8dc4b6Seschrock  * do the errlog processing.  Then, if we encounter an I/O error during this
337ea8dc4b6Seschrock  * process, we can successfully add the error to the list.  Note that this will
338ea8dc4b6Seschrock  * result in the perpetual recycling of errors, but it is an unlikely situation
339ea8dc4b6Seschrock  * and not a performance critical operation.
340ea8dc4b6Seschrock  */
341ea8dc4b6Seschrock void
342ea8dc4b6Seschrock spa_errlog_sync(spa_t *spa, uint64_t txg)
343ea8dc4b6Seschrock {
344ea8dc4b6Seschrock 	dmu_tx_t *tx;
345ea8dc4b6Seschrock 	avl_tree_t scrub, last;
346ea8dc4b6Seschrock 	int scrub_finished;
347ea8dc4b6Seschrock 
348ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
349ea8dc4b6Seschrock 
350ea8dc4b6Seschrock 	/*
351ea8dc4b6Seschrock 	 * Bail out early under normal circumstances.
352ea8dc4b6Seschrock 	 */
353ea8dc4b6Seschrock 	if (avl_numnodes(&spa->spa_errlist_scrub) == 0 &&
354ea8dc4b6Seschrock 	    avl_numnodes(&spa->spa_errlist_last) == 0 &&
355ea8dc4b6Seschrock 	    !spa->spa_scrub_finished) {
356ea8dc4b6Seschrock 		mutex_exit(&spa->spa_errlist_lock);
357ea8dc4b6Seschrock 		return;
358ea8dc4b6Seschrock 	}
359ea8dc4b6Seschrock 
360ea8dc4b6Seschrock 	spa_get_errlists(spa, &last, &scrub);
361ea8dc4b6Seschrock 	scrub_finished = spa->spa_scrub_finished;
362ea8dc4b6Seschrock 	spa->spa_scrub_finished = B_FALSE;
363ea8dc4b6Seschrock 
364ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
365ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlog_lock);
366ea8dc4b6Seschrock 
367ea8dc4b6Seschrock 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
368ea8dc4b6Seschrock 
369ea8dc4b6Seschrock 	/*
370ea8dc4b6Seschrock 	 * Sync out the current list of errors.
371ea8dc4b6Seschrock 	 */
372ea8dc4b6Seschrock 	sync_error_list(spa, &last, &spa->spa_errlog_last, tx);
373ea8dc4b6Seschrock 
374ea8dc4b6Seschrock 	/*
375ea8dc4b6Seschrock 	 * Rotate the log if necessary.
376ea8dc4b6Seschrock 	 */
377ea8dc4b6Seschrock 	if (scrub_finished) {
378ea8dc4b6Seschrock 		if (spa->spa_errlog_last != 0)
379ea8dc4b6Seschrock 			VERIFY(dmu_object_free(spa->spa_meta_objset,
380ea8dc4b6Seschrock 			    spa->spa_errlog_last, tx) == 0);
381ea8dc4b6Seschrock 		spa->spa_errlog_last = spa->spa_errlog_scrub;
382ea8dc4b6Seschrock 		spa->spa_errlog_scrub = 0;
383ea8dc4b6Seschrock 
384ea8dc4b6Seschrock 		sync_error_list(spa, &scrub, &spa->spa_errlog_last, tx);
385ea8dc4b6Seschrock 	}
386ea8dc4b6Seschrock 
387ea8dc4b6Seschrock 	/*
388ea8dc4b6Seschrock 	 * Sync out any pending scrub errors.
389ea8dc4b6Seschrock 	 */
390ea8dc4b6Seschrock 	sync_error_list(spa, &scrub, &spa->spa_errlog_scrub, tx);
391ea8dc4b6Seschrock 
392ea8dc4b6Seschrock 	/*
393ea8dc4b6Seschrock 	 * Update the MOS to reflect the new values.
394ea8dc4b6Seschrock 	 */
395ea8dc4b6Seschrock 	(void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
396ea8dc4b6Seschrock 	    DMU_POOL_ERRLOG_LAST, sizeof (uint64_t), 1,
397ea8dc4b6Seschrock 	    &spa->spa_errlog_last, tx);
398ea8dc4b6Seschrock 	(void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
399ea8dc4b6Seschrock 	    DMU_POOL_ERRLOG_SCRUB, sizeof (uint64_t), 1,
400ea8dc4b6Seschrock 	    &spa->spa_errlog_scrub, tx);
401ea8dc4b6Seschrock 
402ea8dc4b6Seschrock 	dmu_tx_commit(tx);
403ea8dc4b6Seschrock 
404ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlog_lock);
405ea8dc4b6Seschrock }
406