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