spa_history.c revision 088f389458728c464569a5506b58070254fa4f7d
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * CDDL HEADER START
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The contents of this file are subject to the terms of the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Common Development and Distribution License (the "License").
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * You may not use this file except in compliance with the License.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * or http://www.opensolaris.org/os/licensing.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * See the License for the specific language governing permissions
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and limitations under the License.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * When distributing Covered Code, include this CDDL HEADER in each
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * If applicable, add the following below this CDDL HEADER, with the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * information: Portions Copyright [yyyy] [name of copyright owner]
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * CDDL HEADER END
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Use is subject to license terms.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#pragma ident "%Z%%M% %I% %E% SMI"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov#include <sys/spa.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/spa_impl.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/zap.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/dsl_synctask.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/dmu_tx.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/dmu_objset.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/utsname.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/cmn_err.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/sunddi.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifdef _KERNEL
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/zone.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Routines to manage the on-disk history log.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The history log is stored as a dmu object containing
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * <packed record length, record nvlist> tuples.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * "packed record length" is the packed length of the "record nvlist" stored
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * as a little endian uint64_t.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The log is implemented as a ring buffer, though the original creation
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * of the pool ('zpool create') is never overwritten.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * of 'spa_history' stores the offsets for logging/retrieving history as
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * where the 'zpool create' record is stored. This allows us to never
efbf89fbc68a0864d303fe237fc420cf018d52f7Robert Mustacchi * overwrite the original creation of the pool. 'sh_phys_max_off' is the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * physical ending offset in bytes of the log. This tells you the length of
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * is added, 'sh_eof' is incremented by the the size of the record.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes).
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * This is where the consumer should start reading from after reading in
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * the 'zpool create' portion of the log.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * 'sh_records_lost' keeps track of how many records have been overwritten
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and permanently lost.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* convert a logical offset to physical */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic uint64_t
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowespa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe uint64_t phys_len;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return ((log_off - shpp->sh_pool_create_len) % phys_len
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe + shpp->sh_pool_create_len);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowespa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe dmu_buf_t *dbp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe spa_history_phys_t *shpp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe objset_t *mos = spa->spa_meta_objset;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ASSERT(spa->spa_history == 0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe sizeof (spa_history_phys_t), tx);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe DMU_POOL_HISTORY, sizeof (uint64_t), 1,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe &spa->spa_history, tx) == 0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe shpp = dbp->db_data;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe dmu_buf_will_dirty(dbp, tx);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Figure out maximum size of history log. We set it at
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * 1% of pool size, with a max of 32MB and min of 128KB.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe shpp->sh_phys_max_off = spa_get_dspace(spa) / 100;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe dmu_buf_rele(dbp, FTAG);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Change 'sh_bof' to the beginning of the next record.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowespa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe objset_t *mos = spa->spa_meta_objset;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe uint64_t firstread, reclen, phys_bof;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char buf[sizeof (reclen)];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int err;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov buf)) != 0)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (err);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (firstread != sizeof (reclen)) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if ((err = dmu_read(mos, spa->spa_history,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov shpp->sh_pool_create_len, sizeof (reclen) - firstread,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov buf + firstread)) != 0)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (err);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov reclen = LE_64(*((uint64_t *)buf));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov shpp->sh_bof += reclen + sizeof (reclen);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov shpp->sh_records_lost++;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovspa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov dmu_tx_t *tx)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov uint64_t firstwrite, phys_eof;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov objset_t *mos = spa->spa_meta_objset;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int err;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ASSERT(MUTEX_HELD(&spa->spa_history_lock));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* see if we need to reset logical BOF */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (shpp->sh_eof - shpp->sh_bof) <= len) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (err);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov shpp->sh_eof += len;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov len -= firstwrite;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (len > 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* write out the rest at the beginning of physical file */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov len, (char *)buf + firstwrite, tx);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic char *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovspa_history_zone()
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov#ifdef _KERNEL
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (curproc->p_zone->zone_name);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov#else
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return ("global");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov#endif
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Write out a history event.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic void
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovspa_history_log_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
efbf89fbc68a0864d303fe237fc420cf018d52f7Robert Mustacchi spa_t *spa = arg1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov history_arg_t *hap = arg2;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov const char *history_str = hap->ha_history_str;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov objset_t *mos = spa->spa_meta_objset;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov dmu_buf_t *dbp;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov spa_history_phys_t *shpp;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov size_t reclen;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov uint64_t le_len;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nvlist_t *nvrecord;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *record_packed = NULL;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int ret;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * If we have an older pool that doesn't have a command
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * history object, create it now.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov mutex_enter(&spa->spa_history_lock);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (!spa->spa_history)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov spa_history_create_obj(spa, tx);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov mutex_exit(&spa->spa_history_lock);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Get the offset of where we need to write via the bonus buffer.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Update the offset when the write completes.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov shpp = dbp->db_data;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov dmu_buf_will_dirty(dbp, tx);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov#ifdef ZFS_DEBUG
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe dmu_object_info_t doi;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe dmu_object_info_from_db(dbp, &doi);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME,
gethrestime_sec()) == 0);
VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO,
(uint64_t)crgetuid(cr)) == 0);
if (hap->ha_zone[0] != '\0')
VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_ZONE,
hap->ha_zone) == 0);
#ifdef _KERNEL
VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_HOST,
utsname.nodename) == 0);
#endif
if (hap->ha_log_type == LOG_CMD_POOL_CREATE ||
hap->ha_log_type == LOG_CMD_NORMAL) {
VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD,
history_str) == 0);
} else {
VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT,
hap->ha_event) == 0);
VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TXG,
tx->tx_txg) == 0);
VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR,
history_str) == 0);
}
VERIFY(nvlist_size(nvrecord, &reclen, NV_ENCODE_XDR) == 0);
record_packed = kmem_alloc(reclen, KM_SLEEP);
VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
NV_ENCODE_XDR, KM_SLEEP) == 0);
mutex_enter(&spa->spa_history_lock);
if (hap->ha_log_type == LOG_CMD_POOL_CREATE)
VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
/* write out the packed length as little endian */
le_len = LE_64((uint64_t)reclen);
ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
if (!ret)
ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) {
shpp->sh_pool_create_len += sizeof (le_len) + reclen;
shpp->sh_bof = shpp->sh_pool_create_len;
}
mutex_exit(&spa->spa_history_lock);
nvlist_free(nvrecord);
kmem_free(record_packed, reclen);
dmu_buf_rele(dbp, FTAG);
if (hap->ha_log_type == LOG_INTERNAL) {
kmem_free((void*)hap->ha_history_str, HIS_MAX_RECORD_LEN);
kmem_free(hap, sizeof (history_arg_t));
}
}
/*
* Write out a history event.
*/
int
spa_history_log(spa_t *spa, const char *history_str, history_log_type_t what)
{
history_arg_t ha;
ASSERT(what != LOG_INTERNAL);
ha.ha_history_str = history_str;
ha.ha_log_type = what;
(void) strlcpy(ha.ha_zone, spa_history_zone(), sizeof (ha.ha_zone));
return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync,
spa, &ha, 0));
}
/*
* Read out the command history.
*/
int
spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
{
objset_t *mos = spa->spa_meta_objset;
dmu_buf_t *dbp;
uint64_t read_len, phys_read_off, phys_eof;
uint64_t leftover = 0;
spa_history_phys_t *shpp;
int err;
/*
* If the command history doesn't exist (older pool),
* that's ok, just return ENOENT.
*/
if (!spa->spa_history)
return (ENOENT);
if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
return (err);
shpp = dbp->db_data;
#ifdef ZFS_DEBUG
{
dmu_object_info_t doi;
dmu_object_info_from_db(dbp, &doi);
ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
}
#endif
mutex_enter(&spa->spa_history_lock);
phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
if (*offp < shpp->sh_pool_create_len) {
/* read in just the zpool create history */
phys_read_off = *offp;
read_len = MIN(*len, shpp->sh_pool_create_len -
phys_read_off);
} else {
/*
* Need to reset passed in offset to BOF if the passed in
* offset has since been overwritten.
*/
*offp = MAX(*offp, shpp->sh_bof);
phys_read_off = spa_history_log_to_phys(*offp, shpp);
/*
* Read up to the minimum of what the user passed down or
* the EOF (physical or logical). If we hit physical EOF,
* use 'leftover' to read from the physical BOF.
*/
if (phys_read_off <= phys_eof) {
read_len = MIN(*len, phys_eof - phys_read_off);
} else {
read_len = MIN(*len,
shpp->sh_phys_max_off - phys_read_off);
if (phys_read_off + *len > shpp->sh_phys_max_off) {
leftover = MIN(*len - read_len,
phys_eof - shpp->sh_pool_create_len);
}
}
}
/* offset for consumer to use next */
*offp += read_len + leftover;
/* tell the consumer how much you actually read */
*len = read_len + leftover;
if (read_len == 0) {
mutex_exit(&spa->spa_history_lock);
dmu_buf_rele(dbp, FTAG);
return (0);
}
err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf);
if (leftover && err == 0) {
err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
leftover, buf + read_len);
}
mutex_exit(&spa->spa_history_lock);
dmu_buf_rele(dbp, FTAG);
return (err);
}
void
spa_history_internal_log(history_internal_events_t event, spa_t *spa,
dmu_tx_t *tx, cred_t *cr, const char *fmt, ...)
{
history_arg_t *hap;
char *str;
va_list adx;
/*
* If this is part of creating a pool, not everything is
* initialized yet, so don't bother logging the internal events.
*/
if (tx->tx_txg == TXG_INITIAL)
return;
hap = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
str = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
va_start(adx, fmt);
(void) vsnprintf(str, HIS_MAX_RECORD_LEN, fmt, adx);
va_end(adx);
hap->ha_log_type = LOG_INTERNAL;
hap->ha_history_str = str;
hap->ha_event = event;
hap->ha_zone[0] = '\0';
if (dmu_tx_is_syncing(tx)) {
spa_history_log_sync(spa, hap, cr, tx);
} else {
dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
spa_history_log_sync, spa, hap, 0, tx);
}
/* spa_history_log_sync() will free hap and str */
}