xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_history.c (revision 7bfdf011e081684f853a3242d0296695110d9d84)
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 /*
23*7bfdf011SNeil Perrin  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2406eeb2adSek  * Use is subject to license terms.
2506eeb2adSek  */
2606eeb2adSek 
27ecd6cf80Smarks #include <sys/spa.h>
2806eeb2adSek #include <sys/spa_impl.h>
2906eeb2adSek #include <sys/zap.h>
3006eeb2adSek #include <sys/dsl_synctask.h>
31ecd6cf80Smarks #include <sys/dmu_tx.h>
32ecd6cf80Smarks #include <sys/dmu_objset.h>
33ecd6cf80Smarks #include <sys/utsname.h>
34ecd6cf80Smarks #include <sys/cmn_err.h>
35ecd6cf80Smarks #include <sys/sunddi.h>
36ecd6cf80Smarks #ifdef _KERNEL
37ecd6cf80Smarks #include <sys/zone.h>
38ecd6cf80Smarks #endif
3906eeb2adSek 
4006eeb2adSek /*
4106eeb2adSek  * Routines to manage the on-disk history log.
4206eeb2adSek  *
4306eeb2adSek  * The history log is stored as a dmu object containing
4406eeb2adSek  * <packed record length, record nvlist> tuples.
4506eeb2adSek  *
4606eeb2adSek  * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
4706eeb2adSek  * "packed record length" is the packed length of the "record nvlist" stored
4806eeb2adSek  * as a little endian uint64_t.
4906eeb2adSek  *
5006eeb2adSek  * The log is implemented as a ring buffer, though the original creation
5106eeb2adSek  * of the pool ('zpool create') is never overwritten.
5206eeb2adSek  *
5306eeb2adSek  * The history log is tracked as object 'spa_t::spa_history'.  The bonus buffer
5406eeb2adSek  * of 'spa_history' stores the offsets for logging/retrieving history as
5506eeb2adSek  * 'spa_history_phys_t'.  'sh_pool_create_len' is the ending offset in bytes of
5606eeb2adSek  * where the 'zpool create' record is stored.  This allows us to never
5706eeb2adSek  * overwrite the original creation of the pool.  'sh_phys_max_off' is the
5806eeb2adSek  * physical ending offset in bytes of the log.  This tells you the length of
5906eeb2adSek  * the buffer. 'sh_eof' is the logical EOF (in bytes).  Whenever a record
6006eeb2adSek  * is added, 'sh_eof' is incremented by the the size of the record.
6106eeb2adSek  * 'sh_eof' is never decremented.  'sh_bof' is the logical BOF (in bytes).
6206eeb2adSek  * This is where the consumer should start reading from after reading in
6306eeb2adSek  * the 'zpool create' portion of the log.
6406eeb2adSek  *
6506eeb2adSek  * 'sh_records_lost' keeps track of how many records have been overwritten
6606eeb2adSek  * and permanently lost.
6706eeb2adSek  */
6806eeb2adSek 
6906eeb2adSek /* convert a logical offset to physical */
7006eeb2adSek static uint64_t
7106eeb2adSek spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
7206eeb2adSek {
7306eeb2adSek 	uint64_t phys_len;
7406eeb2adSek 
7506eeb2adSek 	phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
7606eeb2adSek 	return ((log_off - shpp->sh_pool_create_len) % phys_len
7706eeb2adSek 	    + shpp->sh_pool_create_len);
7806eeb2adSek }
7906eeb2adSek 
8006eeb2adSek void
8106eeb2adSek spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
8206eeb2adSek {
8306eeb2adSek 	dmu_buf_t *dbp;
8406eeb2adSek 	spa_history_phys_t *shpp;
8506eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
8606eeb2adSek 
8706eeb2adSek 	ASSERT(spa->spa_history == 0);
8806eeb2adSek 	spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
8906eeb2adSek 	    SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
9006eeb2adSek 	    sizeof (spa_history_phys_t), tx);
9106eeb2adSek 
9206eeb2adSek 	VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
9306eeb2adSek 	    DMU_POOL_HISTORY, sizeof (uint64_t), 1,
9406eeb2adSek 	    &spa->spa_history, tx) == 0);
9506eeb2adSek 
9606eeb2adSek 	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
9706eeb2adSek 	ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
9806eeb2adSek 
9906eeb2adSek 	shpp = dbp->db_data;
10006eeb2adSek 	dmu_buf_will_dirty(dbp, tx);
10106eeb2adSek 
10206eeb2adSek 	/*
10306eeb2adSek 	 * Figure out maximum size of history log.  We set it at
10406eeb2adSek 	 * 1% of pool size, with a max of 32MB and min of 128KB.
10506eeb2adSek 	 */
10606eeb2adSek 	shpp->sh_phys_max_off = spa_get_dspace(spa) / 100;
10706eeb2adSek 	shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20);
10806eeb2adSek 	shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
10906eeb2adSek 
11006eeb2adSek 	dmu_buf_rele(dbp, FTAG);
11106eeb2adSek }
11206eeb2adSek 
11306eeb2adSek /*
11406eeb2adSek  * Change 'sh_bof' to the beginning of the next record.
11506eeb2adSek  */
11606eeb2adSek static int
11706eeb2adSek spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
11806eeb2adSek {
11906eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
12006eeb2adSek 	uint64_t firstread, reclen, phys_bof;
12106eeb2adSek 	char buf[sizeof (reclen)];
12206eeb2adSek 	int err;
12306eeb2adSek 
12406eeb2adSek 	phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
12506eeb2adSek 	firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
12606eeb2adSek 
12706eeb2adSek 	if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
128*7bfdf011SNeil Perrin 	    buf, DMU_READ_PREFETCH)) != 0)
12906eeb2adSek 		return (err);
13006eeb2adSek 	if (firstread != sizeof (reclen)) {
13106eeb2adSek 		if ((err = dmu_read(mos, spa->spa_history,
13206eeb2adSek 		    shpp->sh_pool_create_len, sizeof (reclen) - firstread,
133*7bfdf011SNeil Perrin 		    buf + firstread, DMU_READ_PREFETCH)) != 0)
13406eeb2adSek 			return (err);
13506eeb2adSek 	}
13606eeb2adSek 
13706eeb2adSek 	reclen = LE_64(*((uint64_t *)buf));
13806eeb2adSek 	shpp->sh_bof += reclen + sizeof (reclen);
13906eeb2adSek 	shpp->sh_records_lost++;
14006eeb2adSek 	return (0);
14106eeb2adSek }
14206eeb2adSek 
14306eeb2adSek static int
14406eeb2adSek spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
14506eeb2adSek     dmu_tx_t *tx)
14606eeb2adSek {
14706eeb2adSek 	uint64_t firstwrite, phys_eof;
14806eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
14906eeb2adSek 	int err;
15006eeb2adSek 
15106eeb2adSek 	ASSERT(MUTEX_HELD(&spa->spa_history_lock));
15206eeb2adSek 
15306eeb2adSek 	/* see if we need to reset logical BOF */
15406eeb2adSek 	while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
15506eeb2adSek 	    (shpp->sh_eof - shpp->sh_bof) <= len) {
156ecd6cf80Smarks 		if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
15706eeb2adSek 			return (err);
158ecd6cf80Smarks 		}
15906eeb2adSek 	}
16006eeb2adSek 
16106eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
16206eeb2adSek 	firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
16306eeb2adSek 	shpp->sh_eof += len;
16406eeb2adSek 	dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
16506eeb2adSek 
16606eeb2adSek 	len -= firstwrite;
16706eeb2adSek 	if (len > 0) {
16806eeb2adSek 		/* write out the rest at the beginning of physical file */
16906eeb2adSek 		dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
17006eeb2adSek 		    len, (char *)buf + firstwrite, tx);
17106eeb2adSek 	}
17206eeb2adSek 
17306eeb2adSek 	return (0);
17406eeb2adSek }
17506eeb2adSek 
176ecd6cf80Smarks static char *
177ecd6cf80Smarks spa_history_zone()
178ecd6cf80Smarks {
179ecd6cf80Smarks #ifdef _KERNEL
180ecd6cf80Smarks 	return (curproc->p_zone->zone_name);
181ecd6cf80Smarks #else
182ecd6cf80Smarks 	return ("global");
183ecd6cf80Smarks #endif
184ecd6cf80Smarks }
185ecd6cf80Smarks 
18606eeb2adSek /*
18706eeb2adSek  * Write out a history event.
18806eeb2adSek  */
189e7437265Sahrens static void
190ecd6cf80Smarks spa_history_log_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
19106eeb2adSek {
19206eeb2adSek 	spa_t		*spa = arg1;
19306eeb2adSek 	history_arg_t	*hap = arg2;
19406eeb2adSek 	const char	*history_str = hap->ha_history_str;
19506eeb2adSek 	objset_t	*mos = spa->spa_meta_objset;
19606eeb2adSek 	dmu_buf_t	*dbp;
19706eeb2adSek 	spa_history_phys_t *shpp;
19806eeb2adSek 	size_t		reclen;
19906eeb2adSek 	uint64_t	le_len;
20006eeb2adSek 	nvlist_t	*nvrecord;
20106eeb2adSek 	char		*record_packed = NULL;
20206eeb2adSek 	int		ret;
20306eeb2adSek 
20406eeb2adSek 	/*
20506eeb2adSek 	 * If we have an older pool that doesn't have a command
20606eeb2adSek 	 * history object, create it now.
20706eeb2adSek 	 */
20806eeb2adSek 	mutex_enter(&spa->spa_history_lock);
20906eeb2adSek 	if (!spa->spa_history)
21006eeb2adSek 		spa_history_create_obj(spa, tx);
21106eeb2adSek 	mutex_exit(&spa->spa_history_lock);
21206eeb2adSek 
21306eeb2adSek 	/*
21406eeb2adSek 	 * Get the offset of where we need to write via the bonus buffer.
21506eeb2adSek 	 * Update the offset when the write completes.
21606eeb2adSek 	 */
21706eeb2adSek 	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
21806eeb2adSek 	shpp = dbp->db_data;
21906eeb2adSek 
22006eeb2adSek 	dmu_buf_will_dirty(dbp, tx);
22106eeb2adSek 
22206eeb2adSek #ifdef ZFS_DEBUG
22306eeb2adSek 	{
22406eeb2adSek 		dmu_object_info_t doi;
22506eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
22606eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
22706eeb2adSek 	}
22806eeb2adSek #endif
22906eeb2adSek 
23006eeb2adSek 	VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
23106eeb2adSek 	VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME,
23206eeb2adSek 	    gethrestime_sec()) == 0);
233ecd6cf80Smarks 	VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO,
234ecd6cf80Smarks 	    (uint64_t)crgetuid(cr)) == 0);
235ecd6cf80Smarks 	if (hap->ha_zone[0] != '\0')
236ecd6cf80Smarks 		VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_ZONE,
237ecd6cf80Smarks 		    hap->ha_zone) == 0);
238ecd6cf80Smarks #ifdef _KERNEL
239ecd6cf80Smarks 	VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_HOST,
240ecd6cf80Smarks 	    utsname.nodename) == 0);
241ecd6cf80Smarks #endif
242ecd6cf80Smarks 	if (hap->ha_log_type == LOG_CMD_POOL_CREATE ||
243ecd6cf80Smarks 	    hap->ha_log_type == LOG_CMD_NORMAL) {
244ecd6cf80Smarks 		VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD,
245ecd6cf80Smarks 		    history_str) == 0);
246ecd6cf80Smarks 	} else {
247ecd6cf80Smarks 		VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT,
248ecd6cf80Smarks 		    hap->ha_event) == 0);
249ecd6cf80Smarks 		VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TXG,
250ecd6cf80Smarks 		    tx->tx_txg) == 0);
251ecd6cf80Smarks 		VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR,
252ecd6cf80Smarks 		    history_str) == 0);
253ecd6cf80Smarks 	}
254ecd6cf80Smarks 
255da165920Smarks 	VERIFY(nvlist_size(nvrecord, &reclen, NV_ENCODE_XDR) == 0);
256da165920Smarks 	record_packed = kmem_alloc(reclen, KM_SLEEP);
257da165920Smarks 
25806eeb2adSek 	VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
25906eeb2adSek 	    NV_ENCODE_XDR, KM_SLEEP) == 0);
26006eeb2adSek 
26106eeb2adSek 	mutex_enter(&spa->spa_history_lock);
262ecd6cf80Smarks 	if (hap->ha_log_type == LOG_CMD_POOL_CREATE)
26306eeb2adSek 		VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
26406eeb2adSek 
26506eeb2adSek 	/* write out the packed length as little endian */
26655434c77Sek 	le_len = LE_64((uint64_t)reclen);
26706eeb2adSek 	ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
26806eeb2adSek 	if (!ret)
26906eeb2adSek 		ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
27006eeb2adSek 
271ecd6cf80Smarks 	if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) {
27206eeb2adSek 		shpp->sh_pool_create_len += sizeof (le_len) + reclen;
27306eeb2adSek 		shpp->sh_bof = shpp->sh_pool_create_len;
27406eeb2adSek 	}
27506eeb2adSek 
27606eeb2adSek 	mutex_exit(&spa->spa_history_lock);
27706eeb2adSek 	nvlist_free(nvrecord);
27806eeb2adSek 	kmem_free(record_packed, reclen);
27906eeb2adSek 	dmu_buf_rele(dbp, FTAG);
280e7437265Sahrens 
281e7437265Sahrens 	if (hap->ha_log_type == LOG_INTERNAL) {
282e7437265Sahrens 		kmem_free((void*)hap->ha_history_str, HIS_MAX_RECORD_LEN);
283e7437265Sahrens 		kmem_free(hap, sizeof (history_arg_t));
284e7437265Sahrens 	}
28506eeb2adSek }
28606eeb2adSek 
28706eeb2adSek /*
28806eeb2adSek  * Write out a history event.
28906eeb2adSek  */
29006eeb2adSek int
291ecd6cf80Smarks spa_history_log(spa_t *spa, const char *history_str, history_log_type_t what)
29206eeb2adSek {
29306eeb2adSek 	history_arg_t ha;
29406eeb2adSek 
295e7437265Sahrens 	ASSERT(what != LOG_INTERNAL);
296e7437265Sahrens 
29706eeb2adSek 	ha.ha_history_str = history_str;
298ecd6cf80Smarks 	ha.ha_log_type = what;
299ecd6cf80Smarks 	(void) strlcpy(ha.ha_zone, spa_history_zone(), sizeof (ha.ha_zone));
30006eeb2adSek 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync,
30106eeb2adSek 	    spa, &ha, 0));
30206eeb2adSek }
30306eeb2adSek 
30406eeb2adSek /*
30506eeb2adSek  * Read out the command history.
30606eeb2adSek  */
30706eeb2adSek int
30806eeb2adSek spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
30906eeb2adSek {
31006eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
31106eeb2adSek 	dmu_buf_t *dbp;
31206eeb2adSek 	uint64_t read_len, phys_read_off, phys_eof;
31306eeb2adSek 	uint64_t leftover = 0;
31406eeb2adSek 	spa_history_phys_t *shpp;
31506eeb2adSek 	int err;
31606eeb2adSek 
31706eeb2adSek 	/*
31806eeb2adSek 	 * If the command history  doesn't exist (older pool),
31906eeb2adSek 	 * that's ok, just return ENOENT.
32006eeb2adSek 	 */
32106eeb2adSek 	if (!spa->spa_history)
32206eeb2adSek 		return (ENOENT);
32306eeb2adSek 
32406eeb2adSek 	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
32506eeb2adSek 		return (err);
32606eeb2adSek 	shpp = dbp->db_data;
32706eeb2adSek 
32806eeb2adSek #ifdef ZFS_DEBUG
32906eeb2adSek 	{
33006eeb2adSek 		dmu_object_info_t doi;
33106eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
33206eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
33306eeb2adSek 	}
33406eeb2adSek #endif
33506eeb2adSek 
33606eeb2adSek 	mutex_enter(&spa->spa_history_lock);
33706eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
33806eeb2adSek 
33906eeb2adSek 	if (*offp < shpp->sh_pool_create_len) {
34006eeb2adSek 		/* read in just the zpool create history */
34106eeb2adSek 		phys_read_off = *offp;
34206eeb2adSek 		read_len = MIN(*len, shpp->sh_pool_create_len -
34306eeb2adSek 		    phys_read_off);
34406eeb2adSek 	} else {
34506eeb2adSek 		/*
34606eeb2adSek 		 * Need to reset passed in offset to BOF if the passed in
34706eeb2adSek 		 * offset has since been overwritten.
34806eeb2adSek 		 */
34906eeb2adSek 		*offp = MAX(*offp, shpp->sh_bof);
35006eeb2adSek 		phys_read_off = spa_history_log_to_phys(*offp, shpp);
35106eeb2adSek 
35206eeb2adSek 		/*
35306eeb2adSek 		 * Read up to the minimum of what the user passed down or
35406eeb2adSek 		 * the EOF (physical or logical).  If we hit physical EOF,
35506eeb2adSek 		 * use 'leftover' to read from the physical BOF.
35606eeb2adSek 		 */
35706eeb2adSek 		if (phys_read_off <= phys_eof) {
35806eeb2adSek 			read_len = MIN(*len, phys_eof - phys_read_off);
35906eeb2adSek 		} else {
36006eeb2adSek 			read_len = MIN(*len,
36106eeb2adSek 			    shpp->sh_phys_max_off - phys_read_off);
36206eeb2adSek 			if (phys_read_off + *len > shpp->sh_phys_max_off) {
36306eeb2adSek 				leftover = MIN(*len - read_len,
36406eeb2adSek 				    phys_eof - shpp->sh_pool_create_len);
36506eeb2adSek 			}
36606eeb2adSek 		}
36706eeb2adSek 	}
36806eeb2adSek 
36906eeb2adSek 	/* offset for consumer to use next */
37006eeb2adSek 	*offp += read_len + leftover;
37106eeb2adSek 
37206eeb2adSek 	/* tell the consumer how much you actually read */
37306eeb2adSek 	*len = read_len + leftover;
37406eeb2adSek 
37506eeb2adSek 	if (read_len == 0) {
37606eeb2adSek 		mutex_exit(&spa->spa_history_lock);
37706eeb2adSek 		dmu_buf_rele(dbp, FTAG);
37806eeb2adSek 		return (0);
37906eeb2adSek 	}
38006eeb2adSek 
381*7bfdf011SNeil Perrin 	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
382*7bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
38306eeb2adSek 	if (leftover && err == 0) {
38406eeb2adSek 		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
385*7bfdf011SNeil Perrin 		    leftover, buf + read_len, DMU_READ_PREFETCH);
38606eeb2adSek 	}
38706eeb2adSek 	mutex_exit(&spa->spa_history_lock);
38806eeb2adSek 
38906eeb2adSek 	dmu_buf_rele(dbp, FTAG);
39006eeb2adSek 	return (err);
39106eeb2adSek }
392ecd6cf80Smarks 
393ecd6cf80Smarks void
394ecd6cf80Smarks spa_history_internal_log(history_internal_events_t event, spa_t *spa,
395ecd6cf80Smarks     dmu_tx_t *tx, cred_t *cr, const char *fmt, ...)
396ecd6cf80Smarks {
397e7437265Sahrens 	history_arg_t *hap;
398ecd6cf80Smarks 	char *str;
399ecd6cf80Smarks 	va_list adx;
400ecd6cf80Smarks 
401088f3894Sahrens 	/*
402088f3894Sahrens 	 * If this is part of creating a pool, not everything is
403088f3894Sahrens 	 * initialized yet, so don't bother logging the internal events.
404088f3894Sahrens 	 */
405088f3894Sahrens 	if (tx->tx_txg == TXG_INITIAL)
406088f3894Sahrens 		return;
407088f3894Sahrens 
408e7437265Sahrens 	hap = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
409ecd6cf80Smarks 	str = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
410ecd6cf80Smarks 
411ecd6cf80Smarks 	va_start(adx, fmt);
412ecd6cf80Smarks 	(void) vsnprintf(str, HIS_MAX_RECORD_LEN, fmt, adx);
413ecd6cf80Smarks 	va_end(adx);
414ecd6cf80Smarks 
415e7437265Sahrens 	hap->ha_log_type = LOG_INTERNAL;
416e7437265Sahrens 	hap->ha_history_str = str;
417e7437265Sahrens 	hap->ha_event = event;
418e7437265Sahrens 	hap->ha_zone[0] = '\0';
419e7437265Sahrens 
420e7437265Sahrens 	if (dmu_tx_is_syncing(tx)) {
421e7437265Sahrens 		spa_history_log_sync(spa, hap, cr, tx);
422e7437265Sahrens 	} else {
423e7437265Sahrens 		dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
424e7437265Sahrens 		    spa_history_log_sync, spa, hap, 0, tx);
425e7437265Sahrens 	}
426e7437265Sahrens 	/* spa_history_log_sync() will free hap and str */
427ecd6cf80Smarks }
428