xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_errlog.c (revision b16da2e29e074fb6eaeadc4fd7d17ae7340ba240)
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 /*
2214843421SMatthew Ahrens  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23ea8dc4b6Seschrock  * Use is subject to license terms.
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
38ea8dc4b6Seschrock  * zbookmark 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  * This is a stripped-down version of strtoull, suitable only for converting
59ea8dc4b6Seschrock  * lowercase hexidecimal numbers that don't overflow.
60ea8dc4b6Seschrock  */
6114843421SMatthew Ahrens uint64_t
6214843421SMatthew Ahrens strtonum(const char *str, char **nptr)
63ea8dc4b6Seschrock {
64ea8dc4b6Seschrock 	uint64_t val = 0;
65ea8dc4b6Seschrock 	char c;
66ea8dc4b6Seschrock 	int digit;
67ea8dc4b6Seschrock 
68ea8dc4b6Seschrock 	while ((c = *str) != '\0') {
69ea8dc4b6Seschrock 		if (c >= '0' && c <= '9')
70ea8dc4b6Seschrock 			digit = c - '0';
71ea8dc4b6Seschrock 		else if (c >= 'a' && c <= 'f')
72ea8dc4b6Seschrock 			digit = 10 + c - 'a';
73ea8dc4b6Seschrock 		else
74ea8dc4b6Seschrock 			break;
75ea8dc4b6Seschrock 
76ea8dc4b6Seschrock 		val *= 16;
77ea8dc4b6Seschrock 		val += digit;
78ea8dc4b6Seschrock 
79ea8dc4b6Seschrock 		str++;
80ea8dc4b6Seschrock 	}
81ea8dc4b6Seschrock 
8214843421SMatthew Ahrens 	if (nptr)
8314843421SMatthew Ahrens 		*nptr = (char *)str;
84ea8dc4b6Seschrock 
85ea8dc4b6Seschrock 	return (val);
86ea8dc4b6Seschrock }
87ea8dc4b6Seschrock 
88ea8dc4b6Seschrock /*
89ea8dc4b6Seschrock  * Convert a bookmark to a string.
90ea8dc4b6Seschrock  */
91ea8dc4b6Seschrock static void
92ea8dc4b6Seschrock bookmark_to_name(zbookmark_t *zb, char *buf, size_t len)
93ea8dc4b6Seschrock {
94ea8dc4b6Seschrock 	(void) snprintf(buf, len, "%llx:%llx:%llx:%llx",
95ea8dc4b6Seschrock 	    (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
96ea8dc4b6Seschrock 	    (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid);
97ea8dc4b6Seschrock }
98ea8dc4b6Seschrock 
99ea8dc4b6Seschrock /*
100ea8dc4b6Seschrock  * Convert a string to a bookmark
101ea8dc4b6Seschrock  */
1025ad82045Snd #ifdef _KERNEL
103ea8dc4b6Seschrock static void
104ea8dc4b6Seschrock name_to_bookmark(char *buf, zbookmark_t *zb)
105ea8dc4b6Seschrock {
106ea8dc4b6Seschrock 	zb->zb_objset = strtonum(buf, &buf);
107ea8dc4b6Seschrock 	ASSERT(*buf == ':');
108ea8dc4b6Seschrock 	zb->zb_object = strtonum(buf + 1, &buf);
109ea8dc4b6Seschrock 	ASSERT(*buf == ':');
110ea8dc4b6Seschrock 	zb->zb_level = (int)strtonum(buf + 1, &buf);
111ea8dc4b6Seschrock 	ASSERT(*buf == ':');
112ea8dc4b6Seschrock 	zb->zb_blkid = strtonum(buf + 1, &buf);
113ea8dc4b6Seschrock 	ASSERT(*buf == '\0');
114ea8dc4b6Seschrock }
1155ad82045Snd #endif
116ea8dc4b6Seschrock 
117ea8dc4b6Seschrock /*
118ea8dc4b6Seschrock  * Log an uncorrectable error to the persistent error log.  We add it to the
119ea8dc4b6Seschrock  * spa's list of pending errors.  The changes are actually synced out to disk
120ea8dc4b6Seschrock  * during spa_errlog_sync().
121ea8dc4b6Seschrock  */
122ea8dc4b6Seschrock void
123ea8dc4b6Seschrock spa_log_error(spa_t *spa, zio_t *zio)
124ea8dc4b6Seschrock {
125ea8dc4b6Seschrock 	zbookmark_t *zb = &zio->io_logical->io_bookmark;
126ea8dc4b6Seschrock 	spa_error_entry_t search;
127ea8dc4b6Seschrock 	spa_error_entry_t *new;
128ea8dc4b6Seschrock 	avl_tree_t *tree;
129ea8dc4b6Seschrock 	avl_index_t where;
130ea8dc4b6Seschrock 
131ea8dc4b6Seschrock 	/*
132ea8dc4b6Seschrock 	 * If we are trying to import a pool, ignore any errors, as we won't be
133ea8dc4b6Seschrock 	 * writing to the pool any time soon.
134ea8dc4b6Seschrock 	 */
135*b16da2e2SGeorge Wilson 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
136ea8dc4b6Seschrock 		return;
137ea8dc4b6Seschrock 
138ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
139ea8dc4b6Seschrock 
140ea8dc4b6Seschrock 	/*
141ea8dc4b6Seschrock 	 * If we have had a request to rotate the log, log it to the next list
142ea8dc4b6Seschrock 	 * instead of the current one.
143ea8dc4b6Seschrock 	 */
144ea8dc4b6Seschrock 	if (spa->spa_scrub_active || spa->spa_scrub_finished)
145ea8dc4b6Seschrock 		tree = &spa->spa_errlist_scrub;
146ea8dc4b6Seschrock 	else
147ea8dc4b6Seschrock 		tree = &spa->spa_errlist_last;
148ea8dc4b6Seschrock 
149ea8dc4b6Seschrock 	search.se_bookmark = *zb;
150ea8dc4b6Seschrock 	if (avl_find(tree, &search, &where) != NULL) {
151ea8dc4b6Seschrock 		mutex_exit(&spa->spa_errlist_lock);
152ea8dc4b6Seschrock 		return;
153ea8dc4b6Seschrock 	}
154ea8dc4b6Seschrock 
155ea8dc4b6Seschrock 	new = kmem_zalloc(sizeof (spa_error_entry_t), KM_SLEEP);
156ea8dc4b6Seschrock 	new->se_bookmark = *zb;
157ea8dc4b6Seschrock 	avl_insert(tree, new, where);
158ea8dc4b6Seschrock 
159ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
160ea8dc4b6Seschrock }
161ea8dc4b6Seschrock 
162ea8dc4b6Seschrock /*
163ea8dc4b6Seschrock  * Return the number of errors currently in the error log.  This is actually the
164ea8dc4b6Seschrock  * sum of both the last log and the current log, since we don't know the union
165ea8dc4b6Seschrock  * of these logs until we reach userland.
166ea8dc4b6Seschrock  */
167ea8dc4b6Seschrock uint64_t
168ea8dc4b6Seschrock spa_get_errlog_size(spa_t *spa)
169ea8dc4b6Seschrock {
170ea8dc4b6Seschrock 	uint64_t total = 0, count;
171ea8dc4b6Seschrock 
172ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlog_lock);
173ea8dc4b6Seschrock 	if (spa->spa_errlog_scrub != 0 &&
174ea8dc4b6Seschrock 	    zap_count(spa->spa_meta_objset, spa->spa_errlog_scrub,
175ea8dc4b6Seschrock 	    &count) == 0)
176ea8dc4b6Seschrock 		total += count;
177ea8dc4b6Seschrock 
178ea8dc4b6Seschrock 	if (spa->spa_errlog_last != 0 && !spa->spa_scrub_finished &&
179ea8dc4b6Seschrock 	    zap_count(spa->spa_meta_objset, spa->spa_errlog_last,
180ea8dc4b6Seschrock 	    &count) == 0)
181ea8dc4b6Seschrock 		total += count;
182ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlog_lock);
183ea8dc4b6Seschrock 
184ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
185ea8dc4b6Seschrock 	total += avl_numnodes(&spa->spa_errlist_last);
186ea8dc4b6Seschrock 	total += avl_numnodes(&spa->spa_errlist_scrub);
187ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
188ea8dc4b6Seschrock 
189ea8dc4b6Seschrock 	return (total);
190ea8dc4b6Seschrock }
191ea8dc4b6Seschrock 
192ea8dc4b6Seschrock #ifdef _KERNEL
193ea8dc4b6Seschrock static int
194ea8dc4b6Seschrock process_error_log(spa_t *spa, uint64_t obj, void *addr, size_t *count)
195ea8dc4b6Seschrock {
196ea8dc4b6Seschrock 	zap_cursor_t zc;
197ea8dc4b6Seschrock 	zap_attribute_t za;
198ea8dc4b6Seschrock 	zbookmark_t zb;
199ea8dc4b6Seschrock 
200ea8dc4b6Seschrock 	if (obj == 0)
201ea8dc4b6Seschrock 		return (0);
202ea8dc4b6Seschrock 
203ea8dc4b6Seschrock 	for (zap_cursor_init(&zc, spa->spa_meta_objset, obj);
204ea8dc4b6Seschrock 	    zap_cursor_retrieve(&zc, &za) == 0;
205ea8dc4b6Seschrock 	    zap_cursor_advance(&zc)) {
206ea8dc4b6Seschrock 
207ea8dc4b6Seschrock 		if (*count == 0) {
208ea8dc4b6Seschrock 			zap_cursor_fini(&zc);
209ea8dc4b6Seschrock 			return (ENOMEM);
210ea8dc4b6Seschrock 		}
211ea8dc4b6Seschrock 
212ea8dc4b6Seschrock 		name_to_bookmark(za.za_name, &zb);
213ea8dc4b6Seschrock 
214ea8dc4b6Seschrock 		if (copyout(&zb, (char *)addr +
215ea8dc4b6Seschrock 		    (*count - 1) * sizeof (zbookmark_t),
216ea8dc4b6Seschrock 		    sizeof (zbookmark_t)) != 0)
217ea8dc4b6Seschrock 			return (EFAULT);
218ea8dc4b6Seschrock 
219ea8dc4b6Seschrock 		*count -= 1;
220ea8dc4b6Seschrock 	}
221ea8dc4b6Seschrock 
222ea8dc4b6Seschrock 	zap_cursor_fini(&zc);
223ea8dc4b6Seschrock 
224ea8dc4b6Seschrock 	return (0);
225ea8dc4b6Seschrock }
226ea8dc4b6Seschrock 
227ea8dc4b6Seschrock static int
228ea8dc4b6Seschrock process_error_list(avl_tree_t *list, void *addr, size_t *count)
229ea8dc4b6Seschrock {
230ea8dc4b6Seschrock 	spa_error_entry_t *se;
231ea8dc4b6Seschrock 
232ea8dc4b6Seschrock 	for (se = avl_first(list); se != NULL; se = AVL_NEXT(list, se)) {
233ea8dc4b6Seschrock 
234ea8dc4b6Seschrock 		if (*count == 0)
235ea8dc4b6Seschrock 			return (ENOMEM);
236ea8dc4b6Seschrock 
237ea8dc4b6Seschrock 		if (copyout(&se->se_bookmark, (char *)addr +
238ea8dc4b6Seschrock 		    (*count - 1) * sizeof (zbookmark_t),
239ea8dc4b6Seschrock 		    sizeof (zbookmark_t)) != 0)
240ea8dc4b6Seschrock 			return (EFAULT);
241ea8dc4b6Seschrock 
242ea8dc4b6Seschrock 		*count -= 1;
243ea8dc4b6Seschrock 	}
244ea8dc4b6Seschrock 
245ea8dc4b6Seschrock 	return (0);
246ea8dc4b6Seschrock }
247ea8dc4b6Seschrock #endif
248ea8dc4b6Seschrock 
249ea8dc4b6Seschrock /*
250ea8dc4b6Seschrock  * Copy all known errors to userland as an array of bookmarks.  This is
251ea8dc4b6Seschrock  * actually a union of the on-disk last log and current log, as well as any
252ea8dc4b6Seschrock  * pending error requests.
253ea8dc4b6Seschrock  *
254ea8dc4b6Seschrock  * Because the act of reading the on-disk log could cause errors to be
255ea8dc4b6Seschrock  * generated, we have two separate locks: one for the error log and one for the
256ea8dc4b6Seschrock  * in-core error lists.  We only need the error list lock to log and error, so
257ea8dc4b6Seschrock  * we grab the error log lock while we read the on-disk logs, and only pick up
258ea8dc4b6Seschrock  * the error list lock when we are finished.
259ea8dc4b6Seschrock  */
260ea8dc4b6Seschrock int
261ea8dc4b6Seschrock spa_get_errlog(spa_t *spa, void *uaddr, size_t *count)
262ea8dc4b6Seschrock {
263ea8dc4b6Seschrock 	int ret = 0;
264ea8dc4b6Seschrock 
265ea8dc4b6Seschrock #ifdef _KERNEL
266ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlog_lock);
267ea8dc4b6Seschrock 
268ea8dc4b6Seschrock 	ret = process_error_log(spa, spa->spa_errlog_scrub, uaddr, count);
269ea8dc4b6Seschrock 
270ea8dc4b6Seschrock 	if (!ret && !spa->spa_scrub_finished)
271ea8dc4b6Seschrock 		ret = process_error_log(spa, spa->spa_errlog_last, uaddr,
272ea8dc4b6Seschrock 		    count);
273ea8dc4b6Seschrock 
274ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
275ea8dc4b6Seschrock 	if (!ret)
276ea8dc4b6Seschrock 		ret = process_error_list(&spa->spa_errlist_scrub, uaddr,
277ea8dc4b6Seschrock 		    count);
278ea8dc4b6Seschrock 	if (!ret)
279ea8dc4b6Seschrock 		ret = process_error_list(&spa->spa_errlist_last, uaddr,
280ea8dc4b6Seschrock 		    count);
281ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
282ea8dc4b6Seschrock 
283ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlog_lock);
284ea8dc4b6Seschrock #endif
285ea8dc4b6Seschrock 
286ea8dc4b6Seschrock 	return (ret);
287ea8dc4b6Seschrock }
288ea8dc4b6Seschrock 
289ea8dc4b6Seschrock /*
290ea8dc4b6Seschrock  * Called when a scrub completes.  This simply set a bit which tells which AVL
291ea8dc4b6Seschrock  * tree to add new errors.  spa_errlog_sync() is responsible for actually
292ea8dc4b6Seschrock  * syncing the changes to the underlying objects.
293ea8dc4b6Seschrock  */
294ea8dc4b6Seschrock void
295ea8dc4b6Seschrock spa_errlog_rotate(spa_t *spa)
296ea8dc4b6Seschrock {
297ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
298ea8dc4b6Seschrock 	spa->spa_scrub_finished = B_TRUE;
299ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
300ea8dc4b6Seschrock }
301ea8dc4b6Seschrock 
302ea8dc4b6Seschrock /*
303ea8dc4b6Seschrock  * Discard any pending errors from the spa_t.  Called when unloading a faulted
304ea8dc4b6Seschrock  * pool, as the errors encountered during the open cannot be synced to disk.
305ea8dc4b6Seschrock  */
306ea8dc4b6Seschrock void
307ea8dc4b6Seschrock spa_errlog_drain(spa_t *spa)
308ea8dc4b6Seschrock {
309ea8dc4b6Seschrock 	spa_error_entry_t *se;
310ea8dc4b6Seschrock 	void *cookie;
311ea8dc4b6Seschrock 
312ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
313ea8dc4b6Seschrock 
314ea8dc4b6Seschrock 	cookie = NULL;
315ea8dc4b6Seschrock 	while ((se = avl_destroy_nodes(&spa->spa_errlist_last,
316ea8dc4b6Seschrock 	    &cookie)) != NULL)
317ea8dc4b6Seschrock 		kmem_free(se, sizeof (spa_error_entry_t));
318ea8dc4b6Seschrock 	cookie = NULL;
319ea8dc4b6Seschrock 	while ((se = avl_destroy_nodes(&spa->spa_errlist_scrub,
320ea8dc4b6Seschrock 	    &cookie)) != NULL)
321ea8dc4b6Seschrock 		kmem_free(se, sizeof (spa_error_entry_t));
322ea8dc4b6Seschrock 
323ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
324ea8dc4b6Seschrock }
325ea8dc4b6Seschrock 
326ea8dc4b6Seschrock /*
327ea8dc4b6Seschrock  * Process a list of errors into the current on-disk log.
328ea8dc4b6Seschrock  */
329ea8dc4b6Seschrock static void
330ea8dc4b6Seschrock sync_error_list(spa_t *spa, avl_tree_t *t, uint64_t *obj, dmu_tx_t *tx)
331ea8dc4b6Seschrock {
332ea8dc4b6Seschrock 	spa_error_entry_t *se;
333ea8dc4b6Seschrock 	char buf[64];
334ea8dc4b6Seschrock 	void *cookie;
335ea8dc4b6Seschrock 
336ea8dc4b6Seschrock 	if (avl_numnodes(t) != 0) {
337ea8dc4b6Seschrock 		/* create log if necessary */
338ea8dc4b6Seschrock 		if (*obj == 0)
339ea8dc4b6Seschrock 			*obj = zap_create(spa->spa_meta_objset,
340ea8dc4b6Seschrock 			    DMU_OT_ERROR_LOG, DMU_OT_NONE,
341ea8dc4b6Seschrock 			    0, tx);
342ea8dc4b6Seschrock 
343ea8dc4b6Seschrock 		/* add errors to the current log */
344ea8dc4b6Seschrock 		for (se = avl_first(t); se != NULL; se = AVL_NEXT(t, se)) {
345ea8dc4b6Seschrock 			char *name = se->se_name ? se->se_name : "";
346ea8dc4b6Seschrock 
347ea8dc4b6Seschrock 			bookmark_to_name(&se->se_bookmark, buf, sizeof (buf));
348ea8dc4b6Seschrock 
349ea8dc4b6Seschrock 			(void) zap_update(spa->spa_meta_objset,
350ea8dc4b6Seschrock 			    *obj, buf, 1, strlen(name) + 1, name, tx);
351ea8dc4b6Seschrock 		}
352ea8dc4b6Seschrock 
353ea8dc4b6Seschrock 		/* purge the error list */
354ea8dc4b6Seschrock 		cookie = NULL;
355ea8dc4b6Seschrock 		while ((se = avl_destroy_nodes(t, &cookie)) != NULL)
356ea8dc4b6Seschrock 			kmem_free(se, sizeof (spa_error_entry_t));
357ea8dc4b6Seschrock 	}
358ea8dc4b6Seschrock }
359ea8dc4b6Seschrock 
360ea8dc4b6Seschrock /*
361ea8dc4b6Seschrock  * Sync the error log out to disk.  This is a little tricky because the act of
362ea8dc4b6Seschrock  * writing the error log requires the spa_errlist_lock.  So, we need to lock the
363ea8dc4b6Seschrock  * error lists, take a copy of the lists, and then reinitialize them.  Then, we
364ea8dc4b6Seschrock  * drop the error list lock and take the error log lock, at which point we
365ea8dc4b6Seschrock  * do the errlog processing.  Then, if we encounter an I/O error during this
366ea8dc4b6Seschrock  * process, we can successfully add the error to the list.  Note that this will
367ea8dc4b6Seschrock  * result in the perpetual recycling of errors, but it is an unlikely situation
368ea8dc4b6Seschrock  * and not a performance critical operation.
369ea8dc4b6Seschrock  */
370ea8dc4b6Seschrock void
371ea8dc4b6Seschrock spa_errlog_sync(spa_t *spa, uint64_t txg)
372ea8dc4b6Seschrock {
373ea8dc4b6Seschrock 	dmu_tx_t *tx;
374ea8dc4b6Seschrock 	avl_tree_t scrub, last;
375ea8dc4b6Seschrock 	int scrub_finished;
376ea8dc4b6Seschrock 
377ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
378ea8dc4b6Seschrock 
379ea8dc4b6Seschrock 	/*
380ea8dc4b6Seschrock 	 * Bail out early under normal circumstances.
381ea8dc4b6Seschrock 	 */
382ea8dc4b6Seschrock 	if (avl_numnodes(&spa->spa_errlist_scrub) == 0 &&
383ea8dc4b6Seschrock 	    avl_numnodes(&spa->spa_errlist_last) == 0 &&
384ea8dc4b6Seschrock 	    !spa->spa_scrub_finished) {
385ea8dc4b6Seschrock 		mutex_exit(&spa->spa_errlist_lock);
386ea8dc4b6Seschrock 		return;
387ea8dc4b6Seschrock 	}
388ea8dc4b6Seschrock 
389ea8dc4b6Seschrock 	spa_get_errlists(spa, &last, &scrub);
390ea8dc4b6Seschrock 	scrub_finished = spa->spa_scrub_finished;
391ea8dc4b6Seschrock 	spa->spa_scrub_finished = B_FALSE;
392ea8dc4b6Seschrock 
393ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
394ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlog_lock);
395ea8dc4b6Seschrock 
396ea8dc4b6Seschrock 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
397ea8dc4b6Seschrock 
398ea8dc4b6Seschrock 	/*
399ea8dc4b6Seschrock 	 * Sync out the current list of errors.
400ea8dc4b6Seschrock 	 */
401ea8dc4b6Seschrock 	sync_error_list(spa, &last, &spa->spa_errlog_last, tx);
402ea8dc4b6Seschrock 
403ea8dc4b6Seschrock 	/*
404ea8dc4b6Seschrock 	 * Rotate the log if necessary.
405ea8dc4b6Seschrock 	 */
406ea8dc4b6Seschrock 	if (scrub_finished) {
407ea8dc4b6Seschrock 		if (spa->spa_errlog_last != 0)
408ea8dc4b6Seschrock 			VERIFY(dmu_object_free(spa->spa_meta_objset,
409ea8dc4b6Seschrock 			    spa->spa_errlog_last, tx) == 0);
410ea8dc4b6Seschrock 		spa->spa_errlog_last = spa->spa_errlog_scrub;
411ea8dc4b6Seschrock 		spa->spa_errlog_scrub = 0;
412ea8dc4b6Seschrock 
413ea8dc4b6Seschrock 		sync_error_list(spa, &scrub, &spa->spa_errlog_last, tx);
414ea8dc4b6Seschrock 	}
415ea8dc4b6Seschrock 
416ea8dc4b6Seschrock 	/*
417ea8dc4b6Seschrock 	 * Sync out any pending scrub errors.
418ea8dc4b6Seschrock 	 */
419ea8dc4b6Seschrock 	sync_error_list(spa, &scrub, &spa->spa_errlog_scrub, tx);
420ea8dc4b6Seschrock 
421ea8dc4b6Seschrock 	/*
422ea8dc4b6Seschrock 	 * Update the MOS to reflect the new values.
423ea8dc4b6Seschrock 	 */
424ea8dc4b6Seschrock 	(void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
425ea8dc4b6Seschrock 	    DMU_POOL_ERRLOG_LAST, sizeof (uint64_t), 1,
426ea8dc4b6Seschrock 	    &spa->spa_errlog_last, tx);
427ea8dc4b6Seschrock 	(void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
428ea8dc4b6Seschrock 	    DMU_POOL_ERRLOG_SCRUB, sizeof (uint64_t), 1,
429ea8dc4b6Seschrock 	    &spa->spa_errlog_scrub, tx);
430ea8dc4b6Seschrock 
431ea8dc4b6Seschrock 	dmu_tx_commit(tx);
432ea8dc4b6Seschrock 
433ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlog_lock);
434ea8dc4b6Seschrock }
435