]>
Commit | Line | Data |
---|---|---|
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. |
cae5b340 AX |
24 | * Copyright (c) 2011, 2015 by Delphix. All rights reserved. |
25 | * Copyright (c) 2014 Integros [integros.com] | |
26 | * Copyright 2017 Joyent, Inc. | |
34dc7c2f BB |
27 | */ |
28 | ||
34dc7c2f BB |
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> | |
a08ee875 LG |
35 | #include <sys/dsl_dataset.h> |
36 | #include <sys/dsl_dir.h> | |
34dc7c2f BB |
37 | #include <sys/cmn_err.h> |
38 | #include <sys/sunddi.h> | |
a08ee875 | 39 | #include <sys/cred.h> |
428870ff | 40 | #include "zfs_comutil.h" |
34dc7c2f BB |
41 | #ifdef _KERNEL |
42 | #include <sys/zone.h> | |
43 | #endif | |
44 | ||
45 | /* | |
46 | * Routines to manage the on-disk history log. | |
47 | * | |
48 | * The history log is stored as a dmu object containing | |
49 | * <packed record length, record nvlist> tuples. | |
50 | * | |
cae5b340 | 51 | * Where "record nvlist" is an nvlist containing uint64_ts and strings, and |
34dc7c2f BB |
52 | * "packed record length" is the packed length of the "record nvlist" stored |
53 | * as a little endian uint64_t. | |
54 | * | |
55 | * The log is implemented as a ring buffer, though the original creation | |
56 | * of the pool ('zpool create') is never overwritten. | |
57 | * | |
58 | * The history log is tracked as object 'spa_t::spa_history'. The bonus buffer | |
59 | * of 'spa_history' stores the offsets for logging/retrieving history as | |
60 | * 'spa_history_phys_t'. 'sh_pool_create_len' is the ending offset in bytes of | |
61 | * where the 'zpool create' record is stored. This allows us to never | |
62 | * overwrite the original creation of the pool. 'sh_phys_max_off' is the | |
63 | * physical ending offset in bytes of the log. This tells you the length of | |
64 | * the buffer. 'sh_eof' is the logical EOF (in bytes). Whenever a record | |
65 | * is added, 'sh_eof' is incremented by the the size of the record. | |
66 | * 'sh_eof' is never decremented. 'sh_bof' is the logical BOF (in bytes). | |
67 | * This is where the consumer should start reading from after reading in | |
68 | * the 'zpool create' portion of the log. | |
69 | * | |
70 | * 'sh_records_lost' keeps track of how many records have been overwritten | |
71 | * and permanently lost. | |
72 | */ | |
73 | ||
74 | /* convert a logical offset to physical */ | |
75 | static uint64_t | |
76 | spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp) | |
77 | { | |
78 | uint64_t phys_len; | |
79 | ||
80 | phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len; | |
81 | return ((log_off - shpp->sh_pool_create_len) % phys_len | |
82 | + shpp->sh_pool_create_len); | |
83 | } | |
84 | ||
85 | void | |
86 | spa_history_create_obj(spa_t *spa, dmu_tx_t *tx) | |
87 | { | |
88 | dmu_buf_t *dbp; | |
89 | spa_history_phys_t *shpp; | |
90 | objset_t *mos = spa->spa_meta_objset; | |
91 | ||
92 | ASSERT(spa->spa_history == 0); | |
93 | spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY, | |
e10b0808 | 94 | SPA_OLD_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS, |
34dc7c2f BB |
95 | sizeof (spa_history_phys_t), tx); |
96 | ||
97 | VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, | |
98 | DMU_POOL_HISTORY, sizeof (uint64_t), 1, | |
99 | &spa->spa_history, tx) == 0); | |
100 | ||
101 | VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)); | |
102 | ASSERT(dbp->db_size >= sizeof (spa_history_phys_t)); | |
103 | ||
104 | shpp = dbp->db_data; | |
105 | dmu_buf_will_dirty(dbp, tx); | |
106 | ||
107 | /* | |
108 | * Figure out maximum size of history log. We set it at | |
330d06f9 | 109 | * 0.1% of pool size, with a max of 1G and min of 128KB. |
34dc7c2f | 110 | */ |
428870ff | 111 | shpp->sh_phys_max_off = |
330d06f9 MA |
112 | metaslab_class_get_dspace(spa_normal_class(spa)) / 1000; |
113 | shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 1<<30); | |
34dc7c2f BB |
114 | shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10); |
115 | ||
116 | dmu_buf_rele(dbp, FTAG); | |
117 | } | |
118 | ||
119 | /* | |
120 | * Change 'sh_bof' to the beginning of the next record. | |
121 | */ | |
122 | static int | |
123 | spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp) | |
124 | { | |
125 | objset_t *mos = spa->spa_meta_objset; | |
126 | uint64_t firstread, reclen, phys_bof; | |
127 | char buf[sizeof (reclen)]; | |
128 | int err; | |
129 | ||
130 | phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp); | |
131 | firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof); | |
132 | ||
133 | if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread, | |
9babb374 | 134 | buf, DMU_READ_PREFETCH)) != 0) |
34dc7c2f BB |
135 | return (err); |
136 | if (firstread != sizeof (reclen)) { | |
137 | if ((err = dmu_read(mos, spa->spa_history, | |
138 | shpp->sh_pool_create_len, sizeof (reclen) - firstread, | |
9babb374 | 139 | buf + firstread, DMU_READ_PREFETCH)) != 0) |
34dc7c2f BB |
140 | return (err); |
141 | } | |
142 | ||
143 | reclen = LE_64(*((uint64_t *)buf)); | |
144 | shpp->sh_bof += reclen + sizeof (reclen); | |
145 | shpp->sh_records_lost++; | |
146 | return (0); | |
147 | } | |
148 | ||
149 | static int | |
150 | spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp, | |
151 | dmu_tx_t *tx) | |
152 | { | |
153 | uint64_t firstwrite, phys_eof; | |
154 | objset_t *mos = spa->spa_meta_objset; | |
155 | int err; | |
156 | ||
157 | ASSERT(MUTEX_HELD(&spa->spa_history_lock)); | |
158 | ||
159 | /* see if we need to reset logical BOF */ | |
160 | while (shpp->sh_phys_max_off - shpp->sh_pool_create_len - | |
161 | (shpp->sh_eof - shpp->sh_bof) <= len) { | |
162 | if ((err = spa_history_advance_bof(spa, shpp)) != 0) { | |
163 | return (err); | |
164 | } | |
165 | } | |
166 | ||
167 | phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp); | |
168 | firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof); | |
169 | shpp->sh_eof += len; | |
170 | dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx); | |
171 | ||
172 | len -= firstwrite; | |
173 | if (len > 0) { | |
174 | /* write out the rest at the beginning of physical file */ | |
175 | dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len, | |
176 | len, (char *)buf + firstwrite, tx); | |
177 | } | |
178 | ||
179 | return (0); | |
180 | } | |
181 | ||
182 | static char * | |
0bc8fd78 | 183 | spa_history_zone(void) |
34dc7c2f BB |
184 | { |
185 | #ifdef _KERNEL | |
c28b2279 BB |
186 | #ifdef HAVE_SPL |
187 | return ("linux"); | |
188 | #else | |
34dc7c2f | 189 | return (curproc->p_zone->zone_name); |
c28b2279 | 190 | #endif |
34dc7c2f | 191 | #else |
a08ee875 | 192 | return (NULL); |
34dc7c2f BB |
193 | #endif |
194 | } | |
195 | ||
cae5b340 AX |
196 | /* |
197 | * Post a history sysevent. | |
198 | * | |
199 | * The nvlist_t* passed into this function will be transformed into a new | |
200 | * nvlist where: | |
201 | * | |
202 | * 1. Nested nvlists will be flattened to a single level | |
203 | * 2. Keys will have their names normalized (to remove any problematic | |
204 | * characters, such as whitespace) | |
205 | * | |
206 | * The nvlist_t passed into this function will duplicated and should be freed | |
207 | * by caller. | |
208 | * | |
209 | */ | |
210 | static void | |
211 | spa_history_log_notify(spa_t *spa, nvlist_t *nvl) | |
212 | { | |
213 | nvlist_t *hist_nvl = fnvlist_alloc(); | |
214 | uint64_t uint64; | |
215 | char *string; | |
216 | ||
217 | if (nvlist_lookup_string(nvl, ZPOOL_HIST_CMD, &string) == 0) | |
218 | fnvlist_add_string(hist_nvl, ZFS_EV_HIST_CMD, string); | |
219 | ||
220 | if (nvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME, &string) == 0) | |
221 | fnvlist_add_string(hist_nvl, ZFS_EV_HIST_INT_NAME, string); | |
222 | ||
223 | if (nvlist_lookup_string(nvl, ZPOOL_HIST_ZONE, &string) == 0) | |
224 | fnvlist_add_string(hist_nvl, ZFS_EV_HIST_ZONE, string); | |
225 | ||
226 | if (nvlist_lookup_string(nvl, ZPOOL_HIST_HOST, &string) == 0) | |
227 | fnvlist_add_string(hist_nvl, ZFS_EV_HIST_HOST, string); | |
228 | ||
229 | if (nvlist_lookup_string(nvl, ZPOOL_HIST_DSNAME, &string) == 0) | |
230 | fnvlist_add_string(hist_nvl, ZFS_EV_HIST_DSNAME, string); | |
231 | ||
232 | if (nvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR, &string) == 0) | |
233 | fnvlist_add_string(hist_nvl, ZFS_EV_HIST_INT_STR, string); | |
234 | ||
235 | if (nvlist_lookup_string(nvl, ZPOOL_HIST_IOCTL, &string) == 0) | |
236 | fnvlist_add_string(hist_nvl, ZFS_EV_HIST_IOCTL, string); | |
237 | ||
238 | if (nvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME, &string) == 0) | |
239 | fnvlist_add_string(hist_nvl, ZFS_EV_HIST_INT_NAME, string); | |
240 | ||
241 | if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_DSID, &uint64) == 0) | |
242 | fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_DSID, uint64); | |
243 | ||
244 | if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG, &uint64) == 0) | |
245 | fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_TXG, uint64); | |
246 | ||
247 | if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_TIME, &uint64) == 0) | |
248 | fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_TIME, uint64); | |
249 | ||
250 | if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_WHO, &uint64) == 0) | |
251 | fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_WHO, uint64); | |
252 | ||
253 | if (nvlist_lookup_uint64(nvl, ZPOOL_HIST_INT_EVENT, &uint64) == 0) | |
254 | fnvlist_add_uint64(hist_nvl, ZFS_EV_HIST_INT_EVENT, uint64); | |
255 | ||
256 | spa_event_notify(spa, NULL, hist_nvl, ESC_ZFS_HISTORY_EVENT); | |
257 | ||
258 | nvlist_free(hist_nvl); | |
259 | } | |
260 | ||
34dc7c2f BB |
261 | /* |
262 | * Write out a history event. | |
263 | */ | |
428870ff | 264 | /*ARGSUSED*/ |
34dc7c2f | 265 | static void |
a08ee875 | 266 | spa_history_log_sync(void *arg, dmu_tx_t *tx) |
34dc7c2f | 267 | { |
a08ee875 LG |
268 | nvlist_t *nvl = arg; |
269 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
34dc7c2f BB |
270 | objset_t *mos = spa->spa_meta_objset; |
271 | dmu_buf_t *dbp; | |
272 | spa_history_phys_t *shpp; | |
273 | size_t reclen; | |
274 | uint64_t le_len; | |
34dc7c2f BB |
275 | char *record_packed = NULL; |
276 | int ret; | |
277 | ||
278 | /* | |
279 | * If we have an older pool that doesn't have a command | |
280 | * history object, create it now. | |
281 | */ | |
282 | mutex_enter(&spa->spa_history_lock); | |
283 | if (!spa->spa_history) | |
284 | spa_history_create_obj(spa, tx); | |
285 | mutex_exit(&spa->spa_history_lock); | |
286 | ||
287 | /* | |
288 | * Get the offset of where we need to write via the bonus buffer. | |
289 | * Update the offset when the write completes. | |
290 | */ | |
a08ee875 | 291 | VERIFY0(dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)); |
34dc7c2f BB |
292 | shpp = dbp->db_data; |
293 | ||
294 | dmu_buf_will_dirty(dbp, tx); | |
295 | ||
296 | #ifdef ZFS_DEBUG | |
297 | { | |
298 | dmu_object_info_t doi; | |
299 | dmu_object_info_from_db(dbp, &doi); | |
300 | ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS); | |
301 | } | |
302 | #endif | |
303 | ||
a08ee875 | 304 | fnvlist_add_uint64(nvl, ZPOOL_HIST_TIME, gethrestime_sec()); |
ea04106b AX |
305 | fnvlist_add_string(nvl, ZPOOL_HIST_HOST, utsname()->nodename); |
306 | ||
a08ee875 LG |
307 | if (nvlist_exists(nvl, ZPOOL_HIST_CMD)) { |
308 | zfs_dbgmsg("command: %s", | |
309 | fnvlist_lookup_string(nvl, ZPOOL_HIST_CMD)); | |
310 | } else if (nvlist_exists(nvl, ZPOOL_HIST_INT_NAME)) { | |
311 | if (nvlist_exists(nvl, ZPOOL_HIST_DSNAME)) { | |
312 | zfs_dbgmsg("txg %lld %s %s (id %llu) %s", | |
313 | fnvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG), | |
314 | fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME), | |
315 | fnvlist_lookup_string(nvl, ZPOOL_HIST_DSNAME), | |
316 | fnvlist_lookup_uint64(nvl, ZPOOL_HIST_DSID), | |
317 | fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR)); | |
318 | } else { | |
319 | zfs_dbgmsg("txg %lld %s %s", | |
320 | fnvlist_lookup_uint64(nvl, ZPOOL_HIST_TXG), | |
321 | fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_NAME), | |
322 | fnvlist_lookup_string(nvl, ZPOOL_HIST_INT_STR)); | |
323 | } | |
cae5b340 AX |
324 | /* |
325 | * The history sysevent is posted only for internal history | |
326 | * messages to show what has happened, not how it happened. For | |
327 | * example, the following command: | |
328 | * | |
329 | * # zfs destroy -r tank/foo | |
330 | * | |
331 | * will result in one sysevent posted per dataset that is | |
332 | * destroyed as a result of the command - which could be more | |
333 | * than one event in total. By contrast, if the sysevent was | |
334 | * posted as a result of the ZPOOL_HIST_CMD key being present | |
335 | * it would result in only one sysevent being posted with the | |
336 | * full command line arguments, requiring the consumer to know | |
337 | * how to parse and understand zfs(1M) command invocations. | |
338 | */ | |
339 | spa_history_log_notify(spa, nvl); | |
a08ee875 LG |
340 | } else if (nvlist_exists(nvl, ZPOOL_HIST_IOCTL)) { |
341 | zfs_dbgmsg("ioctl %s", | |
342 | fnvlist_lookup_string(nvl, ZPOOL_HIST_IOCTL)); | |
34dc7c2f BB |
343 | } |
344 | ||
a08ee875 | 345 | VERIFY3U(nvlist_pack(nvl, &record_packed, &reclen, NV_ENCODE_NATIVE, |
ea04106b | 346 | KM_SLEEP), ==, 0); |
34dc7c2f BB |
347 | |
348 | mutex_enter(&spa->spa_history_lock); | |
34dc7c2f BB |
349 | |
350 | /* write out the packed length as little endian */ | |
351 | le_len = LE_64((uint64_t)reclen); | |
352 | ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx); | |
353 | if (!ret) | |
354 | ret = spa_history_write(spa, record_packed, reclen, shpp, tx); | |
355 | ||
a08ee875 LG |
356 | /* The first command is the create, which we keep forever */ |
357 | if (ret == 0 && shpp->sh_pool_create_len == 0 && | |
358 | nvlist_exists(nvl, ZPOOL_HIST_CMD)) { | |
359 | shpp->sh_pool_create_len = shpp->sh_bof = shpp->sh_eof; | |
34dc7c2f BB |
360 | } |
361 | ||
362 | mutex_exit(&spa->spa_history_lock); | |
a08ee875 | 363 | fnvlist_pack_free(record_packed, reclen); |
34dc7c2f | 364 | dmu_buf_rele(dbp, FTAG); |
a08ee875 | 365 | fnvlist_free(nvl); |
34dc7c2f BB |
366 | } |
367 | ||
368 | /* | |
369 | * Write out a history event. | |
370 | */ | |
371 | int | |
a08ee875 LG |
372 | spa_history_log(spa_t *spa, const char *msg) |
373 | { | |
374 | int err; | |
ea04106b | 375 | nvlist_t *nvl = fnvlist_alloc(); |
a08ee875 LG |
376 | |
377 | fnvlist_add_string(nvl, ZPOOL_HIST_CMD, msg); | |
378 | err = spa_history_log_nvl(spa, nvl); | |
379 | fnvlist_free(nvl); | |
380 | return (err); | |
381 | } | |
382 | ||
383 | int | |
384 | spa_history_log_nvl(spa_t *spa, nvlist_t *nvl) | |
34dc7c2f | 385 | { |
428870ff BB |
386 | int err = 0; |
387 | dmu_tx_t *tx; | |
a08ee875 | 388 | nvlist_t *nvarg; |
34dc7c2f | 389 | |
a08ee875 LG |
390 | if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY || !spa_writeable(spa)) |
391 | return (SET_ERROR(EINVAL)); | |
34dc7c2f | 392 | |
428870ff BB |
393 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); |
394 | err = dmu_tx_assign(tx, TXG_WAIT); | |
395 | if (err) { | |
396 | dmu_tx_abort(tx); | |
397 | return (err); | |
398 | } | |
399 | ||
ea04106b | 400 | VERIFY0(nvlist_dup(nvl, &nvarg, KM_SLEEP)); |
a08ee875 LG |
401 | if (spa_history_zone() != NULL) { |
402 | fnvlist_add_string(nvarg, ZPOOL_HIST_ZONE, | |
403 | spa_history_zone()); | |
404 | } | |
405 | fnvlist_add_uint64(nvarg, ZPOOL_HIST_WHO, crgetruid(CRED())); | |
428870ff BB |
406 | |
407 | /* Kick this off asynchronously; errors are ignored. */ | |
a08ee875 | 408 | dsl_sync_task_nowait(spa_get_dsl(spa), spa_history_log_sync, |
e10b0808 | 409 | nvarg, 0, ZFS_SPACE_CHECK_NONE, tx); |
428870ff BB |
410 | dmu_tx_commit(tx); |
411 | ||
a08ee875 | 412 | /* spa_history_log_sync will free nvl */ |
428870ff | 413 | return (err); |
a08ee875 | 414 | |
34dc7c2f BB |
415 | } |
416 | ||
417 | /* | |
418 | * Read out the command history. | |
419 | */ | |
420 | int | |
421 | spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf) | |
422 | { | |
423 | objset_t *mos = spa->spa_meta_objset; | |
424 | dmu_buf_t *dbp; | |
425 | uint64_t read_len, phys_read_off, phys_eof; | |
426 | uint64_t leftover = 0; | |
427 | spa_history_phys_t *shpp; | |
428 | int err; | |
429 | ||
430 | /* | |
a08ee875 | 431 | * If the command history doesn't exist (older pool), |
34dc7c2f BB |
432 | * that's ok, just return ENOENT. |
433 | */ | |
434 | if (!spa->spa_history) | |
a08ee875 | 435 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 436 | |
428870ff BB |
437 | /* |
438 | * The history is logged asynchronously, so when they request | |
439 | * the first chunk of history, make sure everything has been | |
440 | * synced to disk so that we get it. | |
441 | */ | |
442 | if (*offp == 0 && spa_writeable(spa)) | |
443 | txg_wait_synced(spa_get_dsl(spa), 0); | |
444 | ||
34dc7c2f BB |
445 | if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0) |
446 | return (err); | |
447 | shpp = dbp->db_data; | |
448 | ||
449 | #ifdef ZFS_DEBUG | |
450 | { | |
451 | dmu_object_info_t doi; | |
452 | dmu_object_info_from_db(dbp, &doi); | |
453 | ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS); | |
454 | } | |
455 | #endif | |
456 | ||
457 | mutex_enter(&spa->spa_history_lock); | |
458 | phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp); | |
459 | ||
460 | if (*offp < shpp->sh_pool_create_len) { | |
461 | /* read in just the zpool create history */ | |
462 | phys_read_off = *offp; | |
463 | read_len = MIN(*len, shpp->sh_pool_create_len - | |
464 | phys_read_off); | |
465 | } else { | |
466 | /* | |
467 | * Need to reset passed in offset to BOF if the passed in | |
468 | * offset has since been overwritten. | |
469 | */ | |
470 | *offp = MAX(*offp, shpp->sh_bof); | |
471 | phys_read_off = spa_history_log_to_phys(*offp, shpp); | |
472 | ||
473 | /* | |
474 | * Read up to the minimum of what the user passed down or | |
475 | * the EOF (physical or logical). If we hit physical EOF, | |
476 | * use 'leftover' to read from the physical BOF. | |
477 | */ | |
478 | if (phys_read_off <= phys_eof) { | |
479 | read_len = MIN(*len, phys_eof - phys_read_off); | |
480 | } else { | |
481 | read_len = MIN(*len, | |
482 | shpp->sh_phys_max_off - phys_read_off); | |
483 | if (phys_read_off + *len > shpp->sh_phys_max_off) { | |
484 | leftover = MIN(*len - read_len, | |
485 | phys_eof - shpp->sh_pool_create_len); | |
486 | } | |
487 | } | |
488 | } | |
489 | ||
490 | /* offset for consumer to use next */ | |
491 | *offp += read_len + leftover; | |
492 | ||
493 | /* tell the consumer how much you actually read */ | |
494 | *len = read_len + leftover; | |
495 | ||
496 | if (read_len == 0) { | |
497 | mutex_exit(&spa->spa_history_lock); | |
498 | dmu_buf_rele(dbp, FTAG); | |
499 | return (0); | |
500 | } | |
501 | ||
9babb374 BB |
502 | err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf, |
503 | DMU_READ_PREFETCH); | |
34dc7c2f BB |
504 | if (leftover && err == 0) { |
505 | err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len, | |
9babb374 | 506 | leftover, buf + read_len, DMU_READ_PREFETCH); |
34dc7c2f BB |
507 | } |
508 | mutex_exit(&spa->spa_history_lock); | |
509 | ||
510 | dmu_buf_rele(dbp, FTAG); | |
511 | return (err); | |
512 | } | |
513 | ||
a08ee875 LG |
514 | /* |
515 | * The nvlist will be consumed by this call. | |
516 | */ | |
45d1cae3 | 517 | static void |
a08ee875 | 518 | log_internal(nvlist_t *nvl, const char *operation, spa_t *spa, |
428870ff | 519 | dmu_tx_t *tx, const char *fmt, va_list adx) |
34dc7c2f | 520 | { |
a08ee875 | 521 | char *msg; |
34dc7c2f | 522 | |
b128c09f BB |
523 | /* |
524 | * If this is part of creating a pool, not everything is | |
525 | * initialized yet, so don't bother logging the internal events. | |
a08ee875 | 526 | * Likewise if the pool is not writeable. |
b128c09f | 527 | */ |
a08ee875 LG |
528 | if (tx->tx_txg == TXG_INITIAL || !spa_writeable(spa)) { |
529 | fnvlist_free(nvl); | |
b128c09f | 530 | return; |
a08ee875 LG |
531 | } |
532 | ||
ea04106b | 533 | msg = kmem_vasprintf(fmt, adx); |
a08ee875 | 534 | fnvlist_add_string(nvl, ZPOOL_HIST_INT_STR, msg); |
ea04106b | 535 | strfree(msg); |
b128c09f | 536 | |
a08ee875 LG |
537 | fnvlist_add_string(nvl, ZPOOL_HIST_INT_NAME, operation); |
538 | fnvlist_add_uint64(nvl, ZPOOL_HIST_TXG, tx->tx_txg); | |
34dc7c2f BB |
539 | |
540 | if (dmu_tx_is_syncing(tx)) { | |
a08ee875 | 541 | spa_history_log_sync(nvl, tx); |
34dc7c2f | 542 | } else { |
a08ee875 | 543 | dsl_sync_task_nowait(spa_get_dsl(spa), |
e10b0808 | 544 | spa_history_log_sync, nvl, 0, ZFS_SPACE_CHECK_NONE, tx); |
34dc7c2f | 545 | } |
a08ee875 | 546 | /* spa_history_log_sync() will free nvl */ |
34dc7c2f | 547 | } |
45d1cae3 BB |
548 | |
549 | void | |
a08ee875 | 550 | spa_history_log_internal(spa_t *spa, const char *operation, |
428870ff | 551 | dmu_tx_t *tx, const char *fmt, ...) |
45d1cae3 BB |
552 | { |
553 | dmu_tx_t *htx = tx; | |
554 | va_list adx; | |
555 | ||
556 | /* create a tx if we didn't get one */ | |
557 | if (tx == NULL) { | |
558 | htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
559 | if (dmu_tx_assign(htx, TXG_WAIT) != 0) { | |
560 | dmu_tx_abort(htx); | |
561 | return; | |
562 | } | |
563 | } | |
564 | ||
565 | va_start(adx, fmt); | |
ea04106b | 566 | log_internal(fnvlist_alloc(), operation, spa, htx, fmt, adx); |
45d1cae3 BB |
567 | va_end(adx); |
568 | ||
569 | /* if we didn't get a tx from the caller, commit the one we made */ | |
570 | if (tx == NULL) | |
571 | dmu_tx_commit(htx); | |
572 | } | |
573 | ||
574 | void | |
a08ee875 LG |
575 | spa_history_log_internal_ds(dsl_dataset_t *ds, const char *operation, |
576 | dmu_tx_t *tx, const char *fmt, ...) | |
577 | { | |
578 | va_list adx; | |
cae5b340 | 579 | char namebuf[ZFS_MAX_DATASET_NAME_LEN]; |
ea04106b | 580 | nvlist_t *nvl = fnvlist_alloc(); |
a08ee875 LG |
581 | |
582 | ASSERT(tx != NULL); | |
583 | ||
584 | dsl_dataset_name(ds, namebuf); | |
a08ee875 LG |
585 | fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf); |
586 | fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID, ds->ds_object); | |
587 | ||
588 | va_start(adx, fmt); | |
589 | log_internal(nvl, operation, dsl_dataset_get_spa(ds), tx, fmt, adx); | |
590 | va_end(adx); | |
591 | } | |
592 | ||
593 | void | |
594 | spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation, | |
595 | dmu_tx_t *tx, const char *fmt, ...) | |
45d1cae3 | 596 | { |
a08ee875 | 597 | va_list adx; |
cae5b340 | 598 | char namebuf[ZFS_MAX_DATASET_NAME_LEN]; |
ea04106b | 599 | nvlist_t *nvl = fnvlist_alloc(); |
a08ee875 LG |
600 | |
601 | ASSERT(tx != NULL); | |
602 | ||
603 | dsl_dir_name(dd, namebuf); | |
a08ee875 LG |
604 | fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf); |
605 | fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID, | |
e10b0808 | 606 | dsl_dir_phys(dd)->dd_head_dataset_obj); |
a08ee875 LG |
607 | |
608 | va_start(adx, fmt); | |
609 | log_internal(nvl, operation, dd->dd_pool->dp_spa, tx, fmt, adx); | |
610 | va_end(adx); | |
611 | } | |
612 | ||
613 | void | |
614 | spa_history_log_version(spa_t *spa, const char *operation) | |
615 | { | |
ea04106b AX |
616 | utsname_t *u = utsname(); |
617 | ||
a08ee875 | 618 | spa_history_log_internal(spa, operation, NULL, |
cae5b340 | 619 | "pool version %llu; software version %llu/%llu; uts %s %s %s %s", |
a08ee875 | 620 | (u_longlong_t)spa_version(spa), SPA_VERSION, ZPL_VERSION, |
ea04106b | 621 | u->nodename, u->release, u->version, u->machine); |
45d1cae3 | 622 | } |
c28b2279 BB |
623 | |
624 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
625 | EXPORT_SYMBOL(spa_history_create_obj); | |
626 | EXPORT_SYMBOL(spa_history_get); | |
627 | EXPORT_SYMBOL(spa_history_log); | |
628 | EXPORT_SYMBOL(spa_history_log_internal); | |
629 | EXPORT_SYMBOL(spa_history_log_version); | |
630 | #endif |