11544Seschrock /* 21544Seschrock * CDDL HEADER START 31544Seschrock * 41544Seschrock * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 71544Seschrock * 81544Seschrock * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91544Seschrock * or http://www.opensolaris.org/os/licensing. 101544Seschrock * See the License for the specific language governing permissions 111544Seschrock * and limitations under the License. 121544Seschrock * 131544Seschrock * When distributing Covered Code, include this CDDL HEADER in each 141544Seschrock * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151544Seschrock * If applicable, add the following below this CDDL HEADER, with the 161544Seschrock * fields enclosed by brackets "[]" replaced with your own identifying 171544Seschrock * information: Portions Copyright [yyyy] [name of copyright owner] 181544Seschrock * 191544Seschrock * CDDL HEADER END 201544Seschrock */ 211544Seschrock /* 221544Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 231544Seschrock * Use is subject to license terms. 241544Seschrock */ 251544Seschrock 261544Seschrock #pragma ident "%Z%%M% %I% %E% SMI" 271544Seschrock 281544Seschrock /* 291544Seschrock * Routines to manage the on-disk persistent error log. 301544Seschrock * 311544Seschrock * Each pool stores a log of all logical data errors seen during normal 321544Seschrock * operation. This is actually the union of two distinct logs: the last log, 331544Seschrock * and the current log. All errors seen are logged to the current log. When a 341544Seschrock * scrub completes, the current log becomes the last log, the last log is thrown 351544Seschrock * out, and the current log is reinitialized. This way, if an error is somehow 361544Seschrock * corrected, a new scrub will show that that it no longer exists, and will be 371544Seschrock * deleted from the log when the scrub completes. 381544Seschrock * 391544Seschrock * The log is stored using a ZAP object whose key is a string form of the 401544Seschrock * zbookmark tuple (objset, object, level, blkid), and whose contents is an 411544Seschrock * optional 'objset:object' human-readable string describing the data. When an 421544Seschrock * error is first logged, this string will be empty, indicating that no name is 431544Seschrock * known. This prevents us from having to issue a potentially large amount of 441544Seschrock * I/O to discover the object name during an error path. Instead, we do the 451544Seschrock * calculation when the data is requested, storing the result so future queries 461544Seschrock * will be faster. 471544Seschrock * 481544Seschrock * This log is then shipped into an nvlist where the key is the dataset name and 491544Seschrock * the value is the object name. Userland is then responsible for uniquifying 501544Seschrock * this list and displaying it to the user. 511544Seschrock */ 521544Seschrock 531544Seschrock #include <sys/dmu_tx.h> 541544Seschrock #include <sys/spa.h> 551544Seschrock #include <sys/spa_impl.h> 561544Seschrock #include <sys/zap.h> 571544Seschrock #include <sys/zio.h> 581544Seschrock 591544Seschrock /* 601544Seschrock * This is a stripped-down version of strtoull, suitable only for converting 611544Seschrock * lowercase hexidecimal numbers that don't overflow. 621544Seschrock */ 63*2856Snd150628 #ifdef _KERNEL 641544Seschrock static uint64_t 651544Seschrock strtonum(char *str, char **nptr) 661544Seschrock { 671544Seschrock uint64_t val = 0; 681544Seschrock char c; 691544Seschrock int digit; 701544Seschrock 711544Seschrock while ((c = *str) != '\0') { 721544Seschrock if (c >= '0' && c <= '9') 731544Seschrock digit = c - '0'; 741544Seschrock else if (c >= 'a' && c <= 'f') 751544Seschrock digit = 10 + c - 'a'; 761544Seschrock else 771544Seschrock break; 781544Seschrock 791544Seschrock val *= 16; 801544Seschrock val += digit; 811544Seschrock 821544Seschrock str++; 831544Seschrock } 841544Seschrock 851544Seschrock *nptr = str; 861544Seschrock 871544Seschrock return (val); 881544Seschrock } 89*2856Snd150628 #endif 901544Seschrock 911544Seschrock /* 921544Seschrock * Convert a bookmark to a string. 931544Seschrock */ 941544Seschrock static void 951544Seschrock bookmark_to_name(zbookmark_t *zb, char *buf, size_t len) 961544Seschrock { 971544Seschrock (void) snprintf(buf, len, "%llx:%llx:%llx:%llx", 981544Seschrock (u_longlong_t)zb->zb_objset, (u_longlong_t)zb->zb_object, 991544Seschrock (u_longlong_t)zb->zb_level, (u_longlong_t)zb->zb_blkid); 1001544Seschrock } 1011544Seschrock 1021544Seschrock /* 1031544Seschrock * Convert a string to a bookmark 1041544Seschrock */ 105*2856Snd150628 #ifdef _KERNEL 1061544Seschrock static void 1071544Seschrock name_to_bookmark(char *buf, zbookmark_t *zb) 1081544Seschrock { 1091544Seschrock zb->zb_objset = strtonum(buf, &buf); 1101544Seschrock ASSERT(*buf == ':'); 1111544Seschrock zb->zb_object = strtonum(buf + 1, &buf); 1121544Seschrock ASSERT(*buf == ':'); 1131544Seschrock zb->zb_level = (int)strtonum(buf + 1, &buf); 1141544Seschrock ASSERT(*buf == ':'); 1151544Seschrock zb->zb_blkid = strtonum(buf + 1, &buf); 1161544Seschrock ASSERT(*buf == '\0'); 1171544Seschrock } 118*2856Snd150628 #endif 1191544Seschrock 1201544Seschrock /* 1211544Seschrock * Log an uncorrectable error to the persistent error log. We add it to the 1221544Seschrock * spa's list of pending errors. The changes are actually synced out to disk 1231544Seschrock * during spa_errlog_sync(). 1241544Seschrock */ 1251544Seschrock void 1261544Seschrock spa_log_error(spa_t *spa, zio_t *zio) 1271544Seschrock { 1281544Seschrock zbookmark_t *zb = &zio->io_logical->io_bookmark; 1291544Seschrock spa_error_entry_t search; 1301544Seschrock spa_error_entry_t *new; 1311544Seschrock avl_tree_t *tree; 1321544Seschrock avl_index_t where; 1331544Seschrock 1341544Seschrock /* 1351544Seschrock * If we are trying to import a pool, ignore any errors, as we won't be 1361544Seschrock * writing to the pool any time soon. 1371544Seschrock */ 1381544Seschrock if (spa->spa_load_state == SPA_LOAD_TRYIMPORT) 1391544Seschrock return; 1401544Seschrock 1411544Seschrock mutex_enter(&spa->spa_errlist_lock); 1421544Seschrock 1431544Seschrock /* 1441544Seschrock * If we have had a request to rotate the log, log it to the next list 1451544Seschrock * instead of the current one. 1461544Seschrock */ 1471544Seschrock if (spa->spa_scrub_active || spa->spa_scrub_finished) 1481544Seschrock tree = &spa->spa_errlist_scrub; 1491544Seschrock else 1501544Seschrock tree = &spa->spa_errlist_last; 1511544Seschrock 1521544Seschrock search.se_bookmark = *zb; 1531544Seschrock if (avl_find(tree, &search, &where) != NULL) { 1541544Seschrock mutex_exit(&spa->spa_errlist_lock); 1551544Seschrock return; 1561544Seschrock } 1571544Seschrock 1581544Seschrock new = kmem_zalloc(sizeof (spa_error_entry_t), KM_SLEEP); 1591544Seschrock new->se_bookmark = *zb; 1601544Seschrock avl_insert(tree, new, where); 1611544Seschrock 1621544Seschrock mutex_exit(&spa->spa_errlist_lock); 1631544Seschrock } 1641544Seschrock 1651544Seschrock /* 1661544Seschrock * Return the number of errors currently in the error log. This is actually the 1671544Seschrock * sum of both the last log and the current log, since we don't know the union 1681544Seschrock * of these logs until we reach userland. 1691544Seschrock */ 1701544Seschrock uint64_t 1711544Seschrock spa_get_errlog_size(spa_t *spa) 1721544Seschrock { 1731544Seschrock uint64_t total = 0, count; 1741544Seschrock 1751544Seschrock mutex_enter(&spa->spa_errlog_lock); 1761544Seschrock if (spa->spa_errlog_scrub != 0 && 1771544Seschrock zap_count(spa->spa_meta_objset, spa->spa_errlog_scrub, 1781544Seschrock &count) == 0) 1791544Seschrock total += count; 1801544Seschrock 1811544Seschrock if (spa->spa_errlog_last != 0 && !spa->spa_scrub_finished && 1821544Seschrock zap_count(spa->spa_meta_objset, spa->spa_errlog_last, 1831544Seschrock &count) == 0) 1841544Seschrock total += count; 1851544Seschrock mutex_exit(&spa->spa_errlog_lock); 1861544Seschrock 1871544Seschrock mutex_enter(&spa->spa_errlist_lock); 1881544Seschrock total += avl_numnodes(&spa->spa_errlist_last); 1891544Seschrock total += avl_numnodes(&spa->spa_errlist_scrub); 1901544Seschrock mutex_exit(&spa->spa_errlist_lock); 1911544Seschrock 1921544Seschrock return (total); 1931544Seschrock } 1941544Seschrock 1951544Seschrock #ifdef _KERNEL 1961544Seschrock static int 1971544Seschrock process_error_log(spa_t *spa, uint64_t obj, void *addr, size_t *count) 1981544Seschrock { 1991544Seschrock zap_cursor_t zc; 2001544Seschrock zap_attribute_t za; 2011544Seschrock zbookmark_t zb; 2021544Seschrock 2031544Seschrock if (obj == 0) 2041544Seschrock return (0); 2051544Seschrock 2061544Seschrock for (zap_cursor_init(&zc, spa->spa_meta_objset, obj); 2071544Seschrock zap_cursor_retrieve(&zc, &za) == 0; 2081544Seschrock zap_cursor_advance(&zc)) { 2091544Seschrock 2101544Seschrock if (*count == 0) { 2111544Seschrock zap_cursor_fini(&zc); 2121544Seschrock return (ENOMEM); 2131544Seschrock } 2141544Seschrock 2151544Seschrock name_to_bookmark(za.za_name, &zb); 2161544Seschrock 2171544Seschrock if (copyout(&zb, (char *)addr + 2181544Seschrock (*count - 1) * sizeof (zbookmark_t), 2191544Seschrock sizeof (zbookmark_t)) != 0) 2201544Seschrock return (EFAULT); 2211544Seschrock 2221544Seschrock *count -= 1; 2231544Seschrock } 2241544Seschrock 2251544Seschrock zap_cursor_fini(&zc); 2261544Seschrock 2271544Seschrock return (0); 2281544Seschrock } 2291544Seschrock 2301544Seschrock static int 2311544Seschrock process_error_list(avl_tree_t *list, void *addr, size_t *count) 2321544Seschrock { 2331544Seschrock spa_error_entry_t *se; 2341544Seschrock 2351544Seschrock for (se = avl_first(list); se != NULL; se = AVL_NEXT(list, se)) { 2361544Seschrock 2371544Seschrock if (*count == 0) 2381544Seschrock return (ENOMEM); 2391544Seschrock 2401544Seschrock if (copyout(&se->se_bookmark, (char *)addr + 2411544Seschrock (*count - 1) * sizeof (zbookmark_t), 2421544Seschrock sizeof (zbookmark_t)) != 0) 2431544Seschrock return (EFAULT); 2441544Seschrock 2451544Seschrock *count -= 1; 2461544Seschrock } 2471544Seschrock 2481544Seschrock return (0); 2491544Seschrock } 2501544Seschrock #endif 2511544Seschrock 2521544Seschrock /* 2531544Seschrock * Copy all known errors to userland as an array of bookmarks. This is 2541544Seschrock * actually a union of the on-disk last log and current log, as well as any 2551544Seschrock * pending error requests. 2561544Seschrock * 2571544Seschrock * Because the act of reading the on-disk log could cause errors to be 2581544Seschrock * generated, we have two separate locks: one for the error log and one for the 2591544Seschrock * in-core error lists. We only need the error list lock to log and error, so 2601544Seschrock * we grab the error log lock while we read the on-disk logs, and only pick up 2611544Seschrock * the error list lock when we are finished. 2621544Seschrock */ 2631544Seschrock int 2641544Seschrock spa_get_errlog(spa_t *spa, void *uaddr, size_t *count) 2651544Seschrock { 2661544Seschrock int ret = 0; 2671544Seschrock 2681544Seschrock #ifdef _KERNEL 2691544Seschrock mutex_enter(&spa->spa_errlog_lock); 2701544Seschrock 2711544Seschrock ret = process_error_log(spa, spa->spa_errlog_scrub, uaddr, count); 2721544Seschrock 2731544Seschrock if (!ret && !spa->spa_scrub_finished) 2741544Seschrock ret = process_error_log(spa, spa->spa_errlog_last, uaddr, 2751544Seschrock count); 2761544Seschrock 2771544Seschrock mutex_enter(&spa->spa_errlist_lock); 2781544Seschrock if (!ret) 2791544Seschrock ret = process_error_list(&spa->spa_errlist_scrub, uaddr, 2801544Seschrock count); 2811544Seschrock if (!ret) 2821544Seschrock ret = process_error_list(&spa->spa_errlist_last, uaddr, 2831544Seschrock count); 2841544Seschrock mutex_exit(&spa->spa_errlist_lock); 2851544Seschrock 2861544Seschrock mutex_exit(&spa->spa_errlog_lock); 2871544Seschrock #endif 2881544Seschrock 2891544Seschrock return (ret); 2901544Seschrock } 2911544Seschrock 2921544Seschrock /* 2931544Seschrock * Called when a scrub completes. This simply set a bit which tells which AVL 2941544Seschrock * tree to add new errors. spa_errlog_sync() is responsible for actually 2951544Seschrock * syncing the changes to the underlying objects. 2961544Seschrock */ 2971544Seschrock void 2981544Seschrock spa_errlog_rotate(spa_t *spa) 2991544Seschrock { 3001544Seschrock mutex_enter(&spa->spa_errlist_lock); 3011544Seschrock 3021544Seschrock ASSERT(!spa->spa_scrub_finished); 3031544Seschrock spa->spa_scrub_finished = B_TRUE; 3041544Seschrock 3051544Seschrock mutex_exit(&spa->spa_errlist_lock); 3061544Seschrock } 3071544Seschrock 3081544Seschrock /* 3091544Seschrock * Discard any pending errors from the spa_t. Called when unloading a faulted 3101544Seschrock * pool, as the errors encountered during the open cannot be synced to disk. 3111544Seschrock */ 3121544Seschrock void 3131544Seschrock spa_errlog_drain(spa_t *spa) 3141544Seschrock { 3151544Seschrock spa_error_entry_t *se; 3161544Seschrock void *cookie; 3171544Seschrock 3181544Seschrock mutex_enter(&spa->spa_errlist_lock); 3191544Seschrock 3201544Seschrock cookie = NULL; 3211544Seschrock while ((se = avl_destroy_nodes(&spa->spa_errlist_last, 3221544Seschrock &cookie)) != NULL) 3231544Seschrock kmem_free(se, sizeof (spa_error_entry_t)); 3241544Seschrock cookie = NULL; 3251544Seschrock while ((se = avl_destroy_nodes(&spa->spa_errlist_scrub, 3261544Seschrock &cookie)) != NULL) 3271544Seschrock kmem_free(se, sizeof (spa_error_entry_t)); 3281544Seschrock 3291544Seschrock mutex_exit(&spa->spa_errlist_lock); 3301544Seschrock } 3311544Seschrock 3321544Seschrock /* 3331544Seschrock * Process a list of errors into the current on-disk log. 3341544Seschrock */ 3351544Seschrock static void 3361544Seschrock sync_error_list(spa_t *spa, avl_tree_t *t, uint64_t *obj, dmu_tx_t *tx) 3371544Seschrock { 3381544Seschrock spa_error_entry_t *se; 3391544Seschrock char buf[64]; 3401544Seschrock void *cookie; 3411544Seschrock 3421544Seschrock if (avl_numnodes(t) != 0) { 3431544Seschrock /* create log if necessary */ 3441544Seschrock if (*obj == 0) 3451544Seschrock *obj = zap_create(spa->spa_meta_objset, 3461544Seschrock DMU_OT_ERROR_LOG, DMU_OT_NONE, 3471544Seschrock 0, tx); 3481544Seschrock 3491544Seschrock /* add errors to the current log */ 3501544Seschrock for (se = avl_first(t); se != NULL; se = AVL_NEXT(t, se)) { 3511544Seschrock char *name = se->se_name ? se->se_name : ""; 3521544Seschrock 3531544Seschrock bookmark_to_name(&se->se_bookmark, buf, sizeof (buf)); 3541544Seschrock 3551544Seschrock (void) zap_update(spa->spa_meta_objset, 3561544Seschrock *obj, buf, 1, strlen(name) + 1, name, tx); 3571544Seschrock } 3581544Seschrock 3591544Seschrock /* purge the error list */ 3601544Seschrock cookie = NULL; 3611544Seschrock while ((se = avl_destroy_nodes(t, &cookie)) != NULL) 3621544Seschrock kmem_free(se, sizeof (spa_error_entry_t)); 3631544Seschrock } 3641544Seschrock } 3651544Seschrock 3661544Seschrock /* 3671544Seschrock * Sync the error log out to disk. This is a little tricky because the act of 3681544Seschrock * writing the error log requires the spa_errlist_lock. So, we need to lock the 3691544Seschrock * error lists, take a copy of the lists, and then reinitialize them. Then, we 3701544Seschrock * drop the error list lock and take the error log lock, at which point we 3711544Seschrock * do the errlog processing. Then, if we encounter an I/O error during this 3721544Seschrock * process, we can successfully add the error to the list. Note that this will 3731544Seschrock * result in the perpetual recycling of errors, but it is an unlikely situation 3741544Seschrock * and not a performance critical operation. 3751544Seschrock */ 3761544Seschrock void 3771544Seschrock spa_errlog_sync(spa_t *spa, uint64_t txg) 3781544Seschrock { 3791544Seschrock dmu_tx_t *tx; 3801544Seschrock avl_tree_t scrub, last; 3811544Seschrock int scrub_finished; 3821544Seschrock 3831544Seschrock mutex_enter(&spa->spa_errlist_lock); 3841544Seschrock 3851544Seschrock /* 3861544Seschrock * Bail out early under normal circumstances. 3871544Seschrock */ 3881544Seschrock if (avl_numnodes(&spa->spa_errlist_scrub) == 0 && 3891544Seschrock avl_numnodes(&spa->spa_errlist_last) == 0 && 3901544Seschrock !spa->spa_scrub_finished) { 3911544Seschrock mutex_exit(&spa->spa_errlist_lock); 3921544Seschrock return; 3931544Seschrock } 3941544Seschrock 3951544Seschrock spa_get_errlists(spa, &last, &scrub); 3961544Seschrock scrub_finished = spa->spa_scrub_finished; 3971544Seschrock spa->spa_scrub_finished = B_FALSE; 3981544Seschrock 3991544Seschrock mutex_exit(&spa->spa_errlist_lock); 4001544Seschrock mutex_enter(&spa->spa_errlog_lock); 4011544Seschrock 4021544Seschrock tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); 4031544Seschrock 4041544Seschrock /* 4051544Seschrock * Sync out the current list of errors. 4061544Seschrock */ 4071544Seschrock sync_error_list(spa, &last, &spa->spa_errlog_last, tx); 4081544Seschrock 4091544Seschrock /* 4101544Seschrock * Rotate the log if necessary. 4111544Seschrock */ 4121544Seschrock if (scrub_finished) { 4131544Seschrock if (spa->spa_errlog_last != 0) 4141544Seschrock VERIFY(dmu_object_free(spa->spa_meta_objset, 4151544Seschrock spa->spa_errlog_last, tx) == 0); 4161544Seschrock spa->spa_errlog_last = spa->spa_errlog_scrub; 4171544Seschrock spa->spa_errlog_scrub = 0; 4181544Seschrock 4191544Seschrock sync_error_list(spa, &scrub, &spa->spa_errlog_last, tx); 4201544Seschrock } 4211544Seschrock 4221544Seschrock /* 4231544Seschrock * Sync out any pending scrub errors. 4241544Seschrock */ 4251544Seschrock sync_error_list(spa, &scrub, &spa->spa_errlog_scrub, tx); 4261544Seschrock 4271544Seschrock /* 4281544Seschrock * Update the MOS to reflect the new values. 4291544Seschrock */ 4301544Seschrock (void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 4311544Seschrock DMU_POOL_ERRLOG_LAST, sizeof (uint64_t), 1, 4321544Seschrock &spa->spa_errlog_last, tx); 4331544Seschrock (void) zap_update(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 4341544Seschrock DMU_POOL_ERRLOG_SCRUB, sizeof (uint64_t), 1, 4351544Seschrock &spa->spa_errlog_scrub, tx); 4361544Seschrock 4371544Seschrock dmu_tx_commit(tx); 4381544Seschrock 4391544Seschrock mutex_exit(&spa->spa_errlog_lock); 4401544Seschrock } 441