]> git.proxmox.com Git - mirror_zfs-debian.git/blame - zfs/lib/libzpool/spa_history.c
Rebase to OpenSolaris b103, in the process we are removing any code which did not...
[mirror_zfs-debian.git] / zfs / lib / libzpool / 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/*
b128c09f 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
34dc7c2f
BB
24 * Use is subject to license terms.
25 */
26
b128c09f 27#pragma ident "%Z%%M% %I% %E% SMI"
34dc7c2f
BB
28
29#include <sys/spa.h>
30#include <sys/spa_impl.h>
31#include <sys/zap.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/cmn_err.h>
37#include <sys/sunddi.h>
38#ifdef _KERNEL
39#include <sys/zone.h>
40#endif
41
42/*
43 * Routines to manage the on-disk history log.
44 *
45 * The history log is stored as a dmu object containing
46 * <packed record length, record nvlist> tuples.
47 *
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.
51 *
52 * The log is implemented as a ring buffer, though the original creation
53 * of the pool ('zpool create') is never overwritten.
54 *
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.
66 *
67 * 'sh_records_lost' keeps track of how many records have been overwritten
68 * and permanently lost.
69 */
70
71/* convert a logical offset to physical */
72static uint64_t
73spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
74{
75 uint64_t phys_len;
76
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);
80}
81
82void
83spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
84{
85 dmu_buf_t *dbp;
86 spa_history_phys_t *shpp;
87 objset_t *mos = spa->spa_meta_objset;
88
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);
93
94 VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
95 DMU_POOL_HISTORY, sizeof (uint64_t), 1,
96 &spa->spa_history, tx) == 0);
97
98 VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
99 ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
100
101 shpp = dbp->db_data;
102 dmu_buf_will_dirty(dbp, tx);
103
104 /*
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.
107 */
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);
111
112 dmu_buf_rele(dbp, FTAG);
113}
114
115/*
116 * Change 'sh_bof' to the beginning of the next record.
117 */
118static int
119spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
120{
121 objset_t *mos = spa->spa_meta_objset;
122 uint64_t firstread, reclen, phys_bof;
123 char buf[sizeof (reclen)];
124 int err;
125
126 phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
127 firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
128
129 if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
130 buf)) != 0)
131 return (err);
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)
136 return (err);
137 }
138
139 reclen = LE_64(*((uint64_t *)buf));
140 shpp->sh_bof += reclen + sizeof (reclen);
141 shpp->sh_records_lost++;
142 return (0);
143}
144
145static int
146spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
147 dmu_tx_t *tx)
148{
149 uint64_t firstwrite, phys_eof;
150 objset_t *mos = spa->spa_meta_objset;
151 int err;
152
153 ASSERT(MUTEX_HELD(&spa->spa_history_lock));
154
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) {
159 return (err);
160 }
161 }
162
163 phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
164 firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
165 shpp->sh_eof += len;
166 dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
167
168 len -= firstwrite;
169 if (len > 0) {
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);
173 }
174
175 return (0);
176}
177
178static char *
179spa_history_zone()
180{
181#ifdef _KERNEL
182 return (curproc->p_zone->zone_name);
183#else
184 return ("global");
185#endif
186}
187
188/*
189 * Write out a history event.
190 */
191static void
192spa_history_log_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
193{
194 spa_t *spa = arg1;
195 history_arg_t *hap = arg2;
196 const char *history_str = hap->ha_history_str;
197 objset_t *mos = spa->spa_meta_objset;
198 dmu_buf_t *dbp;
199 spa_history_phys_t *shpp;
200 size_t reclen;
201 uint64_t le_len;
202 nvlist_t *nvrecord;
203 char *record_packed = NULL;
204 int ret;
205
206 /*
207 * If we have an older pool that doesn't have a command
208 * history object, create it now.
209 */
210 mutex_enter(&spa->spa_history_lock);
211 if (!spa->spa_history)
212 spa_history_create_obj(spa, tx);
213 mutex_exit(&spa->spa_history_lock);
214
215 /*
216 * Get the offset of where we need to write via the bonus buffer.
217 * Update the offset when the write completes.
218 */
219 VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
220 shpp = dbp->db_data;
221
222 dmu_buf_will_dirty(dbp, tx);
223
224#ifdef ZFS_DEBUG
225 {
226 dmu_object_info_t doi;
227 dmu_object_info_from_db(dbp, &doi);
228 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
229 }
230#endif
231
232 VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
233 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME,
234 gethrestime_sec()) == 0);
235 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO,
236 (uint64_t)crgetuid(cr)) == 0);
237 if (hap->ha_zone[0] != '\0')
238 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_ZONE,
239 hap->ha_zone) == 0);
240#ifdef _KERNEL
241 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_HOST,
242 utsname.nodename) == 0);
243#endif
244 if (hap->ha_log_type == LOG_CMD_POOL_CREATE ||
245 hap->ha_log_type == LOG_CMD_NORMAL) {
246 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD,
247 history_str) == 0);
248 } else {
249 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT,
250 hap->ha_event) == 0);
251 VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TXG,
252 tx->tx_txg) == 0);
253 VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR,
254 history_str) == 0);
255 }
256
257 VERIFY(nvlist_size(nvrecord, &reclen, NV_ENCODE_XDR) == 0);
258 record_packed = kmem_alloc(reclen, KM_SLEEP);
259
260 VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
261 NV_ENCODE_XDR, KM_SLEEP) == 0);
262
263 mutex_enter(&spa->spa_history_lock);
264 if (hap->ha_log_type == LOG_CMD_POOL_CREATE)
265 VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
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
273 if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) {
274 shpp->sh_pool_create_len += sizeof (le_len) + reclen;
275 shpp->sh_bof = shpp->sh_pool_create_len;
276 }
277
278 mutex_exit(&spa->spa_history_lock);
279 nvlist_free(nvrecord);
280 kmem_free(record_packed, reclen);
281 dmu_buf_rele(dbp, FTAG);
282
283 if (hap->ha_log_type == LOG_INTERNAL) {
284 kmem_free((void*)hap->ha_history_str, HIS_MAX_RECORD_LEN);
285 kmem_free(hap, sizeof (history_arg_t));
286 }
287}
288
289/*
290 * Write out a history event.
291 */
292int
293spa_history_log(spa_t *spa, const char *history_str, history_log_type_t what)
294{
295 history_arg_t ha;
296
297 ASSERT(what != LOG_INTERNAL);
298
299 ha.ha_history_str = history_str;
300 ha.ha_log_type = what;
301 (void) strlcpy(ha.ha_zone, spa_history_zone(), sizeof (ha.ha_zone));
302 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync,
303 spa, &ha, 0));
304}
305
306/*
307 * Read out the command history.
308 */
309int
310spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
311{
312 objset_t *mos = spa->spa_meta_objset;
313 dmu_buf_t *dbp;
314 uint64_t read_len, phys_read_off, phys_eof;
315 uint64_t leftover = 0;
316 spa_history_phys_t *shpp;
317 int err;
318
319 /*
320 * If the command history doesn't exist (older pool),
321 * that's ok, just return ENOENT.
322 */
323 if (!spa->spa_history)
324 return (ENOENT);
325
326 if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
327 return (err);
328 shpp = dbp->db_data;
329
330#ifdef ZFS_DEBUG
331 {
332 dmu_object_info_t doi;
333 dmu_object_info_from_db(dbp, &doi);
334 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
335 }
336#endif
337
338 mutex_enter(&spa->spa_history_lock);
339 phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
340
341 if (*offp < shpp->sh_pool_create_len) {
342 /* read in just the zpool create history */
343 phys_read_off = *offp;
344 read_len = MIN(*len, shpp->sh_pool_create_len -
345 phys_read_off);
346 } else {
347 /*
348 * Need to reset passed in offset to BOF if the passed in
349 * offset has since been overwritten.
350 */
351 *offp = MAX(*offp, shpp->sh_bof);
352 phys_read_off = spa_history_log_to_phys(*offp, shpp);
353
354 /*
355 * Read up to the minimum of what the user passed down or
356 * the EOF (physical or logical). If we hit physical EOF,
357 * use 'leftover' to read from the physical BOF.
358 */
359 if (phys_read_off <= phys_eof) {
360 read_len = MIN(*len, phys_eof - phys_read_off);
361 } else {
362 read_len = MIN(*len,
363 shpp->sh_phys_max_off - phys_read_off);
364 if (phys_read_off + *len > shpp->sh_phys_max_off) {
365 leftover = MIN(*len - read_len,
366 phys_eof - shpp->sh_pool_create_len);
367 }
368 }
369 }
370
371 /* offset for consumer to use next */
372 *offp += read_len + leftover;
373
374 /* tell the consumer how much you actually read */
375 *len = read_len + leftover;
376
377 if (read_len == 0) {
378 mutex_exit(&spa->spa_history_lock);
379 dmu_buf_rele(dbp, FTAG);
380 return (0);
381 }
382
383 err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf);
384 if (leftover && err == 0) {
385 err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
386 leftover, buf + read_len);
387 }
388 mutex_exit(&spa->spa_history_lock);
389
390 dmu_buf_rele(dbp, FTAG);
391 return (err);
392}
393
394void
395spa_history_internal_log(history_internal_events_t event, spa_t *spa,
396 dmu_tx_t *tx, cred_t *cr, const char *fmt, ...)
397{
398 history_arg_t *hap;
399 char *str;
400 va_list adx;
401
b128c09f
BB
402 /*
403 * If this is part of creating a pool, not everything is
404 * initialized yet, so don't bother logging the internal events.
405 */
406 if (tx->tx_txg == TXG_INITIAL)
407 return;
408
34dc7c2f
BB
409 hap = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
410 str = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
411
412 va_start(adx, fmt);
413 (void) vsnprintf(str, HIS_MAX_RECORD_LEN, fmt, adx);
414 va_end(adx);
415
416 hap->ha_log_type = LOG_INTERNAL;
417 hap->ha_history_str = str;
418 hap->ha_event = event;
419 hap->ha_zone[0] = '\0';
420
421 if (dmu_tx_is_syncing(tx)) {
422 spa_history_log_sync(spa, hap, cr, tx);
423 } else {
424 dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
425 spa_history_log_sync, spa, hap, 0, tx);
426 }
427 /* spa_history_log_sync() will free hap and str */
428}