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 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/utsname.h>
34 #include <sys/cmn_err.h>
35 #include <sys/sunddi.h>
36 #include "zfs_comutil.h"
42 * Routines to manage the on-disk history log.
44 * The history log is stored as a dmu object containing
45 * <packed record length, record nvlist> tuples.
47 * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
48 * "packed record length" is the packed length of the "record nvlist" stored
49 * as a little endian uint64_t.
51 * The log is implemented as a ring buffer, though the original creation
52 * of the pool ('zpool create') is never overwritten.
54 * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer
55 * of 'spa_history' stores the offsets for logging/retrieving history as
56 * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of
57 * where the 'zpool create' record is stored. This allows us to never
58 * overwrite the original creation of the pool. 'sh_phys_max_off' is the
59 * physical ending offset in bytes of the log. This tells you the length of
60 * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record
61 * is added, 'sh_eof' is incremented by the the size of the record.
62 * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes).
63 * This is where the consumer should start reading from after reading in
64 * the 'zpool create' portion of the log.
66 * 'sh_records_lost' keeps track of how many records have been overwritten
67 * and permanently lost.
70 /* convert a logical offset to physical */
72 spa_history_log_to_phys(uint64_t log_off
, spa_history_phys_t
*shpp
)
76 phys_len
= shpp
->sh_phys_max_off
- shpp
->sh_pool_create_len
;
77 return ((log_off
- shpp
->sh_pool_create_len
) % phys_len
78 + shpp
->sh_pool_create_len
);
82 spa_history_create_obj(spa_t
*spa
, dmu_tx_t
*tx
)
85 spa_history_phys_t
*shpp
;
86 objset_t
*mos
= spa
->spa_meta_objset
;
88 ASSERT(spa
->spa_history
== 0);
89 spa
->spa_history
= dmu_object_alloc(mos
, DMU_OT_SPA_HISTORY
,
90 SPA_MAXBLOCKSIZE
, DMU_OT_SPA_HISTORY_OFFSETS
,
91 sizeof (spa_history_phys_t
), tx
);
93 VERIFY(zap_add(mos
, DMU_POOL_DIRECTORY_OBJECT
,
94 DMU_POOL_HISTORY
, sizeof (uint64_t), 1,
95 &spa
->spa_history
, tx
) == 0);
97 VERIFY(0 == dmu_bonus_hold(mos
, spa
->spa_history
, FTAG
, &dbp
));
98 ASSERT(dbp
->db_size
>= sizeof (spa_history_phys_t
));
101 dmu_buf_will_dirty(dbp
, tx
);
104 * Figure out maximum size of history log. We set it at
105 * 0.1% of pool size, with a max of 1G and min of 128KB.
107 shpp
->sh_phys_max_off
=
108 metaslab_class_get_dspace(spa_normal_class(spa
)) / 1000;
109 shpp
->sh_phys_max_off
= MIN(shpp
->sh_phys_max_off
, 1<<30);
110 shpp
->sh_phys_max_off
= MAX(shpp
->sh_phys_max_off
, 128<<10);
112 dmu_buf_rele(dbp
, FTAG
);
116 * Change 'sh_bof' to the beginning of the next record.
119 spa_history_advance_bof(spa_t
*spa
, spa_history_phys_t
*shpp
)
121 objset_t
*mos
= spa
->spa_meta_objset
;
122 uint64_t firstread
, reclen
, phys_bof
;
123 char buf
[sizeof (reclen
)];
126 phys_bof
= spa_history_log_to_phys(shpp
->sh_bof
, shpp
);
127 firstread
= MIN(sizeof (reclen
), shpp
->sh_phys_max_off
- phys_bof
);
129 if ((err
= dmu_read(mos
, spa
->spa_history
, phys_bof
, firstread
,
130 buf
, DMU_READ_PREFETCH
)) != 0)
132 if (firstread
!= sizeof (reclen
)) {
133 if ((err
= dmu_read(mos
, spa
->spa_history
,
134 shpp
->sh_pool_create_len
, sizeof (reclen
) - firstread
,
135 buf
+ firstread
, DMU_READ_PREFETCH
)) != 0)
139 reclen
= LE_64(*((uint64_t *)buf
));
140 shpp
->sh_bof
+= reclen
+ sizeof (reclen
);
141 shpp
->sh_records_lost
++;
146 spa_history_write(spa_t
*spa
, void *buf
, uint64_t len
, spa_history_phys_t
*shpp
,
149 uint64_t firstwrite
, phys_eof
;
150 objset_t
*mos
= spa
->spa_meta_objset
;
153 ASSERT(MUTEX_HELD(&spa
->spa_history_lock
));
155 /* see if we need to reset logical BOF */
156 while (shpp
->sh_phys_max_off
- shpp
->sh_pool_create_len
-
157 (shpp
->sh_eof
- shpp
->sh_bof
) <= len
) {
158 if ((err
= spa_history_advance_bof(spa
, shpp
)) != 0) {
163 phys_eof
= spa_history_log_to_phys(shpp
->sh_eof
, shpp
);
164 firstwrite
= MIN(len
, shpp
->sh_phys_max_off
- phys_eof
);
166 dmu_write(mos
, spa
->spa_history
, phys_eof
, firstwrite
, buf
, tx
);
170 /* write out the rest at the beginning of physical file */
171 dmu_write(mos
, spa
->spa_history
, shpp
->sh_pool_create_len
,
172 len
, (char *)buf
+ firstwrite
, tx
);
179 spa_history_zone(void)
185 return (curproc
->p_zone
->zone_name
);
193 * Write out a history event.
197 spa_history_log_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
200 history_arg_t
*hap
= arg2
;
201 const char *history_str
= hap
->ha_history_str
;
202 objset_t
*mos
= spa
->spa_meta_objset
;
204 spa_history_phys_t
*shpp
;
208 char *record_packed
= NULL
;
212 * If we have an older pool that doesn't have a command
213 * history object, create it now.
215 mutex_enter(&spa
->spa_history_lock
);
216 if (!spa
->spa_history
)
217 spa_history_create_obj(spa
, tx
);
218 mutex_exit(&spa
->spa_history_lock
);
221 * Get the offset of where we need to write via the bonus buffer.
222 * Update the offset when the write completes.
224 VERIFY(0 == dmu_bonus_hold(mos
, spa
->spa_history
, FTAG
, &dbp
));
227 dmu_buf_will_dirty(dbp
, tx
);
231 dmu_object_info_t doi
;
232 dmu_object_info_from_db(dbp
, &doi
);
233 ASSERT3U(doi
.doi_bonus_type
, ==, DMU_OT_SPA_HISTORY_OFFSETS
);
237 VERIFY(nvlist_alloc(&nvrecord
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
238 VERIFY(nvlist_add_uint64(nvrecord
, ZPOOL_HIST_TIME
,
239 gethrestime_sec()) == 0);
240 VERIFY(nvlist_add_uint64(nvrecord
, ZPOOL_HIST_WHO
, hap
->ha_uid
) == 0);
241 if (hap
->ha_zone
!= NULL
)
242 VERIFY(nvlist_add_string(nvrecord
, ZPOOL_HIST_ZONE
,
245 VERIFY(nvlist_add_string(nvrecord
, ZPOOL_HIST_HOST
,
246 utsname
.nodename
) == 0);
248 if (hap
->ha_log_type
== LOG_CMD_POOL_CREATE
||
249 hap
->ha_log_type
== LOG_CMD_NORMAL
) {
250 VERIFY(nvlist_add_string(nvrecord
, ZPOOL_HIST_CMD
,
253 zfs_dbgmsg("command: %s", history_str
);
255 VERIFY(nvlist_add_uint64(nvrecord
, ZPOOL_HIST_INT_EVENT
,
256 hap
->ha_event
) == 0);
257 VERIFY(nvlist_add_uint64(nvrecord
, ZPOOL_HIST_TXG
,
259 VERIFY(nvlist_add_string(nvrecord
, ZPOOL_HIST_INT_STR
,
262 zfs_dbgmsg("internal %s pool:%s txg:%llu %s",
263 zfs_history_event_names
[hap
->ha_event
], spa_name(spa
),
264 (longlong_t
)tx
->tx_txg
, history_str
);
268 VERIFY(nvlist_size(nvrecord
, &reclen
, NV_ENCODE_XDR
) == 0);
269 record_packed
= kmem_alloc(reclen
, KM_SLEEP
);
271 VERIFY(nvlist_pack(nvrecord
, &record_packed
, &reclen
,
272 NV_ENCODE_XDR
, KM_SLEEP
) == 0);
274 mutex_enter(&spa
->spa_history_lock
);
275 if (hap
->ha_log_type
== LOG_CMD_POOL_CREATE
)
276 VERIFY(shpp
->sh_eof
== shpp
->sh_pool_create_len
);
278 /* write out the packed length as little endian */
279 le_len
= LE_64((uint64_t)reclen
);
280 ret
= spa_history_write(spa
, &le_len
, sizeof (le_len
), shpp
, tx
);
282 ret
= spa_history_write(spa
, record_packed
, reclen
, shpp
, tx
);
284 if (!ret
&& hap
->ha_log_type
== LOG_CMD_POOL_CREATE
) {
285 shpp
->sh_pool_create_len
+= sizeof (le_len
) + reclen
;
286 shpp
->sh_bof
= shpp
->sh_pool_create_len
;
289 mutex_exit(&spa
->spa_history_lock
);
290 nvlist_free(nvrecord
);
291 kmem_free(record_packed
, reclen
);
292 dmu_buf_rele(dbp
, FTAG
);
294 strfree(hap
->ha_history_str
);
295 if (hap
->ha_zone
!= NULL
)
296 strfree(hap
->ha_zone
);
297 kmem_free(hap
, sizeof (history_arg_t
));
301 * Write out a history event.
304 spa_history_log(spa_t
*spa
, const char *history_str
, history_log_type_t what
)
310 ASSERT(what
!= LOG_INTERNAL
);
312 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
313 err
= dmu_tx_assign(tx
, TXG_WAIT
);
319 ha
= kmem_alloc(sizeof (history_arg_t
), KM_SLEEP
);
320 ha
->ha_history_str
= strdup(history_str
);
321 ha
->ha_zone
= strdup(spa_history_zone());
322 ha
->ha_log_type
= what
;
323 ha
->ha_uid
= crgetuid(CRED());
325 /* Kick this off asynchronously; errors are ignored. */
326 dsl_sync_task_do_nowait(spa_get_dsl(spa
), NULL
,
327 spa_history_log_sync
, spa
, ha
, 0, tx
);
330 /* spa_history_log_sync will free ha and strings */
335 * Read out the command history.
338 spa_history_get(spa_t
*spa
, uint64_t *offp
, uint64_t *len
, char *buf
)
340 objset_t
*mos
= spa
->spa_meta_objset
;
342 uint64_t read_len
, phys_read_off
, phys_eof
;
343 uint64_t leftover
= 0;
344 spa_history_phys_t
*shpp
;
348 * If the command history doesn't exist (older pool),
349 * that's ok, just return ENOENT.
351 if (!spa
->spa_history
)
355 * The history is logged asynchronously, so when they request
356 * the first chunk of history, make sure everything has been
357 * synced to disk so that we get it.
359 if (*offp
== 0 && spa_writeable(spa
))
360 txg_wait_synced(spa_get_dsl(spa
), 0);
362 if ((err
= dmu_bonus_hold(mos
, spa
->spa_history
, FTAG
, &dbp
)) != 0)
368 dmu_object_info_t doi
;
369 dmu_object_info_from_db(dbp
, &doi
);
370 ASSERT3U(doi
.doi_bonus_type
, ==, DMU_OT_SPA_HISTORY_OFFSETS
);
374 mutex_enter(&spa
->spa_history_lock
);
375 phys_eof
= spa_history_log_to_phys(shpp
->sh_eof
, shpp
);
377 if (*offp
< shpp
->sh_pool_create_len
) {
378 /* read in just the zpool create history */
379 phys_read_off
= *offp
;
380 read_len
= MIN(*len
, shpp
->sh_pool_create_len
-
384 * Need to reset passed in offset to BOF if the passed in
385 * offset has since been overwritten.
387 *offp
= MAX(*offp
, shpp
->sh_bof
);
388 phys_read_off
= spa_history_log_to_phys(*offp
, shpp
);
391 * Read up to the minimum of what the user passed down or
392 * the EOF (physical or logical). If we hit physical EOF,
393 * use 'leftover' to read from the physical BOF.
395 if (phys_read_off
<= phys_eof
) {
396 read_len
= MIN(*len
, phys_eof
- phys_read_off
);
399 shpp
->sh_phys_max_off
- phys_read_off
);
400 if (phys_read_off
+ *len
> shpp
->sh_phys_max_off
) {
401 leftover
= MIN(*len
- read_len
,
402 phys_eof
- shpp
->sh_pool_create_len
);
407 /* offset for consumer to use next */
408 *offp
+= read_len
+ leftover
;
410 /* tell the consumer how much you actually read */
411 *len
= read_len
+ leftover
;
414 mutex_exit(&spa
->spa_history_lock
);
415 dmu_buf_rele(dbp
, FTAG
);
419 err
= dmu_read(mos
, spa
->spa_history
, phys_read_off
, read_len
, buf
,
421 if (leftover
&& err
== 0) {
422 err
= dmu_read(mos
, spa
->spa_history
, shpp
->sh_pool_create_len
,
423 leftover
, buf
+ read_len
, DMU_READ_PREFETCH
);
425 mutex_exit(&spa
->spa_history_lock
);
427 dmu_buf_rele(dbp
, FTAG
);
432 log_internal(history_internal_events_t event
, spa_t
*spa
,
433 dmu_tx_t
*tx
, const char *fmt
, va_list adx
)
439 * If this is part of creating a pool, not everything is
440 * initialized yet, so don't bother logging the internal events.
442 if (tx
->tx_txg
== TXG_INITIAL
)
445 ha
= kmem_alloc(sizeof (history_arg_t
), KM_SLEEP
);
446 va_copy(adx_copy
, adx
);
447 ha
->ha_history_str
= kmem_vasprintf(fmt
, adx_copy
);
449 ha
->ha_log_type
= LOG_INTERNAL
;
450 ha
->ha_event
= event
;
454 if (dmu_tx_is_syncing(tx
)) {
455 spa_history_log_sync(spa
, ha
, tx
);
457 dsl_sync_task_do_nowait(spa_get_dsl(spa
), NULL
,
458 spa_history_log_sync
, spa
, ha
, 0, tx
);
460 /* spa_history_log_sync() will free ha and strings */
464 spa_history_log_internal(history_internal_events_t event
, spa_t
*spa
,
465 dmu_tx_t
*tx
, const char *fmt
, ...)
470 /* create a tx if we didn't get one */
472 htx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
473 if (dmu_tx_assign(htx
, TXG_WAIT
) != 0) {
480 log_internal(event
, spa
, htx
, fmt
, adx
);
483 /* if we didn't get a tx from the caller, commit the one we made */
489 spa_history_log_version(spa_t
*spa
, history_internal_events_t event
)
492 uint64_t current_vers
= spa_version(spa
);
494 if (current_vers
>= SPA_VERSION_ZPOOL_HISTORY
) {
495 spa_history_log_internal(event
, spa
, NULL
,
496 "pool spa %llu; zfs spa %llu; zpl %d; uts %s %s %s %s",
497 (u_longlong_t
)current_vers
, SPA_VERSION
, ZPL_VERSION
,
498 utsname
.nodename
, utsname
.release
, utsname
.version
,
501 cmn_err(CE_CONT
, "!%s version %llu pool %s using %llu",
502 event
== LOG_POOL_IMPORT
? "imported" :
503 event
== LOG_POOL_CREATE
? "created" : "accessed",
504 (u_longlong_t
)current_vers
, spa_name(spa
), SPA_VERSION
);
508 #if defined(_KERNEL) && defined(HAVE_SPL)
509 EXPORT_SYMBOL(spa_history_create_obj
);
510 EXPORT_SYMBOL(spa_history_get
);
511 EXPORT_SYMBOL(spa_history_log
);
512 EXPORT_SYMBOL(spa_history_log_internal
);
513 EXPORT_SYMBOL(spa_history_log_version
);