spa_history.c revision 347eec8e8d911407001c48ea3fad34d03bcfb742
06eeb2ad640ce72d394ac521094bed7681044408ek * CDDL HEADER START
06eeb2ad640ce72d394ac521094bed7681044408ek * The contents of this file are subject to the terms of the
06eeb2ad640ce72d394ac521094bed7681044408ek * Common Development and Distribution License (the "License").
06eeb2ad640ce72d394ac521094bed7681044408ek * You may not use this file except in compliance with the License.
06eeb2ad640ce72d394ac521094bed7681044408ek * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
06eeb2ad640ce72d394ac521094bed7681044408ek * See the License for the specific language governing permissions
06eeb2ad640ce72d394ac521094bed7681044408ek * and limitations under the License.
06eeb2ad640ce72d394ac521094bed7681044408ek * When distributing Covered Code, include this CDDL HEADER in each
06eeb2ad640ce72d394ac521094bed7681044408ek * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
06eeb2ad640ce72d394ac521094bed7681044408ek * If applicable, add the following below this CDDL HEADER, with the
06eeb2ad640ce72d394ac521094bed7681044408ek * fields enclosed by brackets "[]" replaced with your own identifying
06eeb2ad640ce72d394ac521094bed7681044408ek * information: Portions Copyright [yyyy] [name of copyright owner]
06eeb2ad640ce72d394ac521094bed7681044408ek * CDDL HEADER END
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Copyright (c) 2012 by Delphix. All rights reserved.
06eeb2ad640ce72d394ac521094bed7681044408ek * Routines to manage the on-disk history log.
06eeb2ad640ce72d394ac521094bed7681044408ek * The history log is stored as a dmu object containing
06eeb2ad640ce72d394ac521094bed7681044408ek * <packed record length, record nvlist> tuples.
06eeb2ad640ce72d394ac521094bed7681044408ek * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
06eeb2ad640ce72d394ac521094bed7681044408ek * "packed record length" is the packed length of the "record nvlist" stored
06eeb2ad640ce72d394ac521094bed7681044408ek * as a little endian uint64_t.
06eeb2ad640ce72d394ac521094bed7681044408ek * The log is implemented as a ring buffer, though the original creation
06eeb2ad640ce72d394ac521094bed7681044408ek * of the pool ('zpool create') is never overwritten.
06eeb2ad640ce72d394ac521094bed7681044408ek * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer
06eeb2ad640ce72d394ac521094bed7681044408ek * of 'spa_history' stores the offsets for logging/retrieving history as
06eeb2ad640ce72d394ac521094bed7681044408ek * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of
06eeb2ad640ce72d394ac521094bed7681044408ek * where the 'zpool create' record is stored. This allows us to never
06eeb2ad640ce72d394ac521094bed7681044408ek * overwrite the original creation of the pool. 'sh_phys_max_off' is the
06eeb2ad640ce72d394ac521094bed7681044408ek * physical ending offset in bytes of the log. This tells you the length of
06eeb2ad640ce72d394ac521094bed7681044408ek * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record
06eeb2ad640ce72d394ac521094bed7681044408ek * is added, 'sh_eof' is incremented by the the size of the record.
06eeb2ad640ce72d394ac521094bed7681044408ek * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes).
06eeb2ad640ce72d394ac521094bed7681044408ek * This is where the consumer should start reading from after reading in
06eeb2ad640ce72d394ac521094bed7681044408ek * the 'zpool create' portion of the log.
06eeb2ad640ce72d394ac521094bed7681044408ek * 'sh_records_lost' keeps track of how many records have been overwritten
06eeb2ad640ce72d394ac521094bed7681044408ek * and permanently lost.
06eeb2ad640ce72d394ac521094bed7681044408ek/* convert a logical offset to physical */
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
06eeb2ad640ce72d394ac521094bed7681044408ek phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
06eeb2ad640ce72d394ac521094bed7681044408ek spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
06eeb2ad640ce72d394ac521094bed7681044408ek * Figure out maximum size of history log. We set it at
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * 0.1% of pool size, with a max of 1G and min of 128KB.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens metaslab_class_get_dspace(spa_normal_class(spa)) / 1000;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 1<<30);
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
06eeb2ad640ce72d394ac521094bed7681044408ek * Change 'sh_bof' to the beginning of the next record.
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
06eeb2ad640ce72d394ac521094bed7681044408ek firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
06eeb2ad640ce72d394ac521094bed7681044408ek if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
06eeb2ad640ce72d394ac521094bed7681044408ek return (0);
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
06eeb2ad640ce72d394ac521094bed7681044408ek /* see if we need to reset logical BOF */
06eeb2ad640ce72d394ac521094bed7681044408ek while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
06eeb2ad640ce72d394ac521094bed7681044408ek if (len > 0) {
06eeb2ad640ce72d394ac521094bed7681044408ek /* write out the rest at the beginning of physical file */
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
06eeb2ad640ce72d394ac521094bed7681044408ek return (0);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarksstatic char *
06eeb2ad640ce72d394ac521094bed7681044408ek * Write out a history event.
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Lingspa_history_log_sync(void *arg1, void *arg2, dmu_tx_t *tx)
06eeb2ad640ce72d394ac521094bed7681044408ek * If we have an older pool that doesn't have a command
06eeb2ad640ce72d394ac521094bed7681044408ek * history object, create it now.
06eeb2ad640ce72d394ac521094bed7681044408ek * Get the offset of where we need to write via the bonus buffer.
06eeb2ad640ce72d394ac521094bed7681044408ek * Update the offset when the write completes.
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
06eeb2ad640ce72d394ac521094bed7681044408ek ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_uint64(nvl, ZPOOL_HIST_TIME, gethrestime_sec());
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(nvl, ZPOOL_HIST_HOST, utsname.nodename);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens } else if (nvlist_exists(nvl, ZPOOL_HIST_INT_NAME)) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_lookup_string(nvl, ZPOOL_HIST_DSNAME),
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens } else if (nvlist_exists(nvl, ZPOOL_HIST_IOCTL)) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_lookup_string(nvl, ZPOOL_HIST_IOCTL));
06eeb2ad640ce72d394ac521094bed7681044408ek /* write out the packed length as little endian */
06eeb2ad640ce72d394ac521094bed7681044408ek ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
06eeb2ad640ce72d394ac521094bed7681044408ek ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* The first command is the create, which we keep forever */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (ret == 0 && shpp->sh_pool_create_len == 0 &&
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens shpp->sh_pool_create_len = shpp->sh_bof = shpp->sh_eof;
06eeb2ad640ce72d394ac521094bed7681044408ek * Write out a history event.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(nvl, ZPOOL_HIST_CMD, msg);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensspa_history_log_nvl(spa_t *spa, nvlist_t *nvl)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY)
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_uint64(nvarg, ZPOOL_HIST_WHO, crgetruid(CRED()));
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens /* Kick this off asynchronously; errors are ignored. */
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* spa_history_log_sync will free nvl */
06eeb2ad640ce72d394ac521094bed7681044408ek * Read out the command history.
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If the command history doesn't exist (older pool),
06eeb2ad640ce72d394ac521094bed7681044408ek * that's ok, just return ENOENT.
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens * The history is logged asynchronously, so when they request
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens * the first chunk of history, make sure everything has been
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens * synced to disk so that we get it.
06eeb2ad640ce72d394ac521094bed7681044408ek if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
06eeb2ad640ce72d394ac521094bed7681044408ek ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
06eeb2ad640ce72d394ac521094bed7681044408ek /* read in just the zpool create history */
06eeb2ad640ce72d394ac521094bed7681044408ek * Need to reset passed in offset to BOF if the passed in
06eeb2ad640ce72d394ac521094bed7681044408ek * offset has since been overwritten.
06eeb2ad640ce72d394ac521094bed7681044408ek * Read up to the minimum of what the user passed down or
06eeb2ad640ce72d394ac521094bed7681044408ek * the EOF (physical or logical). If we hit physical EOF,
06eeb2ad640ce72d394ac521094bed7681044408ek * use 'leftover' to read from the physical BOF.
06eeb2ad640ce72d394ac521094bed7681044408ek /* offset for consumer to use next */
06eeb2ad640ce72d394ac521094bed7681044408ek /* tell the consumer how much you actually read */
06eeb2ad640ce72d394ac521094bed7681044408ek if (read_len == 0) {
06eeb2ad640ce72d394ac521094bed7681044408ek return (0);
7bfdf011e081684f853a3242d0296695110d9d84Neil Perrin err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
06eeb2ad640ce72d394ac521094bed7681044408ek err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The nvlist will be consumed by this call.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslog_internal(nvlist_t *nvl, const char *operation, spa_t *spa,
088f389458728c464569a5506b58070254fa4f7dahrens * If this is part of creating a pool, not everything is
088f389458728c464569a5506b58070254fa4f7dahrens * initialized yet, so don't bother logging the internal events.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens msg = kmem_alloc(vsnprintf(NULL, 0, fmt, adx) + 1, KM_SLEEP);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(nvl, ZPOOL_HIST_INT_STR, msg);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(nvl, ZPOOL_HIST_INT_NAME, operation);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_uint64(nvl, ZPOOL_HIST_TXG, tx->tx_txg);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* spa_history_log_sync() will free nvl */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensspa_history_log_internal(spa_t *spa, const char *operation,
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante /* create a tx if we didn't get one */
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens log_internal(fnvlist_alloc(), operation, spa, htx, fmt, adx);
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante /* if we didn't get a tx from the caller, commit the one we made */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensspa_history_log_internal_ds(dsl_dataset_t *ds, const char *operation,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID, ds->ds_object);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens log_internal(nvl, operation, dsl_dataset_get_spa(ds), tx, fmt, adx);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensspa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens log_internal(nvl, operation, dd->dd_pool->dp_spa, tx, fmt, adx);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensspa_history_log_version(spa_t *spa, const char *operation)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens spa_history_log_internal(spa, operation, NULL,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens "pool version %llu; software version %llu/%d; uts %s %s %s %s",
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (u_longlong_t)current_vers, SPA_VERSION, ZPL_VERSION,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens utsname.nodename, utsname.release, utsname.version,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens cmn_err(CE_CONT, "!%s version %llu pool %s using %llu", operation,