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