spa_history.c revision 55434c770c89aa1b84474f2559a106803511aba0
06eeb2ad640ce72d394ac521094bed7681044408ek/*
06eeb2ad640ce72d394ac521094bed7681044408ek * CDDL HEADER START
06eeb2ad640ce72d394ac521094bed7681044408ek *
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 *
06eeb2ad640ce72d394ac521094bed7681044408ek * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
06eeb2ad640ce72d394ac521094bed7681044408ek * or http://www.opensolaris.org/os/licensing.
06eeb2ad640ce72d394ac521094bed7681044408ek * See the License for the specific language governing permissions
06eeb2ad640ce72d394ac521094bed7681044408ek * and limitations under the License.
06eeb2ad640ce72d394ac521094bed7681044408ek *
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 *
06eeb2ad640ce72d394ac521094bed7681044408ek * CDDL HEADER END
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek/*
55434c770c89aa1b84474f2559a106803511aba0ek * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
06eeb2ad640ce72d394ac521094bed7681044408ek * Use is subject to license terms.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek#pragma ident "%Z%%M% %I% %E% SMI"
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek#include <sys/spa_impl.h>
06eeb2ad640ce72d394ac521094bed7681044408ek#include <sys/zap.h>
06eeb2ad640ce72d394ac521094bed7681044408ek#include <sys/dsl_synctask.h>
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek/*
06eeb2ad640ce72d394ac521094bed7681044408ek * Routines to manage the on-disk history log.
06eeb2ad640ce72d394ac521094bed7681044408ek *
06eeb2ad640ce72d394ac521094bed7681044408ek * The history log is stored as a dmu object containing
06eeb2ad640ce72d394ac521094bed7681044408ek * <packed record length, record nvlist> tuples.
06eeb2ad640ce72d394ac521094bed7681044408ek *
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 *
06eeb2ad640ce72d394ac521094bed7681044408ek * The log is implemented as a ring buffer, though the original creation
06eeb2ad640ce72d394ac521094bed7681044408ek * of the pool ('zpool create') is never overwritten.
06eeb2ad640ce72d394ac521094bed7681044408ek *
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 *
06eeb2ad640ce72d394ac521094bed7681044408ek * 'sh_records_lost' keeps track of how many records have been overwritten
06eeb2ad640ce72d394ac521094bed7681044408ek * and permanently lost.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ektypedef enum history_log_type {
06eeb2ad640ce72d394ac521094bed7681044408ek LOG_CMD_CREATE,
06eeb2ad640ce72d394ac521094bed7681044408ek LOG_CMD_NO_CREATE
06eeb2ad640ce72d394ac521094bed7681044408ek} history_log_type_t;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ektypedef struct history_arg {
06eeb2ad640ce72d394ac521094bed7681044408ek const char *ha_history_str;
06eeb2ad640ce72d394ac521094bed7681044408ek history_log_type_t ha_log_type;
06eeb2ad640ce72d394ac521094bed7681044408ek} history_arg_t;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek/* convert a logical offset to physical */
06eeb2ad640ce72d394ac521094bed7681044408ekstatic uint64_t
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
06eeb2ad640ce72d394ac521094bed7681044408ek{
06eeb2ad640ce72d394ac521094bed7681044408ek uint64_t phys_len;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
06eeb2ad640ce72d394ac521094bed7681044408ek return ((log_off - shpp->sh_pool_create_len) % phys_len
06eeb2ad640ce72d394ac521094bed7681044408ek + shpp->sh_pool_create_len);
06eeb2ad640ce72d394ac521094bed7681044408ek}
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ekvoid
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
06eeb2ad640ce72d394ac521094bed7681044408ek{
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_buf_t *dbp;
06eeb2ad640ce72d394ac521094bed7681044408ek spa_history_phys_t *shpp;
06eeb2ad640ce72d394ac521094bed7681044408ek objset_t *mos = spa->spa_meta_objset;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek ASSERT(spa->spa_history == 0);
06eeb2ad640ce72d394ac521094bed7681044408ek spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
06eeb2ad640ce72d394ac521094bed7681044408ek SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
06eeb2ad640ce72d394ac521094bed7681044408ek sizeof (spa_history_phys_t), tx);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
06eeb2ad640ce72d394ac521094bed7681044408ek DMU_POOL_HISTORY, sizeof (uint64_t), 1,
06eeb2ad640ce72d394ac521094bed7681044408ek &spa->spa_history, tx) == 0);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
06eeb2ad640ce72d394ac521094bed7681044408ek ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek shpp = dbp->db_data;
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_buf_will_dirty(dbp, tx);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek /*
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 */
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_phys_max_off = spa_get_dspace(spa) / 100;
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
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_buf_rele(dbp, FTAG);
06eeb2ad640ce72d394ac521094bed7681044408ek}
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek/*
06eeb2ad640ce72d394ac521094bed7681044408ek * Change 'sh_bof' to the beginning of the next record.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ekstatic int
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
06eeb2ad640ce72d394ac521094bed7681044408ek{
06eeb2ad640ce72d394ac521094bed7681044408ek objset_t *mos = spa->spa_meta_objset;
06eeb2ad640ce72d394ac521094bed7681044408ek uint64_t firstread, reclen, phys_bof;
06eeb2ad640ce72d394ac521094bed7681044408ek char buf[sizeof (reclen)];
06eeb2ad640ce72d394ac521094bed7681044408ek int err;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
06eeb2ad640ce72d394ac521094bed7681044408ek firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
06eeb2ad640ce72d394ac521094bed7681044408ek buf)) != 0)
06eeb2ad640ce72d394ac521094bed7681044408ek return (err);
06eeb2ad640ce72d394ac521094bed7681044408ek if (firstread != sizeof (reclen)) {
06eeb2ad640ce72d394ac521094bed7681044408ek if ((err = dmu_read(mos, spa->spa_history,
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_pool_create_len, sizeof (reclen) - firstread,
06eeb2ad640ce72d394ac521094bed7681044408ek buf + firstread)) != 0)
06eeb2ad640ce72d394ac521094bed7681044408ek return (err);
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek reclen = LE_64(*((uint64_t *)buf));
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_bof += reclen + sizeof (reclen);
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_records_lost++;
06eeb2ad640ce72d394ac521094bed7681044408ek return (0);
06eeb2ad640ce72d394ac521094bed7681044408ek}
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ekstatic int
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_tx_t *tx)
06eeb2ad640ce72d394ac521094bed7681044408ek{
06eeb2ad640ce72d394ac521094bed7681044408ek uint64_t firstwrite, phys_eof;
06eeb2ad640ce72d394ac521094bed7681044408ek objset_t *mos = spa->spa_meta_objset;
06eeb2ad640ce72d394ac521094bed7681044408ek int err;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek ASSERT(MUTEX_HELD(&spa->spa_history_lock));
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek /* see if we need to reset logical BOF */
06eeb2ad640ce72d394ac521094bed7681044408ek while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
06eeb2ad640ce72d394ac521094bed7681044408ek (shpp->sh_eof - shpp->sh_bof) <= len) {
06eeb2ad640ce72d394ac521094bed7681044408ek if ((err = spa_history_advance_bof(spa, shpp)) != 0)
06eeb2ad640ce72d394ac521094bed7681044408ek return (err);
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
06eeb2ad640ce72d394ac521094bed7681044408ek firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_eof += len;
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek len -= firstwrite;
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 len, (char *)buf + firstwrite, tx);
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek return (0);
06eeb2ad640ce72d394ac521094bed7681044408ek}
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek/*
06eeb2ad640ce72d394ac521094bed7681044408ek * Write out a history event.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ekvoid
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_log_sync(void *arg1, void *arg2, dmu_tx_t *tx)
06eeb2ad640ce72d394ac521094bed7681044408ek{
06eeb2ad640ce72d394ac521094bed7681044408ek spa_t *spa = arg1;
06eeb2ad640ce72d394ac521094bed7681044408ek history_arg_t *hap = arg2;
06eeb2ad640ce72d394ac521094bed7681044408ek const char *history_str = hap->ha_history_str;
06eeb2ad640ce72d394ac521094bed7681044408ek objset_t *mos = spa->spa_meta_objset;
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_buf_t *dbp;
06eeb2ad640ce72d394ac521094bed7681044408ek spa_history_phys_t *shpp;
06eeb2ad640ce72d394ac521094bed7681044408ek size_t reclen;
06eeb2ad640ce72d394ac521094bed7681044408ek uint64_t le_len;
06eeb2ad640ce72d394ac521094bed7681044408ek nvlist_t *nvrecord;
06eeb2ad640ce72d394ac521094bed7681044408ek char *record_packed = NULL;
06eeb2ad640ce72d394ac521094bed7681044408ek int ret;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek if (history_str == NULL)
06eeb2ad640ce72d394ac521094bed7681044408ek return;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek /*
06eeb2ad640ce72d394ac521094bed7681044408ek * If we have an older pool that doesn't have a command
06eeb2ad640ce72d394ac521094bed7681044408ek * history object, create it now.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ek mutex_enter(&spa->spa_history_lock);
06eeb2ad640ce72d394ac521094bed7681044408ek if (!spa->spa_history)
06eeb2ad640ce72d394ac521094bed7681044408ek spa_history_create_obj(spa, tx);
06eeb2ad640ce72d394ac521094bed7681044408ek mutex_exit(&spa->spa_history_lock);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek /*
06eeb2ad640ce72d394ac521094bed7681044408ek * Get the offset of where we need to write via the bonus buffer.
06eeb2ad640ce72d394ac521094bed7681044408ek * Update the offset when the write completes.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
06eeb2ad640ce72d394ac521094bed7681044408ek shpp = dbp->db_data;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_buf_will_dirty(dbp, tx);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek#ifdef ZFS_DEBUG
06eeb2ad640ce72d394ac521094bed7681044408ek {
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_object_info_t doi;
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_object_info_from_db(dbp, &doi);
06eeb2ad640ce72d394ac521094bed7681044408ek ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek#endif
06eeb2ad640ce72d394ac521094bed7681044408ek
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_uint64(nvrecord, ZPOOL_HIST_TIME,
06eeb2ad640ce72d394ac521094bed7681044408ek gethrestime_sec()) == 0);
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD, history_str) == 0);
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
06eeb2ad640ce72d394ac521094bed7681044408ek NV_ENCODE_XDR, KM_SLEEP) == 0);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek mutex_enter(&spa->spa_history_lock);
06eeb2ad640ce72d394ac521094bed7681044408ek if (hap->ha_log_type == LOG_CMD_CREATE)
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek /* write out the packed length as little endian */
55434c770c89aa1b84474f2559a106803511aba0ek le_len = LE_64((uint64_t)reclen);
06eeb2ad640ce72d394ac521094bed7681044408ek ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
06eeb2ad640ce72d394ac521094bed7681044408ek if (!ret)
06eeb2ad640ce72d394ac521094bed7681044408ek ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek if (!ret && hap->ha_log_type == LOG_CMD_CREATE) {
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_pool_create_len += sizeof (le_len) + reclen;
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_bof = shpp->sh_pool_create_len;
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek mutex_exit(&spa->spa_history_lock);
06eeb2ad640ce72d394ac521094bed7681044408ek nvlist_free(nvrecord);
06eeb2ad640ce72d394ac521094bed7681044408ek kmem_free(record_packed, reclen);
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_buf_rele(dbp, FTAG);
06eeb2ad640ce72d394ac521094bed7681044408ek}
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek/*
06eeb2ad640ce72d394ac521094bed7681044408ek * Write out a history event.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ekint
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_log(spa_t *spa, const char *history_str, uint64_t pool_create)
06eeb2ad640ce72d394ac521094bed7681044408ek{
06eeb2ad640ce72d394ac521094bed7681044408ek history_arg_t ha;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek ha.ha_history_str = history_str;
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 spa, &ha, 0));
06eeb2ad640ce72d394ac521094bed7681044408ek}
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek/*
06eeb2ad640ce72d394ac521094bed7681044408ek * Read out the command history.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ekint
06eeb2ad640ce72d394ac521094bed7681044408ekspa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
06eeb2ad640ce72d394ac521094bed7681044408ek{
06eeb2ad640ce72d394ac521094bed7681044408ek objset_t *mos = spa->spa_meta_objset;
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_buf_t *dbp;
06eeb2ad640ce72d394ac521094bed7681044408ek uint64_t read_len, phys_read_off, phys_eof;
06eeb2ad640ce72d394ac521094bed7681044408ek uint64_t leftover = 0;
06eeb2ad640ce72d394ac521094bed7681044408ek spa_history_phys_t *shpp;
06eeb2ad640ce72d394ac521094bed7681044408ek int err;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek /*
06eeb2ad640ce72d394ac521094bed7681044408ek * If the command history doesn't exist (older pool),
06eeb2ad640ce72d394ac521094bed7681044408ek * that's ok, just return ENOENT.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ek if (!spa->spa_history)
06eeb2ad640ce72d394ac521094bed7681044408ek return (ENOENT);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
06eeb2ad640ce72d394ac521094bed7681044408ek return (err);
06eeb2ad640ce72d394ac521094bed7681044408ek shpp = dbp->db_data;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek#ifdef ZFS_DEBUG
06eeb2ad640ce72d394ac521094bed7681044408ek {
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_object_info_t doi;
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_object_info_from_db(dbp, &doi);
06eeb2ad640ce72d394ac521094bed7681044408ek ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek#endif
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek mutex_enter(&spa->spa_history_lock);
06eeb2ad640ce72d394ac521094bed7681044408ek phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek if (*offp < shpp->sh_pool_create_len) {
06eeb2ad640ce72d394ac521094bed7681044408ek /* read in just the zpool create history */
06eeb2ad640ce72d394ac521094bed7681044408ek phys_read_off = *offp;
06eeb2ad640ce72d394ac521094bed7681044408ek read_len = MIN(*len, shpp->sh_pool_create_len -
06eeb2ad640ce72d394ac521094bed7681044408ek phys_read_off);
06eeb2ad640ce72d394ac521094bed7681044408ek } else {
06eeb2ad640ce72d394ac521094bed7681044408ek /*
06eeb2ad640ce72d394ac521094bed7681044408ek * Need to reset passed in offset to BOF if the passed in
06eeb2ad640ce72d394ac521094bed7681044408ek * offset has since been overwritten.
06eeb2ad640ce72d394ac521094bed7681044408ek */
06eeb2ad640ce72d394ac521094bed7681044408ek *offp = MAX(*offp, shpp->sh_bof);
06eeb2ad640ce72d394ac521094bed7681044408ek phys_read_off = spa_history_log_to_phys(*offp, shpp);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek /*
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 */
06eeb2ad640ce72d394ac521094bed7681044408ek if (phys_read_off <= phys_eof) {
06eeb2ad640ce72d394ac521094bed7681044408ek read_len = MIN(*len, phys_eof - phys_read_off);
06eeb2ad640ce72d394ac521094bed7681044408ek } else {
06eeb2ad640ce72d394ac521094bed7681044408ek read_len = MIN(*len,
06eeb2ad640ce72d394ac521094bed7681044408ek shpp->sh_phys_max_off - phys_read_off);
06eeb2ad640ce72d394ac521094bed7681044408ek if (phys_read_off + *len > shpp->sh_phys_max_off) {
06eeb2ad640ce72d394ac521094bed7681044408ek leftover = MIN(*len - read_len,
06eeb2ad640ce72d394ac521094bed7681044408ek phys_eof - shpp->sh_pool_create_len);
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek /* offset for consumer to use next */
06eeb2ad640ce72d394ac521094bed7681044408ek *offp += read_len + leftover;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek /* tell the consumer how much you actually read */
06eeb2ad640ce72d394ac521094bed7681044408ek *len = read_len + leftover;
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek if (read_len == 0) {
06eeb2ad640ce72d394ac521094bed7681044408ek mutex_exit(&spa->spa_history_lock);
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_buf_rele(dbp, FTAG);
06eeb2ad640ce72d394ac521094bed7681044408ek return (0);
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf);
06eeb2ad640ce72d394ac521094bed7681044408ek if (leftover && err == 0) {
06eeb2ad640ce72d394ac521094bed7681044408ek err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
06eeb2ad640ce72d394ac521094bed7681044408ek leftover, buf + read_len);
06eeb2ad640ce72d394ac521094bed7681044408ek }
06eeb2ad640ce72d394ac521094bed7681044408ek mutex_exit(&spa->spa_history_lock);
06eeb2ad640ce72d394ac521094bed7681044408ek
06eeb2ad640ce72d394ac521094bed7681044408ek dmu_buf_rele(dbp, FTAG);
06eeb2ad640ce72d394ac521094bed7681044408ek return (err);
06eeb2ad640ce72d394ac521094bed7681044408ek}