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