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