1ea8dc4beschrock/*
2ea8dc4beschrock * CDDL HEADER START
3ea8dc4beschrock *
4ea8dc4beschrock * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7ea8dc4beschrock *
8ea8dc4beschrock * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ea8dc4beschrock * or http://www.opensolaris.org/os/licensing.
10ea8dc4beschrock * See the License for the specific language governing permissions
11ea8dc4beschrock * and limitations under the License.
12ea8dc4beschrock *
13ea8dc4beschrock * When distributing Covered Code, include this CDDL HEADER in each
14ea8dc4beschrock * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ea8dc4beschrock * If applicable, add the following below this CDDL HEADER, with the
16ea8dc4beschrock * fields enclosed by brackets "[]" replaced with your own identifying
17ea8dc4beschrock * information: Portions Copyright [yyyy] [name of copyright owner]
18ea8dc4beschrock *
19ea8dc4beschrock * CDDL HEADER END
20ea8dc4beschrock */
21ea8dc4beschrock/*
223f9d6adLin Ling * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
237802d7bMatthew Ahrens * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
24ea8dc4beschrock */
25ea8dc4beschrock
26ea8dc4beschrock/*
27ea8dc4beschrock * Routines to manage the on-disk persistent error log.
28ea8dc4beschrock *
29ea8dc4beschrock * Each pool stores a log of all logical data errors seen during normal
30ea8dc4beschrock * operation.  This is actually the union of two distinct logs: the last log,
31ea8dc4beschrock * and the current log.  All errors seen are logged to the current log.  When a
32ea8dc4beschrock * scrub completes, the current log becomes the last log, the last log is thrown
33ea8dc4beschrock * out, and the current log is reinitialized.  This way, if an error is somehow
34ea8dc4beschrock * corrected, a new scrub will show that that it no longer exists, and will be
35ea8dc4beschrock * deleted from the log when the scrub completes.
36ea8dc4beschrock *
37ea8dc4beschrock * The log is stored using a ZAP object whose key is a string form of the
387802d7bMatthew Ahrens * zbookmark_phys tuple (objset, object, level, blkid), and whose contents is an
39ea8dc4beschrock * optional 'objset:object' human-readable string describing the data.  When an
40ea8dc4beschrock * error is first logged, this string will be empty, indicating that no name is
41ea8dc4beschrock * known.  This prevents us from having to issue a potentially large amount of
42ea8dc4beschrock * I/O to discover the object name during an error path.  Instead, we do the
43ea8dc4beschrock * calculation when the data is requested, storing the result so future queries
44ea8dc4beschrock * will be faster.
45ea8dc4beschrock *
46ea8dc4beschrock * This log is then shipped into an nvlist where the key is the dataset name and
47ea8dc4beschrock * the value is the object name.  Userland is then responsible for uniquifying
48ea8dc4beschrock * this list and displaying it to the user.
49ea8dc4beschrock */
50ea8dc4beschrock
51ea8dc4beschrock#include <sys/dmu_tx.h>
52ea8dc4beschrock#include <sys/spa.h>
53ea8dc4beschrock#include <sys/spa_impl.h>
54ea8dc4beschrock#include <sys/zap.h>
55ea8dc4beschrock#include <sys/zio.h>
56ea8dc4beschrock
57ea8dc4beschrock
58ea8dc4beschrock/*
59ea8dc4beschrock * Convert a bookmark to a string.
60ea8dc4beschrock */
61ea8dc4beschrockstatic void
627802d7bMatthew Ahrensbookmark_to_name(zbookmark_phys_t *zb, char *buf, size_t len)
63ea8dc4beschrock{
64ea8dc4beschrock	(void) snprintf(buf, len, "%llx:%llx:%llx:%llx",
65ea8dc4beschrock	    (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
66ea8dc4beschrock	    (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid);
67ea8dc4beschrock}
68ea8dc4beschrock
69ea8dc4beschrock/*
70ea8dc4beschrock * Convert a string to a bookmark
71ea8dc4beschrock */
725ad8204nd#ifdef _KERNEL
73ea8dc4beschrockstatic void
747802d7bMatthew Ahrensname_to_bookmark(char *buf, zbookmark_phys_t *zb)
75ea8dc4beschrock{
764585130Yuri Pankov	zb->zb_objset = zfs_strtonum(buf, &buf);
77ea8dc4beschrock	ASSERT(*buf == ':');
784585130Yuri Pankov	zb->zb_object = zfs_strtonum(buf + 1, &buf);
79ea8dc4beschrock	ASSERT(*buf == ':');
804585130Yuri Pankov	zb->zb_level = (int)zfs_strtonum(buf + 1, &buf);
81ea8dc4beschrock	ASSERT(*buf == ':');
824585130Yuri Pankov	zb->zb_blkid = zfs_strtonum(buf + 1, &buf);
83ea8dc4beschrock	ASSERT(*buf == '\0');
84ea8dc4beschrock}
855ad8204nd#endif
86ea8dc4beschrock
87ea8dc4beschrock/*
88ea8dc4beschrock * Log an uncorrectable error to the persistent error log.  We add it to the
89ea8dc4beschrock * spa's list of pending errors.  The changes are actually synced out to disk
90ea8dc4beschrock * during spa_errlog_sync().
91ea8dc4beschrock */
92ea8dc4beschrockvoid
93eb63303Tom Caputispa_log_error(spa_t *spa, const zbookmark_phys_t *zb)
94ea8dc4beschrock{
95ea8dc4beschrock	spa_error_entry_t search;
96ea8dc4beschrock	spa_error_entry_t *new;
97ea8dc4beschrock	avl_tree_t *tree;
98ea8dc4beschrock	avl_index_t where;
99ea8dc4beschrock
100ea8dc4beschrock	/*
101ea8dc4beschrock	 * If we are trying to import a pool, ignore any errors, as we won't be
102ea8dc4beschrock	 * writing to the pool any time soon.
103ea8dc4beschrock	 */
104b16da2eGeorge Wilson	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
105ea8dc4beschrock		return;
106ea8dc4beschrock
107ea8dc4beschrock	mutex_enter(&spa->spa_errlist_lock);
108ea8dc4beschrock
109ea8dc4beschrock	/*
110ea8dc4beschrock	 * If we have had a request to rotate the log, log it to the next list
111ea8dc4beschrock	 * instead of the current one.
112ea8dc4beschrock	 */
113ea8dc4beschrock	if (spa->spa_scrub_active || spa->spa_scrub_finished)
114ea8dc4beschrock		tree = &spa->spa_errlist_scrub;
115ea8dc4beschrock	else
116ea8dc4beschrock		tree = &spa->spa_errlist_last;
117ea8dc4beschrock
118ea8dc4beschrock	search.se_bookmark = *zb;
119ea8dc4beschrock	if (avl_find(tree, &search, &where) != NULL) {
120ea8dc4beschrock		mutex_exit(&spa->spa_errlist_lock);
121ea8dc4beschrock		return;
122ea8dc4beschrock	}
123ea8dc4beschrock
124ea8dc4beschrock	new = kmem_zalloc(sizeof (spa_error_entry_t), KM_SLEEP);
125ea8dc4beschrock	new->se_bookmark = *zb;
126ea8dc4beschrock	avl_insert(tree, new, where);
127ea8dc4beschrock
128ea8dc4beschrock	mutex_exit(&spa->spa_errlist_lock);
129ea8dc4beschrock}
130ea8dc4beschrock
131ea8dc4beschrock/*
132ea8dc4beschrock * Return the number of errors currently in the error log.  This is actually the
133ea8dc4beschrock * sum of both the last log and the current log, since we don't know the union
134ea8dc4beschrock * of these logs until we reach userland.
135ea8dc4beschrock */
136ea8dc4beschrockuint64_t
137ea8dc4beschrockspa_get_errlog_size(spa_t *spa)
138ea8dc4beschrock{
139ea8dc4beschrock	uint64_t total = 0, count;
140ea8dc4beschrock
141ea8dc4beschrock	mutex_enter(&spa->spa_errlog_lock);
142ea8dc4beschrock	if (spa->spa_errlog_scrub != 0 &&
143ea8dc4beschrock	    zap_count(spa->spa_meta_objset, spa->spa_errlog_scrub,
144ea8dc4beschrock	    &count) == 0)
145ea8dc4beschrock		total += count;
146ea8dc4beschrock
147ea8dc4beschrock	if (spa->spa_errlog_last != 0 && !spa->spa_scrub_finished &&
148ea8dc4beschrock	    zap_count(spa->spa_meta_objset, spa->spa_errlog_last,
149ea8dc4beschrock	    &count) == 0)
150ea8dc4beschrock		total += count;
151ea8dc4beschrock	mutex_exit(&spa->spa_errlog_lock);
152ea8dc4beschrock
153ea8dc4beschrock	mutex_enter(&spa->spa_errlist_lock);
154ea8dc4beschrock	total += avl_numnodes(&spa->spa_errlist_last);
155ea8dc4beschrock	total += avl_numnodes(&spa->spa_errlist_scrub);
156ea8dc4beschrock	mutex_exit(&spa->spa_errlist_lock);
157ea8dc4beschrock
158ea8dc4beschrock	return (total);
159ea8dc4beschrock}
160ea8dc4beschrock
161ea8dc4beschrock#ifdef _KERNEL
162ea8dc4beschrockstatic int
163ea8dc4beschrockprocess_error_log(spa_t *spa, uint64_t obj, void *addr, size_t *count)
164ea8dc4beschrock{
165ea8dc4beschrock	zap_cursor_t zc;
166ea8dc4beschrock	zap_attribute_t za;
1677802d7bMatthew Ahrens	zbookmark_phys_t zb;
168ea8dc4beschrock
169ea8dc4beschrock	if (obj == 0)
170ea8dc4beschrock		return (0);
171ea8dc4beschrock
172ea8dc4beschrock	for (zap_cursor_init(&zc, spa->spa_meta_objset, obj);
173ea8dc4beschrock	    zap_cursor_retrieve(&zc, &za) == 0;
174ea8dc4beschrock	    zap_cursor_advance(&zc)) {
175ea8dc4beschrock
176ea8dc4beschrock		if (*count == 0) {
177ea8dc4beschrock			zap_cursor_fini(&zc);
178be6fd75Matthew Ahrens			return (SET_ERROR(ENOMEM));
179ea8dc4beschrock		}
180ea8dc4beschrock
181ea8dc4beschrock		name_to_bookmark(za.za_name, &zb);
182ea8dc4beschrock
183ea8dc4beschrock		if (copyout(&zb, (char *)addr +
1847802d7bMatthew Ahrens		    (*count - 1) * sizeof (zbookmark_phys_t),
1857802d7bMatthew Ahrens		    sizeof (zbookmark_phys_t)) != 0) {
186b287be1Will Andrews			zap_cursor_fini(&zc);
187be6fd75Matthew Ahrens			return (SET_ERROR(EFAULT));
188b287be1Will Andrews		}
189ea8dc4beschrock
190ea8dc4beschrock		*count -= 1;
191ea8dc4beschrock	}
192ea8dc4beschrock
193ea8dc4beschrock	zap_cursor_fini(&zc);
194ea8dc4beschrock
195ea8dc4beschrock	return (0);
196