spa_errlog.c revision ea8dc4b6d2251b437950c0056bc626b311c73c27
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/*
22ea8dc4beschrock * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23ea8dc4beschrock * Use is subject to license terms.
24ea8dc4beschrock */
25ea8dc4beschrock
26ea8dc4beschrock#pragma ident	"%Z%%M%	%I%	%E% SMI"
27ea8dc4beschrock
28ea8dc4beschrock/*
29ea8dc4beschrock * Routines to manage the on-disk persistent error log.
30ea8dc4beschrock *
31ea8dc4beschrock * Each pool stores a log of all logical data errors seen during normal
32ea8dc4beschrock * operation.  This is actually the union of two distinct logs: the last log,
33ea8dc4beschrock * and the current log.  All errors seen are logged to the current log.  When a
34ea8dc4beschrock * scrub completes, the current log becomes the last log, the last log is thrown
35ea8dc4beschrock * out, and the current log is reinitialized.  This way, if an error is somehow
36ea8dc4beschrock * corrected, a new scrub will show that that it no longer exists, and will be
37ea8dc4beschrock * deleted from the log when the scrub completes.
38ea8dc4beschrock *
39ea8dc4beschrock * The log is stored using a ZAP object whose key is a string form of the
40ea8dc4beschrock * zbookmark tuple (objset, object, level, blkid), and whose contents is an
41ea8dc4beschrock * optional 'objset:object' human-readable string describing the data.  When an
42ea8dc4beschrock * error is first logged, this string will be empty, indicating that no name is
43ea8dc4beschrock * known.  This prevents us from having to issue a potentially large amount of
44ea8dc4beschrock * I/O to discover the object name during an error path.  Instead, we do the
45ea8dc4beschrock * calculation when the data is requested, storing the result so future queries
46ea8dc4beschrock * will be faster.
47ea8dc4beschrock *
48ea8dc4beschrock * This log is then shipped into an nvlist where the key is the dataset name and
49ea8dc4beschrock * the value is the object name.  Userland is then responsible for uniquifying
50ea8dc4beschrock * this list and displaying it to the user.
51ea8dc4beschrock */
52ea8dc4beschrock
53ea8dc4beschrock#include <sys/dmu_tx.h>
54ea8dc4beschrock#include <sys/spa.h>
55ea8dc4beschrock#include <sys/spa_impl.h>
56ea8dc4beschrock#include <sys/zap.h>
57ea8dc4beschrock#include <sys/zio.h>
58ea8dc4beschrock
59ea8dc4beschrock/*
60ea8dc4beschrock * This is a stripped-down version of strtoull, suitable only for converting
61ea8dc4beschrock * lowercase hexidecimal numbers that don't overflow.
62ea8dc4beschrock */
63ea8dc4beschrockstatic uint64_t
64ea8dc4beschrockstrtonum(char *str, char **nptr)
65ea8dc4beschrock{
66ea8dc4beschrock	uint64_t val = 0;
67ea8dc4beschrock	char c;
68ea8dc4beschrock	int digit;
69ea8dc4beschrock
70ea8dc4beschrock	while ((c = *str) != '\0') {
71ea8dc4beschrock		if (c >= '0' && c <= '9')
72ea8dc4beschrock			digit = c - '0';
73ea8dc4beschrock		else if (c >= 'a' && c <= 'f')
74ea8dc4beschrock			digit = 10 + c - 'a';
75ea8dc4beschrock		else
76ea8dc4beschrock			break;
77ea8dc4beschrock
78ea8dc4beschrock		val *= 16;
79ea8dc4beschrock		val += digit;
80ea8dc4beschrock
81ea8dc4beschrock		str++;
82ea8dc4beschrock	}
83ea8dc4beschrock
84ea8dc4beschrock	*nptr = str;
85ea8dc4beschrock
86ea8dc4beschrock	return (val);
87ea8dc4beschrock}
88ea8dc4beschrock
89ea8dc4beschrock/*
90ea8dc4beschrock * Convert a bookmark to a string.
91ea8dc4beschrock */
92ea8dc4beschrockstatic void
93ea8dc4beschrockbookmark_to_name(zbookmark_t *zb, char *buf, size_t len)
94ea8dc4beschrock{
95ea8dc4beschrock	(void) snprintf(buf, len, "%llx:%llx:%llx:%llx",
96ea8dc4beschrock	    (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object,
97ea8dc4beschrock	    (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid);
98ea8dc4beschrock}
99ea8dc4beschrock
100ea8dc4beschrock/*
101ea8dc4beschrock * Convert a string to a bookmark
102ea8dc4beschrock */
103ea8dc4beschrockstatic void
104ea8dc4beschrockname_to_bookmark(char *buf, zbookmark_t *zb)
105ea8dc4beschrock{
106ea8dc4beschrock	zb->zb_objset = strtonum(buf, &buf);
107ea8dc4beschrock	ASSERT(*buf == ':');
108ea8dc4beschrock	zb->zb_object = strtonum(buf + 1, &buf);
109ea8dc4beschrock	ASSERT(*buf == ':');
110ea8dc4beschrock	zb->zb_level = (int)strtonum(buf + 1, &buf);
111ea8dc4beschrock	ASSERT(*buf == ':');
112ea8dc4beschrock	zb->zb_blkid = strtonum(buf + 1, &buf);
113ea8dc4beschrock	ASSERT(*buf == '\0');
114ea8dc4beschrock}
115ea8dc4beschrock
116ea8dc4beschrock/*
117ea8dc4beschrock * Log an uncorrectable error to the persistent error log.  We add it to the
118ea8dc4beschrock * spa's list of pending errors.  The changes are actually synced out to disk
119ea8dc4beschrock * during spa_errlog_sync().
120ea8dc4beschrock */
121ea8dc4beschrockvoid
122ea8dc4beschrockspa_log_error(spa_t *spa, zio_t *zio)
123ea8dc4beschrock{
124ea8dc4beschrock	zbookmark_t *zb = &zio->io_logical->io_bookmark;
125ea8dc4beschrock	spa_error_entry_t search;
126ea8dc4beschrock	spa_error_entry_t *new;
127ea8dc4beschrock	avl_tree_t *tree;
128ea8dc4beschrock	avl_index_t where;
129ea8dc4beschrock
130ea8dc4beschrock	/*
131ea8dc4beschrock	 * If we are trying to import a pool, ignore any errors, as we won't be
132ea8dc4beschrock	 * writing to the pool any time soon.
133ea8dc4beschrock	 */
134ea8dc4beschrock	if (spa->spa_load_state == SPA_LOAD_TRYIMPORT)
135ea8dc4beschrock		return;
136ea8dc4beschrock
137ea8dc4beschrock	mutex_enter(&spa->spa_errlist_lock);
138ea8dc4beschrock
139ea8dc4beschrock	/*
140ea8dc4beschrock	 * If we have had a request to rotate the log, log it to the next list
141ea8dc4beschrock	 * instead of the current one.
142ea8dc4beschrock	 */
143ea8dc4beschrock	if (spa->spa_scrub_active || spa->spa_scrub_finished)
144ea8dc4beschrock		tree = &spa->spa_errlist_scrub;
145ea8dc4beschrock	else
146ea8dc4beschrock		tree = &spa->spa_errlist_last;
147ea8dc4beschrock
148ea8dc4beschrock	search.se_bookmark = *zb;
149ea8dc4beschrock	if (avl_find(tree, &search, &where) != NULL) {
150ea8dc4beschrock		mutex_exit(&spa->spa_errlist_lock);
151ea8dc4beschrock		return;
152ea8dc4beschrock	}
153ea8dc4beschrock
154ea8dc4beschrock	new = kmem_zalloc(sizeof (spa_error_entry_t), KM_SLEEP);
155ea8dc4beschrock	new->se_bookmark = *zb;
156ea8dc4beschrock	avl_insert(tree, new, where);
157ea8dc4beschrock
158ea8dc4beschrock	mutex_exit(&spa->spa_errlist_lock);
159ea8dc4beschrock}
160ea8dc4beschrock
161ea8dc4beschrock/*
162ea8dc4beschrock * Return the number of errors currently in the error log.  This is actually the
163ea8dc4beschrock * sum of both the last log and the current log, since we don't know the union
164ea8dc4beschrock * of these logs until we reach userland.
165ea8dc4beschrock */
166ea8dc4beschrockuint64_t
167ea8dc4beschrockspa_get_errlog_size(spa_t *spa)
168ea8dc4beschrock{
169ea8dc4beschrock	uint64_t total = 0, count;
170ea8dc4beschrock
171ea8dc4beschrock	mutex_enter(&spa->spa_errlog_lock);
172ea8dc4beschrock	if (spa->spa_errlog_scrub != 0 &&
173ea8dc4beschrock	    zap_count(spa->spa_meta_objset, spa->spa_errlog_scrub,
174ea8dc4beschrock	    &count) == 0)
175ea8dc4beschrock		total += count;
176ea8dc4beschrock
177ea8dc4beschrock	if (spa->spa_errlog_last != 0 && !spa->spa_scrub_finished &&
178ea8dc4beschrock	    zap_count(spa->spa_meta_objset, spa->spa_errlog_last,
179ea8dc4beschrock	    &count) == 0)
180ea8dc4beschrock		total += count;
181