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) 2012 by Delphix. All rights reserved.
28 #include <sys/spa_impl.h>
30 #include <sys/dsl_synctask.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/utsname.h>
36 #include <sys/cmn_err.h>
37 #include <sys/sunddi.h>
39 #include "zfs_comutil.h"
45 * Routines to manage the on-disk history log.
47 * The history log is stored as a dmu object containing
48 * <packed record length, record nvlist> tuples.
50 * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
51 * "packed record length" is the packed length of the "record nvlist" stored
52 * as a little endian uint64_t.
54 * The log is implemented as a ring buffer, though the original creation
55 * of the pool ('zpool create') is never overwritten.
57 * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer
58 * of 'spa_history' stores the offsets for logging/retrieving history as
59 * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of
60 * where the 'zpool create' record is stored. This allows us to never
61 * overwrite the original creation of the pool. 'sh_phys_max_off' is the
62 * physical ending offset in bytes of the log. This tells you the length of
63 * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record
64 * is added, 'sh_eof' is incremented by the the size of the record.
65 * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes).
66 * This is where the consumer should start reading from after reading in
67 * the 'zpool create' portion of the log.
69 * 'sh_records_lost' keeps track of how many records have been overwritten
70 * and permanently lost.
73 /* convert a logical offset to physical */
75 spa_history_log_to_phys(uint64_t log_off
, spa_history_phys_t
*shpp
)
79 phys_len
= shpp
->sh_phys_max_off
- shpp
->sh_pool_create_len
;
80 return ((log_off
- shpp
->sh_pool_create_len
) % phys_len
81 + shpp
->sh_pool_create_len
);
85 spa_history_create_obj(spa_t
*spa
, dmu_tx_t
*tx
)
88 spa_history_phys_t
*shpp
;
89 objset_t
*mos
= spa
->spa_meta_objset
;
91 ASSERT(spa
->spa_history
== 0);
92 spa
->spa_history
= dmu_object_alloc(mos
, DMU_OT_SPA_HISTORY
,
93 SPA_MAXBLOCKSIZE
, DMU_OT_SPA_HISTORY_OFFSETS
,
94 sizeof (spa_history_phys_t
), tx
);
96 VERIFY(zap_add(mos
, DMU_POOL_DIRECTORY_OBJECT
,
97 DMU_POOL_HISTORY
, sizeof (uint64_t), 1,
98 &spa
->spa_history
, tx
) == 0);
100 VERIFY(0 == dmu_bonus_hold(mos
, spa
->spa_history
, FTAG
, &dbp
));
101 ASSERT(dbp
->db_size
>= sizeof (spa_history_phys_t
));
104 dmu_buf_will_dirty(dbp
, tx
);
107 * Figure out maximum size of history log. We set it at
108 * 0.1% of pool size, with a max of 1G and min of 128KB.
110 shpp
->sh_phys_max_off
=
111 metaslab_class_get_dspace(spa_normal_class(spa
)) / 1000;
112 shpp
->sh_phys_max_off
= MIN(shpp
->sh_phys_max_off
, 1<<30);
113 shpp
->sh_phys_max_off
= MAX(shpp
->sh_phys_max_off
, 128<<10);
115 dmu_buf_rele(dbp
, FTAG
);
119 * Change 'sh_bof' to the beginning of the next record.
122 spa_history_advance_bof(spa_t
*spa
, spa_history_phys_t
*shpp
)
124 objset_t
*mos
= spa
->spa_meta_objset
;
125 uint64_t firstread
, reclen
, phys_bof
;
126 char buf
[sizeof (reclen
)];
129 phys_bof
= spa_history_log_to_phys(shpp
->sh_bof
, shpp
);
130 firstread
= MIN(sizeof (reclen
), shpp
->sh_phys_max_off
- phys_bof
);
132 if ((err
= dmu_read(mos
, spa
->spa_history
, phys_bof
, firstread
,
133 buf
, DMU_READ_PREFETCH
)) != 0)
135 if (firstread
!= sizeof (reclen
)) {
136 if ((err
= dmu_read(mos
, spa
->spa_history
,
137 shpp
->sh_pool_create_len
, sizeof (reclen
) - firstread
,
138 buf
+ firstread
, DMU_READ_PREFETCH
)) != 0)
142 reclen
= LE_64(*((uint64_t *)buf
));
143 shpp
->sh_bof
+= reclen
+ sizeof (reclen
);
144 shpp
->sh_records_lost
++;
149 spa_history_write(spa_t
*spa
, void *buf
, uint64_t len
, spa_history_phys_t
*shpp
,
152 uint64_t firstwrite
, phys_eof
;
153 objset_t
*mos
= spa
->spa_meta_objset
;
156 ASSERT(MUTEX_HELD(&spa
->spa_history_lock
));
158 /* see if we need to reset logical BOF */
159 while (shpp
->sh_phys_max_off
- shpp
->sh_pool_create_len
-
160 (shpp
->sh_eof
- shpp
->sh_bof
) <= len
) {
161 if ((err
= spa_history_advance_bof(spa
, shpp
)) != 0) {
166 phys_eof
= spa_history_log_to_phys(shpp
->sh_eof
, shpp
);
167 firstwrite
= MIN(len
, shpp
->sh_phys_max_off
- phys_eof
);
169 dmu_write(mos
, spa
->spa_history
, phys_eof
, firstwrite
, buf
, tx
);
173 /* write out the rest at the beginning of physical file */
174 dmu_write(mos
, spa
->spa_history
, shpp
->sh_pool_create_len
,
175 len
, (char *)buf
+ firstwrite
, tx
);
182 spa_history_zone(void)
188 return (curproc
->p_zone
->zone_name
);
196 * Write out a history event.
200 spa_history_log_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
203 nvlist_t
*nvl
= arg2
;
204 objset_t
*mos
= spa
->spa_meta_objset
;
206 spa_history_phys_t
*shpp
;
209 char *record_packed
= NULL
;
213 * If we have an older pool that doesn't have a command
214 * history object, create it now.
216 mutex_enter(&spa
->spa_history_lock
);
217 if (!spa
->spa_history
)
218 spa_history_create_obj(spa
, tx
);
219 mutex_exit(&spa
->spa_history_lock
);
222 * Get the offset of where we need to write via the bonus buffer.
223 * Update the offset when the write completes.
225 VERIFY(0 == dmu_bonus_hold(mos
, spa
->spa_history
, FTAG
, &dbp
));
228 dmu_buf_will_dirty(dbp
, tx
);
232 dmu_object_info_t doi
;
233 dmu_object_info_from_db(dbp
, &doi
);
234 ASSERT3U(doi
.doi_bonus_type
, ==, DMU_OT_SPA_HISTORY_OFFSETS
);
238 fnvlist_add_uint64(nvl
, ZPOOL_HIST_TIME
, gethrestime_sec());
240 fnvlist_add_string(nvl
, ZPOOL_HIST_HOST
, utsname
.nodename
);
242 if (nvlist_exists(nvl
, ZPOOL_HIST_CMD
)) {
243 zfs_dbgmsg("command: %s",
244 fnvlist_lookup_string(nvl
, ZPOOL_HIST_CMD
));
245 } else if (nvlist_exists(nvl
, ZPOOL_HIST_INT_NAME
)) {
246 if (nvlist_exists(nvl
, ZPOOL_HIST_DSNAME
)) {
247 zfs_dbgmsg("txg %lld %s %s (id %llu) %s",
248 fnvlist_lookup_uint64(nvl
, ZPOOL_HIST_TXG
),
249 fnvlist_lookup_string(nvl
, ZPOOL_HIST_INT_NAME
),
250 fnvlist_lookup_string(nvl
, ZPOOL_HIST_DSNAME
),
251 fnvlist_lookup_uint64(nvl
, ZPOOL_HIST_DSID
),
252 fnvlist_lookup_string(nvl
, ZPOOL_HIST_INT_STR
));
254 zfs_dbgmsg("txg %lld %s %s",
255 fnvlist_lookup_uint64(nvl
, ZPOOL_HIST_TXG
),
256 fnvlist_lookup_string(nvl
, ZPOOL_HIST_INT_NAME
),
257 fnvlist_lookup_string(nvl
, ZPOOL_HIST_INT_STR
));
259 } else if (nvlist_exists(nvl
, ZPOOL_HIST_IOCTL
)) {
260 zfs_dbgmsg("ioctl %s",
261 fnvlist_lookup_string(nvl
, ZPOOL_HIST_IOCTL
));
264 VERIFY3U(nvlist_pack(nvl
, &record_packed
, &reclen
, NV_ENCODE_NATIVE
,
265 KM_PUSHPAGE
), ==, 0);
267 mutex_enter(&spa
->spa_history_lock
);
269 /* write out the packed length as little endian */
270 le_len
= LE_64((uint64_t)reclen
);
271 ret
= spa_history_write(spa
, &le_len
, sizeof (le_len
), shpp
, tx
);
273 ret
= spa_history_write(spa
, record_packed
, reclen
, shpp
, tx
);
275 /* The first command is the create, which we keep forever */
276 if (ret
== 0 && shpp
->sh_pool_create_len
== 0 &&
277 nvlist_exists(nvl
, ZPOOL_HIST_CMD
)) {
278 shpp
->sh_pool_create_len
= shpp
->sh_bof
= shpp
->sh_eof
;
281 mutex_exit(&spa
->spa_history_lock
);
282 fnvlist_pack_free(record_packed
, reclen
);
283 dmu_buf_rele(dbp
, FTAG
);
288 * Write out a history event.
291 spa_history_log(spa_t
*spa
, const char *msg
)
296 VERIFY0(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
));
298 fnvlist_add_string(nvl
, ZPOOL_HIST_CMD
, msg
);
299 err
= spa_history_log_nvl(spa
, nvl
);
305 spa_history_log_nvl(spa_t
*spa
, nvlist_t
*nvl
)
311 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
|| !spa_writeable(spa
))
314 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
315 err
= dmu_tx_assign(tx
, TXG_WAIT
);
321 VERIFY0(nvlist_dup(nvl
, &nvarg
, KM_PUSHPAGE
));
322 if (spa_history_zone() != NULL
) {
323 fnvlist_add_string(nvarg
, ZPOOL_HIST_ZONE
,
326 fnvlist_add_uint64(nvarg
, ZPOOL_HIST_WHO
, crgetruid(CRED()));
328 /* Kick this off asynchronously; errors are ignored. */
329 dsl_sync_task_do_nowait(spa_get_dsl(spa
), NULL
,
330 spa_history_log_sync
, spa
, nvarg
, 0, tx
);
333 /* spa_history_log_sync will free nvl */
339 * Read out the command history.
342 spa_history_get(spa_t
*spa
, uint64_t *offp
, uint64_t *len
, char *buf
)
344 objset_t
*mos
= spa
->spa_meta_objset
;
346 uint64_t read_len
, phys_read_off
, phys_eof
;
347 uint64_t leftover
= 0;
348 spa_history_phys_t
*shpp
;
352 * If the command history doesn't exist (older pool),
353 * that's ok, just return ENOENT.
355 if (!spa
->spa_history
)
359 * The history is logged asynchronously, so when they request
360 * the first chunk of history, make sure everything has been
361 * synced to disk so that we get it.
363 if (*offp
== 0 && spa_writeable(spa
))
364 txg_wait_synced(spa_get_dsl(spa
), 0);
366 if ((err
= dmu_bonus_hold(mos
, spa
->spa_history
, FTAG
, &dbp
)) != 0)
372 dmu_object_info_t doi
;
373 dmu_object_info_from_db(dbp
, &doi
);
374 ASSERT3U(doi
.doi_bonus_type
, ==, DMU_OT_SPA_HISTORY_OFFSETS
);
378 mutex_enter(&spa
->spa_history_lock
);
379 phys_eof
= spa_history_log_to_phys(shpp
->sh_eof
, shpp
);
381 if (*offp
< shpp
->sh_pool_create_len
) {
382 /* read in just the zpool create history */
383 phys_read_off
= *offp
;
384 read_len
= MIN(*len
, shpp
->sh_pool_create_len
-
388 * Need to reset passed in offset to BOF if the passed in
389 * offset has since been overwritten.
391 *offp
= MAX(*offp
, shpp
->sh_bof
);
392 phys_read_off
= spa_history_log_to_phys(*offp
, shpp
);
395 * Read up to the minimum of what the user passed down or
396 * the EOF (physical or logical). If we hit physical EOF,
397 * use 'leftover' to read from the physical BOF.
399 if (phys_read_off
<= phys_eof
) {
400 read_len
= MIN(*len
, phys_eof
- phys_read_off
);
403 shpp
->sh_phys_max_off
- phys_read_off
);
404 if (phys_read_off
+ *len
> shpp
->sh_phys_max_off
) {
405 leftover
= MIN(*len
- read_len
,
406 phys_eof
- shpp
->sh_pool_create_len
);
411 /* offset for consumer to use next */
412 *offp
+= read_len
+ leftover
;
414 /* tell the consumer how much you actually read */
415 *len
= read_len
+ leftover
;
418 mutex_exit(&spa
->spa_history_lock
);
419 dmu_buf_rele(dbp
, FTAG
);
423 err
= dmu_read(mos
, spa
->spa_history
, phys_read_off
, read_len
, buf
,
425 if (leftover
&& err
== 0) {
426 err
= dmu_read(mos
, spa
->spa_history
, shpp
->sh_pool_create_len
,
427 leftover
, buf
+ read_len
, DMU_READ_PREFETCH
);
429 mutex_exit(&spa
->spa_history_lock
);
431 dmu_buf_rele(dbp
, FTAG
);
436 * The nvlist will be consumed by this call.
439 log_internal(nvlist_t
*nvl
, const char *operation
, spa_t
*spa
,
440 dmu_tx_t
*tx
, const char *fmt
, va_list adx
)
446 * If this is part of creating a pool, not everything is
447 * initialized yet, so don't bother logging the internal events.
448 * Likewise if the pool is not writeable.
450 if (tx
->tx_txg
== TXG_INITIAL
|| !spa_writeable(spa
)) {
456 msg
= kmem_alloc(vsnprintf(NULL
, 0, fmt
, adx1
) + 1, KM_PUSHPAGE
);
459 (void) vsprintf(msg
, fmt
, adx1
);
461 fnvlist_add_string(nvl
, ZPOOL_HIST_INT_STR
, msg
);
464 fnvlist_add_string(nvl
, ZPOOL_HIST_INT_NAME
, operation
);
465 fnvlist_add_uint64(nvl
, ZPOOL_HIST_TXG
, tx
->tx_txg
);
467 if (dmu_tx_is_syncing(tx
)) {
468 spa_history_log_sync(spa
, nvl
, tx
);
470 dsl_sync_task_do_nowait(spa_get_dsl(spa
), NULL
,
471 spa_history_log_sync
, spa
, nvl
, 0, tx
);
473 /* spa_history_log_sync() will free nvl */
477 spa_history_log_internal(spa_t
*spa
, const char *operation
,
478 dmu_tx_t
*tx
, const char *fmt
, ...)
484 /* create a tx if we didn't get one */
486 htx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
487 if (dmu_tx_assign(htx
, TXG_WAIT
) != 0) {
494 VERIFY0(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
));
495 log_internal(nvl
, operation
, spa
, htx
, fmt
, adx
);
498 /* if we didn't get a tx from the caller, commit the one we made */
504 spa_history_log_internal_ds(dsl_dataset_t
*ds
, const char *operation
,
505 dmu_tx_t
*tx
, const char *fmt
, ...)
508 char namebuf
[MAXNAMELEN
];
513 dsl_dataset_name(ds
, namebuf
);
514 VERIFY0(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
));
515 fnvlist_add_string(nvl
, ZPOOL_HIST_DSNAME
, namebuf
);
516 fnvlist_add_uint64(nvl
, ZPOOL_HIST_DSID
, ds
->ds_object
);
519 log_internal(nvl
, operation
, dsl_dataset_get_spa(ds
), tx
, fmt
, adx
);
524 spa_history_log_internal_dd(dsl_dir_t
*dd
, const char *operation
,
525 dmu_tx_t
*tx
, const char *fmt
, ...)
528 char namebuf
[MAXNAMELEN
];
533 dsl_dir_name(dd
, namebuf
);
534 VERIFY0(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
));
535 fnvlist_add_string(nvl
, ZPOOL_HIST_DSNAME
, namebuf
);
536 fnvlist_add_uint64(nvl
, ZPOOL_HIST_DSID
,
537 dd
->dd_phys
->dd_head_dataset_obj
);
540 log_internal(nvl
, operation
, dd
->dd_pool
->dp_spa
, tx
, fmt
, adx
);
545 spa_history_log_version(spa_t
*spa
, const char *operation
)
548 uint64_t current_vers
= spa_version(spa
);
550 spa_history_log_internal(spa
, operation
, NULL
,
551 "pool version %llu; software version %llu/%d; uts %s %s %s %s",
552 (u_longlong_t
)current_vers
, SPA_VERSION
, ZPL_VERSION
,
553 utsname
.nodename
, utsname
.release
, utsname
.version
,
555 cmn_err(CE_CONT
, "!%s version %llu pool %s using %llu", operation
,
556 (u_longlong_t
)current_vers
, spa_name(spa
), SPA_VERSION
);
560 #if defined(_KERNEL) && defined(HAVE_SPL)
561 EXPORT_SYMBOL(spa_history_create_obj
);
562 EXPORT_SYMBOL(spa_history_get
);
563 EXPORT_SYMBOL(spa_history_log
);
564 EXPORT_SYMBOL(spa_history_log_internal
);
565 EXPORT_SYMBOL(spa_history_log_version
);