]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/spa_history.c
Illumos 5314 - Remove "dbuf phys" db->db_data pointer aliases in ZFS
[mirror_zfs.git] / module / zfs / spa_history.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22/*
428870ff 23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2e528b49 24 * Copyright (c) 2013 by Delphix. All rights reserved.
34dc7c2f
BB
25 */
26
34dc7c2f
BB
27#include <sys/spa.h>
28#include <sys/spa_impl.h>
29#include <sys/zap.h>
30#include <sys/dsl_synctask.h>
31#include <sys/dmu_tx.h>
32#include <sys/dmu_objset.h>
6f1ffb06
MA
33#include <sys/dsl_dataset.h>
34#include <sys/dsl_dir.h>
34dc7c2f
BB
35#include <sys/cmn_err.h>
36#include <sys/sunddi.h>
6f1ffb06 37#include <sys/cred.h>
428870ff 38#include "zfs_comutil.h"
34dc7c2f
BB
39#ifdef _KERNEL
40#include <sys/zone.h>
41#endif
42
43/*
44 * Routines to manage the on-disk history log.
45 *
46 * The history log is stored as a dmu object containing
47 * <packed record length, record nvlist> tuples.
48 *
49 * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
50 * "packed record length" is the packed length of the "record nvlist" stored
51 * as a little endian uint64_t.
52 *
53 * The log is implemented as a ring buffer, though the original creation
54 * of the pool ('zpool create') is never overwritten.
55 *
56 * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer
57 * of 'spa_history' stores the offsets for logging/retrieving history as
58 * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of
59 * where the 'zpool create' record is stored. This allows us to never
60 * overwrite the original creation of the pool. 'sh_phys_max_off' is the
61 * physical ending offset in bytes of the log. This tells you the length of
62 * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record
63 * is added, 'sh_eof' is incremented by the the size of the record.
64 * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes).
65 * This is where the consumer should start reading from after reading in
66 * the 'zpool create' portion of the log.
67 *
68 * 'sh_records_lost' keeps track of how many records have been overwritten
69 * and permanently lost.
70 */
71
72/* convert a logical offset to physical */
73static uint64_t
74spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
75{
76 uint64_t phys_len;
77
78 phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
79 return ((log_off - shpp->sh_pool_create_len) % phys_len
80 + shpp->sh_pool_create_len);
81}
82
83void
84spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
85{
86 dmu_buf_t *dbp;
87 spa_history_phys_t *shpp;
88 objset_t *mos = spa->spa_meta_objset;
89
90 ASSERT(spa->spa_history == 0);
91 spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
92 SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
93 sizeof (spa_history_phys_t), tx);
94
95 VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
96 DMU_POOL_HISTORY, sizeof (uint64_t), 1,
97 &spa->spa_history, tx) == 0);
98
99 VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
100 ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
101
102 shpp = dbp->db_data;
103 dmu_buf_will_dirty(dbp, tx);
104
105 /*
106 * Figure out maximum size of history log. We set it at
330d06f9 107 * 0.1% of pool size, with a max of 1G and min of 128KB.
34dc7c2f 108 */
428870ff 109 shpp->sh_phys_max_off =
330d06f9
MA
110 metaslab_class_get_dspace(spa_normal_class(spa)) / 1000;
111 shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 1<<30);
34dc7c2f
BB
112 shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
113
114 dmu_buf_rele(dbp, FTAG);
115}
116
117/*
118 * Change 'sh_bof' to the beginning of the next record.
119 */
120static int
121spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
122{
123 objset_t *mos = spa->spa_meta_objset;
124 uint64_t firstread, reclen, phys_bof;
125 char buf[sizeof (reclen)];
126 int err;
127
128 phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
129 firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
130
131 if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
9babb374 132 buf, DMU_READ_PREFETCH)) != 0)
34dc7c2f
BB
133 return (err);
134 if (firstread != sizeof (reclen)) {
135 if ((err = dmu_read(mos, spa->spa_history,
136 shpp->sh_pool_create_len, sizeof (reclen) - firstread,
9babb374 137 buf + firstread, DMU_READ_PREFETCH)) != 0)
34dc7c2f
BB
138 return (err);
139 }
140
141 reclen = LE_64(*((uint64_t *)buf));
142 shpp->sh_bof += reclen + sizeof (reclen);
143 shpp->sh_records_lost++;
144 return (0);
145}
146
147static int
148spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
149 dmu_tx_t *tx)
150{
151 uint64_t firstwrite, phys_eof;
152 objset_t *mos = spa->spa_meta_objset;
153 int err;
154
155 ASSERT(MUTEX_HELD(&spa->spa_history_lock));
156
157 /* see if we need to reset logical BOF */
158 while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
159 (shpp->sh_eof - shpp->sh_bof) <= len) {
160 if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
161 return (err);
162 }
163 }
164
165 phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
166 firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
167 shpp->sh_eof += len;
168 dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
169
170 len -= firstwrite;
171 if (len > 0) {
172 /* write out the rest at the beginning of physical file */
173 dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
174 len, (char *)buf + firstwrite, tx);
175 }
176
177 return (0);
178}
179
180static char *
0bc8fd78 181spa_history_zone(void)
34dc7c2f
BB
182{
183#ifdef _KERNEL
c28b2279
BB
184#ifdef HAVE_SPL
185 return ("linux");
186#else
34dc7c2f 187 return (curproc->p_zone->zone_name);
c28b2279 188#endif
34dc7c2f 189#else
6f1ffb06 190 return (NULL);
34dc7c2f
BB
191#endif
192}
193
194/*
195 * Write out a history event.
196 */
428870ff 197/*ARGSUSED*/
34dc7c2f 198static void
13fe0198 199spa_history_log_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 200{
13fe0198
MA
201 nvlist_t *nvl = arg;
202 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
34dc7c2f
BB
203 objset_t *mos = spa->spa_meta_objset;
204 dmu_buf_t *dbp;
205 spa_history_phys_t *shpp;
206 size_t reclen;
207 uint64_t le_len;
34dc7c2f
BB
208 char *record_packed = NULL;
209 int ret;
210
211 /*
212 * If we have an older pool that doesn't have a command
213 * history object, create it now.
214 */
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);
219
220 /*
221 * Get the offset of where we need to write via the bonus buffer.
222 * Update the offset when the write completes.
223 */
13fe0198 224 VERIFY0(dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
34dc7c2f
BB
225 shpp = dbp->db_data;
226
227 dmu_buf_will_dirty(dbp, tx);
228
229#ifdef ZFS_DEBUG
230 {
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);
234 }
235#endif
236
6f1ffb06 237 fnvlist_add_uint64(nvl, ZPOOL_HIST_TIME, gethrestime_sec());
f0e324f2
BB
238 fnvlist_add_string(nvl, ZPOOL_HIST_HOST, utsname()->nodename);
239
6f1ffb06
MA
240 if (nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
241 zfs_dbgmsg("command: %s",
242 fnvlist_lookup_string(nvl, ZPOOL_HIST_CMD));
243 } else if (nvlist_exists(nvl, ZPOOL_HIST_INT_NAME)) {
244 if (nvlist_exists(nvl, ZPOOL_HIST_DSNAME)) {
245 zfs_dbgmsg("txg %lld %s %s (id %llu) %s",
246 fnvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG),
247 fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
248 fnvlist_lookup_string(nvl, ZPOOL_HIST_DSNAME),
249 fnvlist_lookup_uint64(nvl, ZPOOL_HIST_DSID),
250 fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
251 } else {
252 zfs_dbgmsg("txg %lld %s %s",
253 fnvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG),
254 fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME),
255 fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR));
256 }
257 } else if (nvlist_exists(nvl, ZPOOL_HIST_IOCTL)) {
258 zfs_dbgmsg("ioctl %s",
259 fnvlist_lookup_string(nvl, ZPOOL_HIST_IOCTL));
34dc7c2f
BB
260 }
261
6f1ffb06 262 VERIFY3U(nvlist_pack(nvl, &record_packed, &reclen, NV_ENCODE_NATIVE,
79c76d5b 263 KM_SLEEP), ==, 0);
34dc7c2f
BB
264
265 mutex_enter(&spa->spa_history_lock);
34dc7c2f
BB
266
267 /* write out the packed length as little endian */
268 le_len = LE_64((uint64_t)reclen);
269 ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
270 if (!ret)
271 ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
272
6f1ffb06
MA
273 /* The first command is the create, which we keep forever */
274 if (ret == 0 && shpp->sh_pool_create_len == 0 &&
275 nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
276 shpp->sh_pool_create_len = shpp->sh_bof = shpp->sh_eof;
34dc7c2f
BB
277 }
278
279 mutex_exit(&spa->spa_history_lock);
6f1ffb06 280 fnvlist_pack_free(record_packed, reclen);
34dc7c2f 281 dmu_buf_rele(dbp, FTAG);
6f1ffb06 282 fnvlist_free(nvl);
34dc7c2f
BB
283}
284
285/*
286 * Write out a history event.
287 */
288int
6f1ffb06
MA
289spa_history_log(spa_t *spa, const char *msg)
290{
291 int err;
79c76d5b 292 nvlist_t *nvl = fnvlist_alloc();
6f1ffb06
MA
293
294 fnvlist_add_string(nvl, ZPOOL_HIST_CMD, msg);
295 err = spa_history_log_nvl(spa, nvl);
296 fnvlist_free(nvl);
297 return (err);
298}
299
300int
301spa_history_log_nvl(spa_t *spa, nvlist_t *nvl)
34dc7c2f 302{
428870ff
BB
303 int err = 0;
304 dmu_tx_t *tx;
6f1ffb06 305 nvlist_t *nvarg;
34dc7c2f 306
6f1ffb06 307 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY || !spa_writeable(spa))
2e528b49 308 return (SET_ERROR(EINVAL));
34dc7c2f 309
428870ff
BB
310 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
311 err = dmu_tx_assign(tx, TXG_WAIT);
312 if (err) {
313 dmu_tx_abort(tx);
314 return (err);
315 }
316
79c76d5b 317 VERIFY0(nvlist_dup(nvl, &nvarg, KM_SLEEP));
6f1ffb06
MA
318 if (spa_history_zone() != NULL) {
319 fnvlist_add_string(nvarg, ZPOOL_HIST_ZONE,
320 spa_history_zone());
321 }
322 fnvlist_add_uint64(nvarg, ZPOOL_HIST_WHO, crgetruid(CRED()));
428870ff
BB
323
324 /* Kick this off asynchronously; errors are ignored. */
13fe0198
MA
325 dsl_sync_task_nowait(spa_get_dsl(spa), spa_history_log_sync,
326 nvarg, 0, tx);
428870ff
BB
327 dmu_tx_commit(tx);
328
6f1ffb06 329 /* spa_history_log_sync will free nvl */
428870ff 330 return (err);
6f1ffb06 331
34dc7c2f
BB
332}
333
334/*
335 * Read out the command history.
336 */
337int
338spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
339{
340 objset_t *mos = spa->spa_meta_objset;
341 dmu_buf_t *dbp;
342 uint64_t read_len, phys_read_off, phys_eof;
343 uint64_t leftover = 0;
344 spa_history_phys_t *shpp;
345 int err;
346
347 /*
6f1ffb06 348 * If the command history doesn't exist (older pool),
34dc7c2f
BB
349 * that's ok, just return ENOENT.
350 */
351 if (!spa->spa_history)
2e528b49 352 return (SET_ERROR(ENOENT));
34dc7c2f 353
428870ff
BB
354 /*
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.
358 */
359 if (*offp == 0 && spa_writeable(spa))
360 txg_wait_synced(spa_get_dsl(spa), 0);
361
34dc7c2f
BB
362 if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
363 return (err);
364 shpp = dbp->db_data;
365
366#ifdef ZFS_DEBUG
367 {
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);
371 }
372#endif
373
374 mutex_enter(&spa->spa_history_lock);
375 phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
376
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 -
381 phys_read_off);
382 } else {
383 /*
384 * Need to reset passed in offset to BOF if the passed in
385 * offset has since been overwritten.
386 */
387 *offp = MAX(*offp, shpp->sh_bof);
388 phys_read_off = spa_history_log_to_phys(*offp, shpp);
389
390 /*
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.
394 */
395 if (phys_read_off <= phys_eof) {
396 read_len = MIN(*len, phys_eof - phys_read_off);
397 } else {
398 read_len = MIN(*len,
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);
403 }
404 }
405 }
406
407 /* offset for consumer to use next */
408 *offp += read_len + leftover;
409
410 /* tell the consumer how much you actually read */
411 *len = read_len + leftover;
412
413 if (read_len == 0) {
414 mutex_exit(&spa->spa_history_lock);
415 dmu_buf_rele(dbp, FTAG);
416 return (0);
417 }
418
9babb374
BB
419 err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
420 DMU_READ_PREFETCH);
34dc7c2f
BB
421 if (leftover && err == 0) {
422 err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
9babb374 423 leftover, buf + read_len, DMU_READ_PREFETCH);
34dc7c2f
BB
424 }
425 mutex_exit(&spa->spa_history_lock);
426
427 dmu_buf_rele(dbp, FTAG);
428 return (err);
429}
430
6f1ffb06
MA
431/*
432 * The nvlist will be consumed by this call.
433 */
45d1cae3 434static void
6f1ffb06 435log_internal(nvlist_t *nvl, const char *operation, spa_t *spa,
428870ff 436 dmu_tx_t *tx, const char *fmt, va_list adx)
34dc7c2f 437{
6f1ffb06 438 char *msg;
34dc7c2f 439
b128c09f
BB
440 /*
441 * If this is part of creating a pool, not everything is
442 * initialized yet, so don't bother logging the internal events.
6f1ffb06 443 * Likewise if the pool is not writeable.
b128c09f 444 */
6f1ffb06
MA
445 if (tx->tx_txg == TXG_INITIAL || !spa_writeable(spa)) {
446 fnvlist_free(nvl);
b128c09f 447 return;
6f1ffb06 448 }
b128c09f 449
841c9d43 450 msg = kmem_vasprintf(fmt, adx);
6f1ffb06 451 fnvlist_add_string(nvl, ZPOOL_HIST_INT_STR, msg);
841c9d43 452 strfree(msg);
6f1ffb06
MA
453
454 fnvlist_add_string(nvl, ZPOOL_HIST_INT_NAME, operation);
455 fnvlist_add_uint64(nvl, ZPOOL_HIST_TXG, tx->tx_txg);
34dc7c2f
BB
456
457 if (dmu_tx_is_syncing(tx)) {
13fe0198 458 spa_history_log_sync(nvl, tx);
34dc7c2f 459 } else {
13fe0198
MA
460 dsl_sync_task_nowait(spa_get_dsl(spa),
461 spa_history_log_sync, nvl, 0, tx);
34dc7c2f 462 }
6f1ffb06 463 /* spa_history_log_sync() will free nvl */
34dc7c2f 464}
45d1cae3
BB
465
466void
6f1ffb06 467spa_history_log_internal(spa_t *spa, const char *operation,
428870ff 468 dmu_tx_t *tx, const char *fmt, ...)
45d1cae3
BB
469{
470 dmu_tx_t *htx = tx;
471 va_list adx;
472
473 /* create a tx if we didn't get one */
474 if (tx == NULL) {
475 htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
476 if (dmu_tx_assign(htx, TXG_WAIT) != 0) {
477 dmu_tx_abort(htx);
478 return;
479 }
480 }
481
482 va_start(adx, fmt);
79c76d5b 483 log_internal(fnvlist_alloc(), operation, spa, htx, fmt, adx);
45d1cae3
BB
484 va_end(adx);
485
486 /* if we didn't get a tx from the caller, commit the one we made */
487 if (tx == NULL)
488 dmu_tx_commit(htx);
489}
490
491void
6f1ffb06
MA
492spa_history_log_internal_ds(dsl_dataset_t *ds, const char *operation,
493 dmu_tx_t *tx, const char *fmt, ...)
494{
495 va_list adx;
496 char namebuf[MAXNAMELEN];
79c76d5b 497 nvlist_t *nvl = fnvlist_alloc();
6f1ffb06
MA
498
499 ASSERT(tx != NULL);
500
501 dsl_dataset_name(ds, namebuf);
6f1ffb06
MA
502 fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
503 fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID, ds->ds_object);
504
505 va_start(adx, fmt);
506 log_internal(nvl, operation, dsl_dataset_get_spa(ds), tx, fmt, adx);
507 va_end(adx);
508}
509
510void
511spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
512 dmu_tx_t *tx, const char *fmt, ...)
513{
514 va_list adx;
515 char namebuf[MAXNAMELEN];
79c76d5b 516 nvlist_t *nvl = fnvlist_alloc();
6f1ffb06
MA
517
518 ASSERT(tx != NULL);
519
520 dsl_dir_name(dd, namebuf);
6f1ffb06
MA
521 fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
522 fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID,
d683ddbb 523 dsl_dir_phys(dd)->dd_head_dataset_obj);
6f1ffb06
MA
524
525 va_start(adx, fmt);
526 log_internal(nvl, operation, dd->dd_pool->dp_spa, tx, fmt, adx);
527 va_end(adx);
528}
529
530void
531spa_history_log_version(spa_t *spa, const char *operation)
45d1cae3 532{
f0e324f2
BB
533 utsname_t *u = utsname();
534
6f1ffb06
MA
535 spa_history_log_internal(spa, operation, NULL,
536 "pool version %llu; software version %llu/%d; uts %s %s %s %s",
13fe0198 537 (u_longlong_t)spa_version(spa), SPA_VERSION, ZPL_VERSION,
f0e324f2 538 u->nodename, u->release, u->version, u->machine);
45d1cae3 539}
c28b2279
BB
540
541#if defined(_KERNEL) && defined(HAVE_SPL)
542EXPORT_SYMBOL(spa_history_create_obj);
543EXPORT_SYMBOL(spa_history_get);
544EXPORT_SYMBOL(spa_history_log);
545EXPORT_SYMBOL(spa_history_log_internal);
546EXPORT_SYMBOL(spa_history_log_version);
547#endif