xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_history.c (revision c3d26abc9ee97b4f60233556aadeb57e0bd30bb9)
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.
25*c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
2606eeb2adSek  */
2706eeb2adSek 
28ecd6cf80Smarks #include <sys/spa.h>
2906eeb2adSek #include <sys/spa_impl.h>
3006eeb2adSek #include <sys/zap.h>
3106eeb2adSek #include <sys/dsl_synctask.h>
32ecd6cf80Smarks #include <sys/dmu_tx.h>
33ecd6cf80Smarks #include <sys/dmu_objset.h>
344445fffbSMatthew Ahrens #include <sys/dsl_dataset.h>
354445fffbSMatthew Ahrens #include <sys/dsl_dir.h>
36ecd6cf80Smarks #include <sys/utsname.h>
37ecd6cf80Smarks #include <sys/cmn_err.h>
38ecd6cf80Smarks #include <sys/sunddi.h>
394445fffbSMatthew Ahrens #include <sys/cred.h>
403f9d6ad7SLin Ling #include "zfs_comutil.h"
41ecd6cf80Smarks #ifdef _KERNEL
42ecd6cf80Smarks #include <sys/zone.h>
43ecd6cf80Smarks #endif
4406eeb2adSek 
4506eeb2adSek /*
4606eeb2adSek  * Routines to manage the on-disk history log.
4706eeb2adSek  *
4806eeb2adSek  * The history log is stored as a dmu object containing
4906eeb2adSek  * <packed record length, record nvlist> tuples.
5006eeb2adSek  *
5106eeb2adSek  * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
5206eeb2adSek  * "packed record length" is the packed length of the "record nvlist" stored
5306eeb2adSek  * as a little endian uint64_t.
5406eeb2adSek  *
5506eeb2adSek  * The log is implemented as a ring buffer, though the original creation
5606eeb2adSek  * of the pool ('zpool create') is never overwritten.
5706eeb2adSek  *
5806eeb2adSek  * The history log is tracked as object 'spa_t::spa_history'.  The bonus buffer
5906eeb2adSek  * of 'spa_history' stores the offsets for logging/retrieving history as
6006eeb2adSek  * 'spa_history_phys_t'.  'sh_pool_create_len' is the ending offset in bytes of
6106eeb2adSek  * where the 'zpool create' record is stored.  This allows us to never
6206eeb2adSek  * overwrite the original creation of the pool.  'sh_phys_max_off' is the
6306eeb2adSek  * physical ending offset in bytes of the log.  This tells you the length of
6406eeb2adSek  * the buffer. 'sh_eof' is the logical EOF (in bytes).  Whenever a record
6506eeb2adSek  * is added, 'sh_eof' is incremented by the the size of the record.
6606eeb2adSek  * 'sh_eof' is never decremented.  'sh_bof' is the logical BOF (in bytes).
6706eeb2adSek  * This is where the consumer should start reading from after reading in
6806eeb2adSek  * the 'zpool create' portion of the log.
6906eeb2adSek  *
7006eeb2adSek  * 'sh_records_lost' keeps track of how many records have been overwritten
7106eeb2adSek  * and permanently lost.
7206eeb2adSek  */
7306eeb2adSek 
7406eeb2adSek /* convert a logical offset to physical */
7506eeb2adSek static uint64_t
7606eeb2adSek spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
7706eeb2adSek {
7806eeb2adSek 	uint64_t phys_len;
7906eeb2adSek 
8006eeb2adSek 	phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
8106eeb2adSek 	return ((log_off - shpp->sh_pool_create_len) % phys_len
8206eeb2adSek 	    + shpp->sh_pool_create_len);
8306eeb2adSek }
8406eeb2adSek 
8506eeb2adSek void
8606eeb2adSek spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
8706eeb2adSek {
8806eeb2adSek 	dmu_buf_t *dbp;
8906eeb2adSek 	spa_history_phys_t *shpp;
9006eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
9106eeb2adSek 
9206eeb2adSek 	ASSERT(spa->spa_history == 0);
9306eeb2adSek 	spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
94b5152584SMatthew Ahrens 	    SPA_OLD_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
9506eeb2adSek 	    sizeof (spa_history_phys_t), tx);
9606eeb2adSek 
9706eeb2adSek 	VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
9806eeb2adSek 	    DMU_POOL_HISTORY, sizeof (uint64_t), 1,
9906eeb2adSek 	    &spa->spa_history, tx) == 0);
10006eeb2adSek 
10106eeb2adSek 	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
10206eeb2adSek 	ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
10306eeb2adSek 
10406eeb2adSek 	shpp = dbp->db_data;
10506eeb2adSek 	dmu_buf_will_dirty(dbp, tx);
10606eeb2adSek 
10706eeb2adSek 	/*
10806eeb2adSek 	 * Figure out maximum size of history log.  We set it at
10919b94df9SMatthew Ahrens 	 * 0.1% of pool size, with a max of 1G and min of 128KB.
11006eeb2adSek 	 */
111b24ab676SJeff Bonwick 	shpp->sh_phys_max_off =
11219b94df9SMatthew Ahrens 	    metaslab_class_get_dspace(spa_normal_class(spa)) / 1000;
11319b94df9SMatthew Ahrens 	shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 1<<30);
11406eeb2adSek 	shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
11506eeb2adSek 
11606eeb2adSek 	dmu_buf_rele(dbp, FTAG);
11706eeb2adSek }
11806eeb2adSek 
11906eeb2adSek /*
12006eeb2adSek  * Change 'sh_bof' to the beginning of the next record.
12106eeb2adSek  */
12206eeb2adSek static int
12306eeb2adSek spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
12406eeb2adSek {
12506eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
12606eeb2adSek 	uint64_t firstread, reclen, phys_bof;
12706eeb2adSek 	char buf[sizeof (reclen)];
12806eeb2adSek 	int err;
12906eeb2adSek 
13006eeb2adSek 	phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
13106eeb2adSek 	firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
13206eeb2adSek 
13306eeb2adSek 	if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
1347bfdf011SNeil Perrin 	    buf, DMU_READ_PREFETCH)) != 0)
13506eeb2adSek 		return (err);
13606eeb2adSek 	if (firstread != sizeof (reclen)) {
13706eeb2adSek 		if ((err = dmu_read(mos, spa->spa_history,
13806eeb2adSek 		    shpp->sh_pool_create_len, sizeof (reclen) - firstread,
1397bfdf011SNeil Perrin 		    buf + firstread, DMU_READ_PREFETCH)) != 0)
14006eeb2adSek 			return (err);
14106eeb2adSek 	}
14206eeb2adSek 
14306eeb2adSek 	reclen = LE_64(*((uint64_t *)buf));
14406eeb2adSek 	shpp->sh_bof += reclen + sizeof (reclen);
14506eeb2adSek 	shpp->sh_records_lost++;
14606eeb2adSek 	return (0);
14706eeb2adSek }
14806eeb2adSek 
14906eeb2adSek static int
15006eeb2adSek spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
15106eeb2adSek     dmu_tx_t *tx)
15206eeb2adSek {
15306eeb2adSek 	uint64_t firstwrite, phys_eof;
15406eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
15506eeb2adSek 	int err;
15606eeb2adSek 
15706eeb2adSek 	ASSERT(MUTEX_HELD(&spa->spa_history_lock));
15806eeb2adSek 
15906eeb2adSek 	/* see if we need to reset logical BOF */
16006eeb2adSek 	while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
16106eeb2adSek 	    (shpp->sh_eof - shpp->sh_bof) <= len) {
162ecd6cf80Smarks 		if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
16306eeb2adSek 			return (err);
164ecd6cf80Smarks 		}
16506eeb2adSek 	}
16606eeb2adSek 
16706eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
16806eeb2adSek 	firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
16906eeb2adSek 	shpp->sh_eof += len;
17006eeb2adSek 	dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
17106eeb2adSek 
17206eeb2adSek 	len -= firstwrite;
17306eeb2adSek 	if (len > 0) {
17406eeb2adSek 		/* write out the rest at the beginning of physical file */
17506eeb2adSek 		dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
17606eeb2adSek 		    len, (char *)buf + firstwrite, tx);
17706eeb2adSek 	}
17806eeb2adSek 
17906eeb2adSek 	return (0);
18006eeb2adSek }
18106eeb2adSek 
182ecd6cf80Smarks static char *
1834445fffbSMatthew Ahrens spa_history_zone(void)
184ecd6cf80Smarks {
185ecd6cf80Smarks #ifdef _KERNEL
1864445fffbSMatthew Ahrens 	if (INGLOBALZONE(curproc))
1874445fffbSMatthew Ahrens 		return (NULL);
188ecd6cf80Smarks 	return (curproc->p_zone->zone_name);
189ecd6cf80Smarks #else
1904445fffbSMatthew Ahrens 	return (NULL);
191ecd6cf80Smarks #endif
192ecd6cf80Smarks }
193ecd6cf80Smarks 
19406eeb2adSek /*
19506eeb2adSek  * Write out a history event.
19606eeb2adSek  */
197495807d7SMatthew Ahrens /*ARGSUSED*/
198e7437265Sahrens static void
1993b2aab18SMatthew Ahrens spa_history_log_sync(void *arg, dmu_tx_t *tx)
20006eeb2adSek {
2013b2aab18SMatthew Ahrens 	nvlist_t	*nvl = arg;
2023b2aab18SMatthew Ahrens 	spa_t		*spa = dmu_tx_pool(tx)->dp_spa;
20306eeb2adSek 	objset_t	*mos = spa->spa_meta_objset;
20406eeb2adSek 	dmu_buf_t	*dbp;
20506eeb2adSek 	spa_history_phys_t *shpp;
20606eeb2adSek 	size_t		reclen;
20706eeb2adSek 	uint64_t	le_len;
20806eeb2adSek 	char		*record_packed = NULL;
20906eeb2adSek 	int		ret;
21006eeb2adSek 
21106eeb2adSek 	/*
21206eeb2adSek 	 * If we have an older pool that doesn't have a command
21306eeb2adSek 	 * history object, create it now.
21406eeb2adSek 	 */
21506eeb2adSek 	mutex_enter(&spa->spa_history_lock);
21606eeb2adSek 	if (!spa->spa_history)
21706eeb2adSek 		spa_history_create_obj(spa, tx);
21806eeb2adSek 	mutex_exit(&spa->spa_history_lock);
21906eeb2adSek 
22006eeb2adSek 	/*
22106eeb2adSek 	 * Get the offset of where we need to write via the bonus buffer.
22206eeb2adSek 	 * Update the offset when the write completes.
22306eeb2adSek 	 */
2243b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
22506eeb2adSek 	shpp = dbp->db_data;
22606eeb2adSek 
22706eeb2adSek 	dmu_buf_will_dirty(dbp, tx);
22806eeb2adSek 
22906eeb2adSek #ifdef ZFS_DEBUG
23006eeb2adSek 	{
23106eeb2adSek 		dmu_object_info_t doi;
23206eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
23306eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
23406eeb2adSek 	}
23506eeb2adSek #endif
23606eeb2adSek 
2374445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvl, ZPOOL_HIST_TIME, gethrestime_sec());
238ecd6cf80Smarks #ifdef _KERNEL
2394445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_HOST, utsname.nodename);
240ecd6cf80Smarks #endif
2414445fffbSMatthew Ahrens 	if (nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
2424445fffbSMatthew Ahrens 		zfs_dbgmsg("command: %s",
2434445fffbSMatthew Ahrens 		    fnvlist_lookup_string(nvl, ZPOOL_HIST_CMD));
2444445fffbSMatthew Ahrens 	} else if (nvlist_exists(nvl, ZPOOL_HIST_INT_NAME)) {
2454445fffbSMatthew Ahrens 		if (nvlist_exists(nvl, ZPOOL_HIST_DSNAME)) {
2464445fffbSMatthew Ahrens 			zfs_dbgmsg("txg %lld %s %s (id %llu) %s",
2474445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG),
2484445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
2494445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_DSNAME),
2504445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(nvl, ZPOOL_HIST_DSID),
2514445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
2524445fffbSMatthew Ahrens 		} else {
2534445fffbSMatthew Ahrens 			zfs_dbgmsg("txg %lld %s %s",
2544445fffbSMatthew Ahrens 			    fnvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG),
2554445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
2564445fffbSMatthew Ahrens 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
2574445fffbSMatthew Ahrens 		}
2584445fffbSMatthew Ahrens 	} else if (nvlist_exists(nvl, ZPOOL_HIST_IOCTL)) {
2594445fffbSMatthew Ahrens 		zfs_dbgmsg("ioctl %s",
2604445fffbSMatthew Ahrens 		    fnvlist_lookup_string(nvl, ZPOOL_HIST_IOCTL));
261ecd6cf80Smarks 	}
262ecd6cf80Smarks 
2634445fffbSMatthew Ahrens 	record_packed = fnvlist_pack(nvl, &reclen);
26406eeb2adSek 
26506eeb2adSek 	mutex_enter(&spa->spa_history_lock);
26606eeb2adSek 
26706eeb2adSek 	/* write out the packed length as little endian */
26855434c77Sek 	le_len = LE_64((uint64_t)reclen);
26906eeb2adSek 	ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
27006eeb2adSek 	if (!ret)
27106eeb2adSek 		ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
27206eeb2adSek 
2734445fffbSMatthew Ahrens 	/* The first command is the create, which we keep forever */
2744445fffbSMatthew Ahrens 	if (ret == 0 && shpp->sh_pool_create_len == 0 &&
2754445fffbSMatthew Ahrens 	    nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
2764445fffbSMatthew Ahrens 		shpp->sh_pool_create_len = shpp->sh_bof = shpp->sh_eof;
27706eeb2adSek 	}
27806eeb2adSek 
27906eeb2adSek 	mutex_exit(&spa->spa_history_lock);
2804445fffbSMatthew Ahrens 	fnvlist_pack_free(record_packed, reclen);
28106eeb2adSek 	dmu_buf_rele(dbp, FTAG);
2824445fffbSMatthew Ahrens 	fnvlist_free(nvl);
28306eeb2adSek }
28406eeb2adSek 
28506eeb2adSek /*
28606eeb2adSek  * Write out a history event.
28706eeb2adSek  */
28806eeb2adSek int
2894445fffbSMatthew Ahrens spa_history_log(spa_t *spa, const char *msg)
2904445fffbSMatthew Ahrens {
2914445fffbSMatthew Ahrens 	int err;
2924445fffbSMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
2934445fffbSMatthew Ahrens 
2944445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_CMD, msg);
2954445fffbSMatthew Ahrens 	err = spa_history_log_nvl(spa, nvl);
2964445fffbSMatthew Ahrens 	fnvlist_free(nvl);
2974445fffbSMatthew Ahrens 	return (err);
2984445fffbSMatthew Ahrens }
2994445fffbSMatthew Ahrens 
3004445fffbSMatthew Ahrens int
3014445fffbSMatthew Ahrens spa_history_log_nvl(spa_t *spa, nvlist_t *nvl)
30206eeb2adSek {
303495807d7SMatthew Ahrens 	int err = 0;
304495807d7SMatthew Ahrens 	dmu_tx_t *tx;
3054445fffbSMatthew Ahrens 	nvlist_t *nvarg;
30606eeb2adSek 
307cd1c8b85SMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY || !spa_writeable(spa))
308be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
309e7437265Sahrens 
310495807d7SMatthew Ahrens 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
311495807d7SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
312495807d7SMatthew Ahrens 	if (err) {
313495807d7SMatthew Ahrens 		dmu_tx_abort(tx);
314495807d7SMatthew Ahrens 		return (err);
315495807d7SMatthew Ahrens 	}
316495807d7SMatthew Ahrens 
3174445fffbSMatthew Ahrens 	nvarg = fnvlist_dup(nvl);
3184445fffbSMatthew Ahrens 	if (spa_history_zone() != NULL) {
3194445fffbSMatthew Ahrens 		fnvlist_add_string(nvarg, ZPOOL_HIST_ZONE,
3204445fffbSMatthew Ahrens 		    spa_history_zone());
3214445fffbSMatthew Ahrens 	}
3224445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvarg, ZPOOL_HIST_WHO, crgetruid(CRED()));
323495807d7SMatthew Ahrens 
324495807d7SMatthew Ahrens 	/* Kick this off asynchronously; errors are ignored. */
3253b2aab18SMatthew Ahrens 	dsl_sync_task_nowait(spa_get_dsl(spa), spa_history_log_sync,
3267d46dc6cSMatthew Ahrens 	    nvarg, 0, ZFS_SPACE_CHECK_NONE, tx);
327495807d7SMatthew Ahrens 	dmu_tx_commit(tx);
328495807d7SMatthew Ahrens 
3294445fffbSMatthew Ahrens 	/* spa_history_log_sync will free nvl */
330495807d7SMatthew Ahrens 	return (err);
3314445fffbSMatthew Ahrens 
33206eeb2adSek }
33306eeb2adSek 
33406eeb2adSek /*
33506eeb2adSek  * Read out the command history.
33606eeb2adSek  */
33706eeb2adSek int
33806eeb2adSek spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
33906eeb2adSek {
34006eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
34106eeb2adSek 	dmu_buf_t *dbp;
34206eeb2adSek 	uint64_t read_len, phys_read_off, phys_eof;
34306eeb2adSek 	uint64_t leftover = 0;
34406eeb2adSek 	spa_history_phys_t *shpp;
34506eeb2adSek 	int err;
34606eeb2adSek 
34706eeb2adSek 	/*
3484445fffbSMatthew Ahrens 	 * If the command history doesn't exist (older pool),
34906eeb2adSek 	 * that's ok, just return ENOENT.
35006eeb2adSek 	 */
35106eeb2adSek 	if (!spa->spa_history)
352be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
35306eeb2adSek 
354495807d7SMatthew Ahrens 	/*
355495807d7SMatthew Ahrens 	 * The history is logged asynchronously, so when they request
356495807d7SMatthew Ahrens 	 * the first chunk of history, make sure everything has been
357495807d7SMatthew Ahrens 	 * synced to disk so that we get it.
358495807d7SMatthew Ahrens 	 */
3593c708518SMark J Musante 	if (*offp == 0 && spa_writeable(spa))
360495807d7SMatthew Ahrens 		txg_wait_synced(spa_get_dsl(spa), 0);
361495807d7SMatthew Ahrens 
36206eeb2adSek 	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
36306eeb2adSek 		return (err);
36406eeb2adSek 	shpp = dbp->db_data;
36506eeb2adSek 
36606eeb2adSek #ifdef ZFS_DEBUG
36706eeb2adSek 	{
36806eeb2adSek 		dmu_object_info_t doi;
36906eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
37006eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
37106eeb2adSek 	}
37206eeb2adSek #endif
37306eeb2adSek 
37406eeb2adSek 	mutex_enter(&spa->spa_history_lock);
37506eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
37606eeb2adSek 
37706eeb2adSek 	if (*offp < shpp->sh_pool_create_len) {
37806eeb2adSek 		/* read in just the zpool create history */
37906eeb2adSek 		phys_read_off = *offp;
38006eeb2adSek 		read_len = MIN(*len, shpp->sh_pool_create_len -
38106eeb2adSek 		    phys_read_off);
38206eeb2adSek 	} else {
38306eeb2adSek 		/*
38406eeb2adSek 		 * Need to reset passed in offset to BOF if the passed in
38506eeb2adSek 		 * offset has since been overwritten.
38606eeb2adSek 		 */
38706eeb2adSek 		*offp = MAX(*offp, shpp->sh_bof);
38806eeb2adSek 		phys_read_off = spa_history_log_to_phys(*offp, shpp);
38906eeb2adSek 
39006eeb2adSek 		/*
39106eeb2adSek 		 * Read up to the minimum of what the user passed down or
39206eeb2adSek 		 * the EOF (physical or logical).  If we hit physical EOF,
39306eeb2adSek 		 * use 'leftover' to read from the physical BOF.
39406eeb2adSek 		 */
39506eeb2adSek 		if (phys_read_off <= phys_eof) {
39606eeb2adSek 			read_len = MIN(*len, phys_eof - phys_read_off);
39706eeb2adSek 		} else {
39806eeb2adSek 			read_len = MIN(*len,
39906eeb2adSek 			    shpp->sh_phys_max_off - phys_read_off);
40006eeb2adSek 			if (phys_read_off + *len > shpp->sh_phys_max_off) {
40106eeb2adSek 				leftover = MIN(*len - read_len,
40206eeb2adSek 				    phys_eof - shpp->sh_pool_create_len);
40306eeb2adSek 			}
40406eeb2adSek 		}
40506eeb2adSek 	}
40606eeb2adSek 
40706eeb2adSek 	/* offset for consumer to use next */
40806eeb2adSek 	*offp += read_len + leftover;
40906eeb2adSek 
41006eeb2adSek 	/* tell the consumer how much you actually read */
41106eeb2adSek 	*len = read_len + leftover;
41206eeb2adSek 
41306eeb2adSek 	if (read_len == 0) {
41406eeb2adSek 		mutex_exit(&spa->spa_history_lock);
41506eeb2adSek 		dmu_buf_rele(dbp, FTAG);
41606eeb2adSek 		return (0);
41706eeb2adSek 	}
41806eeb2adSek 
4197bfdf011SNeil Perrin 	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
4207bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
42106eeb2adSek 	if (leftover && err == 0) {
42206eeb2adSek 		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
4237bfdf011SNeil Perrin 		    leftover, buf + read_len, DMU_READ_PREFETCH);
42406eeb2adSek 	}
42506eeb2adSek 	mutex_exit(&spa->spa_history_lock);
42606eeb2adSek 
42706eeb2adSek 	dmu_buf_rele(dbp, FTAG);
42806eeb2adSek 	return (err);
42906eeb2adSek }
430ecd6cf80Smarks 
4314445fffbSMatthew Ahrens /*
4324445fffbSMatthew Ahrens  * The nvlist will be consumed by this call.
4334445fffbSMatthew Ahrens  */
434c8e1f6d2SMark J Musante static void
4354445fffbSMatthew Ahrens log_internal(nvlist_t *nvl, const char *operation, spa_t *spa,
4363f9d6ad7SLin Ling     dmu_tx_t *tx, const char *fmt, va_list adx)
437ecd6cf80Smarks {
4384445fffbSMatthew Ahrens 	char *msg;
439ecd6cf80Smarks 
440088f3894Sahrens 	/*
441088f3894Sahrens 	 * If this is part of creating a pool, not everything is
442088f3894Sahrens 	 * initialized yet, so don't bother logging the internal events.
443cd1c8b85SMatthew Ahrens 	 * Likewise if the pool is not writeable.
444088f3894Sahrens 	 */
445cd1c8b85SMatthew Ahrens 	if (tx->tx_txg == TXG_INITIAL || !spa_writeable(spa)) {
446347eec8eSChristopher Siden 		fnvlist_free(nvl);
447088f3894Sahrens 		return;
448347eec8eSChristopher Siden 	}
449088f3894Sahrens 
4504445fffbSMatthew Ahrens 	msg = kmem_alloc(vsnprintf(NULL, 0, fmt, adx) + 1, KM_SLEEP);
4514445fffbSMatthew Ahrens 	(void) vsprintf(msg, fmt, adx);
4524445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_INT_STR, msg);
4534445fffbSMatthew Ahrens 	strfree(msg);
454ecd6cf80Smarks 
4554445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_INT_NAME, operation);
4564445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvl, ZPOOL_HIST_TXG, tx->tx_txg);
457e7437265Sahrens 
458e7437265Sahrens 	if (dmu_tx_is_syncing(tx)) {
4593b2aab18SMatthew Ahrens 		spa_history_log_sync(nvl, tx);
460e7437265Sahrens 	} else {
4613b2aab18SMatthew Ahrens 		dsl_sync_task_nowait(spa_get_dsl(spa),
4627d46dc6cSMatthew Ahrens 		    spa_history_log_sync, nvl, 0, ZFS_SPACE_CHECK_NONE, tx);
463e7437265Sahrens 	}
4644445fffbSMatthew Ahrens 	/* spa_history_log_sync() will free nvl */
465ecd6cf80Smarks }
466c8e1f6d2SMark J Musante 
467c8e1f6d2SMark J Musante void
4684445fffbSMatthew Ahrens spa_history_log_internal(spa_t *spa, const char *operation,
4693f9d6ad7SLin Ling     dmu_tx_t *tx, const char *fmt, ...)
470c8e1f6d2SMark J Musante {
471c8e1f6d2SMark J Musante 	dmu_tx_t *htx = tx;
472c8e1f6d2SMark J Musante 	va_list adx;
473c8e1f6d2SMark J Musante 
474c8e1f6d2SMark J Musante 	/* create a tx if we didn't get one */
475c8e1f6d2SMark J Musante 	if (tx == NULL) {
476c8e1f6d2SMark J Musante 		htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
477c8e1f6d2SMark J Musante 		if (dmu_tx_assign(htx, TXG_WAIT) != 0) {
478c8e1f6d2SMark J Musante 			dmu_tx_abort(htx);
479c8e1f6d2SMark J Musante 			return;
480c8e1f6d2SMark J Musante 		}
481c8e1f6d2SMark J Musante 	}
482c8e1f6d2SMark J Musante 
483c8e1f6d2SMark J Musante 	va_start(adx, fmt);
4844445fffbSMatthew Ahrens 	log_internal(fnvlist_alloc(), operation, spa, htx, fmt, adx);
485c8e1f6d2SMark J Musante 	va_end(adx);
486c8e1f6d2SMark J Musante 
487c8e1f6d2SMark J Musante 	/* if we didn't get a tx from the caller, commit the one we made */
488c8e1f6d2SMark J Musante 	if (tx == NULL)
489c8e1f6d2SMark J Musante 		dmu_tx_commit(htx);
490c8e1f6d2SMark J Musante }
491c8e1f6d2SMark J Musante 
492c8e1f6d2SMark J Musante void
4934445fffbSMatthew Ahrens spa_history_log_internal_ds(dsl_dataset_t *ds, const char *operation,
4944445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...)
4954445fffbSMatthew Ahrens {
4964445fffbSMatthew Ahrens 	va_list adx;
4974445fffbSMatthew Ahrens 	char namebuf[MAXNAMELEN];
4984445fffbSMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
4994445fffbSMatthew Ahrens 
5004445fffbSMatthew Ahrens 	ASSERT(tx != NULL);
5014445fffbSMatthew Ahrens 
5024445fffbSMatthew Ahrens 	dsl_dataset_name(ds, namebuf);
5034445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
5044445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID, ds->ds_object);
5054445fffbSMatthew Ahrens 
5064445fffbSMatthew Ahrens 	va_start(adx, fmt);
5074445fffbSMatthew Ahrens 	log_internal(nvl, operation, dsl_dataset_get_spa(ds), tx, fmt, adx);
5084445fffbSMatthew Ahrens 	va_end(adx);
5094445fffbSMatthew Ahrens }
5104445fffbSMatthew Ahrens 
5114445fffbSMatthew Ahrens void
5124445fffbSMatthew Ahrens spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
5134445fffbSMatthew Ahrens     dmu_tx_t *tx, const char *fmt, ...)
5144445fffbSMatthew Ahrens {
5154445fffbSMatthew Ahrens 	va_list adx;
5164445fffbSMatthew Ahrens 	char namebuf[MAXNAMELEN];
5174445fffbSMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
5184445fffbSMatthew Ahrens 
5194445fffbSMatthew Ahrens 	ASSERT(tx != NULL);
5204445fffbSMatthew Ahrens 
5214445fffbSMatthew Ahrens 	dsl_dir_name(dd, namebuf);
5224445fffbSMatthew Ahrens 	fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
5234445fffbSMatthew Ahrens 	fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID,
524c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj);
5254445fffbSMatthew Ahrens 
5264445fffbSMatthew Ahrens 	va_start(adx, fmt);
5274445fffbSMatthew Ahrens 	log_internal(nvl, operation, dd->dd_pool->dp_spa, tx, fmt, adx);
5284445fffbSMatthew Ahrens 	va_end(adx);
5294445fffbSMatthew Ahrens }
5304445fffbSMatthew Ahrens 
5314445fffbSMatthew Ahrens void
5324445fffbSMatthew Ahrens spa_history_log_version(spa_t *spa, const char *operation)
533c8e1f6d2SMark J Musante {
5344445fffbSMatthew Ahrens 	spa_history_log_internal(spa, operation, NULL,
5354445fffbSMatthew Ahrens 	    "pool version %llu; software version %llu/%d; uts %s %s %s %s",
5363b2aab18SMatthew Ahrens 	    (u_longlong_t)spa_version(spa), SPA_VERSION, ZPL_VERSION,
5374445fffbSMatthew Ahrens 	    utsname.nodename, utsname.release, utsname.version,
5384445fffbSMatthew Ahrens 	    utsname.machine);
539c8e1f6d2SMark J Musante }
540