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.
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.
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]
22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
27 * Routines to manage the on-disk persistent error log.
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 it no longer exists, and will be
35 * deleted from the log when the scrub completes.
37 * The log is stored using a ZAP object whose key is a string form of the
38 * zbookmark_phys 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
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.
51 #include <sys/dmu_tx.h>
53 #include <sys/spa_impl.h>
59 * Convert a bookmark to a string.
62 bookmark_to_name(zbookmark_phys_t
*zb
, char *buf
, size_t len
)
64 (void) snprintf(buf
, len
, "%llx:%llx:%llx:%llx",
65 (u_longlong_t
)zb
->zb_objset
, (u_longlong_t
)zb
->zb_object
,
66 (u_longlong_t
)zb
->zb_level
, (u_longlong_t
)zb
->zb_blkid
);
70 * Convert a string to a bookmark
74 name_to_bookmark(char *buf
, zbookmark_phys_t
*zb
)
76 zb
->zb_objset
= zfs_strtonum(buf
, &buf
);
78 zb
->zb_object
= zfs_strtonum(buf
+ 1, &buf
);
80 zb
->zb_level
= (int)zfs_strtonum(buf
+ 1, &buf
);
82 zb
->zb_blkid
= zfs_strtonum(buf
+ 1, &buf
);
88 * Log an uncorrectable error to the persistent error log. We add it to the
89 * spa's list of pending errors. The changes are actually synced out to disk
90 * during spa_errlog_sync().
93 spa_log_error(spa_t
*spa
, const zbookmark_phys_t
*zb
)
95 spa_error_entry_t search
;
96 spa_error_entry_t
*new;
101 * If we are trying to import a pool, ignore any errors, as we won't be
102 * writing to the pool any time soon.
104 if (spa_load_state(spa
) == SPA_LOAD_TRYIMPORT
)
107 mutex_enter(&spa
->spa_errlist_lock
);
110 * If we have had a request to rotate the log, log it to the next list
111 * instead of the current one.
113 if (spa
->spa_scrub_active
|| spa
->spa_scrub_finished
)
114 tree
= &spa
->spa_errlist_scrub
;
116 tree
= &spa
->spa_errlist_last
;
118 search
.se_bookmark
= *zb
;
119 if (avl_find(tree
, &search
, &where
) != NULL
) {
120 mutex_exit(&spa
->spa_errlist_lock
);
124 new = kmem_zalloc(sizeof (spa_error_entry_t
), KM_SLEEP
);
125 new->se_bookmark
= *zb
;
126 avl_insert(tree
, new, where
);
128 mutex_exit(&spa
->spa_errlist_lock
);
132 * Return the number of errors currently in the error log. This is actually the
133 * sum of both the last log and the current log, since we don't know the union
134 * of these logs until we reach userland.
137 spa_get_errlog_size(spa_t
*spa
)
139 uint64_t total
= 0, count
;
141 mutex_enter(&spa
->spa_errlog_lock
);
142 if (spa
->spa_errlog_scrub
!= 0 &&
143 zap_count(spa
->spa_meta_objset
, spa
->spa_errlog_scrub
,
147 if (spa
->spa_errlog_last
!= 0 && !spa
->spa_scrub_finished
&&
148 zap_count(spa
->spa_meta_objset
, spa
->spa_errlog_last
,
151 mutex_exit(&spa
->spa_errlog_lock
);
153 mutex_enter(&spa
->spa_errlist_lock
);
154 total
+= avl_numnodes(&spa
->spa_errlist_last
);
155 total
+= avl_numnodes(&spa
->spa_errlist_scrub
);
156 mutex_exit(&spa
->spa_errlist_lock
);
163 process_error_log(spa_t
*spa
, uint64_t obj
, void *addr
, size_t *count
)
172 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
, obj
);
173 zap_cursor_retrieve(&zc
, &za
) == 0;
174 zap_cursor_advance(&zc
)) {
177 zap_cursor_fini(&zc
);
178 return (SET_ERROR(ENOMEM
));
181 name_to_bookmark(za
.za_name
, &zb
);
183 if (copyout(&zb
, (char *)addr
+
184 (*count
- 1) * sizeof (zbookmark_phys_t
),
185 sizeof (zbookmark_phys_t
)) != 0) {
186 zap_cursor_fini(&zc
);
187 return (SET_ERROR(EFAULT
));
193 zap_cursor_fini(&zc
);
199 process_error_list(avl_tree_t
*list
, void *addr
, size_t *count
)
201 spa_error_entry_t
*se
;
203 for (se
= avl_first(list
); se
!= NULL
; se
= AVL_NEXT(list
, se
)) {
206 return (SET_ERROR(ENOMEM
));
208 if (copyout(&se
->se_bookmark
, (char *)addr
+
209 (*count
- 1) * sizeof (zbookmark_phys_t
),
210 sizeof (zbookmark_phys_t
)) != 0)
211 return (SET_ERROR(EFAULT
));
221 * Copy all known errors to userland as an array of bookmarks. This is
222 * actually a union of the on-disk last log and current log, as well as any
223 * pending error requests.
225 * Because the act of reading the on-disk log could cause errors to be
226 * generated, we have two separate locks: one for the error log and one for the
227 * in-core error lists. We only need the error list lock to log and error, so
228 * we grab the error log lock while we read the on-disk logs, and only pick up
229 * the error list lock when we are finished.
232 spa_get_errlog(spa_t
*spa
, void *uaddr
, size_t *count
)
237 mutex_enter(&spa
->spa_errlog_lock
);
239 ret
= process_error_log(spa
, spa
->spa_errlog_scrub
, uaddr
, count
);
241 if (!ret
&& !spa
->spa_scrub_finished
)
242 ret
= process_error_log(spa
, spa
->spa_errlog_last
, uaddr
,
245 mutex_enter(&spa
->spa_errlist_lock
);
247 ret
= process_error_list(&spa
->spa_errlist_scrub
, uaddr
,
250 ret
= process_error_list(&spa
->spa_errlist_last
, uaddr
,
252 mutex_exit(&spa
->spa_errlist_lock
);
254 mutex_exit(&spa
->spa_errlog_lock
);
256 (void) spa
, (void) uaddr
, (void) count
;
263 * Called when a scrub completes. This simply set a bit which tells which AVL
264 * tree to add new errors. spa_errlog_sync() is responsible for actually
265 * syncing the changes to the underlying objects.
268 spa_errlog_rotate(spa_t
*spa
)
270 mutex_enter(&spa
->spa_errlist_lock
);
271 spa
->spa_scrub_finished
= B_TRUE
;
272 mutex_exit(&spa
->spa_errlist_lock
);
276 * Discard any pending errors from the spa_t. Called when unloading a faulted
277 * pool, as the errors encountered during the open cannot be synced to disk.
280 spa_errlog_drain(spa_t
*spa
)
282 spa_error_entry_t
*se
;
285 mutex_enter(&spa
->spa_errlist_lock
);
288 while ((se
= avl_destroy_nodes(&spa
->spa_errlist_last
,
290 kmem_free(se
, sizeof (spa_error_entry_t
));
292 while ((se
= avl_destroy_nodes(&spa
->spa_errlist_scrub
,
294 kmem_free(se
, sizeof (spa_error_entry_t
));
296 mutex_exit(&spa
->spa_errlist_lock
);
300 * Process a list of errors into the current on-disk log.
303 sync_error_list(spa_t
*spa
, avl_tree_t
*t
, uint64_t *obj
, dmu_tx_t
*tx
)
305 spa_error_entry_t
*se
;
309 if (avl_numnodes(t
) != 0) {
310 /* create log if necessary */
312 *obj
= zap_create(spa
->spa_meta_objset
,
313 DMU_OT_ERROR_LOG
, DMU_OT_NONE
,
316 /* add errors to the current log */
317 for (se
= avl_first(t
); se
!= NULL
; se
= AVL_NEXT(t
, se
)) {
318 char *name
= se
->se_name
? se
->se_name
: "";
320 bookmark_to_name(&se
->se_bookmark
, buf
, sizeof (buf
));
322 (void) zap_update(spa
->spa_meta_objset
,
323 *obj
, buf
, 1, strlen(name
) + 1, name
, tx
);
326 /* purge the error list */
328 while ((se
= avl_destroy_nodes(t
, &cookie
)) != NULL
)
329 kmem_free(se
, sizeof (spa_error_entry_t
));
334 * Sync the error log out to disk. This is a little tricky because the act of
335 * writing the error log requires the spa_errlist_lock. So, we need to lock the
336 * error lists, take a copy of the lists, and then reinitialize them. Then, we
337 * drop the error list lock and take the error log lock, at which point we
338 * do the errlog processing. Then, if we encounter an I/O error during this
339 * process, we can successfully add the error to the list. Note that this will
340 * result in the perpetual recycling of errors, but it is an unlikely situation
341 * and not a performance critical operation.
344 spa_errlog_sync(spa_t
*spa
, uint64_t txg
)
347 avl_tree_t scrub
, last
;
350 mutex_enter(&spa
->spa_errlist_lock
);
353 * Bail out early under normal circumstances.
355 if (avl_numnodes(&spa
->spa_errlist_scrub
) == 0 &&
356 avl_numnodes(&spa
->spa_errlist_last
) == 0 &&
357 !spa
->spa_scrub_finished
) {
358 mutex_exit(&spa
->spa_errlist_lock
);
362 spa_get_errlists(spa
, &last
, &scrub
);
363 scrub_finished
= spa
->spa_scrub_finished
;
364 spa
->spa_scrub_finished
= B_FALSE
;
366 mutex_exit(&spa
->spa_errlist_lock
);
367 mutex_enter(&spa
->spa_errlog_lock
);
369 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
372 * Sync out the current list of errors.
374 sync_error_list(spa
, &last
, &spa
->spa_errlog_last
, tx
);
377 * Rotate the log if necessary.
379 if (scrub_finished
) {
380 if (spa
->spa_errlog_last
!= 0)
381 VERIFY(dmu_object_free(spa
->spa_meta_objset
,
382 spa
->spa_errlog_last
, tx
) == 0);
383 spa
->spa_errlog_last
= spa
->spa_errlog_scrub
;
384 spa
->spa_errlog_scrub
= 0;
386 sync_error_list(spa
, &scrub
, &spa
->spa_errlog_last
, tx
);
390 * Sync out any pending scrub errors.
392 sync_error_list(spa
, &scrub
, &spa
->spa_errlog_scrub
, tx
);
395 * Update the MOS to reflect the new values.
397 (void) zap_update(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
398 DMU_POOL_ERRLOG_LAST
, sizeof (uint64_t), 1,
399 &spa
->spa_errlog_last
, tx
);
400 (void) zap_update(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
401 DMU_POOL_ERRLOG_SCRUB
, sizeof (uint64_t), 1,
402 &spa
->spa_errlog_scrub
, tx
);
406 mutex_exit(&spa
->spa_errlog_lock
);
411 EXPORT_SYMBOL(spa_log_error
);
412 EXPORT_SYMBOL(spa_get_errlog_size
);
413 EXPORT_SYMBOL(spa_get_errlog
);
414 EXPORT_SYMBOL(spa_errlog_rotate
);
415 EXPORT_SYMBOL(spa_errlog_drain
);
416 EXPORT_SYMBOL(spa_errlog_sync
);
417 EXPORT_SYMBOL(spa_get_errlists
);