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