xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_history.c (revision 495807d7dceb9c0357efb5f483dbf9f951dfeec6)
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*495807d7SMatthew Ahrens  * Copyright 2010 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 	 */
106b24ab676SJeff Bonwick 	shpp->sh_phys_max_off =
107b24ab676SJeff 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  */
190*495807d7SMatthew Ahrens /*ARGSUSED*/
191e7437265Sahrens 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);
235*495807d7SMatthew Ahrens 	VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO, hap->ha_uid) == 0);
236*495807d7SMatthew Ahrens 	if (hap->ha_zone != NULL)
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 
282*495807d7SMatthew Ahrens 	strfree(hap->ha_history_str);
283*495807d7SMatthew Ahrens 	if (hap->ha_zone != NULL)
284*495807d7SMatthew Ahrens 		strfree(hap->ha_zone);
285*495807d7SMatthew Ahrens 	kmem_free(hap, sizeof (history_arg_t));
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 {
294*495807d7SMatthew Ahrens 	history_arg_t *ha;
295*495807d7SMatthew Ahrens 	int err = 0;
296*495807d7SMatthew Ahrens 	dmu_tx_t *tx;
29706eeb2adSek 
298e7437265Sahrens 	ASSERT(what != LOG_INTERNAL);
299e7437265Sahrens 
300*495807d7SMatthew Ahrens 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
301*495807d7SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
302*495807d7SMatthew Ahrens 	if (err) {
303*495807d7SMatthew Ahrens 		dmu_tx_abort(tx);
304*495807d7SMatthew Ahrens 		return (err);
305*495807d7SMatthew Ahrens 	}
306*495807d7SMatthew Ahrens 
307*495807d7SMatthew Ahrens 	ha = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
308*495807d7SMatthew Ahrens 	ha->ha_history_str = strdup(history_str);
309*495807d7SMatthew Ahrens 	ha->ha_zone = strdup(spa_history_zone());
310*495807d7SMatthew Ahrens 	ha->ha_log_type = what;
311*495807d7SMatthew Ahrens 	ha->ha_uid = crgetuid(CRED());
312*495807d7SMatthew Ahrens 
313*495807d7SMatthew Ahrens 	/* Kick this off asynchronously; errors are ignored. */
314*495807d7SMatthew Ahrens 	dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
315*495807d7SMatthew Ahrens 	    spa_history_log_sync, spa, ha, 0, tx);
316*495807d7SMatthew Ahrens 	dmu_tx_commit(tx);
317*495807d7SMatthew Ahrens 
318*495807d7SMatthew Ahrens 	/* spa_history_log_sync will free ha and strings */
319*495807d7SMatthew Ahrens 	return (err);
32006eeb2adSek }
32106eeb2adSek 
32206eeb2adSek /*
32306eeb2adSek  * Read out the command history.
32406eeb2adSek  */
32506eeb2adSek int
32606eeb2adSek spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
32706eeb2adSek {
32806eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
32906eeb2adSek 	dmu_buf_t *dbp;
33006eeb2adSek 	uint64_t read_len, phys_read_off, phys_eof;
33106eeb2adSek 	uint64_t leftover = 0;
33206eeb2adSek 	spa_history_phys_t *shpp;
33306eeb2adSek 	int err;
33406eeb2adSek 
33506eeb2adSek 	/*
33606eeb2adSek 	 * If the command history  doesn't exist (older pool),
33706eeb2adSek 	 * that's ok, just return ENOENT.
33806eeb2adSek 	 */
33906eeb2adSek 	if (!spa->spa_history)
34006eeb2adSek 		return (ENOENT);
34106eeb2adSek 
342*495807d7SMatthew Ahrens 	/*
343*495807d7SMatthew Ahrens 	 * The history is logged asynchronously, so when they request
344*495807d7SMatthew Ahrens 	 * the first chunk of history, make sure everything has been
345*495807d7SMatthew Ahrens 	 * synced to disk so that we get it.
346*495807d7SMatthew Ahrens 	 */
347*495807d7SMatthew Ahrens 	if (*offp == 0)
348*495807d7SMatthew Ahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
349*495807d7SMatthew Ahrens 
35006eeb2adSek 	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
35106eeb2adSek 		return (err);
35206eeb2adSek 	shpp = dbp->db_data;
35306eeb2adSek 
35406eeb2adSek #ifdef ZFS_DEBUG
35506eeb2adSek 	{
35606eeb2adSek 		dmu_object_info_t doi;
35706eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
35806eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
35906eeb2adSek 	}
36006eeb2adSek #endif
36106eeb2adSek 
36206eeb2adSek 	mutex_enter(&spa->spa_history_lock);
36306eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
36406eeb2adSek 
36506eeb2adSek 	if (*offp < shpp->sh_pool_create_len) {
36606eeb2adSek 		/* read in just the zpool create history */
36706eeb2adSek 		phys_read_off = *offp;
36806eeb2adSek 		read_len = MIN(*len, shpp->sh_pool_create_len -
36906eeb2adSek 		    phys_read_off);
37006eeb2adSek 	} else {
37106eeb2adSek 		/*
37206eeb2adSek 		 * Need to reset passed in offset to BOF if the passed in
37306eeb2adSek 		 * offset has since been overwritten.
37406eeb2adSek 		 */
37506eeb2adSek 		*offp = MAX(*offp, shpp->sh_bof);
37606eeb2adSek 		phys_read_off = spa_history_log_to_phys(*offp, shpp);
37706eeb2adSek 
37806eeb2adSek 		/*
37906eeb2adSek 		 * Read up to the minimum of what the user passed down or
38006eeb2adSek 		 * the EOF (physical or logical).  If we hit physical EOF,
38106eeb2adSek 		 * use 'leftover' to read from the physical BOF.
38206eeb2adSek 		 */
38306eeb2adSek 		if (phys_read_off <= phys_eof) {
38406eeb2adSek 			read_len = MIN(*len, phys_eof - phys_read_off);
38506eeb2adSek 		} else {
38606eeb2adSek 			read_len = MIN(*len,
38706eeb2adSek 			    shpp->sh_phys_max_off - phys_read_off);
38806eeb2adSek 			if (phys_read_off + *len > shpp->sh_phys_max_off) {
38906eeb2adSek 				leftover = MIN(*len - read_len,
39006eeb2adSek 				    phys_eof - shpp->sh_pool_create_len);
39106eeb2adSek 			}
39206eeb2adSek 		}
39306eeb2adSek 	}
39406eeb2adSek 
39506eeb2adSek 	/* offset for consumer to use next */
39606eeb2adSek 	*offp += read_len + leftover;
39706eeb2adSek 
39806eeb2adSek 	/* tell the consumer how much you actually read */
39906eeb2adSek 	*len = read_len + leftover;
40006eeb2adSek 
40106eeb2adSek 	if (read_len == 0) {
40206eeb2adSek 		mutex_exit(&spa->spa_history_lock);
40306eeb2adSek 		dmu_buf_rele(dbp, FTAG);
40406eeb2adSek 		return (0);
40506eeb2adSek 	}
40606eeb2adSek 
4077bfdf011SNeil Perrin 	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
4087bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
40906eeb2adSek 	if (leftover && err == 0) {
41006eeb2adSek 		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
4117bfdf011SNeil Perrin 		    leftover, buf + read_len, DMU_READ_PREFETCH);
41206eeb2adSek 	}
41306eeb2adSek 	mutex_exit(&spa->spa_history_lock);
41406eeb2adSek 
41506eeb2adSek 	dmu_buf_rele(dbp, FTAG);
41606eeb2adSek 	return (err);
41706eeb2adSek }
418ecd6cf80Smarks 
419c8e1f6d2SMark J Musante static void
420c8e1f6d2SMark J Musante log_internal(history_internal_events_t event, spa_t *spa,
421c8e1f6d2SMark J Musante     dmu_tx_t *tx, cred_t *cr, const char *fmt, va_list adx)
422ecd6cf80Smarks {
423*495807d7SMatthew Ahrens 	history_arg_t *ha;
424ecd6cf80Smarks 
425088f3894Sahrens 	/*
426088f3894Sahrens 	 * If this is part of creating a pool, not everything is
427088f3894Sahrens 	 * initialized yet, so don't bother logging the internal events.
428088f3894Sahrens 	 */
429088f3894Sahrens 	if (tx->tx_txg == TXG_INITIAL)
430088f3894Sahrens 		return;
431088f3894Sahrens 
432*495807d7SMatthew Ahrens 	ha = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
433*495807d7SMatthew Ahrens 	ha->ha_history_str = kmem_alloc(vsnprintf(NULL, 0, fmt, adx) + 1,
434*495807d7SMatthew Ahrens 	    KM_SLEEP);
435ecd6cf80Smarks 
436*495807d7SMatthew Ahrens 	(void) vsprintf(ha->ha_history_str, fmt, adx);
437ecd6cf80Smarks 
438*495807d7SMatthew Ahrens 	ha->ha_log_type = LOG_INTERNAL;
439*495807d7SMatthew Ahrens 	ha->ha_event = event;
440*495807d7SMatthew Ahrens 	ha->ha_zone = NULL;
441*495807d7SMatthew Ahrens 	ha->ha_uid = 0;
442e7437265Sahrens 
443e7437265Sahrens 	if (dmu_tx_is_syncing(tx)) {
444*495807d7SMatthew Ahrens 		spa_history_log_sync(spa, ha, cr, tx);
445e7437265Sahrens 	} else {
446e7437265Sahrens 		dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
447*495807d7SMatthew Ahrens 		    spa_history_log_sync, spa, ha, 0, tx);
448e7437265Sahrens 	}
449*495807d7SMatthew Ahrens 	/* spa_history_log_sync() will free ha and strings */
450ecd6cf80Smarks }
451c8e1f6d2SMark J Musante 
452c8e1f6d2SMark J Musante void
453c8e1f6d2SMark J Musante spa_history_internal_log(history_internal_events_t event, spa_t *spa,
454c8e1f6d2SMark J Musante     dmu_tx_t *tx, cred_t *cr, const char *fmt, ...)
455c8e1f6d2SMark J Musante {
456c8e1f6d2SMark J Musante 	dmu_tx_t *htx = tx;
457c8e1f6d2SMark J Musante 	va_list adx;
458c8e1f6d2SMark J Musante 
459c8e1f6d2SMark J Musante 	/* create a tx if we didn't get one */
460c8e1f6d2SMark J Musante 	if (tx == NULL) {
461c8e1f6d2SMark J Musante 		htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
462c8e1f6d2SMark J Musante 		if (dmu_tx_assign(htx, TXG_WAIT) != 0) {
463c8e1f6d2SMark J Musante 			dmu_tx_abort(htx);
464c8e1f6d2SMark J Musante 			return;
465c8e1f6d2SMark J Musante 		}
466c8e1f6d2SMark J Musante 	}
467c8e1f6d2SMark J Musante 
468c8e1f6d2SMark J Musante 	va_start(adx, fmt);
469c8e1f6d2SMark J Musante 	log_internal(event, spa, htx, cr, fmt, adx);
470c8e1f6d2SMark J Musante 	va_end(adx);
471c8e1f6d2SMark J Musante 
472c8e1f6d2SMark J Musante 	/* if we didn't get a tx from the caller, commit the one we made */
473c8e1f6d2SMark J Musante 	if (tx == NULL)
474c8e1f6d2SMark J Musante 		dmu_tx_commit(htx);
475c8e1f6d2SMark J Musante }
476c8e1f6d2SMark J Musante 
477c8e1f6d2SMark J Musante void
478c8e1f6d2SMark J Musante spa_history_log_version(spa_t *spa, history_internal_events_t event)
479c8e1f6d2SMark J Musante {
480c8e1f6d2SMark J Musante #ifdef _KERNEL
481c8e1f6d2SMark J Musante 	uint64_t current_vers = spa_version(spa);
482c8e1f6d2SMark J Musante 
483c8e1f6d2SMark J Musante 	if (current_vers >= SPA_VERSION_ZPOOL_HISTORY) {
484c8e1f6d2SMark J Musante 		spa_history_internal_log(event, spa, NULL, CRED(),
485c8e1f6d2SMark J Musante 		    "pool spa %llu; zfs spa %llu; zpl %d; uts %s %s %s %s",
486c8e1f6d2SMark J Musante 		    (u_longlong_t)current_vers, SPA_VERSION, ZPL_VERSION,
487c8e1f6d2SMark J Musante 		    utsname.nodename, utsname.release, utsname.version,
488c8e1f6d2SMark J Musante 		    utsname.machine);
489c8e1f6d2SMark J Musante 	}
490c8e1f6d2SMark J Musante 	cmn_err(CE_CONT, "!%s version %llu pool %s using %llu",
491c8e1f6d2SMark J Musante 	    event == LOG_POOL_IMPORT ? "imported" :
492c8e1f6d2SMark J Musante 	    event == LOG_POOL_CREATE ? "created" : "accessed",
493c8e1f6d2SMark J Musante 	    (u_longlong_t)current_vers, spa_name(spa), SPA_VERSION);
494c8e1f6d2SMark J Musante #endif
495c8e1f6d2SMark J Musante }
496