spa_history.c revision 55434c770c89aa1b84474f2559a106803511aba0
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
55434c770c89aa1b84474f2559a106803511aba0ek * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
06eeb2ad640ce72d394ac521094bed7681044408ek * Use is subject to license terms.
06eeb2ad640ce72d394ac521094bed7681044408ek#pragma ident "%Z%%M% %I% %E% SMI"
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.
06eeb2ad640ce72d394ac521094bed7681044408ektypedef enum history_log_type {
06eeb2ad640ce72d394ac521094bed7681044408ektypedef struct history_arg {
06eeb2ad640ce72d394ac521094bed7681044408ek const char *ha_history_str;
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
06eeb2ad640ce72d394ac521094bed7681044408ek * 1% of pool size, with a max of 32MB and min of 128KB.
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20);
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 -
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);
06eeb2ad640ce72d394ac521094bed7681044408ek * Write out a history event.
06eeb2ad640ce72d394ac521094bed7681044408ekspa_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);
06eeb2ad640ce72d394ac521094bed7681044408ek /* construct a nvlist of the current time and cmd string */
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD, history_str) == 0);
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);
06eeb2ad640ce72d394ac521094bed7681044408ek * Write out a history event.
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_log(spa_t *spa, const char *history_str, uint64_t pool_create)
06eeb2ad640ce72d394ac521094bed7681044408ek ha.ha_log_type = pool_create ? LOG_CMD_CREATE : LOG_CMD_NO_CREATE;
06eeb2ad640ce72d394ac521094bed7681044408ek return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync,
06eeb2ad640ce72d394ac521094bed7681044408ek * Read out the command history.
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
06eeb2ad640ce72d394ac521094bed7681044408ek * If the command history doesn't exist (older pool),
06eeb2ad640ce72d394ac521094bed7681044408ek * that's ok, just return ENOENT.
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);
06eeb2ad640ce72d394ac521094bed7681044408ek err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf);