106eeb2adSek /* 206eeb2adSek * CDDL HEADER START 306eeb2adSek * 406eeb2adSek * The contents of this file are subject to the terms of the 506eeb2adSek * Common Development and Distribution License (the "License"). 606eeb2adSek * You may not use this file except in compliance with the License. 706eeb2adSek * 806eeb2adSek * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 906eeb2adSek * or http://www.opensolaris.org/os/licensing. 1006eeb2adSek * See the License for the specific language governing permissions 1106eeb2adSek * and limitations under the License. 1206eeb2adSek * 1306eeb2adSek * When distributing Covered Code, include this CDDL HEADER in each 1406eeb2adSek * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1506eeb2adSek * If applicable, add the following below this CDDL HEADER, with the 1606eeb2adSek * fields enclosed by brackets "[]" replaced with your own identifying 1706eeb2adSek * information: Portions Copyright [yyyy] [name of copyright owner] 1806eeb2adSek * 1906eeb2adSek * CDDL HEADER END 2006eeb2adSek */ 2106eeb2adSek 2206eeb2adSek /* 2355434c77Sek * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 2406eeb2adSek * Use is subject to license terms. 2506eeb2adSek */ 2606eeb2adSek 2706eeb2adSek #pragma ident "%Z%%M% %I% %E% SMI" 2806eeb2adSek 29ecd6cf80Smarks #include <sys/spa.h> 3006eeb2adSek #include <sys/spa_impl.h> 3106eeb2adSek #include <sys/zap.h> 3206eeb2adSek #include <sys/dsl_synctask.h> 33ecd6cf80Smarks #include <sys/dmu_tx.h> 34ecd6cf80Smarks #include <sys/dmu_objset.h> 35ecd6cf80Smarks #include <sys/utsname.h> 36ecd6cf80Smarks #include <sys/cmn_err.h> 37ecd6cf80Smarks #include <sys/sunddi.h> 38ecd6cf80Smarks #ifdef _KERNEL 39ecd6cf80Smarks #include <sys/zone.h> 40ecd6cf80Smarks #endif 4106eeb2adSek 4206eeb2adSek /* 4306eeb2adSek * Routines to manage the on-disk history log. 4406eeb2adSek * 4506eeb2adSek * The history log is stored as a dmu object containing 4606eeb2adSek * <packed record length, record nvlist> tuples. 4706eeb2adSek * 4806eeb2adSek * Where "record nvlist" is a nvlist containing uint64_ts and strings, and 4906eeb2adSek * "packed record length" is the packed length of the "record nvlist" stored 5006eeb2adSek * as a little endian uint64_t. 5106eeb2adSek * 5206eeb2adSek * The log is implemented as a ring buffer, though the original creation 5306eeb2adSek * of the pool ('zpool create') is never overwritten. 5406eeb2adSek * 5506eeb2adSek * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer 5606eeb2adSek * of 'spa_history' stores the offsets for logging/retrieving history as 5706eeb2adSek * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of 5806eeb2adSek * where the 'zpool create' record is stored. This allows us to never 5906eeb2adSek * overwrite the original creation of the pool. 'sh_phys_max_off' is the 6006eeb2adSek * physical ending offset in bytes of the log. This tells you the length of 6106eeb2adSek * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record 6206eeb2adSek * is added, 'sh_eof' is incremented by the the size of the record. 6306eeb2adSek * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes). 6406eeb2adSek * This is where the consumer should start reading from after reading in 6506eeb2adSek * the 'zpool create' portion of the log. 6606eeb2adSek * 6706eeb2adSek * 'sh_records_lost' keeps track of how many records have been overwritten 6806eeb2adSek * and permanently lost. 6906eeb2adSek */ 7006eeb2adSek 7106eeb2adSek /* convert a logical offset to physical */ 7206eeb2adSek static uint64_t 7306eeb2adSek spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp) 7406eeb2adSek { 7506eeb2adSek uint64_t phys_len; 7606eeb2adSek 7706eeb2adSek phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len; 7806eeb2adSek return ((log_off - shpp->sh_pool_create_len) % phys_len 7906eeb2adSek + shpp->sh_pool_create_len); 8006eeb2adSek } 8106eeb2adSek 8206eeb2adSek void 8306eeb2adSek spa_history_create_obj(spa_t *spa, dmu_tx_t *tx) 8406eeb2adSek { 8506eeb2adSek dmu_buf_t *dbp; 8606eeb2adSek spa_history_phys_t *shpp; 8706eeb2adSek objset_t *mos = spa->spa_meta_objset; 8806eeb2adSek 8906eeb2adSek ASSERT(spa->spa_history == 0); 9006eeb2adSek spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY, 9106eeb2adSek SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS, 9206eeb2adSek sizeof (spa_history_phys_t), tx); 9306eeb2adSek 9406eeb2adSek VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 9506eeb2adSek DMU_POOL_HISTORY, sizeof (uint64_t), 1, 9606eeb2adSek &spa->spa_history, tx) == 0); 9706eeb2adSek 9806eeb2adSek VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)); 9906eeb2adSek ASSERT(dbp->db_size >= sizeof (spa_history_phys_t)); 10006eeb2adSek 10106eeb2adSek shpp = dbp->db_data; 10206eeb2adSek dmu_buf_will_dirty(dbp, tx); 10306eeb2adSek 10406eeb2adSek /* 10506eeb2adSek * Figure out maximum size of history log. We set it at 10606eeb2adSek * 1% of pool size, with a max of 32MB and min of 128KB. 10706eeb2adSek */ 10806eeb2adSek shpp->sh_phys_max_off = spa_get_dspace(spa) / 100; 10906eeb2adSek shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20); 11006eeb2adSek shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10); 11106eeb2adSek 11206eeb2adSek dmu_buf_rele(dbp, FTAG); 11306eeb2adSek } 11406eeb2adSek 11506eeb2adSek /* 11606eeb2adSek * Change 'sh_bof' to the beginning of the next record. 11706eeb2adSek */ 11806eeb2adSek static int 11906eeb2adSek spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp) 12006eeb2adSek { 12106eeb2adSek objset_t *mos = spa->spa_meta_objset; 12206eeb2adSek uint64_t firstread, reclen, phys_bof; 12306eeb2adSek char buf[sizeof (reclen)]; 12406eeb2adSek int err; 12506eeb2adSek 12606eeb2adSek phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp); 12706eeb2adSek firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof); 12806eeb2adSek 12906eeb2adSek if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread, 13006eeb2adSek buf)) != 0) 13106eeb2adSek return (err); 13206eeb2adSek if (firstread != sizeof (reclen)) { 13306eeb2adSek if ((err = dmu_read(mos, spa->spa_history, 13406eeb2adSek shpp->sh_pool_create_len, sizeof (reclen) - firstread, 13506eeb2adSek buf + firstread)) != 0) 13606eeb2adSek return (err); 13706eeb2adSek } 13806eeb2adSek 13906eeb2adSek reclen = LE_64(*((uint64_t *)buf)); 14006eeb2adSek shpp->sh_bof += reclen + sizeof (reclen); 14106eeb2adSek shpp->sh_records_lost++; 14206eeb2adSek return (0); 14306eeb2adSek } 14406eeb2adSek 14506eeb2adSek static int 14606eeb2adSek spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp, 14706eeb2adSek dmu_tx_t *tx) 14806eeb2adSek { 14906eeb2adSek uint64_t firstwrite, phys_eof; 15006eeb2adSek objset_t *mos = spa->spa_meta_objset; 15106eeb2adSek int err; 15206eeb2adSek 15306eeb2adSek ASSERT(MUTEX_HELD(&spa->spa_history_lock)); 15406eeb2adSek 15506eeb2adSek /* see if we need to reset logical BOF */ 15606eeb2adSek while (shpp->sh_phys_max_off - shpp->sh_pool_create_len - 15706eeb2adSek (shpp->sh_eof - shpp->sh_bof) <= len) { 158ecd6cf80Smarks if ((err = spa_history_advance_bof(spa, shpp)) != 0) { 15906eeb2adSek return (err); 160ecd6cf80Smarks } 16106eeb2adSek } 16206eeb2adSek 16306eeb2adSek phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp); 16406eeb2adSek firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof); 16506eeb2adSek shpp->sh_eof += len; 16606eeb2adSek dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx); 16706eeb2adSek 16806eeb2adSek len -= firstwrite; 16906eeb2adSek if (len > 0) { 17006eeb2adSek /* write out the rest at the beginning of physical file */ 17106eeb2adSek dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len, 17206eeb2adSek len, (char *)buf + firstwrite, tx); 17306eeb2adSek } 17406eeb2adSek 17506eeb2adSek return (0); 17606eeb2adSek } 17706eeb2adSek 178ecd6cf80Smarks static char * 179ecd6cf80Smarks spa_history_zone() 180ecd6cf80Smarks { 181ecd6cf80Smarks #ifdef _KERNEL 182ecd6cf80Smarks return (curproc->p_zone->zone_name); 183ecd6cf80Smarks #else 184ecd6cf80Smarks return ("global"); 185ecd6cf80Smarks #endif 186ecd6cf80Smarks } 187ecd6cf80Smarks 18806eeb2adSek /* 18906eeb2adSek * Write out a history event. 19006eeb2adSek */ 191*e7437265Sahrens static void 192ecd6cf80Smarks spa_history_log_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 19306eeb2adSek { 19406eeb2adSek spa_t *spa = arg1; 19506eeb2adSek history_arg_t *hap = arg2; 19606eeb2adSek const char *history_str = hap->ha_history_str; 19706eeb2adSek objset_t *mos = spa->spa_meta_objset; 19806eeb2adSek dmu_buf_t *dbp; 19906eeb2adSek spa_history_phys_t *shpp; 20006eeb2adSek size_t reclen; 20106eeb2adSek uint64_t le_len; 20206eeb2adSek nvlist_t *nvrecord; 20306eeb2adSek char *record_packed = NULL; 20406eeb2adSek int ret; 20506eeb2adSek 20606eeb2adSek /* 20706eeb2adSek * If we have an older pool that doesn't have a command 20806eeb2adSek * history object, create it now. 20906eeb2adSek */ 21006eeb2adSek mutex_enter(&spa->spa_history_lock); 21106eeb2adSek if (!spa->spa_history) 21206eeb2adSek spa_history_create_obj(spa, tx); 21306eeb2adSek mutex_exit(&spa->spa_history_lock); 21406eeb2adSek 21506eeb2adSek /* 21606eeb2adSek * Get the offset of where we need to write via the bonus buffer. 21706eeb2adSek * Update the offset when the write completes. 21806eeb2adSek */ 21906eeb2adSek VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)); 22006eeb2adSek shpp = dbp->db_data; 22106eeb2adSek 22206eeb2adSek dmu_buf_will_dirty(dbp, tx); 22306eeb2adSek 22406eeb2adSek #ifdef ZFS_DEBUG 22506eeb2adSek { 22606eeb2adSek dmu_object_info_t doi; 22706eeb2adSek dmu_object_info_from_db(dbp, &doi); 22806eeb2adSek ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS); 22906eeb2adSek } 23006eeb2adSek #endif 23106eeb2adSek 23206eeb2adSek VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0); 23306eeb2adSek VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME, 23406eeb2adSek gethrestime_sec()) == 0); 235ecd6cf80Smarks VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO, 236ecd6cf80Smarks (uint64_t)crgetuid(cr)) == 0); 237ecd6cf80Smarks if (hap->ha_zone[0] != '\0') 238ecd6cf80Smarks VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_ZONE, 239ecd6cf80Smarks hap->ha_zone) == 0); 240ecd6cf80Smarks #ifdef _KERNEL 241ecd6cf80Smarks VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_HOST, 242ecd6cf80Smarks utsname.nodename) == 0); 243ecd6cf80Smarks #endif 244ecd6cf80Smarks if (hap->ha_log_type == LOG_CMD_POOL_CREATE || 245ecd6cf80Smarks hap->ha_log_type == LOG_CMD_NORMAL) { 246ecd6cf80Smarks VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD, 247ecd6cf80Smarks history_str) == 0); 248ecd6cf80Smarks } else { 249ecd6cf80Smarks VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT, 250ecd6cf80Smarks hap->ha_event) == 0); 251ecd6cf80Smarks VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TXG, 252ecd6cf80Smarks tx->tx_txg) == 0); 253ecd6cf80Smarks VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR, 254ecd6cf80Smarks history_str) == 0); 255ecd6cf80Smarks } 256ecd6cf80Smarks 25706eeb2adSek VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen, 25806eeb2adSek NV_ENCODE_XDR, KM_SLEEP) == 0); 25906eeb2adSek 26006eeb2adSek mutex_enter(&spa->spa_history_lock); 261ecd6cf80Smarks if (hap->ha_log_type == LOG_CMD_POOL_CREATE) 26206eeb2adSek VERIFY(shpp->sh_eof == shpp->sh_pool_create_len); 26306eeb2adSek 26406eeb2adSek /* write out the packed length as little endian */ 26555434c77Sek le_len = LE_64((uint64_t)reclen); 26606eeb2adSek ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx); 26706eeb2adSek if (!ret) 26806eeb2adSek ret = spa_history_write(spa, record_packed, reclen, shpp, tx); 26906eeb2adSek 270ecd6cf80Smarks if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) { 27106eeb2adSek shpp->sh_pool_create_len += sizeof (le_len) + reclen; 27206eeb2adSek shpp->sh_bof = shpp->sh_pool_create_len; 27306eeb2adSek } 27406eeb2adSek 27506eeb2adSek mutex_exit(&spa->spa_history_lock); 27606eeb2adSek nvlist_free(nvrecord); 27706eeb2adSek kmem_free(record_packed, reclen); 27806eeb2adSek dmu_buf_rele(dbp, FTAG); 279*e7437265Sahrens 280*e7437265Sahrens if (hap->ha_log_type == LOG_INTERNAL) { 281*e7437265Sahrens kmem_free((void*)hap->ha_history_str, HIS_MAX_RECORD_LEN); 282*e7437265Sahrens kmem_free(hap, sizeof (history_arg_t)); 283*e7437265Sahrens } 28406eeb2adSek } 28506eeb2adSek 28606eeb2adSek /* 28706eeb2adSek * Write out a history event. 28806eeb2adSek */ 28906eeb2adSek int 290ecd6cf80Smarks spa_history_log(spa_t *spa, const char *history_str, history_log_type_t what) 29106eeb2adSek { 29206eeb2adSek history_arg_t ha; 29306eeb2adSek 294*e7437265Sahrens ASSERT(what != LOG_INTERNAL); 295*e7437265Sahrens 29606eeb2adSek ha.ha_history_str = history_str; 297ecd6cf80Smarks ha.ha_log_type = what; 298ecd6cf80Smarks (void) strlcpy(ha.ha_zone, spa_history_zone(), sizeof (ha.ha_zone)); 29906eeb2adSek return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync, 30006eeb2adSek spa, &ha, 0)); 30106eeb2adSek } 30206eeb2adSek 30306eeb2adSek /* 30406eeb2adSek * Read out the command history. 30506eeb2adSek */ 30606eeb2adSek int 30706eeb2adSek spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf) 30806eeb2adSek { 30906eeb2adSek objset_t *mos = spa->spa_meta_objset; 31006eeb2adSek dmu_buf_t *dbp; 31106eeb2adSek uint64_t read_len, phys_read_off, phys_eof; 31206eeb2adSek uint64_t leftover = 0; 31306eeb2adSek spa_history_phys_t *shpp; 31406eeb2adSek int err; 31506eeb2adSek 31606eeb2adSek /* 31706eeb2adSek * If the command history doesn't exist (older pool), 31806eeb2adSek * that's ok, just return ENOENT. 31906eeb2adSek */ 32006eeb2adSek if (!spa->spa_history) 32106eeb2adSek return (ENOENT); 32206eeb2adSek 32306eeb2adSek if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0) 32406eeb2adSek return (err); 32506eeb2adSek shpp = dbp->db_data; 32606eeb2adSek 32706eeb2adSek #ifdef ZFS_DEBUG 32806eeb2adSek { 32906eeb2adSek dmu_object_info_t doi; 33006eeb2adSek dmu_object_info_from_db(dbp, &doi); 33106eeb2adSek ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS); 33206eeb2adSek } 33306eeb2adSek #endif 33406eeb2adSek 33506eeb2adSek mutex_enter(&spa->spa_history_lock); 33606eeb2adSek phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp); 33706eeb2adSek 33806eeb2adSek if (*offp < shpp->sh_pool_create_len) { 33906eeb2adSek /* read in just the zpool create history */ 34006eeb2adSek phys_read_off = *offp; 34106eeb2adSek read_len = MIN(*len, shpp->sh_pool_create_len - 34206eeb2adSek phys_read_off); 34306eeb2adSek } else { 34406eeb2adSek /* 34506eeb2adSek * Need to reset passed in offset to BOF if the passed in 34606eeb2adSek * offset has since been overwritten. 34706eeb2adSek */ 34806eeb2adSek *offp = MAX(*offp, shpp->sh_bof); 34906eeb2adSek phys_read_off = spa_history_log_to_phys(*offp, shpp); 35006eeb2adSek 35106eeb2adSek /* 35206eeb2adSek * Read up to the minimum of what the user passed down or 35306eeb2adSek * the EOF (physical or logical). If we hit physical EOF, 35406eeb2adSek * use 'leftover' to read from the physical BOF. 35506eeb2adSek */ 35606eeb2adSek if (phys_read_off <= phys_eof) { 35706eeb2adSek read_len = MIN(*len, phys_eof - phys_read_off); 35806eeb2adSek } else { 35906eeb2adSek read_len = MIN(*len, 36006eeb2adSek shpp->sh_phys_max_off - phys_read_off); 36106eeb2adSek if (phys_read_off + *len > shpp->sh_phys_max_off) { 36206eeb2adSek leftover = MIN(*len - read_len, 36306eeb2adSek phys_eof - shpp->sh_pool_create_len); 36406eeb2adSek } 36506eeb2adSek } 36606eeb2adSek } 36706eeb2adSek 36806eeb2adSek /* offset for consumer to use next */ 36906eeb2adSek *offp += read_len + leftover; 37006eeb2adSek 37106eeb2adSek /* tell the consumer how much you actually read */ 37206eeb2adSek *len = read_len + leftover; 37306eeb2adSek 37406eeb2adSek if (read_len == 0) { 37506eeb2adSek mutex_exit(&spa->spa_history_lock); 37606eeb2adSek dmu_buf_rele(dbp, FTAG); 37706eeb2adSek return (0); 37806eeb2adSek } 37906eeb2adSek 38006eeb2adSek err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf); 38106eeb2adSek if (leftover && err == 0) { 38206eeb2adSek err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len, 38306eeb2adSek leftover, buf + read_len); 38406eeb2adSek } 38506eeb2adSek mutex_exit(&spa->spa_history_lock); 38606eeb2adSek 38706eeb2adSek dmu_buf_rele(dbp, FTAG); 38806eeb2adSek return (err); 38906eeb2adSek } 390ecd6cf80Smarks 391ecd6cf80Smarks void 392ecd6cf80Smarks spa_history_internal_log(history_internal_events_t event, spa_t *spa, 393ecd6cf80Smarks dmu_tx_t *tx, cred_t *cr, const char *fmt, ...) 394ecd6cf80Smarks { 395*e7437265Sahrens history_arg_t *hap; 396ecd6cf80Smarks char *str; 397ecd6cf80Smarks va_list adx; 398ecd6cf80Smarks 399*e7437265Sahrens hap = kmem_alloc(sizeof (history_arg_t), KM_SLEEP); 400ecd6cf80Smarks str = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 401ecd6cf80Smarks 402ecd6cf80Smarks va_start(adx, fmt); 403ecd6cf80Smarks (void) vsnprintf(str, HIS_MAX_RECORD_LEN, fmt, adx); 404ecd6cf80Smarks va_end(adx); 405ecd6cf80Smarks 406*e7437265Sahrens hap->ha_log_type = LOG_INTERNAL; 407*e7437265Sahrens hap->ha_history_str = str; 408*e7437265Sahrens hap->ha_event = event; 409*e7437265Sahrens hap->ha_zone[0] = '\0'; 410*e7437265Sahrens 411*e7437265Sahrens if (dmu_tx_is_syncing(tx)) { 412*e7437265Sahrens spa_history_log_sync(spa, hap, cr, tx); 413*e7437265Sahrens } else { 414*e7437265Sahrens dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL, 415*e7437265Sahrens spa_history_log_sync, spa, hap, 0, tx); 416*e7437265Sahrens } 417*e7437265Sahrens /* spa_history_log_sync() will free hap and str */ 418ecd6cf80Smarks } 419