xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_history.c (revision b24ab6762772a3f6a89393947930c7fa61306783)
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 /*
237bfdf011SNeil 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 	 */
106*b24ab676SJeff Bonwick 	shpp->sh_phys_max_off =
107*b24ab676SJeff Bonwick 	    metaslab_class_get_dspace(spa_normal_class(spa)) / 100;
10806eeb2adSek 	shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20);
10906eeb2adSek 	shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
11006eeb2adSek 
11106eeb2adSek 	dmu_buf_rele(dbp, FTAG);
11206eeb2adSek }
11306eeb2adSek 
11406eeb2adSek /*
11506eeb2adSek  * Change 'sh_bof' to the beginning of the next record.
11606eeb2adSek  */
11706eeb2adSek static int
11806eeb2adSek spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
11906eeb2adSek {
12006eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
12106eeb2adSek 	uint64_t firstread, reclen, phys_bof;
12206eeb2adSek 	char buf[sizeof (reclen)];
12306eeb2adSek 	int err;
12406eeb2adSek 
12506eeb2adSek 	phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
12606eeb2adSek 	firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
12706eeb2adSek 
12806eeb2adSek 	if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
1297bfdf011SNeil Perrin 	    buf, DMU_READ_PREFETCH)) != 0)
13006eeb2adSek 		return (err);
13106eeb2adSek 	if (firstread != sizeof (reclen)) {
13206eeb2adSek 		if ((err = dmu_read(mos, spa->spa_history,
13306eeb2adSek 		    shpp->sh_pool_create_len, sizeof (reclen) - firstread,
1347bfdf011SNeil Perrin 		    buf + firstread, DMU_READ_PREFETCH)) != 0)
13506eeb2adSek 			return (err);
13606eeb2adSek 	}
13706eeb2adSek 
13806eeb2adSek 	reclen = LE_64(*((uint64_t *)buf));
13906eeb2adSek 	shpp->sh_bof += reclen + sizeof (reclen);
14006eeb2adSek 	shpp->sh_records_lost++;
14106eeb2adSek 	return (0);
14206eeb2adSek }
14306eeb2adSek 
14406eeb2adSek static int
14506eeb2adSek spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
14606eeb2adSek     dmu_tx_t *tx)
14706eeb2adSek {
14806eeb2adSek 	uint64_t firstwrite, phys_eof;
14906eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
15006eeb2adSek 	int err;
15106eeb2adSek 
15206eeb2adSek 	ASSERT(MUTEX_HELD(&spa->spa_history_lock));
15306eeb2adSek 
15406eeb2adSek 	/* see if we need to reset logical BOF */
15506eeb2adSek 	while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
15606eeb2adSek 	    (shpp->sh_eof - shpp->sh_bof) <= len) {
157ecd6cf80Smarks 		if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
15806eeb2adSek 			return (err);
159ecd6cf80Smarks 		}
16006eeb2adSek 	}
16106eeb2adSek 
16206eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
16306eeb2adSek 	firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
16406eeb2adSek 	shpp->sh_eof += len;
16506eeb2adSek 	dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
16606eeb2adSek 
16706eeb2adSek 	len -= firstwrite;
16806eeb2adSek 	if (len > 0) {
16906eeb2adSek 		/* write out the rest at the beginning of physical file */
17006eeb2adSek 		dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
17106eeb2adSek 		    len, (char *)buf + firstwrite, tx);
17206eeb2adSek 	}
17306eeb2adSek 
17406eeb2adSek 	return (0);
17506eeb2adSek }
17606eeb2adSek 
177ecd6cf80Smarks static char *
178ecd6cf80Smarks spa_history_zone()
179ecd6cf80Smarks {
180ecd6cf80Smarks #ifdef _KERNEL
181ecd6cf80Smarks 	return (curproc->p_zone->zone_name);
182ecd6cf80Smarks #else
183ecd6cf80Smarks 	return ("global");
184ecd6cf80Smarks #endif
185ecd6cf80Smarks }
186ecd6cf80Smarks 
18706eeb2adSek /*
18806eeb2adSek  * Write out a history event.
18906eeb2adSek  */
190e7437265Sahrens static void
191ecd6cf80Smarks spa_history_log_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
19206eeb2adSek {
19306eeb2adSek 	spa_t		*spa = arg1;
19406eeb2adSek 	history_arg_t	*hap = arg2;
19506eeb2adSek 	const char	*history_str = hap->ha_history_str;
19606eeb2adSek 	objset_t	*mos = spa->spa_meta_objset;
19706eeb2adSek 	dmu_buf_t	*dbp;
19806eeb2adSek 	spa_history_phys_t *shpp;
19906eeb2adSek 	size_t		reclen;
20006eeb2adSek 	uint64_t	le_len;
20106eeb2adSek 	nvlist_t	*nvrecord;
20206eeb2adSek 	char		*record_packed = NULL;
20306eeb2adSek 	int		ret;
20406eeb2adSek 
20506eeb2adSek 	/*
20606eeb2adSek 	 * If we have an older pool that doesn't have a command
20706eeb2adSek 	 * history object, create it now.
20806eeb2adSek 	 */
20906eeb2adSek 	mutex_enter(&spa->spa_history_lock);
21006eeb2adSek 	if (!spa->spa_history)
21106eeb2adSek 		spa_history_create_obj(spa, tx);
21206eeb2adSek 	mutex_exit(&spa->spa_history_lock);
21306eeb2adSek 
21406eeb2adSek 	/*
21506eeb2adSek 	 * Get the offset of where we need to write via the bonus buffer.
21606eeb2adSek 	 * Update the offset when the write completes.
21706eeb2adSek 	 */
21806eeb2adSek 	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
21906eeb2adSek 	shpp = dbp->db_data;
22006eeb2adSek 
22106eeb2adSek 	dmu_buf_will_dirty(dbp, tx);
22206eeb2adSek 
22306eeb2adSek #ifdef ZFS_DEBUG
22406eeb2adSek 	{
22506eeb2adSek 		dmu_object_info_t doi;
22606eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
22706eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
22806eeb2adSek 	}
22906eeb2adSek #endif
23006eeb2adSek 
23106eeb2adSek 	VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
23206eeb2adSek 	VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME,
23306eeb2adSek 	    gethrestime_sec()) == 0);
234ecd6cf80Smarks 	VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO,
235ecd6cf80Smarks 	    (uint64_t)crgetuid(cr)) == 0);
236ecd6cf80Smarks 	if (hap->ha_zone[0] != '\0')
237ecd6cf80Smarks 		VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_ZONE,
238ecd6cf80Smarks 		    hap->ha_zone) == 0);
239ecd6cf80Smarks #ifdef _KERNEL
240ecd6cf80Smarks 	VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_HOST,
241ecd6cf80Smarks 	    utsname.nodename) == 0);
242ecd6cf80Smarks #endif
243ecd6cf80Smarks 	if (hap->ha_log_type == LOG_CMD_POOL_CREATE ||
244ecd6cf80Smarks 	    hap->ha_log_type == LOG_CMD_NORMAL) {
245ecd6cf80Smarks 		VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD,
246ecd6cf80Smarks 		    history_str) == 0);
247ecd6cf80Smarks 	} else {
248ecd6cf80Smarks 		VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT,
249ecd6cf80Smarks 		    hap->ha_event) == 0);
250ecd6cf80Smarks 		VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TXG,
251ecd6cf80Smarks 		    tx->tx_txg) == 0);
252ecd6cf80Smarks 		VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR,
253ecd6cf80Smarks 		    history_str) == 0);
254ecd6cf80Smarks 	}
255ecd6cf80Smarks 
256da165920Smarks 	VERIFY(nvlist_size(nvrecord, &reclen, NV_ENCODE_XDR) == 0);
257da165920Smarks 	record_packed = kmem_alloc(reclen, KM_SLEEP);
258da165920Smarks 
25906eeb2adSek 	VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
26006eeb2adSek 	    NV_ENCODE_XDR, KM_SLEEP) == 0);
26106eeb2adSek 
26206eeb2adSek 	mutex_enter(&spa->spa_history_lock);
263ecd6cf80Smarks 	if (hap->ha_log_type == LOG_CMD_POOL_CREATE)
26406eeb2adSek 		VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
26506eeb2adSek 
26606eeb2adSek 	/* write out the packed length as little endian */
26755434c77Sek 	le_len = LE_64((uint64_t)reclen);
26806eeb2adSek 	ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
26906eeb2adSek 	if (!ret)
27006eeb2adSek 		ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
27106eeb2adSek 
272ecd6cf80Smarks 	if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) {
27306eeb2adSek 		shpp->sh_pool_create_len += sizeof (le_len) + reclen;
27406eeb2adSek 		shpp->sh_bof = shpp->sh_pool_create_len;
27506eeb2adSek 	}
27606eeb2adSek 
27706eeb2adSek 	mutex_exit(&spa->spa_history_lock);
27806eeb2adSek 	nvlist_free(nvrecord);
27906eeb2adSek 	kmem_free(record_packed, reclen);
28006eeb2adSek 	dmu_buf_rele(dbp, FTAG);
281e7437265Sahrens 
282e7437265Sahrens 	if (hap->ha_log_type == LOG_INTERNAL) {
283e7437265Sahrens 		kmem_free((void*)hap->ha_history_str, HIS_MAX_RECORD_LEN);
284e7437265Sahrens 		kmem_free(hap, sizeof (history_arg_t));
285e7437265Sahrens 	}
28606eeb2adSek }
28706eeb2adSek 
28806eeb2adSek /*
28906eeb2adSek  * Write out a history event.
29006eeb2adSek  */
29106eeb2adSek int
292ecd6cf80Smarks spa_history_log(spa_t *spa, const char *history_str, history_log_type_t what)
29306eeb2adSek {
29406eeb2adSek 	history_arg_t ha;
29506eeb2adSek 
296e7437265Sahrens 	ASSERT(what != LOG_INTERNAL);
297e7437265Sahrens 
29806eeb2adSek 	ha.ha_history_str = history_str;
299ecd6cf80Smarks 	ha.ha_log_type = what;
300ecd6cf80Smarks 	(void) strlcpy(ha.ha_zone, spa_history_zone(), sizeof (ha.ha_zone));
30106eeb2adSek 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync,
30206eeb2adSek 	    spa, &ha, 0));
30306eeb2adSek }
30406eeb2adSek 
30506eeb2adSek /*
30606eeb2adSek  * Read out the command history.
30706eeb2adSek  */
30806eeb2adSek int
30906eeb2adSek spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
31006eeb2adSek {
31106eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
31206eeb2adSek 	dmu_buf_t *dbp;
31306eeb2adSek 	uint64_t read_len, phys_read_off, phys_eof;
31406eeb2adSek 	uint64_t leftover = 0;
31506eeb2adSek 	spa_history_phys_t *shpp;
31606eeb2adSek 	int err;
31706eeb2adSek 
31806eeb2adSek 	/*
31906eeb2adSek 	 * If the command history  doesn't exist (older pool),
32006eeb2adSek 	 * that's ok, just return ENOENT.
32106eeb2adSek 	 */
32206eeb2adSek 	if (!spa->spa_history)
32306eeb2adSek 		return (ENOENT);
32406eeb2adSek 
32506eeb2adSek 	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
32606eeb2adSek 		return (err);
32706eeb2adSek 	shpp = dbp->db_data;
32806eeb2adSek 
32906eeb2adSek #ifdef ZFS_DEBUG
33006eeb2adSek 	{
33106eeb2adSek 		dmu_object_info_t doi;
33206eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
33306eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
33406eeb2adSek 	}
33506eeb2adSek #endif
33606eeb2adSek 
33706eeb2adSek 	mutex_enter(&spa->spa_history_lock);
33806eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
33906eeb2adSek 
34006eeb2adSek 	if (*offp < shpp->sh_pool_create_len) {
34106eeb2adSek 		/* read in just the zpool create history */
34206eeb2adSek 		phys_read_off = *offp;
34306eeb2adSek 		read_len = MIN(*len, shpp->sh_pool_create_len -
34406eeb2adSek 		    phys_read_off);
34506eeb2adSek 	} else {
34606eeb2adSek 		/*
34706eeb2adSek 		 * Need to reset passed in offset to BOF if the passed in
34806eeb2adSek 		 * offset has since been overwritten.
34906eeb2adSek 		 */
35006eeb2adSek 		*offp = MAX(*offp, shpp->sh_bof);
35106eeb2adSek 		phys_read_off = spa_history_log_to_phys(*offp, shpp);
35206eeb2adSek 
35306eeb2adSek 		/*
35406eeb2adSek 		 * Read up to the minimum of what the user passed down or
35506eeb2adSek 		 * the EOF (physical or logical).  If we hit physical EOF,
35606eeb2adSek 		 * use 'leftover' to read from the physical BOF.
35706eeb2adSek 		 */
35806eeb2adSek 		if (phys_read_off <= phys_eof) {
35906eeb2adSek 			read_len = MIN(*len, phys_eof - phys_read_off);
36006eeb2adSek 		} else {
36106eeb2adSek 			read_len = MIN(*len,
36206eeb2adSek 			    shpp->sh_phys_max_off - phys_read_off);
36306eeb2adSek 			if (phys_read_off + *len > shpp->sh_phys_max_off) {
36406eeb2adSek 				leftover = MIN(*len - read_len,
36506eeb2adSek 				    phys_eof - shpp->sh_pool_create_len);
36606eeb2adSek 			}
36706eeb2adSek 		}
36806eeb2adSek 	}
36906eeb2adSek 
37006eeb2adSek 	/* offset for consumer to use next */
37106eeb2adSek 	*offp += read_len + leftover;
37206eeb2adSek 
37306eeb2adSek 	/* tell the consumer how much you actually read */
37406eeb2adSek 	*len = read_len + leftover;
37506eeb2adSek 
37606eeb2adSek 	if (read_len == 0) {
37706eeb2adSek 		mutex_exit(&spa->spa_history_lock);
37806eeb2adSek 		dmu_buf_rele(dbp, FTAG);
37906eeb2adSek 		return (0);
38006eeb2adSek 	}
38106eeb2adSek 
3827bfdf011SNeil Perrin 	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
3837bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
38406eeb2adSek 	if (leftover && err == 0) {
38506eeb2adSek 		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
3867bfdf011SNeil Perrin 		    leftover, buf + read_len, DMU_READ_PREFETCH);
38706eeb2adSek 	}
38806eeb2adSek 	mutex_exit(&spa->spa_history_lock);
38906eeb2adSek 
39006eeb2adSek 	dmu_buf_rele(dbp, FTAG);
39106eeb2adSek 	return (err);
39206eeb2adSek }
393ecd6cf80Smarks 
394c8e1f6d2SMark J Musante static void
395c8e1f6d2SMark J Musante log_internal(history_internal_events_t event, spa_t *spa,
396c8e1f6d2SMark J Musante     dmu_tx_t *tx, cred_t *cr, const char *fmt, va_list adx)
397ecd6cf80Smarks {
398e7437265Sahrens 	history_arg_t *hap;
399ecd6cf80Smarks 	char *str;
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 	(void) vsnprintf(str, HIS_MAX_RECORD_LEN, fmt, adx);
412ecd6cf80Smarks 
413e7437265Sahrens 	hap->ha_log_type = LOG_INTERNAL;
414e7437265Sahrens 	hap->ha_history_str = str;
415e7437265Sahrens 	hap->ha_event = event;
416e7437265Sahrens 	hap->ha_zone[0] = '\0';
417e7437265Sahrens 
418e7437265Sahrens 	if (dmu_tx_is_syncing(tx)) {
419e7437265Sahrens 		spa_history_log_sync(spa, hap, cr, tx);
420e7437265Sahrens 	} else {
421e7437265Sahrens 		dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
422e7437265Sahrens 		    spa_history_log_sync, spa, hap, 0, tx);
423e7437265Sahrens 	}
424e7437265Sahrens 	/* spa_history_log_sync() will free hap and str */
425ecd6cf80Smarks }
426c8e1f6d2SMark J Musante 
427c8e1f6d2SMark J Musante void
428c8e1f6d2SMark J Musante spa_history_internal_log(history_internal_events_t event, spa_t *spa,
429c8e1f6d2SMark J Musante     dmu_tx_t *tx, cred_t *cr, const char *fmt, ...)
430c8e1f6d2SMark J Musante {
431c8e1f6d2SMark J Musante 	dmu_tx_t *htx = tx;
432c8e1f6d2SMark J Musante 	va_list adx;
433c8e1f6d2SMark J Musante 
434c8e1f6d2SMark J Musante 	/* create a tx if we didn't get one */
435c8e1f6d2SMark J Musante 	if (tx == NULL) {
436c8e1f6d2SMark J Musante 		htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
437c8e1f6d2SMark J Musante 		if (dmu_tx_assign(htx, TXG_WAIT) != 0) {
438c8e1f6d2SMark J Musante 			dmu_tx_abort(htx);
439c8e1f6d2SMark J Musante 			return;
440c8e1f6d2SMark J Musante 		}
441c8e1f6d2SMark J Musante 	}
442c8e1f6d2SMark J Musante 
443c8e1f6d2SMark J Musante 	va_start(adx, fmt);
444c8e1f6d2SMark J Musante 	log_internal(event, spa, htx, cr, fmt, adx);
445c8e1f6d2SMark J Musante 	va_end(adx);
446c8e1f6d2SMark J Musante 
447c8e1f6d2SMark J Musante 	/* if we didn't get a tx from the caller, commit the one we made */
448c8e1f6d2SMark J Musante 	if (tx == NULL)
449c8e1f6d2SMark J Musante 		dmu_tx_commit(htx);
450c8e1f6d2SMark J Musante }
451c8e1f6d2SMark J Musante 
452c8e1f6d2SMark J Musante void
453c8e1f6d2SMark J Musante spa_history_log_version(spa_t *spa, history_internal_events_t event)
454c8e1f6d2SMark J Musante {
455c8e1f6d2SMark J Musante #ifdef _KERNEL
456c8e1f6d2SMark J Musante 	uint64_t current_vers = spa_version(spa);
457c8e1f6d2SMark J Musante 
458c8e1f6d2SMark J Musante 	if (current_vers >= SPA_VERSION_ZPOOL_HISTORY) {
459c8e1f6d2SMark J Musante 		spa_history_internal_log(event, spa, NULL, CRED(),
460c8e1f6d2SMark J Musante 		    "pool spa %llu; zfs spa %llu; zpl %d; uts %s %s %s %s",
461c8e1f6d2SMark J Musante 		    (u_longlong_t)current_vers, SPA_VERSION, ZPL_VERSION,
462c8e1f6d2SMark J Musante 		    utsname.nodename, utsname.release, utsname.version,
463c8e1f6d2SMark J Musante 		    utsname.machine);
464c8e1f6d2SMark J Musante 	}
465c8e1f6d2SMark J Musante 	cmn_err(CE_CONT, "!%s version %llu pool %s using %llu",
466c8e1f6d2SMark J Musante 	    event == LOG_POOL_IMPORT ? "imported" :
467c8e1f6d2SMark J Musante 	    event == LOG_POOL_CREATE ? "created" : "accessed",
468c8e1f6d2SMark J Musante 	    (u_longlong_t)current_vers, spa_name(spa), SPA_VERSION);
469c8e1f6d2SMark J Musante #endif
470c8e1f6d2SMark J Musante }
471