xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_errlog.c (revision 3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5)
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 /*
22*3f9d6ad7SLin Ling  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
23ea8dc4b6Seschrock  */
24ea8dc4b6Seschrock 
25ea8dc4b6Seschrock /*
26ea8dc4b6Seschrock  * Routines to manage the on-disk persistent error log.
27ea8dc4b6Seschrock  *
28ea8dc4b6Seschrock  * Each pool stores a log of all logical data errors seen during normal
29ea8dc4b6Seschrock  * operation.  This is actually the union of two distinct logs: the last log,
30ea8dc4b6Seschrock  * and the current log.  All errors seen are logged to the current log.  When a
31ea8dc4b6Seschrock  * scrub completes, the current log becomes the last log, the last log is thrown
32ea8dc4b6Seschrock  * out, and the current log is reinitialized.  This way, if an error is somehow
33ea8dc4b6Seschrock  * corrected, a new scrub will show that that it no longer exists, and will be
34ea8dc4b6Seschrock  * deleted from the log when the scrub completes.
35ea8dc4b6Seschrock  *
36ea8dc4b6Seschrock  * The log is stored using a ZAP object whose key is a string form of the
37ea8dc4b6Seschrock  * zbookmark tuple (objset, object, level, blkid), and whose contents is an
38ea8dc4b6Seschrock  * optional 'objset:object' human-readable string describing the data.  When an
39ea8dc4b6Seschrock  * error is first logged, this string will be empty, indicating that no name is
40ea8dc4b6Seschrock  * known.  This prevents us from having to issue a potentially large amount of
41ea8dc4b6Seschrock  * I/O to discover the object name during an error path.  Instead, we do the
42ea8dc4b6Seschrock  * calculation when the data is requested, storing the result so future queries
43ea8dc4b6Seschrock  * will be faster.
44ea8dc4b6Seschrock  *
45ea8dc4b6Seschrock  * This log is then shipped into an nvlist where the key is the dataset name and
46ea8dc4b6Seschrock  * the value is the object name.  Userland is then responsible for uniquifying
47ea8dc4b6Seschrock  * this list and displaying it to the user.
48ea8dc4b6Seschrock  */
49ea8dc4b6Seschrock 
50ea8dc4b6Seschrock #include <sys/dmu_tx.h>
51ea8dc4b6Seschrock #include <sys/spa.h>
52ea8dc4b6Seschrock #include <sys/spa_impl.h>
53ea8dc4b6Seschrock #include <sys/zap.h>
54ea8dc4b6Seschrock #include <sys/zio.h>
55ea8dc4b6Seschrock 
56ea8dc4b6Seschrock 
57ea8dc4b6Seschrock /*
58ea8dc4b6Seschrock  * Convert a bookmark to a string.
59ea8dc4b6Seschrock  */
60ea8dc4b6Seschrock static void
61ea8dc4b6Seschrock bookmark_to_name(zbookmark_t *zb, char *buf, size_t len)
62ea8dc4b6Seschrock {
63ea8dc4b6Seschrock 	(void) snprintf(buf, len, "%llx:%llx:%llx:%llx",
64ea8dc4b6Seschrock 	    (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
65ea8dc4b6Seschrock 	    (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid);
66ea8dc4b6Seschrock }
67ea8dc4b6Seschrock 
68ea8dc4b6Seschrock /*
69ea8dc4b6Seschrock  * Convert a string to a bookmark
70ea8dc4b6Seschrock  */
715ad82045Snd #ifdef _KERNEL
72ea8dc4b6Seschrock static void
73ea8dc4b6Seschrock name_to_bookmark(char *buf, zbookmark_t *zb)
74ea8dc4b6Seschrock {
75ea8dc4b6Seschrock 	zb->zb_objset = strtonum(buf, &buf);
76ea8dc4b6Seschrock 	ASSERT(*buf == ':');
77ea8dc4b6Seschrock 	zb->zb_object = strtonum(buf + 1, &buf);
78ea8dc4b6Seschrock 	ASSERT(*buf == ':');
79ea8dc4b6Seschrock 	zb->zb_level = (int)strtonum(buf + 1, &buf);
80ea8dc4b6Seschrock 	ASSERT(*buf == ':');
81ea8dc4b6Seschrock 	zb->zb_blkid = strtonum(buf + 1, &buf);
82ea8dc4b6Seschrock 	ASSERT(*buf == '\0');
83ea8dc4b6Seschrock }
845ad82045Snd #endif
85ea8dc4b6Seschrock 
86ea8dc4b6Seschrock /*
87ea8dc4b6Seschrock  * Log an uncorrectable error to the persistent error log.  We add it to the
88ea8dc4b6Seschrock  * spa's list of pending errors.  The changes are actually synced out to disk
89ea8dc4b6Seschrock  * during spa_errlog_sync().
90ea8dc4b6Seschrock  */
91ea8dc4b6Seschrock void
92ea8dc4b6Seschrock spa_log_error(spa_t *spa, zio_t *zio)
93ea8dc4b6Seschrock {
94ea8dc4b6Seschrock 	zbookmark_t *zb = &zio->io_logical->io_bookmark;
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;
167ea8dc4b6Seschrock 	zbookmark_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);
178ea8dc4b6Seschrock 			return (ENOMEM);
179ea8dc4b6Seschrock 		}
180ea8dc4b6Seschrock 
181ea8dc4b6Seschrock 		name_to_bookmark(za.za_name, &zb);
182ea8dc4b6Seschrock 
183ea8dc4b6Seschrock 		if (copyout(&zb, (char *)addr +
184ea8dc4b6Seschrock 		    (*count - 1) * sizeof (zbookmark_t),
185ea8dc4b6Seschrock 		    sizeof (zbookmark_t)) != 0)
186ea8dc4b6Seschrock 			return (EFAULT);
187ea8dc4b6Seschrock 
188ea8dc4b6Seschrock 		*count -= 1;
189ea8dc4b6Seschrock 	}
190ea8dc4b6Seschrock 
191ea8dc4b6Seschrock 	zap_cursor_fini(&zc);
192ea8dc4b6Seschrock 
193ea8dc4b6Seschrock 	return (0);
194ea8dc4b6Seschrock }
195ea8dc4b6Seschrock 
196ea8dc4b6Seschrock static int
197ea8dc4b6Seschrock process_error_list(avl_tree_t *list, void *addr, size_t *count)
198ea8dc4b6Seschrock {
199ea8dc4b6Seschrock 	spa_error_entry_t *se;
200ea8dc4b6Seschrock 
201ea8dc4b6Seschrock 	for (se = avl_first(list); se != NULL; se = AVL_NEXT(list, se)) {
202ea8dc4b6Seschrock 
203ea8dc4b6Seschrock 		if (*count == 0)
204ea8dc4b6Seschrock 			return (ENOMEM);
205ea8dc4b6Seschrock 
206ea8dc4b6Seschrock 		if (copyout(&se->se_bookmark, (char *)addr +
207ea8dc4b6Seschrock 		    (*count - 1) * sizeof (zbookmark_t),
208ea8dc4b6Seschrock 		    sizeof (zbookmark_t)) != 0)
209ea8dc4b6Seschrock 			return (EFAULT);
210ea8dc4b6Seschrock 
211ea8dc4b6Seschrock 		*count -= 1;
212ea8dc4b6Seschrock 	}
213ea8dc4b6Seschrock 
214ea8dc4b6Seschrock 	return (0);
215ea8dc4b6Seschrock }
216ea8dc4b6Seschrock #endif
217ea8dc4b6Seschrock 
218ea8dc4b6Seschrock /*
219ea8dc4b6Seschrock  * Copy all known errors to userland as an array of bookmarks.  This is
220ea8dc4b6Seschrock  * actually a union of the on-disk last log and current log, as well as any
221ea8dc4b6Seschrock  * pending error requests.
222ea8dc4b6Seschrock  *
223ea8dc4b6Seschrock  * Because the act of reading the on-disk log could cause errors to be
224ea8dc4b6Seschrock  * generated, we have two separate locks: one for the error log and one for the
225ea8dc4b6Seschrock  * in-core error lists.  We only need the error list lock to log and error, so
226ea8dc4b6Seschrock  * we grab the error log lock while we read the on-disk logs, and only pick up
227ea8dc4b6Seschrock  * the error list lock when we are finished.
228ea8dc4b6Seschrock  */
229ea8dc4b6Seschrock int
230ea8dc4b6Seschrock spa_get_errlog(spa_t *spa, void *uaddr, size_t *count)
231ea8dc4b6Seschrock {
232ea8dc4b6Seschrock 	int ret = 0;
233ea8dc4b6Seschrock 
234ea8dc4b6Seschrock #ifdef _KERNEL
235ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlog_lock);
236ea8dc4b6Seschrock 
237ea8dc4b6Seschrock 	ret = process_error_log(spa, spa->spa_errlog_scrub, uaddr, count);
238ea8dc4b6Seschrock 
239ea8dc4b6Seschrock 	if (!ret && !spa->spa_scrub_finished)
240ea8dc4b6Seschrock 		ret = process_error_log(spa, spa->spa_errlog_last, uaddr,
241ea8dc4b6Seschrock 		    count);
242ea8dc4b6Seschrock 
243ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
244ea8dc4b6Seschrock 	if (!ret)
245ea8dc4b6Seschrock 		ret = process_error_list(&spa->spa_errlist_scrub, uaddr,
246ea8dc4b6Seschrock 		    count);
247ea8dc4b6Seschrock 	if (!ret)
248ea8dc4b6Seschrock 		ret = process_error_list(&spa->spa_errlist_last, uaddr,
249ea8dc4b6Seschrock 		    count);
250ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
251ea8dc4b6Seschrock 
252ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlog_lock);
253ea8dc4b6Seschrock #endif
254ea8dc4b6Seschrock 
255ea8dc4b6Seschrock 	return (ret);
256ea8dc4b6Seschrock }
257ea8dc4b6Seschrock 
258ea8dc4b6Seschrock /*
259ea8dc4b6Seschrock  * Called when a scrub completes.  This simply set a bit which tells which AVL
260ea8dc4b6Seschrock  * tree to add new errors.  spa_errlog_sync() is responsible for actually
261ea8dc4b6Seschrock  * syncing the changes to the underlying objects.
262ea8dc4b6Seschrock  */
263ea8dc4b6Seschrock void
264ea8dc4b6Seschrock spa_errlog_rotate(spa_t *spa)
265ea8dc4b6Seschrock {
266ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
267ea8dc4b6Seschrock 	spa->spa_scrub_finished = B_TRUE;
268ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
269ea8dc4b6Seschrock }
270ea8dc4b6Seschrock 
271ea8dc4b6Seschrock /*
272ea8dc4b6Seschrock  * Discard any pending errors from the spa_t.  Called when unloading a faulted
273ea8dc4b6Seschrock  * pool, as the errors encountered during the open cannot be synced to disk.
274ea8dc4b6Seschrock  */
275ea8dc4b6Seschrock void
276ea8dc4b6Seschrock spa_errlog_drain(spa_t *spa)
277ea8dc4b6Seschrock {
278ea8dc4b6Seschrock 	spa_error_entry_t *se;
279ea8dc4b6Seschrock 	void *cookie;
280ea8dc4b6Seschrock 
281ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
282ea8dc4b6Seschrock 
283ea8dc4b6Seschrock 	cookie = NULL;
284ea8dc4b6Seschrock 	while ((se = avl_destroy_nodes(&spa->spa_errlist_last,
285ea8dc4b6Seschrock 	    &cookie)) != NULL)
286ea8dc4b6Seschrock 		kmem_free(se, sizeof (spa_error_entry_t));
287ea8dc4b6Seschrock 	cookie = NULL;
288ea8dc4b6Seschrock 	while ((se = avl_destroy_nodes(&spa->spa_errlist_scrub,
289ea8dc4b6Seschrock 	    &cookie)) != NULL)
290ea8dc4b6Seschrock 		kmem_free(se, sizeof (spa_error_entry_t));
291ea8dc4b6Seschrock 
292ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
293ea8dc4b6Seschrock }
294ea8dc4b6Seschrock 
295ea8dc4b6Seschrock /*
296ea8dc4b6Seschrock  * Process a list of errors into the current on-disk log.
297ea8dc4b6Seschrock  */
298ea8dc4b6Seschrock static void
299ea8dc4b6Seschrock sync_error_list(spa_t *spa, avl_tree_t *t, uint64_t *obj, dmu_tx_t *tx)
300ea8dc4b6Seschrock {
301ea8dc4b6Seschrock 	spa_error_entry_t *se;
302ea8dc4b6Seschrock 	char buf[64];
303ea8dc4b6Seschrock 	void *cookie;
304ea8dc4b6Seschrock 
305ea8dc4b6Seschrock 	if (avl_numnodes(t) != 0) {
306ea8dc4b6Seschrock 		/* create log if necessary */
307ea8dc4b6Seschrock 		if (*obj == 0)
308ea8dc4b6Seschrock 			*obj = zap_create(spa->spa_meta_objset,
309ea8dc4b6Seschrock 			    DMU_OT_ERROR_LOG, DMU_OT_NONE,
310ea8dc4b6Seschrock 			    0, tx);
311ea8dc4b6Seschrock 
312ea8dc4b6Seschrock 		/* add errors to the current log */
313ea8dc4b6Seschrock 		for (se = avl_first(t); se != NULL; se = AVL_NEXT(t, se)) {
314ea8dc4b6Seschrock 			char *name = se->se_name ? se->se_name : "";
315ea8dc4b6Seschrock 
316ea8dc4b6Seschrock 			bookmark_to_name(&se->se_bookmark, buf, sizeof (buf));
317ea8dc4b6Seschrock 
318ea8dc4b6Seschrock 			(void) zap_update(spa->spa_meta_objset,
319ea8dc4b6Seschrock 			    *obj, buf, 1, strlen(name) + 1, name, tx);
320ea8dc4b6Seschrock 		}
321ea8dc4b6Seschrock 
322ea8dc4b6Seschrock 		/* purge the error list */
323ea8dc4b6Seschrock 		cookie = NULL;
324ea8dc4b6Seschrock 		while ((se = avl_destroy_nodes(t, &cookie)) != NULL)
325ea8dc4b6Seschrock 			kmem_free(se, sizeof (spa_error_entry_t));
326ea8dc4b6Seschrock 	}
327ea8dc4b6Seschrock }
328ea8dc4b6Seschrock 
329ea8dc4b6Seschrock /*
330ea8dc4b6Seschrock  * Sync the error log out to disk.  This is a little tricky because the act of
331ea8dc4b6Seschrock  * writing the error log requires the spa_errlist_lock.  So, we need to lock the
332ea8dc4b6Seschrock  * error lists, take a copy of the lists, and then reinitialize them.  Then, we
333ea8dc4b6Seschrock  * drop the error list lock and take the error log lock, at which point we
334ea8dc4b6Seschrock  * do the errlog processing.  Then, if we encounter an I/O error during this
335ea8dc4b6Seschrock  * process, we can successfully add the error to the list.  Note that this will
336ea8dc4b6Seschrock  * result in the perpetual recycling of errors, but it is an unlikely situation
337ea8dc4b6Seschrock  * and not a performance critical operation.
338ea8dc4b6Seschrock  */
339ea8dc4b6Seschrock void
340ea8dc4b6Seschrock spa_errlog_sync(spa_t *spa, uint64_t txg)
341ea8dc4b6Seschrock {
342ea8dc4b6Seschrock 	dmu_tx_t *tx;
343ea8dc4b6Seschrock 	avl_tree_t scrub, last;
344ea8dc4b6Seschrock 	int scrub_finished;
345ea8dc4b6Seschrock 
346ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
347ea8dc4b6Seschrock 
348ea8dc4b6Seschrock 	/*
349ea8dc4b6Seschrock 	 * Bail out early under normal circumstances.
350ea8dc4b6Seschrock 	 */
351ea8dc4b6Seschrock 	if (avl_numnodes(&spa->spa_errlist_scrub) == 0 &&
352ea8dc4b6Seschrock 	    avl_numnodes(&spa->spa_errlist_last) == 0 &&
353ea8dc4b6Seschrock 	    !spa->spa_scrub_finished) {
354ea8dc4b6Seschrock 		mutex_exit(&spa->spa_errlist_lock);
355ea8dc4b6Seschrock 		return;
356ea8dc4b6Seschrock 	}
357ea8dc4b6Seschrock 
358ea8dc4b6Seschrock 	spa_get_errlists(spa, &last, &scrub);
359ea8dc4b6Seschrock 	scrub_finished = spa->spa_scrub_finished;
360ea8dc4b6Seschrock 	spa->spa_scrub_finished = B_FALSE;
361ea8dc4b6Seschrock 
362ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
363ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlog_lock);
364ea8dc4b6Seschrock 
365ea8dc4b6Seschrock 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
366ea8dc4b6Seschrock 
367ea8dc4b6Seschrock 	/*
368ea8dc4b6Seschrock 	 * Sync out the current list of errors.
369ea8dc4b6Seschrock 	 */
370ea8dc4b6Seschrock 	sync_error_list(spa, &last, &spa->spa_errlog_last, tx);
371ea8dc4b6Seschrock 
372ea8dc4b6Seschrock 	/*
373ea8dc4b6Seschrock 	 * Rotate the log if necessary.
374ea8dc4b6Seschrock 	 */
375ea8dc4b6Seschrock 	if (scrub_finished) {
376ea8dc4b6Seschrock 		if (spa->spa_errlog_last != 0)
377ea8dc4b6Seschrock 			VERIFY(dmu_object_free(spa->spa_meta_objset,
378ea8dc4b6Seschrock 			    spa->spa_errlog_last, tx) == 0);
379ea8dc4b6Seschrock 		spa->spa_errlog_last = spa->spa_errlog_scrub;
380ea8dc4b6Seschrock 		spa->spa_errlog_scrub = 0;
381ea8dc4b6Seschrock 
382ea8dc4b6Seschrock 		sync_error_list(spa, &scrub, &spa->spa_errlog_last, tx);
383ea8dc4b6Seschrock 	}
384ea8dc4b6Seschrock 
385ea8dc4b6Seschrock 	/*
386ea8dc4b6Seschrock 	 * Sync out any pending scrub errors.
387ea8dc4b6Seschrock 	 */
388ea8dc4b6Seschrock 	sync_error_list(spa, &scrub, &spa->spa_errlog_scrub, tx);
389ea8dc4b6Seschrock 
390ea8dc4b6Seschrock 	/*
391ea8dc4b6Seschrock 	 * Update the MOS to reflect the new values.
392ea8dc4b6Seschrock 	 */
393ea8dc4b6Seschrock 	(void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
394ea8dc4b6Seschrock 	    DMU_POOL_ERRLOG_LAST, sizeof (uint64_t), 1,
395ea8dc4b6Seschrock 	    &spa->spa_errlog_last, tx);
396ea8dc4b6Seschrock 	(void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
397ea8dc4b6Seschrock 	    DMU_POOL_ERRLOG_SCRUB, sizeof (uint64_t), 1,
398ea8dc4b6Seschrock 	    &spa->spa_errlog_scrub, tx);
399ea8dc4b6Seschrock 
400ea8dc4b6Seschrock 	dmu_tx_commit(tx);
401ea8dc4b6Seschrock 
402ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlog_lock);
403ea8dc4b6Seschrock }
404