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