xref: /freebsd/sys/contrib/openzfs/module/zfs/spa_history.c (revision 34f9f5680c9d5d5138e427ba3aeab0138cec3882)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 /*
14  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
15  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
16  * Copyright (c) 2014 Integros [integros.com]
17  * Copyright 2017 Joyent, Inc.
18  */
19 
20 #include <sys/spa.h>
21 #include <sys/spa_impl.h>
22 #include <sys/zap.h>
23 #include <sys/dsl_synctask.h>
24 #include <sys/dmu_tx.h>
25 #include <sys/dmu_objset.h>
26 #include <sys/dsl_dataset.h>
27 #include <sys/dsl_dir.h>
28 #include <sys/cmn_err.h>
29 #include <sys/sunddi.h>
30 #include <sys/cred.h>
31 #include "zfs_comutil.h"
32 #include "zfs_gitrev.h"
33 
34 /*
35  * Routines to manage the on-disk history log.
36  *
37  * The history log is stored as a dmu object containing
38  * <packed record length, record nvlist> tuples.
39  *
40  * Where "record nvlist" is an nvlist containing uint64_ts and strings, and
41  * "packed record length" is the packed length of the "record nvlist" stored
42  * as a little endian uint64_t.
43  *
44  * The log is implemented as a ring buffer, though the original creation
45  * of the pool ('zpool create') is never overwritten.
46  *
47  * The history log is tracked as object 'spa_t::spa_history'.  The bonus buffer
48  * of 'spa_history' stores the offsets for logging/retrieving history as
49  * 'spa_history_phys_t'.  'sh_pool_create_len' is the ending offset in bytes of
50  * where the 'zpool create' record is stored.  This allows us to never
51  * overwrite the original creation of the pool.  'sh_phys_max_off' is the
52  * physical ending offset in bytes of the log.  This tells you the length of
53  * the buffer. 'sh_eof' is the logical EOF (in bytes).  Whenever a record
54  * is added, 'sh_eof' is incremented by the size of the record.
55  * 'sh_eof' is never decremented.  'sh_bof' is the logical BOF (in bytes).
56  * This is where the consumer should start reading from after reading in
57  * the 'zpool create' portion of the log.
58  *
59  * 'sh_records_lost' keeps track of how many records have been overwritten
60  * and permanently lost.
61  */
62 
63 /* convert a logical offset to physical */
64 static uint64_t
65 spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
66 {
67 	uint64_t phys_len;
68 
69 	phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
70 	return ((log_off - shpp->sh_pool_create_len) % phys_len
71 	    + shpp->sh_pool_create_len);
72 }
73 
74 void
75 spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
76 {
77 	dmu_buf_t *dbp;
78 	spa_history_phys_t *shpp;
79 	objset_t *mos = spa->spa_meta_objset;
80 
81 	ASSERT0(spa->spa_history);
82 	spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
83 	    SPA_OLD_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
84 	    sizeof (spa_history_phys_t), tx);
85 
86 	VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
87 	    DMU_POOL_HISTORY, sizeof (uint64_t), 1,
88 	    &spa->spa_history, tx));
89 
90 	VERIFY0(dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
91 	ASSERT3U(dbp->db_size, >=, sizeof (spa_history_phys_t));
92 
93 	shpp = dbp->db_data;
94 	dmu_buf_will_dirty(dbp, tx);
95 
96 	/*
97 	 * Figure out maximum size of history log.  We set it at
98 	 * 0.1% of pool size, with a max of 1G and min of 128KB.
99 	 */
100 	shpp->sh_phys_max_off =
101 	    metaslab_class_get_dspace(spa_normal_class(spa)) / 1000;
102 	shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 1<<30);
103 	shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
104 
105 	dmu_buf_rele(dbp, FTAG);
106 }
107 
108 /*
109  * Change 'sh_bof' to the beginning of the next record.
110  */
111 static int
112 spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
113 {
114 	objset_t *mos = spa->spa_meta_objset;
115 	uint64_t firstread, reclen, phys_bof;
116 	char buf[sizeof (reclen)];
117 	int err;
118 
119 	phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
120 	firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
121 
122 	if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
123 	    buf, DMU_READ_PREFETCH)) != 0)
124 		return (err);
125 	if (firstread != sizeof (reclen)) {
126 		if ((err = dmu_read(mos, spa->spa_history,
127 		    shpp->sh_pool_create_len, sizeof (reclen) - firstread,
128 		    buf + firstread, DMU_READ_PREFETCH)) != 0)
129 			return (err);
130 	}
131 
132 	reclen = LE_64(*((uint64_t *)buf));
133 	shpp->sh_bof += reclen + sizeof (reclen);
134 	shpp->sh_records_lost++;
135 	return (0);
136 }
137 
138 static int
139 spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
140     dmu_tx_t *tx)
141 {
142 	uint64_t firstwrite, phys_eof;
143 	objset_t *mos = spa->spa_meta_objset;
144 	int err;
145 
146 	ASSERT(MUTEX_HELD(&spa->spa_history_lock));
147 
148 	/* see if we need to reset logical BOF */
149 	while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
150 	    (shpp->sh_eof - shpp->sh_bof) <= len) {
151 		if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
152 			return (err);
153 		}
154 	}
155 
156 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
157 	firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
158 	shpp->sh_eof += len;
159 	dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx,
160 	    DMU_READ_NO_PREFETCH);
161 
162 	len -= firstwrite;
163 	if (len > 0) {
164 		/* write out the rest at the beginning of physical file */
165 		dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
166 		    len, (char *)buf + firstwrite, tx, DMU_READ_NO_PREFETCH);
167 	}
168 
169 	return (0);
170 }
171 
172 /*
173  * Post a history sysevent.
174  *
175  * The nvlist_t* passed into this function will be transformed into a new
176  * nvlist where:
177  *
178  * 1. Nested nvlists will be flattened to a single level
179  * 2. Keys will have their names normalized (to remove any problematic
180  * characters, such as whitespace)
181  *
182  * The nvlist_t passed into this function will duplicated and should be freed
183  * by caller.
184  *
185  */
186 static void
187 spa_history_log_notify(spa_t *spa, nvlist_t *nvl)
188 {
189 	nvlist_t *hist_nvl = fnvlist_alloc();
190 	uint64_t uint64;
191 	const char *string;
192 
193 	if (nvlist_lookup_string(nvl, ZPOOL_HIST_CMD, &string) == 0)
194 		fnvlist_add_string(hist_nvl, ZFS_EV_HIST_CMD, string);
195 
196 	if (nvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME, &string) == 0)
197 		fnvlist_add_string(hist_nvl, ZFS_EV_HIST_INT_NAME, string);
198 
199 	if (nvlist_lookup_string(nvl, ZPOOL_HIST_ZONE, &string) == 0)
200 		fnvlist_add_string(hist_nvl, ZFS_EV_HIST_ZONE, string);
201 
202 	if (nvlist_lookup_string(nvl, ZPOOL_HIST_HOST, &string) == 0)
203 		fnvlist_add_string(hist_nvl, ZFS_EV_HIST_HOST, string);
204 
205 	if (nvlist_lookup_string(nvl, ZPOOL_HIST_DSNAME, &string) == 0)
206 		fnvlist_add_string(hist_nvl, ZFS_EV_HIST_DSNAME, string);
207 
208 	if (nvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR, &string) == 0)
209 		fnvlist_add_string(hist_nvl, ZFS_EV_HIST_INT_STR, string);
210 
211 	if (nvlist_lookup_string(nvl, ZPOOL_HIST_IOCTL, &string) == 0)
212 		fnvlist_add_string(hist_nvl, ZFS_EV_HIST_IOCTL, string);
213 
214 	if (nvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME, &string) == 0)
215 		fnvlist_add_string(hist_nvl, ZFS_EV_HIST_INT_NAME, string);
216 
217 	if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_DSID, &uint64) == 0)
218 		fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_DSID, uint64);
219 
220 	if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG, &uint64) == 0)
221 		fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_TXG, uint64);
222 
223 	if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_TIME, &uint64) == 0)
224 		fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_TIME, uint64);
225 
226 	if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_WHO, &uint64) == 0)
227 		fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_WHO, uint64);
228 
229 	if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_INT_EVENT, &uint64) == 0)
230 		fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_INT_EVENT, uint64);
231 
232 	spa_event_notify(spa, NULL, hist_nvl, ESC_ZFS_HISTORY_EVENT);
233 
234 	nvlist_free(hist_nvl);
235 }
236 
237 /*
238  * Write out a history event.
239  */
240 static void
241 spa_history_log_sync(void *arg, dmu_tx_t *tx)
242 {
243 	nvlist_t	*nvl = arg;
244 	spa_t		*spa = dmu_tx_pool(tx)->dp_spa;
245 	objset_t	*mos = spa->spa_meta_objset;
246 	dmu_buf_t	*dbp;
247 	spa_history_phys_t *shpp;
248 	size_t		reclen;
249 	uint64_t	le_len;
250 	char		*record_packed = NULL;
251 	int		ret;
252 
253 	/*
254 	 * If we have an older pool that doesn't have a command
255 	 * history object, create it now.
256 	 */
257 	mutex_enter(&spa->spa_history_lock);
258 	if (!spa->spa_history)
259 		spa_history_create_obj(spa, tx);
260 	mutex_exit(&spa->spa_history_lock);
261 
262 	/*
263 	 * Get the offset of where we need to write via the bonus buffer.
264 	 * Update the offset when the write completes.
265 	 */
266 	VERIFY0(dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
267 	shpp = dbp->db_data;
268 
269 	dmu_buf_will_dirty(dbp, tx);
270 
271 #ifdef ZFS_DEBUG
272 	{
273 		dmu_object_info_t doi;
274 		dmu_object_info_from_db(dbp, &doi);
275 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
276 	}
277 #endif
278 
279 	fnvlist_add_string(nvl, ZPOOL_HIST_HOST, utsname()->nodename);
280 
281 	if (nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
282 		zfs_dbgmsg("command: %s",
283 		    fnvlist_lookup_string(nvl, ZPOOL_HIST_CMD));
284 	} else if (nvlist_exists(nvl, ZPOOL_HIST_INT_NAME)) {
285 		if (nvlist_exists(nvl, ZPOOL_HIST_DSNAME)) {
286 			zfs_dbgmsg("txg %lld %s %s (id %llu) %s",
287 			    (longlong_t)fnvlist_lookup_uint64(nvl,
288 			    ZPOOL_HIST_TXG),
289 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
290 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_DSNAME),
291 			    (u_longlong_t)fnvlist_lookup_uint64(nvl,
292 			    ZPOOL_HIST_DSID),
293 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
294 		} else {
295 			zfs_dbgmsg("txg %lld %s %s",
296 			    (longlong_t)fnvlist_lookup_uint64(nvl,
297 			    ZPOOL_HIST_TXG),
298 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
299 			    fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
300 		}
301 		/*
302 		 * The history sysevent is posted only for internal history
303 		 * messages to show what has happened, not how it happened. For
304 		 * example, the following command:
305 		 *
306 		 * # zfs destroy -r tank/foo
307 		 *
308 		 * will result in one sysevent posted per dataset that is
309 		 * destroyed as a result of the command - which could be more
310 		 * than one event in total.  By contrast, if the sysevent was
311 		 * posted as a result of the ZPOOL_HIST_CMD key being present
312 		 * it would result in only one sysevent being posted with the
313 		 * full command line arguments, requiring the consumer to know
314 		 * how to parse and understand zfs(8) command invocations.
315 		 */
316 		spa_history_log_notify(spa, nvl);
317 	} else if (nvlist_exists(nvl, ZPOOL_HIST_IOCTL)) {
318 		zfs_dbgmsg("ioctl %s",
319 		    fnvlist_lookup_string(nvl, ZPOOL_HIST_IOCTL));
320 	}
321 
322 	VERIFY3U(nvlist_pack(nvl, &record_packed, &reclen, NV_ENCODE_NATIVE,
323 	    KM_SLEEP), ==, 0);
324 
325 	mutex_enter(&spa->spa_history_lock);
326 
327 	/* write out the packed length as little endian */
328 	le_len = LE_64((uint64_t)reclen);
329 	ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
330 	if (!ret)
331 		ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
332 
333 	/* The first command is the create, which we keep forever */
334 	if (ret == 0 && shpp->sh_pool_create_len == 0 &&
335 	    nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
336 		shpp->sh_pool_create_len = shpp->sh_bof = shpp->sh_eof;
337 	}
338 
339 	mutex_exit(&spa->spa_history_lock);
340 	fnvlist_pack_free(record_packed, reclen);
341 	dmu_buf_rele(dbp, FTAG);
342 	fnvlist_free(nvl);
343 }
344 
345 /*
346  * Write out a history event.
347  */
348 int
349 spa_history_log(spa_t *spa, const char *msg)
350 {
351 	int err;
352 	nvlist_t *nvl = fnvlist_alloc();
353 
354 	fnvlist_add_string(nvl, ZPOOL_HIST_CMD, msg);
355 	err = spa_history_log_nvl(spa, nvl);
356 	fnvlist_free(nvl);
357 	return (err);
358 }
359 
360 int
361 spa_history_log_nvl(spa_t *spa, nvlist_t *nvl)
362 {
363 	int err = 0;
364 	dmu_tx_t *tx;
365 	nvlist_t *nvarg, *in_nvl = NULL;
366 
367 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY || !spa_writeable(spa))
368 		return (SET_ERROR(EINVAL));
369 
370 	err = nvlist_lookup_nvlist(nvl, ZPOOL_HIST_INPUT_NVL, &in_nvl);
371 	if (err == 0) {
372 		(void) nvlist_remove_all(in_nvl, ZPOOL_HIDDEN_ARGS);
373 	}
374 
375 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
376 	err = dmu_tx_assign(tx, DMU_TX_WAIT);
377 	if (err) {
378 		dmu_tx_abort(tx);
379 		return (err);
380 	}
381 
382 	ASSERT3UF(tx->tx_txg, <=, spa_final_dirty_txg(spa),
383 	    "Logged %s after final txg was set!", "nvlist");
384 
385 	VERIFY0(nvlist_dup(nvl, &nvarg, KM_SLEEP));
386 	if (spa_history_zone() != NULL) {
387 		fnvlist_add_string(nvarg, ZPOOL_HIST_ZONE,
388 		    spa_history_zone());
389 	}
390 	fnvlist_add_uint64(nvarg, ZPOOL_HIST_WHO, crgetruid(CRED()));
391 
392 	/*
393 	 * Since the history is recorded asynchronously, the effective time is
394 	 * now, which may be considerably before the change is made on disk.
395 	 */
396 	fnvlist_add_uint64(nvarg, ZPOOL_HIST_TIME, gethrestime_sec());
397 
398 	/* Kick this off asynchronously; errors are ignored. */
399 	dsl_sync_task_nowait(spa_get_dsl(spa), spa_history_log_sync, nvarg, tx);
400 	dmu_tx_commit(tx);
401 
402 	/* spa_history_log_sync will free nvl */
403 	return (err);
404 }
405 
406 /*
407  * Read out the command history.
408  */
409 int
410 spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
411 {
412 	objset_t *mos = spa->spa_meta_objset;
413 	dmu_buf_t *dbp;
414 	uint64_t read_len, phys_read_off, phys_eof;
415 	uint64_t leftover = 0;
416 	spa_history_phys_t *shpp;
417 	int err;
418 
419 	/*
420 	 * If the command history doesn't exist (older pool),
421 	 * that's ok, just return ENOENT.
422 	 */
423 	if (!spa->spa_history)
424 		return (SET_ERROR(ENOENT));
425 
426 	/*
427 	 * The history is logged asynchronously, so when they request
428 	 * the first chunk of history, make sure everything has been
429 	 * synced to disk so that we get it.
430 	 */
431 	if (*offp == 0 && spa_writeable(spa))
432 		txg_wait_synced(spa_get_dsl(spa), 0);
433 
434 	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
435 		return (err);
436 	shpp = dbp->db_data;
437 
438 #ifdef ZFS_DEBUG
439 	{
440 		dmu_object_info_t doi;
441 		dmu_object_info_from_db(dbp, &doi);
442 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
443 	}
444 #endif
445 
446 	mutex_enter(&spa->spa_history_lock);
447 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
448 
449 	if (*offp < shpp->sh_pool_create_len) {
450 		/* read in just the zpool create history */
451 		phys_read_off = *offp;
452 		read_len = MIN(*len, shpp->sh_pool_create_len -
453 		    phys_read_off);
454 	} else {
455 		/*
456 		 * Need to reset passed in offset to BOF if the passed in
457 		 * offset has since been overwritten.
458 		 */
459 		*offp = MAX(*offp, shpp->sh_bof);
460 		phys_read_off = spa_history_log_to_phys(*offp, shpp);
461 
462 		/*
463 		 * Read up to the minimum of what the user passed down or
464 		 * the EOF (physical or logical).  If we hit physical EOF,
465 		 * use 'leftover' to read from the physical BOF.
466 		 */
467 		if (phys_read_off <= phys_eof) {
468 			read_len = MIN(*len, phys_eof - phys_read_off);
469 		} else {
470 			read_len = MIN(*len,
471 			    shpp->sh_phys_max_off - phys_read_off);
472 			if (phys_read_off + *len > shpp->sh_phys_max_off) {
473 				leftover = MIN(*len - read_len,
474 				    phys_eof - shpp->sh_pool_create_len);
475 			}
476 		}
477 	}
478 
479 	/* offset for consumer to use next */
480 	*offp += read_len + leftover;
481 
482 	/* tell the consumer how much you actually read */
483 	*len = read_len + leftover;
484 
485 	if (read_len == 0) {
486 		mutex_exit(&spa->spa_history_lock);
487 		dmu_buf_rele(dbp, FTAG);
488 		return (0);
489 	}
490 
491 	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
492 	    DMU_READ_PREFETCH);
493 	if (leftover && err == 0) {
494 		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
495 		    leftover, buf + read_len, DMU_READ_PREFETCH);
496 	}
497 	mutex_exit(&spa->spa_history_lock);
498 
499 	dmu_buf_rele(dbp, FTAG);
500 	return (err);
501 }
502 
503 /*
504  * The nvlist will be consumed by this call.
505  */
506 static void
507 log_internal(nvlist_t *nvl, const char *operation, spa_t *spa,
508     dmu_tx_t *tx, const char *fmt, va_list adx)
509 {
510 	char *msg;
511 
512 	/*
513 	 * If this is part of creating a pool, not everything is
514 	 * initialized yet, so don't bother logging the internal events.
515 	 * Likewise if the pool is not writeable.
516 	 */
517 	if (spa_is_initializing(spa) || !spa_writeable(spa)) {
518 		fnvlist_free(nvl);
519 		return;
520 	}
521 
522 	ASSERT3UF(tx->tx_txg, <=, spa_final_dirty_txg(spa),
523 	    "Logged after final txg was set: %s %s", operation, fmt);
524 
525 	msg = kmem_vasprintf(fmt, adx);
526 	fnvlist_add_string(nvl, ZPOOL_HIST_INT_STR, msg);
527 	kmem_strfree(msg);
528 
529 	fnvlist_add_string(nvl, ZPOOL_HIST_INT_NAME, operation);
530 	fnvlist_add_uint64(nvl, ZPOOL_HIST_TXG, tx->tx_txg);
531 	fnvlist_add_uint64(nvl, ZPOOL_HIST_TIME, gethrestime_sec());
532 
533 	if (dmu_tx_is_syncing(tx)) {
534 		spa_history_log_sync(nvl, tx);
535 	} else {
536 		dsl_sync_task_nowait(spa_get_dsl(spa),
537 		    spa_history_log_sync, nvl, tx);
538 	}
539 	/* spa_history_log_sync() will free nvl */
540 }
541 
542 void
543 spa_history_log_internal(spa_t *spa, const char *operation,
544     dmu_tx_t *tx, const char *fmt, ...)
545 {
546 	dmu_tx_t *htx = tx;
547 	va_list adx;
548 
549 	/* create a tx if we didn't get one */
550 	if (tx == NULL) {
551 		htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
552 		if (dmu_tx_assign(htx, DMU_TX_WAIT) != 0) {
553 			dmu_tx_abort(htx);
554 			return;
555 		}
556 	}
557 
558 	va_start(adx, fmt);
559 	log_internal(fnvlist_alloc(), operation, spa, htx, fmt, adx);
560 	va_end(adx);
561 
562 	/* if we didn't get a tx from the caller, commit the one we made */
563 	if (tx == NULL)
564 		dmu_tx_commit(htx);
565 }
566 
567 void
568 spa_history_log_internal_ds(dsl_dataset_t *ds, const char *operation,
569     dmu_tx_t *tx, const char *fmt, ...)
570 {
571 	va_list adx;
572 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
573 	nvlist_t *nvl = fnvlist_alloc();
574 
575 	ASSERT(tx != NULL);
576 
577 	dsl_dataset_name(ds, namebuf);
578 	fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
579 	fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID, ds->ds_object);
580 
581 	va_start(adx, fmt);
582 	log_internal(nvl, operation, dsl_dataset_get_spa(ds), tx, fmt, adx);
583 	va_end(adx);
584 }
585 
586 void
587 spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
588     dmu_tx_t *tx, const char *fmt, ...)
589 {
590 	va_list adx;
591 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
592 	nvlist_t *nvl = fnvlist_alloc();
593 
594 	ASSERT(tx != NULL);
595 
596 	dsl_dir_name(dd, namebuf);
597 	fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
598 	fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID,
599 	    dsl_dir_phys(dd)->dd_head_dataset_obj);
600 
601 	va_start(adx, fmt);
602 	log_internal(nvl, operation, dd->dd_pool->dp_spa, tx, fmt, adx);
603 	va_end(adx);
604 }
605 
606 void
607 spa_history_log_version(spa_t *spa, const char *operation, dmu_tx_t *tx)
608 {
609 	utsname_t *u = utsname();
610 
611 	spa_history_log_internal(spa, operation, tx,
612 	    "pool version %llu; software version %s; uts %s %s %s %s",
613 	    (u_longlong_t)spa_version(spa), ZFS_META_GITREV,
614 	    u->nodename, u->release, u->version, u->machine);
615 }
616 
617 #ifndef _KERNEL
618 const char *
619 spa_history_zone(void)
620 {
621 	return (NULL);
622 }
623 #endif
624 
625 #if defined(_KERNEL)
626 EXPORT_SYMBOL(spa_history_create_obj);
627 EXPORT_SYMBOL(spa_history_get);
628 EXPORT_SYMBOL(spa_history_log);
629 EXPORT_SYMBOL(spa_history_log_internal);
630 EXPORT_SYMBOL(spa_history_log_version);
631 #endif
632