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