spa_history.c revision 177698
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 2007 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29#include <sys/spa_impl.h>
30#include <sys/zap.h>
31#include <sys/dsl_synctask.h>
32
33/*
34 * Routines to manage the on-disk history log.
35 *
36 * The history log is stored as a dmu object containing
37 * <packed record length, record nvlist> tuples.
38 *
39 * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
40 * "packed record length" is the packed length of the "record nvlist" stored
41 * as a little endian uint64_t.
42 *
43 * The log is implemented as a ring buffer, though the original creation
44 * of the pool ('zpool create') is never overwritten.
45 *
46 * The history log is tracked as object 'spa_t::spa_history'.  The bonus buffer
47 * of 'spa_history' stores the offsets for logging/retrieving history as
48 * 'spa_history_phys_t'.  'sh_pool_create_len' is the ending offset in bytes of
49 * where the 'zpool create' record is stored.  This allows us to never
50 * overwrite the original creation of the pool.  'sh_phys_max_off' is the
51 * physical ending offset in bytes of the log.  This tells you the length of
52 * the buffer. 'sh_eof' is the logical EOF (in bytes).  Whenever a record
53 * is added, 'sh_eof' is incremented by the the size of the record.
54 * 'sh_eof' is never decremented.  'sh_bof' is the logical BOF (in bytes).
55 * This is where the consumer should start reading from after reading in
56 * the 'zpool create' portion of the log.
57 *
58 * 'sh_records_lost' keeps track of how many records have been overwritten
59 * and permanently lost.
60 */
61
62typedef enum history_log_type {
63	LOG_CMD_CREATE,
64	LOG_CMD_NO_CREATE
65} history_log_type_t;
66
67typedef struct history_arg {
68	const char *ha_history_str;
69	history_log_type_t ha_log_type;
70} history_arg_t;
71
72/* convert a logical offset to physical */
73static uint64_t
74spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
75{
76	uint64_t phys_len;
77
78	phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
79	return ((log_off - shpp->sh_pool_create_len) % phys_len
80	    + shpp->sh_pool_create_len);
81}
82
83void
84spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
85{
86	dmu_buf_t *dbp;
87	spa_history_phys_t *shpp;
88	objset_t *mos = spa->spa_meta_objset;
89
90	ASSERT(spa->spa_history == 0);
91	spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
92	    SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
93	    sizeof (spa_history_phys_t), tx);
94
95	VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
96	    DMU_POOL_HISTORY, sizeof (uint64_t), 1,
97	    &spa->spa_history, tx) == 0);
98
99	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
100	ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
101
102	shpp = dbp->db_data;
103	dmu_buf_will_dirty(dbp, tx);
104
105	/*
106	 * Figure out maximum size of history log.  We set it at
107	 * 1% of pool size, with a max of 32MB and min of 128KB.
108	 */
109	shpp->sh_phys_max_off = spa_get_dspace(spa) / 100;
110	shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20);
111	shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
112
113	dmu_buf_rele(dbp, FTAG);
114}
115
116/*
117 * Change 'sh_bof' to the beginning of the next record.
118 */
119static int
120spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
121{
122	objset_t *mos = spa->spa_meta_objset;
123	uint64_t firstread, reclen, phys_bof;
124	char buf[sizeof (reclen)];
125	int err;
126
127	phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
128	firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
129
130	if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
131	    buf)) != 0)
132		return (err);
133	if (firstread != sizeof (reclen)) {
134		if ((err = dmu_read(mos, spa->spa_history,
135		    shpp->sh_pool_create_len, sizeof (reclen) - firstread,
136		    buf + firstread)) != 0)
137			return (err);
138	}
139
140	reclen = LE_64(*((uint64_t *)buf));
141	shpp->sh_bof += reclen + sizeof (reclen);
142	shpp->sh_records_lost++;
143	return (0);
144}
145
146static int
147spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
148    dmu_tx_t *tx)
149{
150	uint64_t firstwrite, phys_eof;
151	objset_t *mos = spa->spa_meta_objset;
152	int err;
153
154	ASSERT(MUTEX_HELD(&spa->spa_history_lock));
155
156	/* see if we need to reset logical BOF */
157	while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
158	    (shpp->sh_eof - shpp->sh_bof) <= len) {
159		if ((err = spa_history_advance_bof(spa, shpp)) != 0)
160			return (err);
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
178/*
179 * Write out a history event.
180 */
181void
182spa_history_log_sync(void *arg1, void *arg2, dmu_tx_t *tx)
183{
184	spa_t		*spa = arg1;
185	history_arg_t	*hap = arg2;
186	const char	*history_str = hap->ha_history_str;
187	objset_t	*mos = spa->spa_meta_objset;
188	dmu_buf_t	*dbp;
189	spa_history_phys_t *shpp;
190	size_t		reclen;
191	uint64_t	le_len;
192	nvlist_t	*nvrecord;
193	char		*record_packed = NULL;
194	int		ret;
195
196	if (history_str == NULL)
197		return;
198
199	/*
200	 * If we have an older pool that doesn't have a command
201	 * history object, create it now.
202	 */
203	mutex_enter(&spa->spa_history_lock);
204	if (!spa->spa_history)
205		spa_history_create_obj(spa, tx);
206	mutex_exit(&spa->spa_history_lock);
207
208	/*
209	 * Get the offset of where we need to write via the bonus buffer.
210	 * Update the offset when the write completes.
211	 */
212	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
213	shpp = dbp->db_data;
214
215	dmu_buf_will_dirty(dbp, tx);
216
217#ifdef ZFS_DEBUG
218	{
219		dmu_object_info_t doi;
220		dmu_object_info_from_db(dbp, &doi);
221		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
222	}
223#endif
224
225	/* construct a nvlist of the current time and cmd string */
226	VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
227	VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME,
228	    gethrestime_sec()) == 0);
229	VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD, history_str) == 0);
230	VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
231	    NV_ENCODE_XDR, KM_SLEEP) == 0);
232
233	mutex_enter(&spa->spa_history_lock);
234	if (hap->ha_log_type == LOG_CMD_CREATE)
235		VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
236
237	/* write out the packed length as little endian */
238	le_len = LE_64((uint64_t)reclen);
239	ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
240	if (!ret)
241		ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
242
243	if (!ret && hap->ha_log_type == LOG_CMD_CREATE) {
244		shpp->sh_pool_create_len += sizeof (le_len) + reclen;
245		shpp->sh_bof = shpp->sh_pool_create_len;
246	}
247
248	mutex_exit(&spa->spa_history_lock);
249	nvlist_free(nvrecord);
250	kmem_free(record_packed, reclen);
251	dmu_buf_rele(dbp, FTAG);
252}
253
254/*
255 * Write out a history event.
256 */
257int
258spa_history_log(spa_t *spa, const char *history_str, uint64_t pool_create)
259{
260	history_arg_t ha;
261
262	ha.ha_history_str = history_str;
263	ha.ha_log_type = pool_create ? LOG_CMD_CREATE : LOG_CMD_NO_CREATE;
264	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync,
265	    spa, &ha, 0));
266}
267
268/*
269 * Read out the command history.
270 */
271int
272spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
273{
274	objset_t *mos = spa->spa_meta_objset;
275	dmu_buf_t *dbp;
276	uint64_t read_len, phys_read_off, phys_eof;
277	uint64_t leftover = 0;
278	spa_history_phys_t *shpp;
279	int err;
280
281	/*
282	 * If the command history  doesn't exist (older pool),
283	 * that's ok, just return ENOENT.
284	 */
285	if (!spa->spa_history)
286		return (ENOENT);
287
288	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
289		return (err);
290	shpp = dbp->db_data;
291
292#ifdef ZFS_DEBUG
293	{
294		dmu_object_info_t doi;
295		dmu_object_info_from_db(dbp, &doi);
296		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
297	}
298#endif
299
300	mutex_enter(&spa->spa_history_lock);
301	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
302
303	if (*offp < shpp->sh_pool_create_len) {
304		/* read in just the zpool create history */
305		phys_read_off = *offp;
306		read_len = MIN(*len, shpp->sh_pool_create_len -
307		    phys_read_off);
308	} else {
309		/*
310		 * Need to reset passed in offset to BOF if the passed in
311		 * offset has since been overwritten.
312		 */
313		*offp = MAX(*offp, shpp->sh_bof);
314		phys_read_off = spa_history_log_to_phys(*offp, shpp);
315
316		/*
317		 * Read up to the minimum of what the user passed down or
318		 * the EOF (physical or logical).  If we hit physical EOF,
319		 * use 'leftover' to read from the physical BOF.
320		 */
321		if (phys_read_off <= phys_eof) {
322			read_len = MIN(*len, phys_eof - phys_read_off);
323		} else {
324			read_len = MIN(*len,
325			    shpp->sh_phys_max_off - phys_read_off);
326			if (phys_read_off + *len > shpp->sh_phys_max_off) {
327				leftover = MIN(*len - read_len,
328				    phys_eof - shpp->sh_pool_create_len);
329			}
330		}
331	}
332
333	/* offset for consumer to use next */
334	*offp += read_len + leftover;
335
336	/* tell the consumer how much you actually read */
337	*len = read_len + leftover;
338
339	if (read_len == 0) {
340		mutex_exit(&spa->spa_history_lock);
341		dmu_buf_rele(dbp, FTAG);
342		return (0);
343	}
344
345	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf);
346	if (leftover && err == 0) {
347		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
348		    leftover, buf + read_len);
349	}
350	mutex_exit(&spa->spa_history_lock);
351
352	dmu_buf_rele(dbp, FTAG);
353	return (err);
354}
355