]>
Commit | Line | Data |
---|---|---|
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 | /* | |
23 | * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. | |
24 | */ | |
25 | ||
26 | #include <sys/spa.h> | |
27 | #include <sys/spa_impl.h> | |
28 | #include <sys/zap.h> | |
29 | #include <sys/dsl_synctask.h> | |
30 | #include <sys/dmu_tx.h> | |
31 | #include <sys/dmu_objset.h> | |
32 | #include <sys/utsname.h> | |
33 | #include <sys/cmn_err.h> | |
34 | #include <sys/sunddi.h> | |
35 | #include "zfs_comutil.h" | |
36 | #ifdef _KERNEL | |
37 | #include <sys/zone.h> | |
38 | #endif | |
39 | ||
40 | /* | |
41 | * Routines to manage the on-disk history log. | |
42 | * | |
43 | * The history log is stored as a dmu object containing | |
44 | * <packed record length, record nvlist> tuples. | |
45 | * | |
46 | * Where "record nvlist" is a nvlist containing uint64_ts and strings, and | |
47 | * "packed record length" is the packed length of the "record nvlist" stored | |
48 | * as a little endian uint64_t. | |
49 | * | |
50 | * The log is implemented as a ring buffer, though the original creation | |
51 | * of the pool ('zpool create') is never overwritten. | |
52 | * | |
53 | * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer | |
54 | * of 'spa_history' stores the offsets for logging/retrieving history as | |
55 | * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of | |
56 | * where the 'zpool create' record is stored. This allows us to never | |
57 | * overwrite the original creation of the pool. 'sh_phys_max_off' is the | |
58 | * physical ending offset in bytes of the log. This tells you the length of | |
59 | * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record | |
60 | * is added, 'sh_eof' is incremented by the the size of the record. | |
61 | * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes). | |
62 | * This is where the consumer should start reading from after reading in | |
63 | * the 'zpool create' portion of the log. | |
64 | * | |
65 | * 'sh_records_lost' keeps track of how many records have been overwritten | |
66 | * and permanently lost. | |
67 | */ | |
68 | ||
69 | /* convert a logical offset to physical */ | |
70 | static uint64_t | |
71 | spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp) | |
72 | { | |
73 | uint64_t phys_len; | |
74 | ||
75 | phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len; | |
76 | return ((log_off - shpp->sh_pool_create_len) % phys_len | |
77 | + shpp->sh_pool_create_len); | |
78 | } | |
79 | ||
80 | void | |
81 | spa_history_create_obj(spa_t *spa, dmu_tx_t *tx) | |
82 | { | |
83 | dmu_buf_t *dbp; | |
84 | spa_history_phys_t *shpp; | |
85 | objset_t *mos = spa->spa_meta_objset; | |
86 | ||
87 | ASSERT(spa->spa_history == 0); | |
88 | spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY, | |
89 | SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS, | |
90 | sizeof (spa_history_phys_t), tx); | |
91 | ||
92 | VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, | |
93 | DMU_POOL_HISTORY, sizeof (uint64_t), 1, | |
94 | &spa->spa_history, tx) == 0); | |
95 | ||
96 | VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)); | |
97 | ASSERT(dbp->db_size >= sizeof (spa_history_phys_t)); | |
98 | ||
99 | shpp = dbp->db_data; | |
100 | dmu_buf_will_dirty(dbp, tx); | |
101 | ||
102 | /* | |
103 | * Figure out maximum size of history log. We set it at | |
104 | * 1% of pool size, with a max of 32MB and min of 128KB. | |
105 | */ | |
106 | shpp->sh_phys_max_off = | |
107 | metaslab_class_get_dspace(spa_normal_class(spa)) / 100; | |
108 | shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20); | |
109 | shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10); | |
110 | ||
111 | dmu_buf_rele(dbp, FTAG); | |
112 | } | |
113 | ||
114 | /* | |
115 | * Change 'sh_bof' to the beginning of the next record. | |
116 | */ | |
117 | static int | |
118 | spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp) | |
119 | { | |
120 | objset_t *mos = spa->spa_meta_objset; | |
121 | uint64_t firstread, reclen, phys_bof; | |
122 | char buf[sizeof (reclen)]; | |
123 | int err; | |
124 | ||
125 | phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp); | |
126 | firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof); | |
127 | ||
128 | if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread, | |
129 | buf, DMU_READ_PREFETCH)) != 0) | |
130 | return (err); | |
131 | if (firstread != sizeof (reclen)) { | |
132 | if ((err = dmu_read(mos, spa->spa_history, | |
133 | shpp->sh_pool_create_len, sizeof (reclen) - firstread, | |
134 | buf + firstread, DMU_READ_PREFETCH)) != 0) | |
135 | return (err); | |
136 | } | |
137 | ||
138 | reclen = LE_64(*((uint64_t *)buf)); | |
139 | shpp->sh_bof += reclen + sizeof (reclen); | |
140 | shpp->sh_records_lost++; | |
141 | return (0); | |
142 | } | |
143 | ||
144 | static int | |
145 | spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp, | |
146 | dmu_tx_t *tx) | |
147 | { | |
148 | uint64_t firstwrite, phys_eof; | |
149 | objset_t *mos = spa->spa_meta_objset; | |
150 | int err; | |
151 | ||
152 | ASSERT(MUTEX_HELD(&spa->spa_history_lock)); | |
153 | ||
154 | /* see if we need to reset logical BOF */ | |
155 | while (shpp->sh_phys_max_off - shpp->sh_pool_create_len - | |
156 | (shpp->sh_eof - shpp->sh_bof) <= len) { | |
157 | if ((err = spa_history_advance_bof(spa, shpp)) != 0) { | |
158 | return (err); | |
159 | } | |
160 | } | |
161 | ||
162 | phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp); | |
163 | firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof); | |
164 | shpp->sh_eof += len; | |
165 | dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx); | |
166 | ||
167 | len -= firstwrite; | |
168 | if (len > 0) { | |
169 | /* write out the rest at the beginning of physical file */ | |
170 | dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len, | |
171 | len, (char *)buf + firstwrite, tx); | |
172 | } | |
173 | ||
174 | return (0); | |
175 | } | |
176 | ||
177 | static char * | |
178 | spa_history_zone(void) | |
179 | { | |
180 | #ifdef _KERNEL | |
181 | #ifdef HAVE_SPL | |
182 | return ("linux"); | |
183 | #else | |
184 | return (curproc->p_zone->zone_name); | |
185 | #endif | |
186 | #else | |
187 | return ("global"); | |
188 | #endif | |
189 | } | |
190 | ||
191 | /* | |
192 | * Write out a history event. | |
193 | */ | |
194 | /*ARGSUSED*/ | |
195 | static void | |
196 | spa_history_log_sync(void *arg1, void *arg2, dmu_tx_t *tx) | |
197 | { | |
198 | spa_t *spa = arg1; | |
199 | history_arg_t *hap = arg2; | |
200 | const char *history_str = hap->ha_history_str; | |
201 | objset_t *mos = spa->spa_meta_objset; | |
202 | dmu_buf_t *dbp; | |
203 | spa_history_phys_t *shpp; | |
204 | size_t reclen; | |
205 | uint64_t le_len; | |
206 | nvlist_t *nvrecord; | |
207 | char *record_packed = NULL; | |
208 | int ret; | |
209 | ||
210 | /* | |
211 | * If we have an older pool that doesn't have a command | |
212 | * history object, create it now. | |
213 | */ | |
214 | mutex_enter(&spa->spa_history_lock); | |
215 | if (!spa->spa_history) | |
216 | spa_history_create_obj(spa, tx); | |
217 | mutex_exit(&spa->spa_history_lock); | |
218 | ||
219 | /* | |
220 | * Get the offset of where we need to write via the bonus buffer. | |
221 | * Update the offset when the write completes. | |
222 | */ | |
223 | VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)); | |
224 | shpp = dbp->db_data; | |
225 | ||
226 | dmu_buf_will_dirty(dbp, tx); | |
227 | ||
228 | #ifdef ZFS_DEBUG | |
229 | { | |
230 | dmu_object_info_t doi; | |
231 | dmu_object_info_from_db(dbp, &doi); | |
232 | ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS); | |
233 | } | |
234 | #endif | |
235 | ||
236 | VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
237 | VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME, | |
238 | gethrestime_sec()) == 0); | |
239 | VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO, hap->ha_uid) == 0); | |
240 | if (hap->ha_zone != NULL) | |
241 | VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_ZONE, | |
242 | hap->ha_zone) == 0); | |
243 | #ifdef _KERNEL | |
244 | VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_HOST, | |
245 | utsname.nodename) == 0); | |
246 | #endif | |
247 | if (hap->ha_log_type == LOG_CMD_POOL_CREATE || | |
248 | hap->ha_log_type == LOG_CMD_NORMAL) { | |
249 | VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD, | |
250 | history_str) == 0); | |
251 | ||
252 | zfs_dbgmsg("command: %s", history_str); | |
253 | } else { | |
254 | VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT, | |
255 | hap->ha_event) == 0); | |
256 | VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TXG, | |
257 | tx->tx_txg) == 0); | |
258 | VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR, | |
259 | history_str) == 0); | |
260 | ||
261 | zfs_dbgmsg("internal %s pool:%s txg:%llu %s", | |
262 | zfs_history_event_names[hap->ha_event], spa_name(spa), | |
263 | (longlong_t)tx->tx_txg, history_str); | |
264 | ||
265 | } | |
266 | ||
267 | VERIFY(nvlist_size(nvrecord, &reclen, NV_ENCODE_XDR) == 0); | |
268 | record_packed = kmem_alloc(reclen, KM_SLEEP); | |
269 | ||
270 | VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen, | |
271 | NV_ENCODE_XDR, KM_SLEEP) == 0); | |
272 | ||
273 | mutex_enter(&spa->spa_history_lock); | |
274 | if (hap->ha_log_type == LOG_CMD_POOL_CREATE) | |
275 | VERIFY(shpp->sh_eof == shpp->sh_pool_create_len); | |
276 | ||
277 | /* write out the packed length as little endian */ | |
278 | le_len = LE_64((uint64_t)reclen); | |
279 | ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx); | |
280 | if (!ret) | |
281 | ret = spa_history_write(spa, record_packed, reclen, shpp, tx); | |
282 | ||
283 | if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) { | |
284 | shpp->sh_pool_create_len += sizeof (le_len) + reclen; | |
285 | shpp->sh_bof = shpp->sh_pool_create_len; | |
286 | } | |
287 | ||
288 | mutex_exit(&spa->spa_history_lock); | |
289 | nvlist_free(nvrecord); | |
290 | kmem_free(record_packed, reclen); | |
291 | dmu_buf_rele(dbp, FTAG); | |
292 | ||
293 | strfree(hap->ha_history_str); | |
294 | if (hap->ha_zone != NULL) | |
295 | strfree(hap->ha_zone); | |
296 | kmem_free(hap, sizeof (history_arg_t)); | |
297 | } | |
298 | ||
299 | /* | |
300 | * Write out a history event. | |
301 | */ | |
302 | int | |
303 | spa_history_log(spa_t *spa, const char *history_str, history_log_type_t what) | |
304 | { | |
305 | history_arg_t *ha; | |
306 | int err = 0; | |
307 | dmu_tx_t *tx; | |
308 | ||
309 | ASSERT(what != LOG_INTERNAL); | |
310 | ||
311 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
312 | err = dmu_tx_assign(tx, TXG_WAIT); | |
313 | if (err) { | |
314 | dmu_tx_abort(tx); | |
315 | return (err); | |
316 | } | |
317 | ||
318 | ha = kmem_alloc(sizeof (history_arg_t), KM_SLEEP); | |
319 | ha->ha_history_str = strdup(history_str); | |
320 | ha->ha_zone = strdup(spa_history_zone()); | |
321 | ha->ha_log_type = what; | |
322 | ha->ha_uid = crgetuid(CRED()); | |
323 | ||
324 | /* Kick this off asynchronously; errors are ignored. */ | |
325 | dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL, | |
326 | spa_history_log_sync, spa, ha, 0, tx); | |
327 | dmu_tx_commit(tx); | |
328 | ||
329 | /* spa_history_log_sync will free ha and strings */ | |
330 | return (err); | |
331 | } | |
332 | ||
333 | /* | |
334 | * Read out the command history. | |
335 | */ | |
336 | int | |
337 | spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf) | |
338 | { | |
339 | objset_t *mos = spa->spa_meta_objset; | |
340 | dmu_buf_t *dbp; | |
341 | uint64_t read_len, phys_read_off, phys_eof; | |
342 | uint64_t leftover = 0; | |
343 | spa_history_phys_t *shpp; | |
344 | int err; | |
345 | ||
346 | /* | |
347 | * If the command history doesn't exist (older pool), | |
348 | * that's ok, just return ENOENT. | |
349 | */ | |
350 | if (!spa->spa_history) | |
351 | return (ENOENT); | |
352 | ||
353 | /* | |
354 | * The history is logged asynchronously, so when they request | |
355 | * the first chunk of history, make sure everything has been | |
356 | * synced to disk so that we get it. | |
357 | */ | |
358 | if (*offp == 0 && spa_writeable(spa)) | |
359 | txg_wait_synced(spa_get_dsl(spa), 0); | |
360 | ||
361 | if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0) | |
362 | return (err); | |
363 | shpp = dbp->db_data; | |
364 | ||
365 | #ifdef ZFS_DEBUG | |
366 | { | |
367 | dmu_object_info_t doi; | |
368 | dmu_object_info_from_db(dbp, &doi); | |
369 | ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS); | |
370 | } | |
371 | #endif | |
372 | ||
373 | mutex_enter(&spa->spa_history_lock); | |
374 | phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp); | |
375 | ||
376 | if (*offp < shpp->sh_pool_create_len) { | |
377 | /* read in just the zpool create history */ | |
378 | phys_read_off = *offp; | |
379 | read_len = MIN(*len, shpp->sh_pool_create_len - | |
380 | phys_read_off); | |
381 | } else { | |
382 | /* | |
383 | * Need to reset passed in offset to BOF if the passed in | |
384 | * offset has since been overwritten. | |
385 | */ | |
386 | *offp = MAX(*offp, shpp->sh_bof); | |
387 | phys_read_off = spa_history_log_to_phys(*offp, shpp); | |
388 | ||
389 | /* | |
390 | * Read up to the minimum of what the user passed down or | |
391 | * the EOF (physical or logical). If we hit physical EOF, | |
392 | * use 'leftover' to read from the physical BOF. | |
393 | */ | |
394 | if (phys_read_off <= phys_eof) { | |
395 | read_len = MIN(*len, phys_eof - phys_read_off); | |
396 | } else { | |
397 | read_len = MIN(*len, | |
398 | shpp->sh_phys_max_off - phys_read_off); | |
399 | if (phys_read_off + *len > shpp->sh_phys_max_off) { | |
400 | leftover = MIN(*len - read_len, | |
401 | phys_eof - shpp->sh_pool_create_len); | |
402 | } | |
403 | } | |
404 | } | |
405 | ||
406 | /* offset for consumer to use next */ | |
407 | *offp += read_len + leftover; | |
408 | ||
409 | /* tell the consumer how much you actually read */ | |
410 | *len = read_len + leftover; | |
411 | ||
412 | if (read_len == 0) { | |
413 | mutex_exit(&spa->spa_history_lock); | |
414 | dmu_buf_rele(dbp, FTAG); | |
415 | return (0); | |
416 | } | |
417 | ||
418 | err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf, | |
419 | DMU_READ_PREFETCH); | |
420 | if (leftover && err == 0) { | |
421 | err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len, | |
422 | leftover, buf + read_len, DMU_READ_PREFETCH); | |
423 | } | |
424 | mutex_exit(&spa->spa_history_lock); | |
425 | ||
426 | dmu_buf_rele(dbp, FTAG); | |
427 | return (err); | |
428 | } | |
429 | ||
430 | static void | |
431 | log_internal(history_internal_events_t event, spa_t *spa, | |
432 | dmu_tx_t *tx, const char *fmt, va_list adx) | |
433 | { | |
434 | history_arg_t *ha; | |
435 | va_list adx_copy; | |
436 | ||
437 | /* | |
438 | * If this is part of creating a pool, not everything is | |
439 | * initialized yet, so don't bother logging the internal events. | |
440 | */ | |
441 | if (tx->tx_txg == TXG_INITIAL) | |
442 | return; | |
443 | ||
444 | ha = kmem_alloc(sizeof (history_arg_t), KM_SLEEP); | |
445 | va_copy(adx_copy, adx); | |
446 | ha->ha_history_str = kmem_vasprintf(fmt, adx_copy); | |
447 | va_end(adx_copy); | |
448 | ha->ha_log_type = LOG_INTERNAL; | |
449 | ha->ha_event = event; | |
450 | ha->ha_zone = NULL; | |
451 | ha->ha_uid = 0; | |
452 | ||
453 | if (dmu_tx_is_syncing(tx)) { | |
454 | spa_history_log_sync(spa, ha, tx); | |
455 | } else { | |
456 | dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL, | |
457 | spa_history_log_sync, spa, ha, 0, tx); | |
458 | } | |
459 | /* spa_history_log_sync() will free ha and strings */ | |
460 | } | |
461 | ||
462 | void | |
463 | spa_history_log_internal(history_internal_events_t event, spa_t *spa, | |
464 | dmu_tx_t *tx, const char *fmt, ...) | |
465 | { | |
466 | dmu_tx_t *htx = tx; | |
467 | va_list adx; | |
468 | ||
469 | /* create a tx if we didn't get one */ | |
470 | if (tx == NULL) { | |
471 | htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
472 | if (dmu_tx_assign(htx, TXG_WAIT) != 0) { | |
473 | dmu_tx_abort(htx); | |
474 | return; | |
475 | } | |
476 | } | |
477 | ||
478 | va_start(adx, fmt); | |
479 | log_internal(event, spa, htx, fmt, adx); | |
480 | va_end(adx); | |
481 | ||
482 | /* if we didn't get a tx from the caller, commit the one we made */ | |
483 | if (tx == NULL) | |
484 | dmu_tx_commit(htx); | |
485 | } | |
486 | ||
487 | void | |
488 | spa_history_log_version(spa_t *spa, history_internal_events_t event) | |
489 | { | |
490 | #ifdef _KERNEL | |
491 | uint64_t current_vers = spa_version(spa); | |
492 | ||
493 | if (current_vers >= SPA_VERSION_ZPOOL_HISTORY) { | |
494 | spa_history_log_internal(event, spa, NULL, | |
495 | "pool spa %llu; zfs spa %llu; zpl %d; uts %s %s %s %s", | |
496 | (u_longlong_t)current_vers, SPA_VERSION, ZPL_VERSION, | |
497 | utsname.nodename, utsname.release, utsname.version, | |
498 | utsname.machine); | |
499 | } | |
500 | cmn_err(CE_CONT, "!%s version %llu pool %s using %llu", | |
501 | event == LOG_POOL_IMPORT ? "imported" : | |
502 | event == LOG_POOL_CREATE ? "created" : "accessed", | |
503 | (u_longlong_t)current_vers, spa_name(spa), SPA_VERSION); | |
504 | #endif | |
505 | } | |
506 | ||
507 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
508 | EXPORT_SYMBOL(spa_history_create_obj); | |
509 | EXPORT_SYMBOL(spa_history_get); | |
510 | EXPORT_SYMBOL(spa_history_log); | |
511 | EXPORT_SYMBOL(spa_history_log_internal); | |
512 | EXPORT_SYMBOL(spa_history_log_version); | |
513 | #endif |