xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_history.c (revision 842727c2f41f01b380de4f5e787d905702870f23)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
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>
33 #include <sys/utsname.h>
34 #include <sys/cmn_err.h>
35 #include <sys/sunddi.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 = spa_get_dspace(spa) / 100;
107 	shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20);
108 	shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
109 
110 	dmu_buf_rele(dbp, FTAG);
111 }
112 
113 /*
114  * Change 'sh_bof' to the beginning of the next record.
115  */
116 static int
117 spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
118 {
119 	objset_t *mos = spa->spa_meta_objset;
120 	uint64_t firstread, reclen, phys_bof;
121 	char buf[sizeof (reclen)];
122 	int err;
123 
124 	phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
125 	firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
126 
127 	if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
128 	    buf, DMU_READ_PREFETCH)) != 0)
129 		return (err);
130 	if (firstread != sizeof (reclen)) {
131 		if ((err = dmu_read(mos, spa->spa_history,
132 		    shpp->sh_pool_create_len, sizeof (reclen) - firstread,
133 		    buf + firstread, DMU_READ_PREFETCH)) != 0)
134 			return (err);
135 	}
136 
137 	reclen = LE_64(*((uint64_t *)buf));
138 	shpp->sh_bof += reclen + sizeof (reclen);
139 	shpp->sh_records_lost++;
140 	return (0);
141 }
142 
143 static int
144 spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
145     dmu_tx_t *tx)
146 {
147 	uint64_t firstwrite, phys_eof;
148 	objset_t *mos = spa->spa_meta_objset;
149 	int err;
150 
151 	ASSERT(MUTEX_HELD(&spa->spa_history_lock));
152 
153 	/* see if we need to reset logical BOF */
154 	while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
155 	    (shpp->sh_eof - shpp->sh_bof) <= len) {
156 		if ((err = spa_history_advance_bof(spa, shpp)) != 0) {
157 			return (err);
158 		}
159 	}
160 
161 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
162 	firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
163 	shpp->sh_eof += len;
164 	dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
165 
166 	len -= firstwrite;
167 	if (len > 0) {
168 		/* write out the rest at the beginning of physical file */
169 		dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
170 		    len, (char *)buf + firstwrite, tx);
171 	}
172 
173 	return (0);
174 }
175 
176 static char *
177 spa_history_zone()
178 {
179 #ifdef _KERNEL
180 	return (curproc->p_zone->zone_name);
181 #else
182 	return ("global");
183 #endif
184 }
185 
186 /*
187  * Write out a history event.
188  */
189 static void
190 spa_history_log_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
191 {
192 	spa_t		*spa = arg1;
193 	history_arg_t	*hap = arg2;
194 	const char	*history_str = hap->ha_history_str;
195 	objset_t	*mos = spa->spa_meta_objset;
196 	dmu_buf_t	*dbp;
197 	spa_history_phys_t *shpp;
198 	size_t		reclen;
199 	uint64_t	le_len;
200 	nvlist_t	*nvrecord;
201 	char		*record_packed = NULL;
202 	int		ret;
203 
204 	/*
205 	 * If we have an older pool that doesn't have a command
206 	 * history object, create it now.
207 	 */
208 	mutex_enter(&spa->spa_history_lock);
209 	if (!spa->spa_history)
210 		spa_history_create_obj(spa, tx);
211 	mutex_exit(&spa->spa_history_lock);
212 
213 	/*
214 	 * Get the offset of where we need to write via the bonus buffer.
215 	 * Update the offset when the write completes.
216 	 */
217 	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
218 	shpp = dbp->db_data;
219 
220 	dmu_buf_will_dirty(dbp, tx);
221 
222 #ifdef ZFS_DEBUG
223 	{
224 		dmu_object_info_t doi;
225 		dmu_object_info_from_db(dbp, &doi);
226 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
227 	}
228 #endif
229 
230 	VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
231 	VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME,
232 	    gethrestime_sec()) == 0);
233 	VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_WHO,
234 	    (uint64_t)crgetuid(cr)) == 0);
235 	if (hap->ha_zone[0] != '\0')
236 		VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_ZONE,
237 		    hap->ha_zone) == 0);
238 #ifdef _KERNEL
239 	VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_HOST,
240 	    utsname.nodename) == 0);
241 #endif
242 	if (hap->ha_log_type == LOG_CMD_POOL_CREATE ||
243 	    hap->ha_log_type == LOG_CMD_NORMAL) {
244 		VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD,
245 		    history_str) == 0);
246 	} else {
247 		VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_INT_EVENT,
248 		    hap->ha_event) == 0);
249 		VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TXG,
250 		    tx->tx_txg) == 0);
251 		VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_INT_STR,
252 		    history_str) == 0);
253 	}
254 
255 	VERIFY(nvlist_size(nvrecord, &reclen, NV_ENCODE_XDR) == 0);
256 	record_packed = kmem_alloc(reclen, KM_SLEEP);
257 
258 	VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
259 	    NV_ENCODE_XDR, KM_SLEEP) == 0);
260 
261 	mutex_enter(&spa->spa_history_lock);
262 	if (hap->ha_log_type == LOG_CMD_POOL_CREATE)
263 		VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
264 
265 	/* write out the packed length as little endian */
266 	le_len = LE_64((uint64_t)reclen);
267 	ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
268 	if (!ret)
269 		ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
270 
271 	if (!ret && hap->ha_log_type == LOG_CMD_POOL_CREATE) {
272 		shpp->sh_pool_create_len += sizeof (le_len) + reclen;
273 		shpp->sh_bof = shpp->sh_pool_create_len;
274 	}
275 
276 	mutex_exit(&spa->spa_history_lock);
277 	nvlist_free(nvrecord);
278 	kmem_free(record_packed, reclen);
279 	dmu_buf_rele(dbp, FTAG);
280 
281 	if (hap->ha_log_type == LOG_INTERNAL) {
282 		kmem_free((void*)hap->ha_history_str, HIS_MAX_RECORD_LEN);
283 		kmem_free(hap, sizeof (history_arg_t));
284 	}
285 }
286 
287 /*
288  * Write out a history event.
289  */
290 int
291 spa_history_log(spa_t *spa, const char *history_str, history_log_type_t what)
292 {
293 	history_arg_t ha;
294 
295 	ASSERT(what != LOG_INTERNAL);
296 
297 	ha.ha_history_str = history_str;
298 	ha.ha_log_type = what;
299 	(void) strlcpy(ha.ha_zone, spa_history_zone(), sizeof (ha.ha_zone));
300 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync,
301 	    spa, &ha, 0));
302 }
303 
304 /*
305  * Read out the command history.
306  */
307 int
308 spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
309 {
310 	objset_t *mos = spa->spa_meta_objset;
311 	dmu_buf_t *dbp;
312 	uint64_t read_len, phys_read_off, phys_eof;
313 	uint64_t leftover = 0;
314 	spa_history_phys_t *shpp;
315 	int err;
316 
317 	/*
318 	 * If the command history  doesn't exist (older pool),
319 	 * that's ok, just return ENOENT.
320 	 */
321 	if (!spa->spa_history)
322 		return (ENOENT);
323 
324 	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
325 		return (err);
326 	shpp = dbp->db_data;
327 
328 #ifdef ZFS_DEBUG
329 	{
330 		dmu_object_info_t doi;
331 		dmu_object_info_from_db(dbp, &doi);
332 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
333 	}
334 #endif
335 
336 	mutex_enter(&spa->spa_history_lock);
337 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
338 
339 	if (*offp < shpp->sh_pool_create_len) {
340 		/* read in just the zpool create history */
341 		phys_read_off = *offp;
342 		read_len = MIN(*len, shpp->sh_pool_create_len -
343 		    phys_read_off);
344 	} else {
345 		/*
346 		 * Need to reset passed in offset to BOF if the passed in
347 		 * offset has since been overwritten.
348 		 */
349 		*offp = MAX(*offp, shpp->sh_bof);
350 		phys_read_off = spa_history_log_to_phys(*offp, shpp);
351 
352 		/*
353 		 * Read up to the minimum of what the user passed down or
354 		 * the EOF (physical or logical).  If we hit physical EOF,
355 		 * use 'leftover' to read from the physical BOF.
356 		 */
357 		if (phys_read_off <= phys_eof) {
358 			read_len = MIN(*len, phys_eof - phys_read_off);
359 		} else {
360 			read_len = MIN(*len,
361 			    shpp->sh_phys_max_off - phys_read_off);
362 			if (phys_read_off + *len > shpp->sh_phys_max_off) {
363 				leftover = MIN(*len - read_len,
364 				    phys_eof - shpp->sh_pool_create_len);
365 			}
366 		}
367 	}
368 
369 	/* offset for consumer to use next */
370 	*offp += read_len + leftover;
371 
372 	/* tell the consumer how much you actually read */
373 	*len = read_len + leftover;
374 
375 	if (read_len == 0) {
376 		mutex_exit(&spa->spa_history_lock);
377 		dmu_buf_rele(dbp, FTAG);
378 		return (0);
379 	}
380 
381 	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf,
382 	    DMU_READ_PREFETCH);
383 	if (leftover && err == 0) {
384 		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
385 		    leftover, buf + read_len, DMU_READ_PREFETCH);
386 	}
387 	mutex_exit(&spa->spa_history_lock);
388 
389 	dmu_buf_rele(dbp, FTAG);
390 	return (err);
391 }
392 
393 static void
394 log_internal(history_internal_events_t event, spa_t *spa,
395     dmu_tx_t *tx, cred_t *cr, const char *fmt, va_list adx)
396 {
397 	history_arg_t *hap;
398 	char *str;
399 
400 	/*
401 	 * If this is part of creating a pool, not everything is
402 	 * initialized yet, so don't bother logging the internal events.
403 	 */
404 	if (tx->tx_txg == TXG_INITIAL)
405 		return;
406 
407 	hap = kmem_alloc(sizeof (history_arg_t), KM_SLEEP);
408 	str = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
409 
410 	(void) vsnprintf(str, HIS_MAX_RECORD_LEN, fmt, adx);
411 
412 	hap->ha_log_type = LOG_INTERNAL;
413 	hap->ha_history_str = str;
414 	hap->ha_event = event;
415 	hap->ha_zone[0] = '\0';
416 
417 	if (dmu_tx_is_syncing(tx)) {
418 		spa_history_log_sync(spa, hap, cr, tx);
419 	} else {
420 		dsl_sync_task_do_nowait(spa_get_dsl(spa), NULL,
421 		    spa_history_log_sync, spa, hap, 0, tx);
422 	}
423 	/* spa_history_log_sync() will free hap and str */
424 }
425 
426 void
427 spa_history_internal_log(history_internal_events_t event, spa_t *spa,
428     dmu_tx_t *tx, cred_t *cr, const char *fmt, ...)
429 {
430 	dmu_tx_t *htx = tx;
431 	va_list adx;
432 
433 	/* create a tx if we didn't get one */
434 	if (tx == NULL) {
435 		htx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
436 		if (dmu_tx_assign(htx, TXG_WAIT) != 0) {
437 			dmu_tx_abort(htx);
438 			return;
439 		}
440 	}
441 
442 	va_start(adx, fmt);
443 	log_internal(event, spa, htx, cr, fmt, adx);
444 	va_end(adx);
445 
446 	/* if we didn't get a tx from the caller, commit the one we made */
447 	if (tx == NULL)
448 		dmu_tx_commit(htx);
449 }
450 
451 void
452 spa_history_log_version(spa_t *spa, history_internal_events_t event)
453 {
454 #ifdef _KERNEL
455 	uint64_t current_vers = spa_version(spa);
456 
457 	if (current_vers >= SPA_VERSION_ZPOOL_HISTORY) {
458 		spa_history_internal_log(event, spa, NULL, CRED(),
459 		    "pool spa %llu; zfs spa %llu; zpl %d; uts %s %s %s %s",
460 		    (u_longlong_t)current_vers, SPA_VERSION, ZPL_VERSION,
461 		    utsname.nodename, utsname.release, utsname.version,
462 		    utsname.machine);
463 	}
464 	cmn_err(CE_CONT, "!%s version %llu pool %s using %llu",
465 	    event == LOG_POOL_IMPORT ? "imported" :
466 	    event == LOG_POOL_CREATE ? "created" : "accessed",
467 	    (u_longlong_t)current_vers, spa_name(spa), SPA_VERSION);
468 #endif
469 }
470