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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
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/utsname.h>
36 #include <sys/sunddi.h>
38 #include <sys/cmn_err.h>
43 * Routines to manage the on-disk history log.
45 * The history log is stored as a dmu object containing
46 * <packed record length, record nvlist> tuples.
48 * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
49 * "packed record length" is the packed length of the "record nvlist" stored
50 * as a little endian uint64_t.
52 * The log is implemented as a ring buffer, though the original creation
53 * of the pool ('zpool create') is never overwritten.
55 * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer
56 * of 'spa_history' stores the offsets for logging/retrieving history as
57 * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of
58 * where the 'zpool create' record is stored. This allows us to never
59 * overwrite the original creation of the pool. 'sh_phys_max_off' is the
60 * physical ending offset in bytes of the log. This tells you the length of
61 * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record
62 * is added, 'sh_eof' is incremented by the the size of the record.
63 * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes).
64 * This is where the consumer should start reading from after reading in
65 * the 'zpool create' portion of the log.
67 * 'sh_records_lost' keeps track of how many records have been overwritten
68 * and permanently lost.
71 /* convert a logical offset to physical */
73 spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
77 phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
78 return ((log_off - shpp->sh_pool_create_len) % phys_len
79 + shpp->sh_pool_create_len);
83 spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
86 spa_history_phys_t *shpp;
87 objset_t *mos = spa->spa_meta_objset;
89 ASSERT(spa->spa_history == 0);
90 spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
91 SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
92 sizeof (spa_history_phys_t), tx);
94 VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
95 DMU_POOL_HISTORY, sizeof (uint64_t), 1,
96 &spa->spa_history, tx) == 0);
98 VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
99 ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
102 dmu_buf_will_dirty(dbp, tx);
105 * Figure out maximum size of history log. We set it at
106 * 1% of pool size, with a max of 32MB and min of 128KB.
108 shpp->sh_phys_max_off = spa_get_dspace(spa) / 100;
109 shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20);
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,
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)) != 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);
182 /* XXX: pr_hostname can be changed by default from within a jail! */
183 if (jailed(curthread->td_ucred))
184 return (curthread->td_ucred->cr_prison->pr_hostname);
190 * Write out a history event.
193 spa_history_log_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
196 history_arg_t *hap = arg2;
197 const char *history_str = hap->ha_history_str;
198 objset_t *mos = spa->spa_meta_objset;
200 spa_history_phys_t *shpp;
204 char *record_packed = NULL;
208 * If we have an older pool that doesn't have a command
209 * history object, create it now.
211 mutex_enter(&spa->spa_history_lock);
212 if (!spa->spa_history)
213 spa_history_create_obj(spa, tx);
214 mutex_exit(&spa->spa_history_lock);
217 * Get the offset of where we need to write via the bonus buffer.
218 * Update the offset when the write completes.
220 VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
223 dmu_buf_will_dirty(dbp, tx);
227 dmu_object_info_t doi;
228 dmu_object_info_from_db(dbp, &doi);
229 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
233 VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
234 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME,
235 gethrestime_sec()) == 0);
236 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO,
237 (uint64_t)crgetuid(cr)) == 0);
238 if (hap->ha_zone[0] != '\0')
239 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_ZONE,
242 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_HOST,
243 utsname.nodename) == 0);
245 if (hap->ha_log_type == LOG_CMD_POOL_CREATE ||
246 hap->ha_log_type == LOG_CMD_NORMAL) {
247 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD,
250 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT,
251 hap->ha_event) == 0);
252 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TXG,
254 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR,
258 VERIFY(nvlist_size(nvrecord, &reclen, NV_ENCODE_XDR) == 0);
259 record_packed = kmem_alloc(reclen, KM_SLEEP);
261 VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
262 NV_ENCODE_XDR, KM_SLEEP) == 0);
264 mutex_enter(&spa->spa_history_lock);
265 if (hap->ha_log_type == LOG_CMD_POOL_CREATE)
266 VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
268 /* write out the packed length as little endian */
269 le_len = LE_64((uint64_t)reclen);
270 ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
272 ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
274 if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) {
275 shpp->sh_pool_create_len += sizeof (le_len) + reclen;
276 shpp->sh_bof = shpp->sh_pool_create_len;
279 mutex_exit(&spa->spa_history_lock);
280 nvlist_free(nvrecord);
281 kmem_free(record_packed, reclen);
282 dmu_buf_rele(dbp, FTAG);
284 if (hap->ha_log_type == LOG_INTERNAL) {
285 kmem_free((void*)hap->ha_history_str, HIS_MAX_RECORD_LEN);
286 kmem_free(hap, sizeof (history_arg_t));
291 * Write out a history event.
294 spa_history_log(spa_t *spa, const char *history_str, history_log_type_t what)
298 ASSERT(what != LOG_INTERNAL);
300 ha.ha_history_str = history_str;
301 ha.ha_log_type = what;
302 (void) strlcpy(ha.ha_zone, spa_history_zone(), sizeof (ha.ha_zone));
303 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync,
308 * Read out the command history.
311 spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
313 objset_t *mos = spa->spa_meta_objset;
315 uint64_t read_len, phys_read_off, phys_eof;
316 uint64_t leftover = 0;
317 spa_history_phys_t *shpp;
321 * If the command history doesn't exist (older pool),
322 * that's ok, just return ENOENT.
324 if (!spa->spa_history)
327 if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
333 dmu_object_info_t doi;
334 dmu_object_info_from_db(dbp, &doi);
335 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
339 mutex_enter(&spa->spa_history_lock);
340 phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
342 if (*offp < shpp->sh_pool_create_len) {
343 /* read in just the zpool create history */
344 phys_read_off = *offp;
345 read_len = MIN(*len, shpp->sh_pool_create_len -
349 * Need to reset passed in offset to BOF if the passed in
350 * offset has since been overwritten.
352 *offp = MAX(*offp, shpp->sh_bof);
353 phys_read_off = spa_history_log_to_phys(*offp, shpp);
356 * Read up to the minimum of what the user passed down or
357 * the EOF (physical or logical). If we hit physical EOF,
358 * use 'leftover' to read from the physical BOF.
360 if (phys_read_off <= phys_eof) {
361 read_len = MIN(*len, phys_eof - phys_read_off);
364 shpp->sh_phys_max_off - phys_read_off);
365 if (phys_read_off + *len > shpp->sh_phys_max_off) {
366 leftover = MIN(*len - read_len,
367 phys_eof - shpp->sh_pool_create_len);
372 /* offset for consumer to use next */
373 *offp += read_len + leftover;
375 /* tell the consumer how much you actually read */
376 *len = read_len + leftover;
379 mutex_exit(&spa->spa_history_lock);
380 dmu_buf_rele(dbp, FTAG);
384 err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf);
385 if (leftover && err == 0) {
386 err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
387 leftover, buf + read_len);
389 mutex_exit(&spa->spa_history_lock);
391 dmu_buf_rele(dbp, FTAG);
396 spa_history_internal_log(history_internal_events_t event, spa_t *spa,
397 dmu_tx_t *tx, cred_t *cr, const char *fmt, ...)
404 * If this is part of creating a pool, not everything is
405 * initialized yet, so don't bother logging the internal events.
407 if (tx->tx_txg == TXG_INITIAL)
410 hap = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
411 str = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
414 (void) vsnprintf(str, HIS_MAX_RECORD_LEN, fmt, adx);
417 hap->ha_log_type = LOG_INTERNAL;
418 hap->ha_history_str = str;
419 hap->ha_event = event;
420 hap->ha_zone[0] = '\0';
422 if (dmu_tx_is_syncing(tx)) {
423 spa_history_log_sync(spa, hap, cr, tx);
425 dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
426 spa_history_log_sync, spa, hap, 0, tx);
428 /* spa_history_log_sync() will free hap and str */