xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_history.c (revision 55434c770c89aa1b84474f2559a106803511aba0)
106eeb2adSek /*
206eeb2adSek  * CDDL HEADER START
306eeb2adSek  *
406eeb2adSek  * The contents of this file are subject to the terms of the
506eeb2adSek  * Common Development and Distribution License (the "License").
606eeb2adSek  * You may not use this file except in compliance with the License.
706eeb2adSek  *
806eeb2adSek  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
906eeb2adSek  * or http://www.opensolaris.org/os/licensing.
1006eeb2adSek  * See the License for the specific language governing permissions
1106eeb2adSek  * and limitations under the License.
1206eeb2adSek  *
1306eeb2adSek  * When distributing Covered Code, include this CDDL HEADER in each
1406eeb2adSek  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1506eeb2adSek  * If applicable, add the following below this CDDL HEADER, with the
1606eeb2adSek  * fields enclosed by brackets "[]" replaced with your own identifying
1706eeb2adSek  * information: Portions Copyright [yyyy] [name of copyright owner]
1806eeb2adSek  *
1906eeb2adSek  * CDDL HEADER END
2006eeb2adSek  */
2106eeb2adSek 
2206eeb2adSek /*
23*55434c77Sek  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2406eeb2adSek  * Use is subject to license terms.
2506eeb2adSek  */
2606eeb2adSek 
2706eeb2adSek #pragma ident	"%Z%%M%	%I%	%E% SMI"
2806eeb2adSek 
2906eeb2adSek #include <sys/spa_impl.h>
3006eeb2adSek #include <sys/zap.h>
3106eeb2adSek #include <sys/dsl_synctask.h>
3206eeb2adSek 
3306eeb2adSek /*
3406eeb2adSek  * Routines to manage the on-disk history log.
3506eeb2adSek  *
3606eeb2adSek  * The history log is stored as a dmu object containing
3706eeb2adSek  * <packed record length, record nvlist> tuples.
3806eeb2adSek  *
3906eeb2adSek  * Where "record nvlist" is a nvlist containing uint64_ts and strings, and
4006eeb2adSek  * "packed record length" is the packed length of the "record nvlist" stored
4106eeb2adSek  * as a little endian uint64_t.
4206eeb2adSek  *
4306eeb2adSek  * The log is implemented as a ring buffer, though the original creation
4406eeb2adSek  * of the pool ('zpool create') is never overwritten.
4506eeb2adSek  *
4606eeb2adSek  * The history log is tracked as object 'spa_t::spa_history'.  The bonus buffer
4706eeb2adSek  * of 'spa_history' stores the offsets for logging/retrieving history as
4806eeb2adSek  * 'spa_history_phys_t'.  'sh_pool_create_len' is the ending offset in bytes of
4906eeb2adSek  * where the 'zpool create' record is stored.  This allows us to never
5006eeb2adSek  * overwrite the original creation of the pool.  'sh_phys_max_off' is the
5106eeb2adSek  * physical ending offset in bytes of the log.  This tells you the length of
5206eeb2adSek  * the buffer. 'sh_eof' is the logical EOF (in bytes).  Whenever a record
5306eeb2adSek  * is added, 'sh_eof' is incremented by the the size of the record.
5406eeb2adSek  * 'sh_eof' is never decremented.  'sh_bof' is the logical BOF (in bytes).
5506eeb2adSek  * This is where the consumer should start reading from after reading in
5606eeb2adSek  * the 'zpool create' portion of the log.
5706eeb2adSek  *
5806eeb2adSek  * 'sh_records_lost' keeps track of how many records have been overwritten
5906eeb2adSek  * and permanently lost.
6006eeb2adSek  */
6106eeb2adSek 
6206eeb2adSek typedef enum history_log_type {
6306eeb2adSek 	LOG_CMD_CREATE,
6406eeb2adSek 	LOG_CMD_NO_CREATE
6506eeb2adSek } history_log_type_t;
6606eeb2adSek 
6706eeb2adSek typedef struct history_arg {
6806eeb2adSek 	const char *ha_history_str;
6906eeb2adSek 	history_log_type_t ha_log_type;
7006eeb2adSek } history_arg_t;
7106eeb2adSek 
7206eeb2adSek /* convert a logical offset to physical */
7306eeb2adSek static uint64_t
7406eeb2adSek spa_history_log_to_phys(uint64_t log_off, spa_history_phys_t *shpp)
7506eeb2adSek {
7606eeb2adSek 	uint64_t phys_len;
7706eeb2adSek 
7806eeb2adSek 	phys_len = shpp->sh_phys_max_off - shpp->sh_pool_create_len;
7906eeb2adSek 	return ((log_off - shpp->sh_pool_create_len) % phys_len
8006eeb2adSek 	    + shpp->sh_pool_create_len);
8106eeb2adSek }
8206eeb2adSek 
8306eeb2adSek void
8406eeb2adSek spa_history_create_obj(spa_t *spa, dmu_tx_t *tx)
8506eeb2adSek {
8606eeb2adSek 	dmu_buf_t *dbp;
8706eeb2adSek 	spa_history_phys_t *shpp;
8806eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
8906eeb2adSek 
9006eeb2adSek 	ASSERT(spa->spa_history == 0);
9106eeb2adSek 	spa->spa_history = dmu_object_alloc(mos, DMU_OT_SPA_HISTORY,
9206eeb2adSek 	    SPA_MAXBLOCKSIZE, DMU_OT_SPA_HISTORY_OFFSETS,
9306eeb2adSek 	    sizeof (spa_history_phys_t), tx);
9406eeb2adSek 
9506eeb2adSek 	VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
9606eeb2adSek 	    DMU_POOL_HISTORY, sizeof (uint64_t), 1,
9706eeb2adSek 	    &spa->spa_history, tx) == 0);
9806eeb2adSek 
9906eeb2adSek 	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
10006eeb2adSek 	ASSERT(dbp->db_size >= sizeof (spa_history_phys_t));
10106eeb2adSek 
10206eeb2adSek 	shpp = dbp->db_data;
10306eeb2adSek 	dmu_buf_will_dirty(dbp, tx);
10406eeb2adSek 
10506eeb2adSek 	/*
10606eeb2adSek 	 * Figure out maximum size of history log.  We set it at
10706eeb2adSek 	 * 1% of pool size, with a max of 32MB and min of 128KB.
10806eeb2adSek 	 */
10906eeb2adSek 	shpp->sh_phys_max_off = spa_get_dspace(spa) / 100;
11006eeb2adSek 	shpp->sh_phys_max_off = MIN(shpp->sh_phys_max_off, 32<<20);
11106eeb2adSek 	shpp->sh_phys_max_off = MAX(shpp->sh_phys_max_off, 128<<10);
11206eeb2adSek 
11306eeb2adSek 	dmu_buf_rele(dbp, FTAG);
11406eeb2adSek }
11506eeb2adSek 
11606eeb2adSek /*
11706eeb2adSek  * Change 'sh_bof' to the beginning of the next record.
11806eeb2adSek  */
11906eeb2adSek static int
12006eeb2adSek spa_history_advance_bof(spa_t *spa, spa_history_phys_t *shpp)
12106eeb2adSek {
12206eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
12306eeb2adSek 	uint64_t firstread, reclen, phys_bof;
12406eeb2adSek 	char buf[sizeof (reclen)];
12506eeb2adSek 	int err;
12606eeb2adSek 
12706eeb2adSek 	phys_bof = spa_history_log_to_phys(shpp->sh_bof, shpp);
12806eeb2adSek 	firstread = MIN(sizeof (reclen), shpp->sh_phys_max_off - phys_bof);
12906eeb2adSek 
13006eeb2adSek 	if ((err = dmu_read(mos, spa->spa_history, phys_bof, firstread,
13106eeb2adSek 	    buf)) != 0)
13206eeb2adSek 		return (err);
13306eeb2adSek 	if (firstread != sizeof (reclen)) {
13406eeb2adSek 		if ((err = dmu_read(mos, spa->spa_history,
13506eeb2adSek 		    shpp->sh_pool_create_len, sizeof (reclen) - firstread,
13606eeb2adSek 		    buf + firstread)) != 0)
13706eeb2adSek 			return (err);
13806eeb2adSek 	}
13906eeb2adSek 
14006eeb2adSek 	reclen = LE_64(*((uint64_t *)buf));
14106eeb2adSek 	shpp->sh_bof += reclen + sizeof (reclen);
14206eeb2adSek 	shpp->sh_records_lost++;
14306eeb2adSek 	return (0);
14406eeb2adSek }
14506eeb2adSek 
14606eeb2adSek static int
14706eeb2adSek spa_history_write(spa_t *spa, void *buf, uint64_t len, spa_history_phys_t *shpp,
14806eeb2adSek     dmu_tx_t *tx)
14906eeb2adSek {
15006eeb2adSek 	uint64_t firstwrite, phys_eof;
15106eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
15206eeb2adSek 	int err;
15306eeb2adSek 
15406eeb2adSek 	ASSERT(MUTEX_HELD(&spa->spa_history_lock));
15506eeb2adSek 
15606eeb2adSek 	/* see if we need to reset logical BOF */
15706eeb2adSek 	while (shpp->sh_phys_max_off - shpp->sh_pool_create_len -
15806eeb2adSek 	    (shpp->sh_eof - shpp->sh_bof) <= len) {
15906eeb2adSek 		if ((err = spa_history_advance_bof(spa, shpp)) != 0)
16006eeb2adSek 			return (err);
16106eeb2adSek 	}
16206eeb2adSek 
16306eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
16406eeb2adSek 	firstwrite = MIN(len, shpp->sh_phys_max_off - phys_eof);
16506eeb2adSek 	shpp->sh_eof += len;
16606eeb2adSek 	dmu_write(mos, spa->spa_history, phys_eof, firstwrite, buf, tx);
16706eeb2adSek 
16806eeb2adSek 	len -= firstwrite;
16906eeb2adSek 	if (len > 0) {
17006eeb2adSek 		/* write out the rest at the beginning of physical file */
17106eeb2adSek 		dmu_write(mos, spa->spa_history, shpp->sh_pool_create_len,
17206eeb2adSek 		    len, (char *)buf + firstwrite, tx);
17306eeb2adSek 	}
17406eeb2adSek 
17506eeb2adSek 	return (0);
17606eeb2adSek }
17706eeb2adSek 
17806eeb2adSek /*
17906eeb2adSek  * Write out a history event.
18006eeb2adSek  */
18106eeb2adSek void
18206eeb2adSek spa_history_log_sync(void *arg1, void *arg2, dmu_tx_t *tx)
18306eeb2adSek {
18406eeb2adSek 	spa_t		*spa = arg1;
18506eeb2adSek 	history_arg_t	*hap = arg2;
18606eeb2adSek 	const char	*history_str = hap->ha_history_str;
18706eeb2adSek 	objset_t	*mos = spa->spa_meta_objset;
18806eeb2adSek 	dmu_buf_t	*dbp;
18906eeb2adSek 	spa_history_phys_t *shpp;
19006eeb2adSek 	size_t		reclen;
19106eeb2adSek 	uint64_t	le_len;
19206eeb2adSek 	nvlist_t	*nvrecord;
19306eeb2adSek 	char		*record_packed = NULL;
19406eeb2adSek 	int		ret;
19506eeb2adSek 
19606eeb2adSek 	if (history_str == NULL)
19706eeb2adSek 		return;
19806eeb2adSek 
19906eeb2adSek 	/*
20006eeb2adSek 	 * If we have an older pool that doesn't have a command
20106eeb2adSek 	 * history object, create it now.
20206eeb2adSek 	 */
20306eeb2adSek 	mutex_enter(&spa->spa_history_lock);
20406eeb2adSek 	if (!spa->spa_history)
20506eeb2adSek 		spa_history_create_obj(spa, tx);
20606eeb2adSek 	mutex_exit(&spa->spa_history_lock);
20706eeb2adSek 
20806eeb2adSek 	/*
20906eeb2adSek 	 * Get the offset of where we need to write via the bonus buffer.
21006eeb2adSek 	 * Update the offset when the write completes.
21106eeb2adSek 	 */
21206eeb2adSek 	VERIFY(0 == dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp));
21306eeb2adSek 	shpp = dbp->db_data;
21406eeb2adSek 
21506eeb2adSek 	dmu_buf_will_dirty(dbp, tx);
21606eeb2adSek 
21706eeb2adSek #ifdef ZFS_DEBUG
21806eeb2adSek 	{
21906eeb2adSek 		dmu_object_info_t doi;
22006eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
22106eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
22206eeb2adSek 	}
22306eeb2adSek #endif
22406eeb2adSek 
22506eeb2adSek 	/* construct a nvlist of the current time and cmd string */
22606eeb2adSek 	VERIFY(nvlist_alloc(&nvrecord, NV_UNIQUE_NAME, KM_SLEEP) == 0);
22706eeb2adSek 	VERIFY(nvlist_add_uint64(nvrecord, ZPOOL_HIST_TIME,
22806eeb2adSek 	    gethrestime_sec()) == 0);
22906eeb2adSek 	VERIFY(nvlist_add_string(nvrecord, ZPOOL_HIST_CMD, history_str) == 0);
23006eeb2adSek 	VERIFY(nvlist_pack(nvrecord, &record_packed, &reclen,
23106eeb2adSek 	    NV_ENCODE_XDR, KM_SLEEP) == 0);
23206eeb2adSek 
23306eeb2adSek 	mutex_enter(&spa->spa_history_lock);
23406eeb2adSek 	if (hap->ha_log_type == LOG_CMD_CREATE)
23506eeb2adSek 		VERIFY(shpp->sh_eof == shpp->sh_pool_create_len);
23606eeb2adSek 
23706eeb2adSek 	/* write out the packed length as little endian */
238*55434c77Sek 	le_len = LE_64((uint64_t)reclen);
23906eeb2adSek 	ret = spa_history_write(spa, &le_len, sizeof (le_len), shpp, tx);
24006eeb2adSek 	if (!ret)
24106eeb2adSek 		ret = spa_history_write(spa, record_packed, reclen, shpp, tx);
24206eeb2adSek 
24306eeb2adSek 	if (!ret && hap->ha_log_type == LOG_CMD_CREATE) {
24406eeb2adSek 		shpp->sh_pool_create_len += sizeof (le_len) + reclen;
24506eeb2adSek 		shpp->sh_bof = shpp->sh_pool_create_len;
24606eeb2adSek 	}
24706eeb2adSek 
24806eeb2adSek 	mutex_exit(&spa->spa_history_lock);
24906eeb2adSek 	nvlist_free(nvrecord);
25006eeb2adSek 	kmem_free(record_packed, reclen);
25106eeb2adSek 	dmu_buf_rele(dbp, FTAG);
25206eeb2adSek }
25306eeb2adSek 
25406eeb2adSek /*
25506eeb2adSek  * Write out a history event.
25606eeb2adSek  */
25706eeb2adSek int
25806eeb2adSek spa_history_log(spa_t *spa, const char *history_str, uint64_t pool_create)
25906eeb2adSek {
26006eeb2adSek 	history_arg_t ha;
26106eeb2adSek 
26206eeb2adSek 	ha.ha_history_str = history_str;
26306eeb2adSek 	ha.ha_log_type = pool_create ? LOG_CMD_CREATE : LOG_CMD_NO_CREATE;
26406eeb2adSek 	return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_history_log_sync,
26506eeb2adSek 	    spa, &ha, 0));
26606eeb2adSek }
26706eeb2adSek 
26806eeb2adSek /*
26906eeb2adSek  * Read out the command history.
27006eeb2adSek  */
27106eeb2adSek int
27206eeb2adSek spa_history_get(spa_t *spa, uint64_t *offp, uint64_t *len, char *buf)
27306eeb2adSek {
27406eeb2adSek 	objset_t *mos = spa->spa_meta_objset;
27506eeb2adSek 	dmu_buf_t *dbp;
27606eeb2adSek 	uint64_t read_len, phys_read_off, phys_eof;
27706eeb2adSek 	uint64_t leftover = 0;
27806eeb2adSek 	spa_history_phys_t *shpp;
27906eeb2adSek 	int err;
28006eeb2adSek 
28106eeb2adSek 	/*
28206eeb2adSek 	 * If the command history  doesn't exist (older pool),
28306eeb2adSek 	 * that's ok, just return ENOENT.
28406eeb2adSek 	 */
28506eeb2adSek 	if (!spa->spa_history)
28606eeb2adSek 		return (ENOENT);
28706eeb2adSek 
28806eeb2adSek 	if ((err = dmu_bonus_hold(mos, spa->spa_history, FTAG, &dbp)) != 0)
28906eeb2adSek 		return (err);
29006eeb2adSek 	shpp = dbp->db_data;
29106eeb2adSek 
29206eeb2adSek #ifdef ZFS_DEBUG
29306eeb2adSek 	{
29406eeb2adSek 		dmu_object_info_t doi;
29506eeb2adSek 		dmu_object_info_from_db(dbp, &doi);
29606eeb2adSek 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_SPA_HISTORY_OFFSETS);
29706eeb2adSek 	}
29806eeb2adSek #endif
29906eeb2adSek 
30006eeb2adSek 	mutex_enter(&spa->spa_history_lock);
30106eeb2adSek 	phys_eof = spa_history_log_to_phys(shpp->sh_eof, shpp);
30206eeb2adSek 
30306eeb2adSek 	if (*offp < shpp->sh_pool_create_len) {
30406eeb2adSek 		/* read in just the zpool create history */
30506eeb2adSek 		phys_read_off = *offp;
30606eeb2adSek 		read_len = MIN(*len, shpp->sh_pool_create_len -
30706eeb2adSek 		    phys_read_off);
30806eeb2adSek 	} else {
30906eeb2adSek 		/*
31006eeb2adSek 		 * Need to reset passed in offset to BOF if the passed in
31106eeb2adSek 		 * offset has since been overwritten.
31206eeb2adSek 		 */
31306eeb2adSek 		*offp = MAX(*offp, shpp->sh_bof);
31406eeb2adSek 		phys_read_off = spa_history_log_to_phys(*offp, shpp);
31506eeb2adSek 
31606eeb2adSek 		/*
31706eeb2adSek 		 * Read up to the minimum of what the user passed down or
31806eeb2adSek 		 * the EOF (physical or logical).  If we hit physical EOF,
31906eeb2adSek 		 * use 'leftover' to read from the physical BOF.
32006eeb2adSek 		 */
32106eeb2adSek 		if (phys_read_off <= phys_eof) {
32206eeb2adSek 			read_len = MIN(*len, phys_eof - phys_read_off);
32306eeb2adSek 		} else {
32406eeb2adSek 			read_len = MIN(*len,
32506eeb2adSek 			    shpp->sh_phys_max_off - phys_read_off);
32606eeb2adSek 			if (phys_read_off + *len > shpp->sh_phys_max_off) {
32706eeb2adSek 				leftover = MIN(*len - read_len,
32806eeb2adSek 				    phys_eof - shpp->sh_pool_create_len);
32906eeb2adSek 			}
33006eeb2adSek 		}
33106eeb2adSek 	}
33206eeb2adSek 
33306eeb2adSek 	/* offset for consumer to use next */
33406eeb2adSek 	*offp += read_len + leftover;
33506eeb2adSek 
33606eeb2adSek 	/* tell the consumer how much you actually read */
33706eeb2adSek 	*len = read_len + leftover;
33806eeb2adSek 
33906eeb2adSek 	if (read_len == 0) {
34006eeb2adSek 		mutex_exit(&spa->spa_history_lock);
34106eeb2adSek 		dmu_buf_rele(dbp, FTAG);
34206eeb2adSek 		return (0);
34306eeb2adSek 	}
34406eeb2adSek 
34506eeb2adSek 	err = dmu_read(mos, spa->spa_history, phys_read_off, read_len, buf);
34606eeb2adSek 	if (leftover && err == 0) {
34706eeb2adSek 		err = dmu_read(mos, spa->spa_history, shpp->sh_pool_create_len,
34806eeb2adSek 		    leftover, buf + read_len);
34906eeb2adSek 	}
35006eeb2adSek 	mutex_exit(&spa->spa_history_lock);
35106eeb2adSek 
35206eeb2adSek 	dmu_buf_rele(dbp, FTAG);
35306eeb2adSek 	return (err);
35406eeb2adSek }
355