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  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25  * Copyright (c) 2014 Integros [integros.com]
26  */
27 
28 #include <sys/spa.h>
29 #include <sys/spa_impl.h>
30 #include <sys/zap.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/utsname.h>
37 #include <sys/sunddi.h>
38 #include <sys/cred.h>
39 #include "zfs_comutil.h"
40 #ifdef _KERNEL
41 #include <sys/cmn_err.h>
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  *
51  * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
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
spa_history_log_to_phys(uint64_t log_off,spa_history_phys_t * shpp)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
spa_history_create_obj(spa_t * spa,dmu_tx_t * tx)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,
94 	    SPA_OLD_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
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
109 	 * 0.1% of pool size, with a max of 1G and min of 128KB.
110 	 */
111 	shpp->sh_phys_max_off =
112 	    metaslab_class_get_dspace(spa_normal_class(spa)) / 1000;
113 	shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 1<<30);
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
spa_history_advance_bof(spa_t * spa,spa_history_phys_t * shpp)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,
134 	    buf, DMU_READ_PREFETCH)) != 0)
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,
139 		    buf + firstread, DMU_READ_PREFETCH)) != 0)
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
spa_history_write(spa_t * spa,void * buf,uint64_t len,spa_history_phys_t * shpp,dmu_tx_t * tx)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 *
spa_history_zone(void)183 spa_history_zone(void)
184 {
185 #ifdef _KERNEL
186 	/* XXX: pr_hostname can be changed by default from within a jail! */
187 	if (jailed(curthread->td_ucred))
188 		return (curthread->td_ucred->cr_prison->pr_hostname);
189 #endif
190 	return (NULL);
191 }
192 
193 /*
194  * Write out a history event.
195  */
196 /*ARGSUSED*/
197 static void
spa_history_log_sync(void * arg,dmu_tx_t * tx)198 spa_history_log_sync(void *arg, dmu_tx_t *tx)
199 {
200 	nvlist_t	*nvl = arg;
201 	spa_t		*spa = dmu_tx_pool(tx)->dp_spa;
202 	objset_t	*mos = spa->spa_meta_objset;
203 	dmu_buf_t	*dbp;
204 	spa_history_phys_t *shpp;
205 	size_t		reclen;
206 	uint64_t	le_len;
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 	VERIFY0(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 	fnvlist_add_uint64(nvl, ZPOOL_HIST_TIME, gethrestime_sec());
237 #ifdef _KERNEL
238 	fnvlist_add_string(nvl, ZPOOL_HIST_HOST, utsname.nodename);
239 #endif
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));
260 	}
261 
262 	record_packed = fnvlist_pack(nvl, &reclen);
263 
264 	mutex_enter(&spa->spa_history_lock);
265 
266 	/* write out the packed length as little endian */
267 	le_len = LE_64((uint64_t)reclen);
268 	ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
269 	if (!ret)
270 		ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
271 
272 	/* The first command is the create, which we keep forever */
273 	if (ret == 0 && shpp->sh_pool_create_len == 0 &&
274 	    nvlist_exists(nvl, ZPOOL_HIST_CMD)) {
275 		shpp->sh_pool_create_len = shpp->sh_bof = shpp->sh_eof;
276 	}
277 
278 	mutex_exit(&spa->spa_history_lock);
279 	fnvlist_pack_free(record_packed, reclen);
280 	dmu_buf_rele(dbp, FTAG);
281 	fnvlist_free(nvl);
282 }
283 
284 /*
285  * Write out a history event.
286  */
287 int
spa_history_log(spa_t * spa,const char * msg)288 spa_history_log(spa_t *spa, const char *msg)
289 {
290 	int err;
291 	nvlist_t *nvl = fnvlist_alloc();
292 
293 	fnvlist_add_string(nvl, ZPOOL_HIST_CMD, msg);
294 	err = spa_history_log_nvl(spa, nvl);
295 	fnvlist_free(nvl);
296 	return (err);
297 }
298 
299 int
spa_history_log_nvl(spa_t * spa,nvlist_t * nvl)300 spa_history_log_nvl(spa_t *spa, nvlist_t *nvl)
301 {
302 	int err = 0;
303 	dmu_tx_t *tx;
304 	nvlist_t *nvarg;
305 
306 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY)
307 		return (EINVAL);
308 
309 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY || !spa_writeable(spa))
310 		return (SET_ERROR(EINVAL));
311 
312 	tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
313 	err = dmu_tx_assign(tx, TXG_WAIT);
314 	if (err) {
315 		dmu_tx_abort(tx);
316 		return (err);
317 	}
318 
319 	nvarg = fnvlist_dup(nvl);
320 	if (spa_history_zone() != NULL) {
321 		fnvlist_add_string(nvarg, ZPOOL_HIST_ZONE,
322 		    spa_history_zone());
323 	}
324 	fnvlist_add_uint64(nvarg, ZPOOL_HIST_WHO, crgetruid(CRED()));
325 
326 	/* Kick this off asynchronously; errors are ignored. */
327 	dsl_sync_task_nowait(spa_get_dsl(spa), spa_history_log_sync,
328 	    nvarg, 0, ZFS_SPACE_CHECK_NONE, tx);
329 	dmu_tx_commit(tx);
330 
331 	/* spa_history_log_sync will free nvl */
332 	return (err);
333 
334 }
335 
336 /*
337  * Read out the command history.
338  */
339 int
spa_history_get(spa_t * spa,uint64_t * offp,uint64_t * len,char * buf)340 spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
341 {
342 	objset_t *mos = spa->spa_meta_objset;
343 	dmu_buf_t *dbp;
344 	uint64_t read_len, phys_read_off, phys_eof;
345 	uint64_t leftover = 0;
346 	spa_history_phys_t *shpp;
347 	int err;
348 
349 	/*
350 	 * If the command history doesn't exist (older pool),
351 	 * that's ok, just return ENOENT.
352 	 */
353 	if (!spa->spa_history)
354 		return (SET_ERROR(ENOENT));
355 
356 	/*
357 	 * The history is logged asynchronously, so when they request
358 	 * the first chunk of history, make sure everything has been
359 	 * synced to disk so that we get it.
360 	 */
361 	if (*offp == 0 && spa_writeable(spa))
362 		txg_wait_synced(spa_get_dsl(spa), 0);
363 
364 	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
365 		return (err);
366 	shpp = dbp->db_data;
367 
368 #ifdef ZFS_DEBUG
369 	{
370 		dmu_object_info_t doi;
371 		dmu_object_info_from_db(dbp, &doi);
372 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
373 	}
374 #endif
375 
376 	mutex_enter(&spa->spa_history_lock);
377 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
378 
379 	if (*offp < shpp->sh_pool_create_len) {
380 		/* read in just the zpool create history */
381 		phys_read_off = *offp;
382 		read_len = MIN(*len, shpp->sh_pool_create_len -
383 		    phys_read_off);
384 	} else {
385 		/*
386 		 * Need to reset passed in offset to BOF if the passed in
387 		 * offset has since been overwritten.
388 		 */
389 		*offp = MAX(*offp, shpp->sh_bof);
390 		phys_read_off = spa_history_log_to_phys(*offp, shpp);
391 
392 		/*
393 		 * Read up to the minimum of what the user passed down or
394 		 * the EOF (physical or logical).  If we hit physical EOF,
395 		 * use 'leftover' to read from the physical BOF.
396 		 */
397 		if (phys_read_off <= phys_eof) {
398 			read_len = MIN(*len, phys_eof - phys_read_off);
399 		} else {
400 			read_len = MIN(*len,
401 			    shpp->sh_phys_max_off - phys_read_off);
402 			if (phys_read_off + *len > shpp->sh_phys_max_off) {
403 				leftover = MIN(*len - read_len,
404 				    phys_eof - shpp->sh_pool_create_len);
405 			}
406 		}
407 	}
408 
409 	/* offset for consumer to use next */
410 	*offp += read_len + leftover;
411 
412 	/* tell the consumer how much you actually read */
413 	*len = read_len + leftover;
414 
415 	if (read_len == 0) {
416 		mutex_exit(&spa->spa_history_lock);
417 		dmu_buf_rele(dbp, FTAG);
418 		return (0);
419 	}
420 
421 	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
422 	    DMU_READ_PREFETCH);
423 	if (leftover && err == 0) {
424 		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
425 		    leftover, buf + read_len, DMU_READ_PREFETCH);
426 	}
427 	mutex_exit(&spa->spa_history_lock);
428 
429 	dmu_buf_rele(dbp, FTAG);
430 	return (err);
431 }
432 
433 /*
434  * The nvlist will be consumed by this call.
435  */
436 static void
log_internal(nvlist_t * nvl,const char * operation,spa_t * spa,dmu_tx_t * tx,const char * fmt,va_list adx)437 log_internal(nvlist_t *nvl, const char *operation, spa_t *spa,
438     dmu_tx_t *tx, const char *fmt, va_list adx)
439 {
440 	char *msg;
441 	va_list adx2;
442 
443 	/*
444 	 * If this is part of creating a pool, not everything is
445 	 * initialized yet, so don't bother logging the internal events.
446 	 * Likewise if the pool is not writeable.
447 	 */
448 	if (tx->tx_txg == TXG_INITIAL || !spa_writeable(spa)) {
449 		fnvlist_free(nvl);
450 		return;
451 	}
452 
453 	va_copy(adx2, adx);
454 
455 	msg = kmem_alloc(vsnprintf(NULL, 0, fmt, adx) + 1, KM_SLEEP);
456 	(void) vsprintf(msg, fmt, adx2);
457 	fnvlist_add_string(nvl, ZPOOL_HIST_INT_STR, msg);
458 	strfree(msg);
459 
460 	va_end(adx2);
461 
462 	fnvlist_add_string(nvl, ZPOOL_HIST_INT_NAME, operation);
463 	fnvlist_add_uint64(nvl, ZPOOL_HIST_TXG, tx->tx_txg);
464 
465 	if (dmu_tx_is_syncing(tx)) {
466 		spa_history_log_sync(nvl, tx);
467 	} else {
468 		dsl_sync_task_nowait(spa_get_dsl(spa),
469 		    spa_history_log_sync, nvl, 0, ZFS_SPACE_CHECK_NONE, tx);
470 	}
471 	/* spa_history_log_sync() will free nvl */
472 }
473 
474 void
spa_history_log_internal(spa_t * spa,const char * operation,dmu_tx_t * tx,const char * fmt,...)475 spa_history_log_internal(spa_t *spa, const char *operation,
476     dmu_tx_t *tx, const char *fmt, ...)
477 {
478 	dmu_tx_t *htx = tx;
479 	va_list adx;
480 
481 	/* create a tx if we didn't get one */
482 	if (tx == NULL) {
483 		htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
484 		if (dmu_tx_assign(htx, TXG_WAIT) != 0) {
485 			dmu_tx_abort(htx);
486 			return;
487 		}
488 	}
489 
490 	va_start(adx, fmt);
491 	log_internal(fnvlist_alloc(), operation, spa, htx, fmt, adx);
492 	va_end(adx);
493 
494 	/* if we didn't get a tx from the caller, commit the one we made */
495 	if (tx == NULL)
496 		dmu_tx_commit(htx);
497 }
498 
499 void
spa_history_log_internal_ds(dsl_dataset_t * ds,const char * operation,dmu_tx_t * tx,const char * fmt,...)500 spa_history_log_internal_ds(dsl_dataset_t *ds, const char *operation,
501     dmu_tx_t *tx, const char *fmt, ...)
502 {
503 	va_list adx;
504 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
505 	nvlist_t *nvl = fnvlist_alloc();
506 
507 	ASSERT(tx != NULL);
508 
509 	dsl_dataset_name(ds, namebuf);
510 	fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
511 	fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID, ds->ds_object);
512 
513 	va_start(adx, fmt);
514 	log_internal(nvl, operation, dsl_dataset_get_spa(ds), tx, fmt, adx);
515 	va_end(adx);
516 }
517 
518 void
spa_history_log_internal_dd(dsl_dir_t * dd,const char * operation,dmu_tx_t * tx,const char * fmt,...)519 spa_history_log_internal_dd(dsl_dir_t *dd, const char *operation,
520     dmu_tx_t *tx, const char *fmt, ...)
521 {
522 	va_list adx;
523 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
524 	nvlist_t *nvl = fnvlist_alloc();
525 
526 	ASSERT(tx != NULL);
527 
528 	dsl_dir_name(dd, namebuf);
529 	fnvlist_add_string(nvl, ZPOOL_HIST_DSNAME, namebuf);
530 	fnvlist_add_uint64(nvl, ZPOOL_HIST_DSID,
531 	    dsl_dir_phys(dd)->dd_head_dataset_obj);
532 
533 	va_start(adx, fmt);
534 	log_internal(nvl, operation, dd->dd_pool->dp_spa, tx, fmt, adx);
535 	va_end(adx);
536 }
537 
538 void
spa_history_log_version(spa_t * spa,const char * operation)539 spa_history_log_version(spa_t *spa, const char *operation)
540 {
541 	spa_history_log_internal(spa, operation, NULL,
542 	    "pool version %llu; software version %llu/%d; uts %s %s %s %s",
543 	    (u_longlong_t)spa_version(spa), SPA_VERSION, ZPL_VERSION,
544 	    utsname.nodename, utsname.release, utsname.version,
545 	    utsname.machine);
546 }
547