spa_history.c revision 168404
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd 22168404Spjd/* 23168404Spjd * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24168404Spjd * Use is subject to license terms. 25168404Spjd */ 26168404Spjd 27168404Spjd#pragma ident "%Z%%M% %I% %E% SMI" 28168404Spjd 29168404Spjd#include <sys/spa_impl.h> 30168404Spjd#include <sys/zap.h> 31168404Spjd#include <sys/dsl_synctask.h> 32168404Spjd 33168404Spjd/* 34168404Spjd * Routines to manage the on-disk history log. 35168404Spjd * 36168404Spjd * The history log is stored as a dmu object containing 37168404Spjd * <packed record length, record nvlist> tuples. 38168404Spjd * 39168404Spjd * Where "record nvlist" is a nvlist containing uint64_ts and strings, and 40168404Spjd * "packed record length" is the packed length of the "record nvlist" stored 41168404Spjd * as a little endian uint64_t. 42168404Spjd * 43168404Spjd * The log is implemented as a ring buffer, though the original creation 44168404Spjd * of the pool ('zpool create') is never overwritten. 45168404Spjd * 46168404Spjd * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer 47168404Spjd * of 'spa_history' stores the offsets for logging/retrieving history as 48168404Spjd * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of 49168404Spjd * where the 'zpool create' record is stored. This allows us to never 50168404Spjd * overwrite the original creation of the pool. 'sh_phys_max_off' is the 51168404Spjd * physical ending offset in bytes of the log. This tells you the length of 52168404Spjd * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record 53168404Spjd * is added, 'sh_eof' is incremented by the the size of the record. 54168404Spjd * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes). 55168404Spjd * This is where the consumer should start reading from after reading in 56168404Spjd * the 'zpool create' portion of the log. 57168404Spjd * 58168404Spjd * 'sh_records_lost' keeps track of how many records have been overwritten 59168404Spjd * and permanently lost. 60168404Spjd */ 61168404Spjd 62168404Spjdtypedef enum history_log_type { 63168404Spjd LOG_CMD_CREATE, 64168404Spjd LOG_CMD_NO_CREATE 65168404Spjd} history_log_type_t; 66168404Spjd 67168404Spjdtypedef struct history_arg { 68168404Spjd const char *ha_history_str; 69168404Spjd history_log_type_t ha_log_type; 70168404Spjd} history_arg_t; 71168404Spjd 72168404Spjd/* convert a logical offset to physical */ 73168404Spjdstatic uint64_t 74168404Spjdspa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp) 75168404Spjd{ 76168404Spjd uint64_t phys_len; 77168404Spjd 78168404Spjd phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len; 79168404Spjd return ((log_off - shpp->sh_pool_create_len) % phys_len 80168404Spjd + shpp->sh_pool_create_len); 81168404Spjd} 82168404Spjd 83168404Spjdvoid 84168404Spjdspa_history_create_obj(spa_t *spa, dmu_tx_t *tx) 85168404Spjd{ 86168404Spjd dmu_buf_t *dbp; 87168404Spjd spa_history_phys_t *shpp; 88168404Spjd objset_t *mos = spa->spa_meta_objset; 89168404Spjd 90168404Spjd ASSERT(spa->spa_history == 0); 91168404Spjd spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY, 92168404Spjd SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS, 93168404Spjd sizeof (spa_history_phys_t), tx); 94168404Spjd 95168404Spjd VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 96168404Spjd DMU_POOL_HISTORY, sizeof (uint64_t), 1, 97168404Spjd &spa->spa_history, tx) == 0); 98168404Spjd 99168404Spjd VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)); 100168404Spjd ASSERT(dbp->db_size >= sizeof (spa_history_phys_t)); 101168404Spjd 102168404Spjd shpp = dbp->db_data; 103168404Spjd dmu_buf_will_dirty(dbp, tx); 104168404Spjd 105168404Spjd /* 106168404Spjd * Figure out maximum size of history log. We set it at 107168404Spjd * 1% of pool size, with a max of 32MB and min of 128KB. 108168404Spjd */ 109168404Spjd shpp->sh_phys_max_off = spa_get_dspace(spa) / 100; 110168404Spjd shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20); 111168404Spjd shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10); 112168404Spjd 113168404Spjd dmu_buf_rele(dbp, FTAG); 114168404Spjd} 115168404Spjd 116168404Spjd/* 117168404Spjd * Change 'sh_bof' to the beginning of the next record. 118168404Spjd */ 119168404Spjdstatic int 120168404Spjdspa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp) 121168404Spjd{ 122168404Spjd objset_t *mos = spa->spa_meta_objset; 123168404Spjd uint64_t firstread, reclen, phys_bof; 124168404Spjd char buf[sizeof (reclen)]; 125168404Spjd int err; 126168404Spjd 127168404Spjd phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp); 128168404Spjd firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof); 129168404Spjd 130168404Spjd if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread, 131168404Spjd buf)) != 0) 132168404Spjd return (err); 133168404Spjd if (firstread != sizeof (reclen)) { 134168404Spjd if ((err = dmu_read(mos, spa->spa_history, 135168404Spjd shpp->sh_pool_create_len, sizeof (reclen) - firstread, 136168404Spjd buf + firstread)) != 0) 137168404Spjd return (err); 138168404Spjd } 139168404Spjd 140168404Spjd reclen = LE_64(*((uint64_t *)buf)); 141168404Spjd shpp->sh_bof += reclen + sizeof (reclen); 142168404Spjd shpp->sh_records_lost++; 143168404Spjd return (0); 144168404Spjd} 145168404Spjd 146168404Spjdstatic int 147168404Spjdspa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp, 148168404Spjd dmu_tx_t *tx) 149168404Spjd{ 150168404Spjd uint64_t firstwrite, phys_eof; 151168404Spjd objset_t *mos = spa->spa_meta_objset; 152168404Spjd int err; 153168404Spjd 154168404Spjd ASSERT(MUTEX_HELD(&spa->spa_history_lock)); 155168404Spjd 156168404Spjd /* see if we need to reset logical BOF */ 157168404Spjd while (shpp->sh_phys_max_off - shpp->sh_pool_create_len - 158168404Spjd (shpp->sh_eof - shpp->sh_bof) <= len) { 159168404Spjd if ((err = spa_history_advance_bof(spa, shpp)) != 0) 160168404Spjd return (err); 161168404Spjd } 162168404Spjd 163168404Spjd phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp); 164168404Spjd firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof); 165168404Spjd shpp->sh_eof += len; 166168404Spjd dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx); 167168404Spjd 168168404Spjd len -= firstwrite; 169168404Spjd if (len > 0) { 170168404Spjd /* write out the rest at the beginning of physical file */ 171168404Spjd dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len, 172168404Spjd len, (char *)buf + firstwrite, tx); 173168404Spjd } 174168404Spjd 175168404Spjd return (0); 176168404Spjd} 177168404Spjd 178168404Spjd/* 179168404Spjd * Write out a history event. 180168404Spjd */ 181168404Spjdvoid 182168404Spjdspa_history_log_sync(void *arg1, void *arg2, dmu_tx_t *tx) 183168404Spjd{ 184168404Spjd spa_t *spa = arg1; 185168404Spjd history_arg_t *hap = arg2; 186168404Spjd const char *history_str = hap->ha_history_str; 187168404Spjd objset_t *mos = spa->spa_meta_objset; 188168404Spjd dmu_buf_t *dbp; 189168404Spjd spa_history_phys_t *shpp; 190168404Spjd size_t reclen; 191168404Spjd uint64_t le_len; 192168404Spjd nvlist_t *nvrecord; 193168404Spjd char *record_packed = NULL; 194168404Spjd int ret; 195168404Spjd 196168404Spjd if (history_str == NULL) 197168404Spjd return; 198168404Spjd 199168404Spjd /* 200168404Spjd * If we have an older pool that doesn't have a command 201168404Spjd * history object, create it now. 202168404Spjd */ 203168404Spjd mutex_enter(&spa->spa_history_lock); 204168404Spjd if (!spa->spa_history) 205168404Spjd spa_history_create_obj(spa, tx); 206168404Spjd mutex_exit(&spa->spa_history_lock); 207168404Spjd 208168404Spjd /* 209168404Spjd * Get the offset of where we need to write via the bonus buffer. 210168404Spjd * Update the offset when the write completes. 211168404Spjd */ 212168404Spjd VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)); 213168404Spjd shpp = dbp->db_data; 214168404Spjd 215168404Spjd dmu_buf_will_dirty(dbp, tx); 216168404Spjd 217168404Spjd#ifdef ZFS_DEBUG 218168404Spjd { 219168404Spjd dmu_object_info_t doi; 220168404Spjd dmu_object_info_from_db(dbp, &doi); 221168404Spjd ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS); 222168404Spjd } 223168404Spjd#endif 224168404Spjd 225168404Spjd /* construct a nvlist of the current time and cmd string */ 226168404Spjd VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0); 227168404Spjd VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME, 228168404Spjd gethrestime_sec()) == 0); 229168404Spjd VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD, history_str) == 0); 230168404Spjd VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen, 231168404Spjd NV_ENCODE_XDR, KM_SLEEP) == 0); 232168404Spjd 233168404Spjd mutex_enter(&spa->spa_history_lock); 234168404Spjd if (hap->ha_log_type == LOG_CMD_CREATE) 235168404Spjd VERIFY(shpp->sh_eof == shpp->sh_pool_create_len); 236168404Spjd 237168404Spjd /* write out the packed length as little endian */ 238168404Spjd le_len = LE_64((uint64_t)reclen); 239168404Spjd ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx); 240168404Spjd if (!ret) 241168404Spjd ret = spa_history_write(spa, record_packed, reclen, shpp, tx); 242168404Spjd 243168404Spjd if (!ret && hap->ha_log_type == LOG_CMD_CREATE) { 244168404Spjd shpp->sh_pool_create_len += sizeof (le_len) + reclen; 245168404Spjd shpp->sh_bof = shpp->sh_pool_create_len; 246168404Spjd } 247168404Spjd 248168404Spjd mutex_exit(&spa->spa_history_lock); 249168404Spjd nvlist_free(nvrecord); 250168404Spjd kmem_free(record_packed, reclen); 251168404Spjd dmu_buf_rele(dbp, FTAG); 252168404Spjd} 253168404Spjd 254168404Spjd/* 255168404Spjd * Write out a history event. 256168404Spjd */ 257168404Spjdint 258168404Spjdspa_history_log(spa_t *spa, const char *history_str, uint64_t pool_create) 259168404Spjd{ 260168404Spjd history_arg_t ha; 261168404Spjd 262168404Spjd ha.ha_history_str = history_str; 263168404Spjd ha.ha_log_type = pool_create ? LOG_CMD_CREATE : LOG_CMD_NO_CREATE; 264168404Spjd return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync, 265168404Spjd spa, &ha, 0)); 266168404Spjd} 267168404Spjd 268168404Spjd/* 269168404Spjd * Read out the command history. 270168404Spjd */ 271168404Spjdint 272168404Spjdspa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf) 273168404Spjd{ 274168404Spjd objset_t *mos = spa->spa_meta_objset; 275168404Spjd dmu_buf_t *dbp; 276168404Spjd uint64_t read_len, phys_read_off, phys_eof; 277168404Spjd uint64_t leftover = 0; 278168404Spjd spa_history_phys_t *shpp; 279168404Spjd int err; 280168404Spjd 281168404Spjd /* 282168404Spjd * If the command history doesn't exist (older pool), 283168404Spjd * that's ok, just return ENOENT. 284168404Spjd */ 285168404Spjd if (!spa->spa_history) 286168404Spjd return (ENOENT); 287168404Spjd 288168404Spjd if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0) 289168404Spjd return (err); 290168404Spjd shpp = dbp->db_data; 291168404Spjd 292168404Spjd#ifdef ZFS_DEBUG 293168404Spjd { 294168404Spjd dmu_object_info_t doi; 295168404Spjd dmu_object_info_from_db(dbp, &doi); 296168404Spjd ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS); 297168404Spjd } 298168404Spjd#endif 299168404Spjd 300168404Spjd mutex_enter(&spa->spa_history_lock); 301168404Spjd phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp); 302168404Spjd 303168404Spjd if (*offp < shpp->sh_pool_create_len) { 304168404Spjd /* read in just the zpool create history */ 305168404Spjd phys_read_off = *offp; 306168404Spjd read_len = MIN(*len, shpp->sh_pool_create_len - 307168404Spjd phys_read_off); 308168404Spjd } else { 309168404Spjd /* 310168404Spjd * Need to reset passed in offset to BOF if the passed in 311168404Spjd * offset has since been overwritten. 312168404Spjd */ 313168404Spjd *offp = MAX(*offp, shpp->sh_bof); 314168404Spjd phys_read_off = spa_history_log_to_phys(*offp, shpp); 315168404Spjd 316168404Spjd /* 317168404Spjd * Read up to the minimum of what the user passed down or 318168404Spjd * the EOF (physical or logical). If we hit physical EOF, 319168404Spjd * use 'leftover' to read from the physical BOF. 320168404Spjd */ 321168404Spjd if (phys_read_off <= phys_eof) { 322168404Spjd read_len = MIN(*len, phys_eof - phys_read_off); 323168404Spjd } else { 324168404Spjd read_len = MIN(*len, 325168404Spjd shpp->sh_phys_max_off - phys_read_off); 326168404Spjd if (phys_read_off + *len > shpp->sh_phys_max_off) { 327168404Spjd leftover = MIN(*len - read_len, 328168404Spjd phys_eof - shpp->sh_pool_create_len); 329168404Spjd } 330168404Spjd } 331168404Spjd } 332168404Spjd 333168404Spjd /* offset for consumer to use next */ 334168404Spjd *offp += read_len + leftover; 335168404Spjd 336168404Spjd /* tell the consumer how much you actually read */ 337168404Spjd *len = read_len + leftover; 338168404Spjd 339168404Spjd if (read_len == 0) { 340168404Spjd mutex_exit(&spa->spa_history_lock); 341168404Spjd dmu_buf_rele(dbp, FTAG); 342168404Spjd return (0); 343168404Spjd } 344168404Spjd 345168404Spjd err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf); 346168404Spjd if (leftover && err == 0) { 347168404Spjd err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len, 348168404Spjd leftover, buf + read_len); 349168404Spjd } 350168404Spjd mutex_exit(&spa->spa_history_lock); 351168404Spjd 352168404Spjd dmu_buf_rele(dbp, FTAG); 353168404Spjd return (err); 354168404Spjd} 355