1*d8ab6e12SDon Brady /*
2*d8ab6e12SDon Brady  * CDDL HEADER START
3*d8ab6e12SDon Brady  *
4*d8ab6e12SDon Brady  * The contents of this file are subject to the terms of the
5*d8ab6e12SDon Brady  * Common Development and Distribution License (the "License").
6*d8ab6e12SDon Brady  * You may not use this file except in compliance with the License.
7*d8ab6e12SDon Brady  *
8*d8ab6e12SDon Brady  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*d8ab6e12SDon Brady  * or http://www.opensolaris.org/os/licensing.
10*d8ab6e12SDon Brady  * See the License for the specific language governing permissions
11*d8ab6e12SDon Brady  * and limitations under the License.
12*d8ab6e12SDon Brady  *
13*d8ab6e12SDon Brady  * When distributing Covered Code, include this CDDL HEADER in each
14*d8ab6e12SDon Brady  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*d8ab6e12SDon Brady  * If applicable, add the following below this CDDL HEADER, with the
16*d8ab6e12SDon Brady  * fields enclosed by brackets "[]" replaced with your own identifying
17*d8ab6e12SDon Brady  * information: Portions Copyright [yyyy] [name of copyright owner]
18*d8ab6e12SDon Brady  *
19*d8ab6e12SDon Brady  * CDDL HEADER END
20*d8ab6e12SDon Brady  */
21*d8ab6e12SDon Brady 
22*d8ab6e12SDon Brady /*
23*d8ab6e12SDon Brady  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24*d8ab6e12SDon Brady  * Copyright 2020 Joyent, Inc.
25*d8ab6e12SDon Brady  */
26*d8ab6e12SDon Brady 
27*d8ab6e12SDon Brady #include <stdio.h>
28*d8ab6e12SDon Brady #include <stdlib.h>
29*d8ab6e12SDon Brady #include <string.h>
30*d8ab6e12SDon Brady #include <sys/nvpair.h>
31*d8ab6e12SDon Brady #include <sys/fs/zfs.h>
32*d8ab6e12SDon Brady #include <sys/sysmacros.h>
33*d8ab6e12SDon Brady #include <unistd.h>
34*d8ab6e12SDon Brady 
35*d8ab6e12SDon Brady #include <libzutil.h>
36*d8ab6e12SDon Brady 
37*d8ab6e12SDon Brady static void
dump_ddt_stat(const ddt_stat_t * dds,int h)38*d8ab6e12SDon Brady dump_ddt_stat(const ddt_stat_t *dds, int h)
39*d8ab6e12SDon Brady {
40*d8ab6e12SDon Brady 	char refcnt[6];
41*d8ab6e12SDon Brady 	char blocks[6], lsize[6], psize[6], dsize[6];
42*d8ab6e12SDon Brady 	char ref_blocks[6], ref_lsize[6], ref_psize[6], ref_dsize[6];
43*d8ab6e12SDon Brady 
44*d8ab6e12SDon Brady 	if (dds == NULL || dds->dds_blocks == 0)
45*d8ab6e12SDon Brady 		return;
46*d8ab6e12SDon Brady 
47*d8ab6e12SDon Brady 	if (h == -1)
48*d8ab6e12SDon Brady 		(void) strcpy(refcnt, "Total");
49*d8ab6e12SDon Brady 	else
50*d8ab6e12SDon Brady 		zfs_nicenum(1ULL << h, refcnt, sizeof (refcnt));
51*d8ab6e12SDon Brady 
52*d8ab6e12SDon Brady 	zfs_nicenum(dds->dds_blocks, blocks, sizeof (blocks));
53*d8ab6e12SDon Brady 	zfs_nicebytes(dds->dds_lsize, lsize, sizeof (lsize));
54*d8ab6e12SDon Brady 	zfs_nicebytes(dds->dds_psize, psize, sizeof (psize));
55*d8ab6e12SDon Brady 	zfs_nicebytes(dds->dds_dsize, dsize, sizeof (dsize));
56*d8ab6e12SDon Brady 	zfs_nicenum(dds->dds_ref_blocks, ref_blocks, sizeof (ref_blocks));
57*d8ab6e12SDon Brady 	zfs_nicebytes(dds->dds_ref_lsize, ref_lsize, sizeof (ref_lsize));
58*d8ab6e12SDon Brady 	zfs_nicebytes(dds->dds_ref_psize, ref_psize, sizeof (ref_psize));
59*d8ab6e12SDon Brady 	zfs_nicebytes(dds->dds_ref_dsize, ref_dsize, sizeof (ref_dsize));
60*d8ab6e12SDon Brady 
61*d8ab6e12SDon Brady 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
62*d8ab6e12SDon Brady 	    refcnt,
63*d8ab6e12SDon Brady 	    blocks, lsize, psize, dsize,
64*d8ab6e12SDon Brady 	    ref_blocks, ref_lsize, ref_psize, ref_dsize);
65*d8ab6e12SDon Brady }
66*d8ab6e12SDon Brady 
67*d8ab6e12SDon Brady /*
68*d8ab6e12SDon Brady  * Print the DDT histogram and the column totals.
69*d8ab6e12SDon Brady  */
70*d8ab6e12SDon Brady void
zpool_dump_ddt(const ddt_stat_t * dds_total,const ddt_histogram_t * ddh)71*d8ab6e12SDon Brady zpool_dump_ddt(const ddt_stat_t *dds_total, const ddt_histogram_t *ddh)
72*d8ab6e12SDon Brady {
73*d8ab6e12SDon Brady 	int h;
74*d8ab6e12SDon Brady 
75*d8ab6e12SDon Brady 	(void) printf("\n");
76*d8ab6e12SDon Brady 
77*d8ab6e12SDon Brady 	(void) printf("bucket   "
78*d8ab6e12SDon Brady 	    "           allocated             "
79*d8ab6e12SDon Brady 	    "          referenced          \n");
80*d8ab6e12SDon Brady 	(void) printf("______   "
81*d8ab6e12SDon Brady 	    "______________________________   "
82*d8ab6e12SDon Brady 	    "______________________________\n");
83*d8ab6e12SDon Brady 
84*d8ab6e12SDon Brady 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
85*d8ab6e12SDon Brady 	    "refcnt",
86*d8ab6e12SDon Brady 	    "blocks", "LSIZE", "PSIZE", "DSIZE",
87*d8ab6e12SDon Brady 	    "blocks", "LSIZE", "PSIZE", "DSIZE");
88*d8ab6e12SDon Brady 
89*d8ab6e12SDon Brady 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
90*d8ab6e12SDon Brady 	    "------",
91*d8ab6e12SDon Brady 	    "------", "-----", "-----", "-----",
92*d8ab6e12SDon Brady 	    "------", "-----", "-----", "-----");
93*d8ab6e12SDon Brady 
94*d8ab6e12SDon Brady 	for (h = 0; h < 64; h++)
95*d8ab6e12SDon Brady 		dump_ddt_stat(&ddh->ddh_stat[h], h);
96*d8ab6e12SDon Brady 
97*d8ab6e12SDon Brady 	dump_ddt_stat(dds_total, -1);
98*d8ab6e12SDon Brady 
99*d8ab6e12SDon Brady 	(void) printf("\n");
100*d8ab6e12SDon Brady }
101*d8ab6e12SDon Brady 
102*d8ab6e12SDon Brady /*
103*d8ab6e12SDon Brady  * Process the buffer of nvlists, unpacking and storing each nvlist record
104*d8ab6e12SDon Brady  * into 'records'.  'leftover' is set to the number of bytes that weren't
105*d8ab6e12SDon Brady  * processed as there wasn't a complete record.
106*d8ab6e12SDon Brady  */
107*d8ab6e12SDon Brady int
zpool_history_unpack(char * buf,uint64_t bytes_read,uint64_t * leftover,nvlist_t *** records,uint_t * numrecords)108*d8ab6e12SDon Brady zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
109*d8ab6e12SDon Brady     nvlist_t ***records, uint_t *numrecords)
110*d8ab6e12SDon Brady {
111*d8ab6e12SDon Brady 	uint64_t reclen;
112*d8ab6e12SDon Brady 	nvlist_t *nv;
113*d8ab6e12SDon Brady 	int i;
114*d8ab6e12SDon Brady 	void *tmp;
115*d8ab6e12SDon Brady 
116*d8ab6e12SDon Brady 	while (bytes_read > sizeof (reclen)) {
117*d8ab6e12SDon Brady 
118*d8ab6e12SDon Brady 		/* get length of packed record (stored as little endian) */
119*d8ab6e12SDon Brady 		for (i = 0, reclen = 0; i < sizeof (reclen); i++)
120*d8ab6e12SDon Brady 			reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
121*d8ab6e12SDon Brady 
122*d8ab6e12SDon Brady 		if (bytes_read < sizeof (reclen) + reclen)
123*d8ab6e12SDon Brady 			break;
124*d8ab6e12SDon Brady 
125*d8ab6e12SDon Brady 		/* unpack record */
126*d8ab6e12SDon Brady 		if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
127*d8ab6e12SDon Brady 			return (ENOMEM);
128*d8ab6e12SDon Brady 		bytes_read -= sizeof (reclen) + reclen;
129*d8ab6e12SDon Brady 		buf += sizeof (reclen) + reclen;
130*d8ab6e12SDon Brady 
131*d8ab6e12SDon Brady 		/* add record to nvlist array */
132*d8ab6e12SDon Brady 		(*numrecords)++;
133*d8ab6e12SDon Brady 		if (ISP2(*numrecords + 1)) {
134*d8ab6e12SDon Brady 			tmp = realloc(*records,
135*d8ab6e12SDon Brady 			    *numrecords * 2 * sizeof (nvlist_t *));
136*d8ab6e12SDon Brady 			if (tmp == NULL) {
137*d8ab6e12SDon Brady 				nvlist_free(nv);
138*d8ab6e12SDon Brady 				(*numrecords)--;
139*d8ab6e12SDon Brady 				return (ENOMEM);
140*d8ab6e12SDon Brady 			}
141*d8ab6e12SDon Brady 			*records = tmp;
142*d8ab6e12SDon Brady 		}
143*d8ab6e12SDon Brady 		(*records)[*numrecords - 1] = nv;
144*d8ab6e12SDon Brady 	}
145*d8ab6e12SDon Brady 
146*d8ab6e12SDon Brady 	*leftover = bytes_read;
147*d8ab6e12SDon Brady 	return (0);
148*d8ab6e12SDon Brady }
149*d8ab6e12SDon Brady 
150*d8ab6e12SDon Brady ulong_t
get_system_hostid(void)151*d8ab6e12SDon Brady get_system_hostid(void)
152*d8ab6e12SDon Brady {
153*d8ab6e12SDon Brady 	char *env;
154*d8ab6e12SDon Brady 
155*d8ab6e12SDon Brady 	/*
156*d8ab6e12SDon Brady 	 * Allow the hostid to be subverted for testing.
157*d8ab6e12SDon Brady 	 */
158*d8ab6e12SDon Brady 	env = getenv("ZFS_HOSTID");
159*d8ab6e12SDon Brady 	if (env) {
160*d8ab6e12SDon Brady 		ulong_t hostid = strtoull(env, NULL, 16);
161*d8ab6e12SDon Brady 		return (hostid & 0xFFFFFFFF);
162*d8ab6e12SDon Brady 	}
163*d8ab6e12SDon Brady 
164*d8ab6e12SDon Brady 	return (gethostid());
165*d8ab6e12SDon Brady }
166