spa_history.c revision 3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5
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.
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
06eeb2ad640ce72d394ac521094bed7681044408ek * 1% of pool size, with a max of 32MB and min of 128KB.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick metaslab_class_get_dspace(spa_normal_class(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 * 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 *
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks return ("global");
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);
06eeb2ad640ce72d394ac521094bed7681044408ek VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO, hap->ha_uid) == 0);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT,
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR,
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling zfs_history_event_names[hap->ha_event], spa_name(spa),
da165920f9fa0e493db9dba43b0c3771051409dcmarks VERIFY(nvlist_size(nvrecord, &reclen, NV_ENCODE_XDR) == 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);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) {
06eeb2ad640ce72d394ac521094bed7681044408ek * Write out a history event.
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarksspa_history_log(spa_t *spa, const char *history_str, history_log_type_t what)
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens ha = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens /* Kick this off asynchronously; errors are ignored. */
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens /* spa_history_log_sync will free ha and strings */
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.
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,
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musantelog_internal(history_internal_events_t event, 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.
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens ha = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens ha->ha_history_str = kmem_alloc(vsnprintf(NULL, 0, fmt, adx) + 1,
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens (void) vsprintf(ha->ha_history_str, fmt, adx);
495807d7dceb9c0357efb5f483dbf9f951dfeec6Matthew Ahrens /* spa_history_log_sync() will free ha and strings */
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Lingspa_history_log_internal(history_internal_events_t event, spa_t *spa,
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);
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante /* if we didn't get a tx from the caller, commit the one we made */
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musantespa_history_log_version(spa_t *spa, history_internal_events_t event)
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante if (current_vers >= SPA_VERSION_ZPOOL_HISTORY) {
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante "pool spa %llu; zfs spa %llu; zpl %d; uts %s %s %s %s",
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante (u_longlong_t)current_vers, SPA_VERSION, ZPL_VERSION,
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante utsname.nodename, utsname.release, utsname.version,
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante cmn_err(CE_CONT, "!%s version %llu pool %s using %llu",
c8e1f6d2e1adb3a8cc413859aaa8c61813665ac6Mark J Musante event == LOG_POOL_CREATE ? "created" : "accessed",