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.
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.
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]
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.
30 #include <sys/spa_impl.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/cmn_err.h>
39 #include <sys/sunddi.h>
41 #include "zfs_comutil.h"
47 * Routines to manage the on-disk history log.
49 * The history log is stored as a dmu object containing
50 * <packed record length, record nvlist> tuples.
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.
56 * The log is implemented as a ring buffer, though the original creation
57 * of the pool ('zpool create') is never overwritten.
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.
71 * 'sh_records_lost' keeps track of how many records have been overwritten
72 * and permanently lost.
75 /* convert a logical offset to physical */
77 spa_history_log_to_phys(uint64_t log_off
, spa_history_phys_t
*shpp
)
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
);
87 spa_history_create_obj(spa_t
*spa
, dmu_tx_t
*tx
)
90 spa_history_phys_t
*shpp
;
91 objset_t
*mos
= spa
->spa_meta_objset
;
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
);
98 VERIFY(zap_add(mos
, DMU_POOL_DIRECTORY_OBJECT
,
99 DMU_POOL_HISTORY
, sizeof (uint64_t), 1,
100 &spa
->spa_history
, tx
) == 0);
102 VERIFY(0 == dmu_bonus_hold(mos
, spa
->spa_history
, FTAG
, &dbp
));
103 ASSERT(dbp
->db_size
>= sizeof (spa_history_phys_t
));
106 dmu_buf_will_dirty(dbp
, tx
);
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.
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);
117 dmu_buf_rele(dbp
, FTAG
);
121 * Change 'sh_bof' to the beginning of the next record.
124 spa_history_advance_bof(spa_t
*spa
, spa_history_phys_t
*shpp
)
126 objset_t
*mos
= spa
->spa_meta_objset
;
127 uint64_t firstread
, reclen
, phys_bof
;
128 char buf
[sizeof (reclen
)];
131 phys_bof
= spa_history_log_to_phys(shpp
->sh_bof
, shpp
);
132 firstread
= MIN(sizeof (reclen
), shpp
->sh_phys_max_off
- phys_bof
);
134 if ((err
= dmu_read(mos
, spa
->spa_history
, phys_bof
, firstread
,
135 buf
, DMU_READ_PREFETCH
)) != 0)
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)
144 reclen
= LE_64(*((uint64_t *)buf
));
145 shpp
->sh_bof
+= reclen
+ sizeof (reclen
);
146 shpp
->sh_records_lost
++;
151 spa_history_write(spa_t
*spa
, void *buf
, uint64_t len
, spa_history_phys_t
*shpp
,
154 uint64_t firstwrite
, phys_eof
;
155 objset_t
*mos
= spa
->spa_meta_objset
;
158 ASSERT(MUTEX_HELD(&spa
->spa_history_lock
));
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) {
168 phys_eof
= spa_history_log_to_phys(shpp
->sh_eof
, shpp
);
169 firstwrite
= MIN(len
, shpp
->sh_phys_max_off
- phys_eof
);
171 dmu_write(mos
, spa
->spa_history
, phys_eof
, firstwrite
, buf
, tx
);
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
);
184 spa_history_zone(void)
187 if (INGLOBALZONE(curproc
))
189 return (curproc
->p_zone
->zone_name
);
196 * Post a history sysevent.
198 * The nvlist_t* passed into this function will be transformed into a new
201 * 1. Nested nvlists will be flattened to a single level
202 * 2. Keys will have their names normalized (to remove any problematic
203 * characters, such as whitespace)
205 * The nvlist_t passed into this function will duplicated and should be freed
210 spa_history_log_notify(spa_t
*spa
, nvlist_t
*nvl
)
212 nvlist_t
*hist_nvl
= fnvlist_alloc();
216 if (nvlist_lookup_string(nvl
, ZPOOL_HIST_CMD
, &string
) == 0)
217 fnvlist_add_string(hist_nvl
, ZFS_EV_HIST_CMD
, string
);
219 if (nvlist_lookup_string(nvl
, ZPOOL_HIST_INT_NAME
, &string
) == 0)
220 fnvlist_add_string(hist_nvl
, ZFS_EV_HIST_INT_NAME
, string
);
222 if (nvlist_lookup_string(nvl
, ZPOOL_HIST_ZONE
, &string
) == 0)
223 fnvlist_add_string(hist_nvl
, ZFS_EV_HIST_ZONE
, string
);
225 if (nvlist_lookup_string(nvl
, ZPOOL_HIST_HOST
, &string
) == 0)
226 fnvlist_add_string(hist_nvl
, ZFS_EV_HIST_HOST
, string
);
228 if (nvlist_lookup_string(nvl
, ZPOOL_HIST_DSNAME
, &string
) == 0)
229 fnvlist_add_string(hist_nvl
, ZFS_EV_HIST_DSNAME
, string
);
231 if (nvlist_lookup_string(nvl
, ZPOOL_HIST_INT_STR
, &string
) == 0)
232 fnvlist_add_string(hist_nvl
, ZFS_EV_HIST_INT_STR
, string
);
234 if (nvlist_lookup_string(nvl
, ZPOOL_HIST_IOCTL
, &string
) == 0)
235 fnvlist_add_string(hist_nvl
, ZFS_EV_HIST_IOCTL
, string
);
237 if (nvlist_lookup_string(nvl
, ZPOOL_HIST_INT_NAME
, &string
) == 0)
238 fnvlist_add_string(hist_nvl
, ZFS_EV_HIST_INT_NAME
, string
);
240 if (nvlist_lookup_uint64(nvl
, ZPOOL_HIST_DSID
, &uint64
) == 0)
241 fnvlist_add_uint64(hist_nvl
, ZFS_EV_HIST_DSID
, uint64
);
243 if (nvlist_lookup_uint64(nvl
, ZPOOL_HIST_TXG
, &uint64
) == 0)
244 fnvlist_add_uint64(hist_nvl
, ZFS_EV_HIST_TXG
, uint64
);
246 if (nvlist_lookup_uint64(nvl
, ZPOOL_HIST_TIME
, &uint64
) == 0)
247 fnvlist_add_uint64(hist_nvl
, ZFS_EV_HIST_TIME
, uint64
);
249 if (nvlist_lookup_uint64(nvl
, ZPOOL_HIST_WHO
, &uint64
) == 0)
250 fnvlist_add_uint64(hist_nvl
, ZFS_EV_HIST_WHO
, uint64
);
252 if (nvlist_lookup_uint64(nvl
, ZPOOL_HIST_INT_EVENT
, &uint64
) == 0)
253 fnvlist_add_uint64(hist_nvl
, ZFS_EV_HIST_INT_EVENT
, uint64
);
255 spa_event_notify(spa
, NULL
, hist_nvl
, ESC_ZFS_HISTORY_EVENT
);
257 nvlist_free(hist_nvl
);
261 * Write out a history event.
265 spa_history_log_sync(void *arg
, dmu_tx_t
*tx
)
268 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
269 objset_t
*mos
= spa
->spa_meta_objset
;
271 spa_history_phys_t
*shpp
;
274 char *record_packed
= NULL
;
278 * If we have an older pool that doesn't have a command
279 * history object, create it now.
281 mutex_enter(&spa
->spa_history_lock
);
282 if (!spa
->spa_history
)
283 spa_history_create_obj(spa
, tx
);
284 mutex_exit(&spa
->spa_history_lock
);
287 * Get the offset of where we need to write via the bonus buffer.
288 * Update the offset when the write completes.
290 VERIFY0(dmu_bonus_hold(mos
, spa
->spa_history
, FTAG
, &dbp
));
293 dmu_buf_will_dirty(dbp
, tx
);
297 dmu_object_info_t doi
;
298 dmu_object_info_from_db(dbp
, &doi
);
299 ASSERT3U(doi
.doi_bonus_type
, ==, DMU_OT_SPA_HISTORY_OFFSETS
);
303 fnvlist_add_uint64(nvl
, ZPOOL_HIST_TIME
, gethrestime_sec());
305 fnvlist_add_string(nvl
, ZPOOL_HIST_HOST
, utsname
.nodename
);
307 if (nvlist_exists(nvl
, ZPOOL_HIST_CMD
)) {
308 zfs_dbgmsg("command: %s",
309 fnvlist_lookup_string(nvl
, ZPOOL_HIST_CMD
));
310 } else if (nvlist_exists(nvl
, ZPOOL_HIST_INT_NAME
)) {
311 if (nvlist_exists(nvl
, ZPOOL_HIST_DSNAME
)) {
312 zfs_dbgmsg("txg %lld %s %s (id %llu) %s",
313 fnvlist_lookup_uint64(nvl
, ZPOOL_HIST_TXG
),
314 fnvlist_lookup_string(nvl
, ZPOOL_HIST_INT_NAME
),
315 fnvlist_lookup_string(nvl
, ZPOOL_HIST_DSNAME
),
316 fnvlist_lookup_uint64(nvl
, ZPOOL_HIST_DSID
),
317 fnvlist_lookup_string(nvl
, ZPOOL_HIST_INT_STR
));
319 zfs_dbgmsg("txg %lld %s %s",
320 fnvlist_lookup_uint64(nvl
, ZPOOL_HIST_TXG
),
321 fnvlist_lookup_string(nvl
, ZPOOL_HIST_INT_NAME
),
322 fnvlist_lookup_string(nvl
, ZPOOL_HIST_INT_STR
));
325 * The history sysevent is posted only for internal history
326 * messages to show what has happened, not how it happened. For
327 * example, the following command:
329 * # zfs destroy -r tank/foo
331 * will result in one sysevent posted per dataset that is
332 * destroyed as a result of the command - which could be more
333 * than one event in total. By contrast, if the sysevent was
334 * posted as a result of the ZPOOL_HIST_CMD key being present
335 * it would result in only one sysevent being posted with the
336 * full command line arguments, requiring the consumer to know
337 * how to parse and understand zfs(1M) command invocations.
339 spa_history_log_notify(spa
, nvl
);
340 } else if (nvlist_exists(nvl
, ZPOOL_HIST_IOCTL
)) {
341 zfs_dbgmsg("ioctl %s",
342 fnvlist_lookup_string(nvl
, ZPOOL_HIST_IOCTL
));
345 record_packed
= fnvlist_pack(nvl
, &reclen
);
347 mutex_enter(&spa
->spa_history_lock
);
349 /* write out the packed length as little endian */
350 le_len
= LE_64((uint64_t)reclen
);
351 ret
= spa_history_write(spa
, &le_len
, sizeof (le_len
), shpp
, tx
);
353 ret
= spa_history_write(spa
, record_packed
, reclen
, shpp
, tx
);
355 /* The first command is the create, which we keep forever */
356 if (ret
== 0 && shpp
->sh_pool_create_len
== 0 &&
357 nvlist_exists(nvl
, ZPOOL_HIST_CMD
)) {
358 shpp
->sh_pool_create_len
= shpp
->sh_bof
= shpp
->sh_eof
;
361 mutex_exit(&spa
->spa_history_lock
);
362 fnvlist_pack_free(record_packed
, reclen
);
363 dmu_buf_rele(dbp
, FTAG
);
368 * Write out a history event.
371 spa_history_log(spa_t
*spa
, const char *msg
)
374 nvlist_t
*nvl
= fnvlist_alloc();
376 fnvlist_add_string(nvl
, ZPOOL_HIST_CMD
, msg
);
377 err
= spa_history_log_nvl(spa
, nvl
);
383 spa_history_log_nvl(spa_t
*spa
, nvlist_t
*nvl
)
389 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
|| !spa_writeable(spa
))
390 return (SET_ERROR(EINVAL
));
392 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
393 err
= dmu_tx_assign(tx
, TXG_WAIT
);
399 nvarg
= fnvlist_dup(nvl
);
400 if (spa_history_zone() != NULL
) {
401 fnvlist_add_string(nvarg
, ZPOOL_HIST_ZONE
,
404 fnvlist_add_uint64(nvarg
, ZPOOL_HIST_WHO
, crgetruid(CRED()));
406 /* Kick this off asynchronously; errors are ignored. */
407 dsl_sync_task_nowait(spa_get_dsl(spa
), spa_history_log_sync
,
408 nvarg
, 0, ZFS_SPACE_CHECK_NONE
, tx
);
411 /* spa_history_log_sync will free nvl */
417 * Read out the command history.
420 spa_history_get(spa_t
*spa
, uint64_t *offp
, uint64_t *len
, char *buf
)
422 objset_t
*mos
= spa
->spa_meta_objset
;
424 uint64_t read_len
, phys_read_off
, phys_eof
;
425 uint64_t leftover
= 0;
426 spa_history_phys_t
*shpp
;
430 * If the command history doesn't exist (older pool),
431 * that's ok, just return ENOENT.
433 if (!spa
->spa_history
)
434 return (SET_ERROR(ENOENT
));
437 * The history is logged asynchronously, so when they request
438 * the first chunk of history, make sure everything has been
439 * synced to disk so that we get it.
441 if (*offp
== 0 && spa_writeable(spa
))
442 txg_wait_synced(spa_get_dsl(spa
), 0);
444 if ((err
= dmu_bonus_hold(mos
, spa
->spa_history
, FTAG
, &dbp
)) != 0)
450 dmu_object_info_t doi
;
451 dmu_object_info_from_db(dbp
, &doi
);
452 ASSERT3U(doi
.doi_bonus_type
, ==, DMU_OT_SPA_HISTORY_OFFSETS
);
456 mutex_enter(&spa
->spa_history_lock
);
457 phys_eof
= spa_history_log_to_phys(shpp
->sh_eof
, shpp
);
459 if (*offp
< shpp
->sh_pool_create_len
) {
460 /* read in just the zpool create history */
461 phys_read_off
= *offp
;
462 read_len
= MIN(*len
, shpp
->sh_pool_create_len
-
466 * Need to reset passed in offset to BOF if the passed in
467 * offset has since been overwritten.
469 *offp
= MAX(*offp
, shpp
->sh_bof
);
470 phys_read_off
= spa_history_log_to_phys(*offp
, shpp
);
473 * Read up to the minimum of what the user passed down or
474 * the EOF (physical or logical). If we hit physical EOF,
475 * use 'leftover' to read from the physical BOF.
477 if (phys_read_off
<= phys_eof
) {
478 read_len
= MIN(*len
, phys_eof
- phys_read_off
);
481 shpp
->sh_phys_max_off
- phys_read_off
);
482 if (phys_read_off
+ *len
> shpp
->sh_phys_max_off
) {
483 leftover
= MIN(*len
- read_len
,
484 phys_eof
- shpp
->sh_pool_create_len
);
489 /* offset for consumer to use next */
490 *offp
+= read_len
+ leftover
;
492 /* tell the consumer how much you actually read */
493 *len
= read_len
+ leftover
;
496 mutex_exit(&spa
->spa_history_lock
);
497 dmu_buf_rele(dbp
, FTAG
);
501 err
= dmu_read(mos
, spa
->spa_history
, phys_read_off
, read_len
, buf
,
503 if (leftover
&& err
== 0) {
504 err
= dmu_read(mos
, spa
->spa_history
, shpp
->sh_pool_create_len
,
505 leftover
, buf
+ read_len
, DMU_READ_PREFETCH
);
507 mutex_exit(&spa
->spa_history_lock
);
509 dmu_buf_rele(dbp
, FTAG
);
514 * The nvlist will be consumed by this call.
517 log_internal(nvlist_t
*nvl
, const char *operation
, spa_t
*spa
,
518 dmu_tx_t
*tx
, const char *fmt
, va_list adx
)
523 * If this is part of creating a pool, not everything is
524 * initialized yet, so don't bother logging the internal events.
525 * Likewise if the pool is not writeable.
527 if (tx
->tx_txg
== TXG_INITIAL
|| !spa_writeable(spa
)) {
532 msg
= kmem_alloc(vsnprintf(NULL
, 0, fmt
, adx
) + 1, KM_SLEEP
);
533 (void) vsprintf(msg
, fmt
, adx
);
534 fnvlist_add_string(nvl
, ZPOOL_HIST_INT_STR
, msg
);
537 fnvlist_add_string(nvl
, ZPOOL_HIST_INT_NAME
, operation
);
538 fnvlist_add_uint64(nvl
, ZPOOL_HIST_TXG
, tx
->tx_txg
);
540 if (dmu_tx_is_syncing(tx
)) {
541 spa_history_log_sync(nvl
, tx
);
543 dsl_sync_task_nowait(spa_get_dsl(spa
),
544 spa_history_log_sync
, nvl
, 0, ZFS_SPACE_CHECK_NONE
, tx
);
546 /* spa_history_log_sync() will free nvl */
550 spa_history_log_internal(spa_t
*spa
, const char *operation
,
551 dmu_tx_t
*tx
, const char *fmt
, ...)
556 /* create a tx if we didn't get one */
558 htx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
559 if (dmu_tx_assign(htx
, TXG_WAIT
) != 0) {
566 log_internal(fnvlist_alloc(), operation
, spa
, htx
, fmt
, adx
);
569 /* if we didn't get a tx from the caller, commit the one we made */
575 spa_history_log_internal_ds(dsl_dataset_t
*ds
, const char *operation
,
576 dmu_tx_t
*tx
, const char *fmt
, ...)
579 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
580 nvlist_t
*nvl
= fnvlist_alloc();
584 dsl_dataset_name(ds
, namebuf
);
585 fnvlist_add_string(nvl
, ZPOOL_HIST_DSNAME
, namebuf
);
586 fnvlist_add_uint64(nvl
, ZPOOL_HIST_DSID
, ds
->ds_object
);
589 log_internal(nvl
, operation
, dsl_dataset_get_spa(ds
), tx
, fmt
, adx
);
594 spa_history_log_internal_dd(dsl_dir_t
*dd
, const char *operation
,
595 dmu_tx_t
*tx
, const char *fmt
, ...)
598 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
599 nvlist_t
*nvl
= fnvlist_alloc();
603 dsl_dir_name(dd
, namebuf
);
604 fnvlist_add_string(nvl
, ZPOOL_HIST_DSNAME
, namebuf
);
605 fnvlist_add_uint64(nvl
, ZPOOL_HIST_DSID
,
606 dsl_dir_phys(dd
)->dd_head_dataset_obj
);
609 log_internal(nvl
, operation
, dd
->dd_pool
->dp_spa
, tx
, fmt
, adx
);
614 spa_history_log_version(spa_t
*spa
, const char *operation
)
616 spa_history_log_internal(spa
, operation
, NULL
,
617 "pool version %llu; software version %llu/%d; uts %s %s %s %s",
618 (u_longlong_t
)spa_version(spa
), SPA_VERSION
, ZPL_VERSION
,
619 utsname
.nodename
, utsname
.release
, utsname
.version
,