xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_history.c (revision c1379625401dfbe1c39b79136dd384a571d47fde)
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 /*
233f9d6ad7SLin Ling  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
247d46dc6cSMatthew Ahrens  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
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>
334445fffbSMatthew Ahrens #include <sys/dsl_dataset.h>
344445fffbSMatthew Ahrens #include <sys/dsl_dir.h>
35ecd6cf80Smarks #include <sys/utsname.h>
36ecd6cf80Smarks #include <sys/cmn_err.h>
37ecd6cf80Smarks #include <sys/sunddi.h>
384445fffbSMatthew Ahrens #include <sys/cred.h>
393f9d6ad7SLin Ling #include "zfs_comutil.h"
40ecd6cf80Smarks #ifdef _KERNEL
41ecd6cf80Smarks #include <sys/zone.h>
42ecd6cf80Smarks #endif
4306eeb2adSek 
4406eeb2adSek /*
4506eeb2adSek  * Routines to manage the on-disk history log.
4606eeb2adSek  *
4706eeb2adSek  * The history log is stored as a dmu object containing
4806eeb2adSek  * <packed record length, record nvlist> tuples.
4906eeb2adSek  *
5006eeb2adSek  * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
5106eeb2adSek  * "packed record length" is the packed length of the "record nvlist" stored
5206eeb2adSek  * as a little endian uint64_t.
5306eeb2adSek  *
5406eeb2adSek  * The log is implemented as a ring buffer, though the original creation
5506eeb2adSek  * of the pool ('zpool create') is never overwritten.
5606eeb2adSek  *
5706eeb2adSek  * The history log is tracked as object 'spa_t::spa_history'.  The bonus buffer
5806eeb2adSek  * of 'spa_history' stores the offsets for logging/retrieving history as
5906eeb2adSek  * 'spa_history_phys_t'.  'sh_pool_create_len' is the ending offset in bytes of
6006eeb2adSek  * where the 'zpool create' record is stored.  This allows us to never
6106eeb2adSek  * overwrite the original creation of the pool.  'sh_phys_max_off' is the
6206eeb2adSek  * physical ending offset in bytes of the log.  This tells you the length of
6306eeb2adSek  * the buffer. 'sh_eof' is the logical EOF (in bytes).  Whenever a record
6406eeb2adSek  * is added, 'sh_eof' is incremented by the the size of the record.
6506eeb2adSek  * 'sh_eof' is never decremented.  'sh_bof' is the logical BOF (in bytes).
6606eeb2adSek  * This is where the consumer should start reading from after reading in
6706eeb2adSek  * the 'zpool create' portion of the log.
6806eeb2adSek  *
6906eeb2adSek  * 'sh_records_lost' keeps track of how many records have been overwritten
7006eeb2adSek  * and permanently lost.
7106eeb2adSek  */
7206eeb2adSek 
7306eeb2adSek /* convert a logical offset to physical */
7406eeb2adSek static uint64_t
7506eeb2adSek spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
7606eeb2adSek {
7706eeb2adSek 	uint64_t phys_len;
7806eeb2adSek 
7906eeb2adSek 	phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
8006eeb2adSek 	return ((log_off - shpp->sh_pool_create_len) % phys_len
8106eeb2adSek 	    + shpp->sh_pool_create_len);
8206eeb2adSek }
8306eeb2adSek 
8406eeb2adSek void
8506eeb2adSek spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
8606eeb2adSek {
8706eeb2adSek 	dmu_buf_t *dbp;
8806eeb2adSek 	spa_history_phys_t *shpp;
8906eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
9006eeb2adSek 
9106eeb2adSek 	ASSERT(spa->spa_history == 0);
9206eeb2adSek 	spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
93b5152584SMatthew Ahrens 	    SPA_OLD_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
9406eeb2adSek 	    sizeof (spa_history_phys_t), tx);
9506eeb2adSek 
9606eeb2adSek 	VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
9706eeb2adSek 	    DMU_POOL_HISTORY, sizeof (uint64_t), 1,
9806eeb2adSek 	    &spa->spa_history, tx) == 0);
9906eeb2adSek 
10006eeb2adSek 	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
10106eeb2adSek 	ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
10206eeb2adSek 
10306eeb2adSek 	shpp = dbp->db_data;
10406eeb2adSek 	dmu_buf_will_dirty(dbp, tx);
10506eeb2adSek 
10606eeb2adSek 	/*
10706eeb2adSek 	 * Figure out maximum size of history log.  We set it at
10819b94df9SMatthew Ahrens 	 * 0.1% of pool size, with a max of 1G and min of 128KB.
10906eeb2adSek 	 */
110b24ab676SJeff Bonwick 	shpp->sh_phys_max_off =
11119b94df9SMatthew Ahrens 	    metaslab_class_get_dspace(spa_normal_class(spa)) / 1000;
11219b94df9SMatthew Ahrens 	shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 1<<30);
11306eeb2adSek 	shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
11406eeb2adSek 
11506eeb2adSek 	dmu_buf_rele(dbp, FTAG);
11606eeb2adSek }
11706eeb2adSek 
11806eeb2adSek /*
11906eeb2adSek  * Change 'sh_bof' to the beginning of the next record.
12006eeb2adSek  */
12106eeb2adSek static int
12206eeb2adSek spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
12306eeb2adSek {
12406eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
12506eeb2adSek 	uint64_t firstread, reclen, phys_bof;
12606eeb2adSek 	char buf[sizeof (reclen)];
12706eeb2adSek 	int err;
12806eeb2adSek 
12906eeb2adSek 	phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
13006eeb2adSek 	firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
13106eeb2adSek 
13206eeb2adSek 	if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
1337bfdf011SNeil Perrin 	    buf, DMU_READ_PREFETCH)) != 0)
13406eeb2adSek 		return (err);
13506eeb2adSek 	if (firstread != sizeof (reclen)) {
13606eeb2adSek 		if ((err = dmu_read(mos, spa->spa_history,
13706eeb2adSek 		    shpp->sh_pool_create_len, sizeof (reclen) - firstread,
1387bfdf011SNeil Perrin 		    buf + firstread, DMU_READ_PREFETCH)) != 0)
13906eeb2adSek 			return (err);
14006eeb2adSek 	}
14106eeb2adSek 
14206eeb2adSek 	reclen = LE_64(*((uint64_t *)buf));
14306eeb2adSek 	shpp->sh_bof += reclen + sizeof (reclen);
14406eeb2adSek 	shpp->sh_records_lost++;
14506eeb2adSek 	return (0);
14606eeb2adSek }
14706eeb2adSek 
14806eeb2adSek static int
14906eeb2adSek spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
15006eeb2adSek     dmu_tx_t *tx)
15106eeb2adSek {
15206eeb2adSek 	uint64_t firstwrite, phys_eof;
15306eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
15406eeb2adSek 	int err;
15506eeb2adSek 
15606eeb2adSek 	ASSERT(MUTEX_HELD(&spa->spa_history_lock));
15706eeb2adSek 
15806eeb2adSek 	/* see if we need to reset logical BOF */
15906eeb2adSek 	while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
16006eeb2adSek 	    (shpp->sh_eof - shpp->sh_bof) <= len) {
161ecd6cf80Smarks 		if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
16206eeb2adSek 			return (err);
163ecd6cf80Smarks 		}
16406eeb2adSek 	}
16506eeb2adSek 
16606eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
16706eeb2adSek 	firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
16806eeb2adSek 	shpp->sh_eof += len;
16906eeb2adSek 	dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
17006eeb2adSek 
17106eeb2adSek 	len -= firstwrite;
17206eeb2adSek 	if (len > 0) {
17306eeb2adSek 		/* write out the rest at the beginning of physical file */
17406eeb2adSek 		dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
17506eeb2adSek 		    len, (char *)buf + firstwrite, tx);
17606eeb2adSek 	}
17706eeb2adSek 
17806eeb2adSek 	return (0);
17906eeb2adSek }
18006eeb2adSek 
181ecd6cf80Smarks static char *
1824445fffbSMatthew Ahrens spa_history_zone(void)
183ecd6cf80Smarks {
184ecd6cf80Smarks #ifdef _KERNEL
1854445fffbSMatthew Ahrens 	if (INGLOBALZONE(curproc))
1864445fffbSMatthew Ahrens 		return (NULL);
187ecd6cf80Smarks 	return (curproc->p_zone->zone_name);
188ecd6cf80Smarks #else
1894445fffbSMatthew Ahrens 	return (NULL);
190ecd6cf80Smarks #endif
191ecd6cf80Smarks }
192ecd6cf80Smarks 
19306eeb2adSek /*
19406eeb2adSek  * Write out a history event.
19506eeb2adSek  */
196495807d7SMatthew Ahrens /*ARGSUSED*/
197e7437265Sahrens static void
1983b2aab18SMatthew Ahrens spa_history_log_sync(void *arg, dmu_tx_t *tx)
19906eeb2adSek {
2003b2aab18SMatthew Ahrens 	nvlist_t	*nvl = arg;
2013b2aab18SMatthew Ahrens 	spa_t		*spa = dmu_tx_pool(tx)->dp_spa;
20206eeb2adSek 	objset_t	*mos = spa->spa_meta_objset;
20306eeb2adSek 	dmu_buf_t	*dbp;
20406eeb2adSek 	spa_history_phys_t *shpp;
20506eeb2adSek 	size_t		reclen;
20606eeb2adSek 	uint64_t	le_len;
20706eeb2adSek 	char		*record_packed = NULL;
20806eeb2adSek 	int		ret;
20906eeb2adSek 
21006eeb2adSek 	/*
21106eeb2adSek 	 * If we have an older pool that doesn't have a command
21206eeb2adSek 	 * history object, create it now.
21306eeb2adSek 	 */
21406eeb2adSek 	mutex_enter(&spa->spa_history_lock);
21506eeb2adSek 	if (!spa->spa_history)
21606eeb2adSek 		spa_history_create_obj(spa, tx);
21706eeb2adSek 	mutex_exit(&spa->spa_history_lock);
21806eeb2adSek 
21906eeb2adSek 	/*
22006eeb2adSek 	 * Get the offset of where we need to write via the bonus buffer.
22106eeb2adSek 	 * Update the offset when the write completes.
22206eeb2adSek 	 */
2233b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
22406eeb2adSek 	shpp = dbp->db_data;
22506eeb2adSek 
22606eeb2adSek 	dmu_buf_will_dirty(dbp, tx);
22706eeb2adSek 
22806eeb2adSek #ifdef ZFS_DEBUG
22906eeb2adSek 	{
23006eeb2adSek 		dmu_object_info_t doi;
23106eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
23206eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
23306eeb2adSek 	}
23406eeb2adSek #endif
23506eeb2adSek 
2364445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvl, ZPOOL_HIST_TIME, gethrestime_sec());
237ecd6cf80Smarks #ifdef _KERNEL
2384445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_HOST, utsname.nodename);
239ecd6cf80Smarks #endif
2404445fffbSMatthew Ahrens 	if (nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
2414445fffbSMatthew Ahrens 		zfs_dbgmsg("command: %s",
2424445fffbSMatthew Ahrens 		    fnvlist_lookup_string(nvl, ZPOOL_HIST_CMD));
2434445fffbSMatthew Ahrens 	} else if (nvlist_exists(nvl, ZPOOL_HIST_INT_NAME)) {
2444445fffbSMatthew Ahrens 		if (nvlist_exists(nvl, ZPOOL_HIST_DSNAME)) {
2454445fffbSMatthew Ahrens 			zfs_dbgmsg("txg %lld %s %s (id %llu) %s",
2464445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG),
2474445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
2484445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_DSNAME),
2494445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(nvl, ZPOOL_HIST_DSID),
2504445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
2514445fffbSMatthew Ahrens 		} else {
2524445fffbSMatthew Ahrens 			zfs_dbgmsg("txg %lld %s %s",
2534445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG),
2544445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
2554445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
2564445fffbSMatthew Ahrens 		}
2574445fffbSMatthew Ahrens 	} else if (nvlist_exists(nvl, ZPOOL_HIST_IOCTL)) {
2584445fffbSMatthew Ahrens 		zfs_dbgmsg("ioctl %s",
2594445fffbSMatthew Ahrens 		    fnvlist_lookup_string(nvl, ZPOOL_HIST_IOCTL));
260ecd6cf80Smarks 	}
261ecd6cf80Smarks 
2624445fffbSMatthew Ahrens 	record_packed = fnvlist_pack(nvl, &reclen);
26306eeb2adSek 
26406eeb2adSek 	mutex_enter(&spa->spa_history_lock);
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 
2724445fffbSMatthew Ahrens 	/* The first command is the create, which we keep forever */
2734445fffbSMatthew Ahrens 	if (ret == 0 && shpp->sh_pool_create_len == 0 &&
2744445fffbSMatthew Ahrens 	    nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
2754445fffbSMatthew Ahrens 		shpp->sh_pool_create_len = shpp->sh_bof = shpp->sh_eof;
27606eeb2adSek 	}
27706eeb2adSek 
27806eeb2adSek 	mutex_exit(&spa->spa_history_lock);
2794445fffbSMatthew Ahrens 	fnvlist_pack_free(record_packed, reclen);
28006eeb2adSek 	dmu_buf_rele(dbp, FTAG);
2814445fffbSMatthew Ahrens 	fnvlist_free(nvl);
28206eeb2adSek }
28306eeb2adSek 
28406eeb2adSek /*
28506eeb2adSek  * Write out a history event.
28606eeb2adSek  */
28706eeb2adSek int
2884445fffbSMatthew Ahrens spa_history_log(spa_t *spa, const char *msg)
2894445fffbSMatthew Ahrens {
2904445fffbSMatthew Ahrens 	int err;
2914445fffbSMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
2924445fffbSMatthew Ahrens 
2934445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_CMD, msg);
2944445fffbSMatthew Ahrens 	err = spa_history_log_nvl(spa, nvl);
2954445fffbSMatthew Ahrens 	fnvlist_free(nvl);
2964445fffbSMatthew Ahrens 	return (err);
2974445fffbSMatthew Ahrens }
2984445fffbSMatthew Ahrens 
2994445fffbSMatthew Ahrens int
3004445fffbSMatthew Ahrens spa_history_log_nvl(spa_t *spa, nvlist_t *nvl)
30106eeb2adSek {
302495807d7SMatthew Ahrens 	int err = 0;
303495807d7SMatthew Ahrens 	dmu_tx_t *tx;
3044445fffbSMatthew Ahrens 	nvlist_t *nvarg;
30506eeb2adSek 
306cd1c8b85SMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY || !spa_writeable(spa))
307be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
308e7437265Sahrens 
309495807d7SMatthew Ahrens 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
310495807d7SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
311495807d7SMatthew Ahrens 	if (err) {
312495807d7SMatthew Ahrens 		dmu_tx_abort(tx);
313495807d7SMatthew Ahrens 		return (err);
314495807d7SMatthew Ahrens 	}
315495807d7SMatthew Ahrens 
3164445fffbSMatthew Ahrens 	nvarg = fnvlist_dup(nvl);
3174445fffbSMatthew Ahrens 	if (spa_history_zone() != NULL) {
3184445fffbSMatthew Ahrens 		fnvlist_add_string(nvarg, ZPOOL_HIST_ZONE,
3194445fffbSMatthew Ahrens 		    spa_history_zone());
3204445fffbSMatthew Ahrens 	}
3214445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvarg, ZPOOL_HIST_WHO, crgetruid(CRED()));
322495807d7SMatthew Ahrens 
323495807d7SMatthew Ahrens 	/* Kick this off asynchronously; errors are ignored. */
3243b2aab18SMatthew Ahrens 	dsl_sync_task_nowait(spa_get_dsl(spa), spa_history_log_sync,
3257d46dc6cSMatthew Ahrens 	    nvarg, 0, ZFS_SPACE_CHECK_NONE, tx);
326495807d7SMatthew Ahrens 	dmu_tx_commit(tx);
327495807d7SMatthew Ahrens 
3284445fffbSMatthew Ahrens 	/* spa_history_log_sync will free nvl */
329495807d7SMatthew Ahrens 	return (err);
3304445fffbSMatthew Ahrens 
33106eeb2adSek }
33206eeb2adSek 
33306eeb2adSek /*
33406eeb2adSek  * Read out the command history.
33506eeb2adSek  */
33606eeb2adSek int
33706eeb2adSek spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
33806eeb2adSek {
33906eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
34006eeb2adSek 	dmu_buf_t *dbp;
34106eeb2adSek 	uint64_t read_len, phys_read_off, phys_eof;
34206eeb2adSek 	uint64_t leftover = 0;
34306eeb2adSek 	spa_history_phys_t *shpp;
34406eeb2adSek 	int err;
34506eeb2adSek 
34606eeb2adSek 	/*
3474445fffbSMatthew Ahrens 	 * If the command history doesn't exist (older pool),
34806eeb2adSek 	 * that's ok, just return ENOENT.
34906eeb2adSek 	 */
35006eeb2adSek 	if (!spa->spa_history)
351be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
35206eeb2adSek 
353495807d7SMatthew Ahrens 	/*
354495807d7SMatthew Ahrens 	 * The history is logged asynchronously, so when they request
355495807d7SMatthew Ahrens 	 * the first chunk of history, make sure everything has been
356495807d7SMatthew Ahrens 	 * synced to disk so that we get it.
357495807d7SMatthew Ahrens 	 */
3583c708518SMark J Musante 	if (*offp == 0 && spa_writeable(spa))
359495807d7SMatthew Ahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
360495807d7SMatthew Ahrens 
36106eeb2adSek 	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
36206eeb2adSek 		return (err);
36306eeb2adSek 	shpp = dbp->db_data;
36406eeb2adSek 
36506eeb2adSek #ifdef ZFS_DEBUG
36606eeb2adSek 	{
36706eeb2adSek 		dmu_object_info_t doi;
36806eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
36906eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
37006eeb2adSek 	}
37106eeb2adSek #endif
37206eeb2adSek 
37306eeb2adSek 	mutex_enter(&spa->spa_history_lock);
37406eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
37506eeb2adSek 
37606eeb2adSek 	if (*offp < shpp->sh_pool_create_len) {
37706eeb2adSek 		/* read in just the zpool create history */
37806eeb2adSek 		phys_read_off = *offp;
37906eeb2adSek 		read_len = MIN(*len, shpp->sh_pool_create_len -
38006eeb2adSek 		    phys_read_off);
38106eeb2adSek 	} else {
38206eeb2adSek 		/*
38306eeb2adSek 		 * Need to reset passed in offset to BOF if the passed in
38406eeb2adSek 		 * offset has since been overwritten.
38506eeb2adSek 		 */
38606eeb2adSek 		*offp = MAX(*offp, shpp->sh_bof);
38706eeb2adSek 		phys_read_off = spa_history_log_to_phys(*offp, shpp);
38806eeb2adSek 
38906eeb2adSek 		/*
39006eeb2adSek 		 * Read up to the minimum of what the user passed down or
39106eeb2adSek 		 * the EOF (physical or logical).  If we hit physical EOF,
39206eeb2adSek 		 * use 'leftover' to read from the physical BOF.
39306eeb2adSek 		 */
39406eeb2adSek 		if (phys_read_off <= phys_eof) {
39506eeb2adSek 			read_len = MIN(*len, phys_eof - phys_read_off);
39606eeb2adSek 		} else {
39706eeb2adSek 			read_len = MIN(*len,
39806eeb2adSek 			    shpp->sh_phys_max_off - phys_read_off);
39906eeb2adSek 			if (phys_read_off + *len > shpp->sh_phys_max_off) {
40006eeb2adSek 				leftover = MIN(*len - read_len,
40106eeb2adSek 				    phys_eof - shpp->sh_pool_create_len);
40206eeb2adSek 			}
40306eeb2adSek 		}
40406eeb2adSek 	}
40506eeb2adSek 
40606eeb2adSek 	/* offset for consumer to use next */
40706eeb2adSek 	*offp += read_len + leftover;
40806eeb2adSek 
40906eeb2adSek 	/* tell the consumer how much you actually read */
41006eeb2adSek 	*len = read_len + leftover;
41106eeb2adSek 
41206eeb2adSek 	if (read_len == 0) {
41306eeb2adSek 		mutex_exit(&spa->spa_history_lock);
41406eeb2adSek 		dmu_buf_rele(dbp, FTAG);
41506eeb2adSek 		return (0);
41606eeb2adSek 	}
41706eeb2adSek 
4187bfdf011SNeil Perrin 	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
4197bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
42006eeb2adSek 	if (leftover && err == 0) {
42106eeb2adSek 		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
4227bfdf011SNeil Perrin 		    leftover, buf + read_len, DMU_READ_PREFETCH);
42306eeb2adSek 	}
42406eeb2adSek 	mutex_exit(&spa->spa_history_lock);
42506eeb2adSek 
42606eeb2adSek 	dmu_buf_rele(dbp, FTAG);
42706eeb2adSek 	return (err);
42806eeb2adSek }
429ecd6cf80Smarks 
4304445fffbSMatthew Ahrens /*
4314445fffbSMatthew Ahrens  * The nvlist will be consumed by this call.
4324445fffbSMatthew Ahrens  */
433c8e1f6d2SMark J Musante static void
4344445fffbSMatthew Ahrens log_internal(nvlist_t *nvl, const char *operation, spa_t *spa,
4353f9d6ad7SLin Ling     dmu_tx_t *tx, const char *fmt, va_list adx)
436ecd6cf80Smarks {
4374445fffbSMatthew Ahrens 	char *msg;
438ecd6cf80Smarks 
439088f3894Sahrens 	/*
440088f3894Sahrens 	 * If this is part of creating a pool, not everything is
441088f3894Sahrens 	 * initialized yet, so don't bother logging the internal events.
442cd1c8b85SMatthew Ahrens 	 * Likewise if the pool is not writeable.
443088f3894Sahrens 	 */
444cd1c8b85SMatthew Ahrens 	if (tx->tx_txg == TXG_INITIAL || !spa_writeable(spa)) {
445347eec8eSChristopher Siden 		fnvlist_free(nvl);
446088f3894Sahrens 		return;
447347eec8eSChristopher Siden 	}
448088f3894Sahrens 
4494445fffbSMatthew Ahrens 	msg = kmem_alloc(vsnprintf(NULL, 0, fmt, adx) + 1, KM_SLEEP);
4504445fffbSMatthew Ahrens 	(void) vsprintf(msg, fmt, adx);
4514445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_INT_STR, msg);
4524445fffbSMatthew Ahrens 	strfree(msg);
453ecd6cf80Smarks 
4544445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_INT_NAME, operation);
4554445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvl, ZPOOL_HIST_TXG, tx->tx_txg);
456e7437265Sahrens 
457e7437265Sahrens 	if (dmu_tx_is_syncing(tx)) {
4583b2aab18SMatthew Ahrens 		spa_history_log_sync(nvl, tx);
459e7437265Sahrens 	} else {
4603b2aab18SMatthew Ahrens 		dsl_sync_task_nowait(spa_get_dsl(spa),
4617d46dc6cSMatthew Ahrens 		    spa_history_log_sync, nvl, 0, ZFS_SPACE_CHECK_NONE, tx);
462e7437265Sahrens 	}
4634445fffbSMatthew Ahrens 	/* spa_history_log_sync() will free nvl */
464ecd6cf80Smarks }
465c8e1f6d2SMark J Musante 
466c8e1f6d2SMark J Musante void
4674445fffbSMatthew Ahrens spa_history_log_internal(spa_t *spa, const char *operation,
4683f9d6ad7SLin Ling     dmu_tx_t *tx, const char *fmt, ...)
469c8e1f6d2SMark J Musante {
470c8e1f6d2SMark J Musante 	dmu_tx_t *htx = tx;
471c8e1f6d2SMark J Musante 	va_list adx;
472c8e1f6d2SMark J Musante 
473c8e1f6d2SMark J Musante 	/* create a tx if we didn't get one */
474c8e1f6d2SMark J Musante 	if (tx == NULL) {
475c8e1f6d2SMark J Musante 		htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
476c8e1f6d2SMark J Musante 		if (dmu_tx_assign(htx, TXG_WAIT) != 0) {
477c8e1f6d2SMark J Musante 			dmu_tx_abort(htx);
478c8e1f6d2SMark J Musante 			return;
479c8e1f6d2SMark J Musante 		}
480c8e1f6d2SMark J Musante 	}
481c8e1f6d2SMark J Musante 
482c8e1f6d2SMark J Musante 	va_start(adx, fmt);
4834445fffbSMatthew Ahrens 	log_internal(fnvlist_alloc(), operation, spa, htx, fmt, adx);
484c8e1f6d2SMark J Musante 	va_end(adx);
485c8e1f6d2SMark J Musante 
486c8e1f6d2SMark J Musante 	/* if we didn't get a tx from the caller, commit the one we made */
487c8e1f6d2SMark J Musante 	if (tx == NULL)
488c8e1f6d2SMark J Musante 		dmu_tx_commit(htx);
489c8e1f6d2SMark J Musante }
490c8e1f6d2SMark J Musante 
491c8e1f6d2SMark J Musante void
4924445fffbSMatthew Ahrens spa_history_log_internal_ds(dsl_dataset_t *ds, const char *operation,
4934445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...)
4944445fffbSMatthew Ahrens {
4954445fffbSMatthew Ahrens 	va_list adx;
4964445fffbSMatthew Ahrens 	char namebuf[MAXNAMELEN];
4974445fffbSMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
4984445fffbSMatthew Ahrens 
4994445fffbSMatthew Ahrens 	ASSERT(tx != NULL);
5004445fffbSMatthew Ahrens 
5014445fffbSMatthew Ahrens 	dsl_dataset_name(ds, namebuf);
5024445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
5034445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID, ds->ds_object);
5044445fffbSMatthew Ahrens 
5054445fffbSMatthew Ahrens 	va_start(adx, fmt);
5064445fffbSMatthew Ahrens 	log_internal(nvl, operation, dsl_dataset_get_spa(ds), tx, fmt, adx);
5074445fffbSMatthew Ahrens 	va_end(adx);
5084445fffbSMatthew Ahrens }
5094445fffbSMatthew Ahrens 
5104445fffbSMatthew Ahrens void
5114445fffbSMatthew Ahrens spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
5124445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...)
5134445fffbSMatthew Ahrens {
5144445fffbSMatthew Ahrens 	va_list adx;
5154445fffbSMatthew Ahrens 	char namebuf[MAXNAMELEN];
5164445fffbSMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
5174445fffbSMatthew Ahrens 
5184445fffbSMatthew Ahrens 	ASSERT(tx != NULL);
5194445fffbSMatthew Ahrens 
5204445fffbSMatthew Ahrens 	dsl_dir_name(dd, namebuf);
5214445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
5224445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID,
523*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj);
5244445fffbSMatthew Ahrens 
5254445fffbSMatthew Ahrens 	va_start(adx, fmt);
5264445fffbSMatthew Ahrens 	log_internal(nvl, operation, dd->dd_pool->dp_spa, tx, fmt, adx);
5274445fffbSMatthew Ahrens 	va_end(adx);
5284445fffbSMatthew Ahrens }
5294445fffbSMatthew Ahrens 
5304445fffbSMatthew Ahrens void
5314445fffbSMatthew Ahrens spa_history_log_version(spa_t *spa, const char *operation)
532c8e1f6d2SMark J Musante {
5334445fffbSMatthew Ahrens 	spa_history_log_internal(spa, operation, NULL,
5344445fffbSMatthew Ahrens 	    "pool version %llu; software version %llu/%d; uts %s %s %s %s",
5353b2aab18SMatthew Ahrens 	    (u_longlong_t)spa_version(spa), SPA_VERSION, ZPL_VERSION,
5364445fffbSMatthew Ahrens 	    utsname.nodename, utsname.release, utsname.version,
5374445fffbSMatthew Ahrens 	    utsname.machine);
538c8e1f6d2SMark J Musante }
539