1c023f65Toomas Soome/*
2199767fToomas Soome * Copyright (c) 2007 Doug Rabson
3199767fToomas Soome * All rights reserved.
4199767fToomas Soome *
5199767fToomas Soome * Redistribution and use in source and binary forms, with or without
6199767fToomas Soome * modification, are permitted provided that the following conditions
7199767fToomas Soome * are met:
8199767fToomas Soome * 1. Redistributions of source code must retain the above copyright
9199767fToomas Soome *    notice, this list of conditions and the following disclaimer.
10199767fToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
11199767fToomas Soome *    notice, this list of conditions and the following disclaimer in the
12199767fToomas Soome *    documentation and/or other materials provided with the distribution.
13199767fToomas Soome *
14199767fToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767fToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767fToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767fToomas Soome * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767fToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767fToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767fToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767fToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767fToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767fToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767fToomas Soome * SUCH DAMAGE.
25199767fToomas Soome */
26199767fToomas Soome
27199767fToomas Soome#include <sys/cdefs.h>
28199767fToomas Soome
29199767fToomas Soome/*
30199767fToomas Soome *	Stand-alone ZFS file reader.
31199767fToomas Soome */
32199767fToomas Soome
3313a6e30Toomas Soome#include <sys/endian.h>
34199767fToomas Soome#include <sys/stat.h>
35199767fToomas Soome#include <sys/stdint.h>
36c023f65Toomas Soome#include <sys/list.h>
37c023f65Toomas Soome#include <inttypes.h>
38199767fToomas Soome
39199767fToomas Soome#include "zfsimpl.h"
40199767fToomas Soome#include "zfssubr.c"
41199767fToomas Soome
42199767fToomas Soome
43199767fToomas Soomestruct zfsmount {
44199767fToomas Soome	const spa_t	*spa;
45199767fToomas Soome	objset_phys_t	objset;
46199767fToomas Soome	uint64_t	rootobj;
47199767fToomas Soome};
48199767fToomas Soome
49199767fToomas Soome/*
50c023f65Toomas Soome * The indirect_child_t represents the vdev that we will read from, when we
51c023f65Toomas Soome * need to read all copies of the data (e.g. for scrub or reconstruction).
52c023f65Toomas Soome * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror),
53c023f65Toomas Soome * ic_vdev is the same as is_vdev.  However, for mirror top-level vdevs,
54c023f65Toomas Soome * ic_vdev is a child of the mirror.
55c023f65Toomas Soome */
56c023f65Toomas Soometypedef struct indirect_child {
57c023f65Toomas Soome	void *ic_data;
58c023f65Toomas Soome	vdev_t *ic_vdev;
59c023f65Toomas Soome} indirect_child_t;
60c023f65Toomas Soome
61c023f65Toomas Soome/*
62c023f65Toomas Soome * The indirect_split_t represents one mapped segment of an i/o to the
63c023f65Toomas Soome * indirect vdev. For non-split (contiguously-mapped) blocks, there will be
64c023f65Toomas Soome * only one indirect_split_t, with is_split_offset==0 and is_size==io_size.
65c023f65Toomas Soome * For split blocks, there will be several of these.
66c023f65Toomas Soome */
67c023f65Toomas Soometypedef struct indirect_split {
68c023f65Toomas Soome	list_node_t is_node; /* link on iv_splits */
69c023f65Toomas Soome
70c023f65Toomas Soome	/*
71c023f65Toomas Soome	 * is_split_offset is the offset into the i/o.
72c023f65Toomas Soome	 * This is the sum of the previous splits' is_size's.
73c023f65Toomas Soome	 */
74c023f65Toomas Soome	uint64_t is_split_offset;
75c023f65Toomas Soome
76c023f65Toomas Soome	vdev_t *is_vdev; /* top-level vdev */
77c023f65Toomas Soome	uint64_t is_target_offset; /* offset on is_vdev */
78c023f65Toomas Soome	uint64_t is_size;
79c023f65Toomas Soome	int is_children; /* number of entries in is_child[] */
80c023f65Toomas Soome
81c023f65Toomas Soome	/*
82c023f65Toomas Soome	 * is_good_child is the child that we are currently using to
83c023f65Toomas Soome	 * attempt reconstruction.
84c023f65Toomas Soome	 */
85c023f65Toomas Soome	int is_good_child;
86c023f65Toomas Soome
87c023f65Toomas Soome	indirect_child_t is_child[1]; /* variable-length */
88c023f65Toomas Soome} indirect_split_t;
89c023f65Toomas Soome
90c023f65Toomas Soome/*
91c023f65Toomas Soome * The indirect_vsd_t is associated with each i/o to the indirect vdev.
92c023f65Toomas Soome * It is the "Vdev-Specific Data" in the zio_t's io_vsd.
93c023f65Toomas Soome */
94c023f65Toomas Soometypedef struct indirect_vsd {
95c023f65Toomas Soome	boolean_t iv_split_block;
96c023f65Toomas Soome	boolean_t iv_reconstruct;
97c023f65Toomas Soome
98c023f65Toomas Soome	list_t iv_splits; /* list of indirect_split_t's */
99c023f65Toomas Soome} indirect_vsd_t;
100c023f65Toomas Soome
101c023f65Toomas Soome/*
102199767fToomas Soome * List of all vdevs, chained through v_alllink.
103199767fToomas Soome */
104199767fToomas Soomestatic vdev_list_t zfs_vdevs;
105199767fToomas Soome
106199767fToomas Soome /*
107199767fToomas Soome * List of ZFS features supported for read
108199767fToomas Soome */
109199767fToomas Soomestatic const char *features_for_read[] = {
110199767fToomas Soome	"org.illumos:lz4_compress",
111199767fToomas Soome	"com.delphix:hole_birth",
112199767fToomas Soome	"com.delphix:extensible_dataset",
113199767fToomas Soome	"com.delphix:embedded_data",
114199767fToomas Soome	"org.open-zfs:large_blocks",
115199767fToomas Soome	"org.illumos:sha512",
1164a04e8dToomas Soome	"org.illumos:skein",
1174a04e8dToomas Soome	"org.illumos:edonr",
118f905073Toomas Soome	"org.zfsonlinux:large_dnode",
1196f8e6e5Alex Wilson	"com.joyent:multi_vdev_crash_dump",
12042b4b09Andy Fiddaman	"com.delphix:spacemap_histogram",
12142b4b09Andy Fiddaman	"com.delphix:zpool_checkpoint",
12242b4b09Andy Fiddaman	"com.delphix:spacemap_v2",
12342b4b09Andy Fiddaman	"com.datto:encryption",
12442b4b09Andy Fiddaman	"com.datto:bookmark_v2",
12542b4b09Andy Fiddaman	"org.zfsonlinux:allocation_classes",
12642b4b09Andy Fiddaman	"com.datto:resilver_defer",
127c023f65Toomas Soome	"com.delphix:device_removal",
128c023f65Toomas Soome	"com.delphix:obsolete_counts",
129199767fToomas Soome	NULL
130199767fToomas Soome};
131199767fToomas Soome
132199767fToomas Soome/*
133199767fToomas Soome * List of all pools, chained through spa_link.
134199767fToomas Soome */
135199767fToomas Soomestatic spa_list_t zfs_pools;
136199767fToomas Soome
137edb3504Toomas Soomestatic const dnode_phys_t *dnode_cache_obj;
138199767fToomas Soomestatic uint64_t dnode_cache_bn;
139199767fToomas Soomestatic char *dnode_cache_buf;
140199767fToomas Soomestatic char *zap_scratch;
141199767fToomas Soomestatic char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
142199767fToomas Soome
143199767fToomas Soome#define TEMP_SIZE	(1024 * 1024)
144199767fToomas Soome
145199767fToomas Soomestatic int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
146199767fToomas Soomestatic int zfs_get_root(const spa_t *spa, uint64_t *objid);
147199767fToomas Soomestatic int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
1484a04e8dToomas Soomestatic int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode,
1494a04e8dToomas Soome    const char *name, uint64_t integer_size, uint64_t num_integers,
1504a04e8dToomas Soome    void *value);
151c023f65Toomas Soomestatic int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t,
152c023f65Toomas Soome    dnode_phys_t *);
153c023f65Toomas Soomestatic int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *,
154c023f65Toomas Soome    size_t);
155c023f65Toomas Soomestatic int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t,
156c023f65Toomas Soome    size_t);
157c023f65Toomas Soomestatic int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t,
158c023f65Toomas Soome    size_t);
159199767fToomas Soome
160199767fToomas Soomestatic void
161199767fToomas Soomezfs_init(void)
162199767fToomas Soome{
163199767fToomas Soome	STAILQ_INIT(&zfs_vdevs);
164199767fToomas Soome	STAILQ_INIT(&zfs_pools);
165199767fToomas Soome
166199767fToomas Soome	zfs_temp_buf = malloc(TEMP_SIZE);
167199767fToomas Soome	zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
168199767fToomas Soome	zfs_temp_ptr = zfs_temp_buf;
169199767fToomas Soome	dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
170199767fToomas Soome	zap_scratch = malloc(SPA_MAXBLOCKSIZE);
171199767fToomas Soome
172199767fToomas Soome	zfs_init_crc();
173199767fToomas Soome}
174199767fToomas Soome
175199767fToomas Soomestatic void *
176199767fToomas Soomezfs_alloc(size_t size)
177199767fToomas Soome{
178199767fToomas Soome	char *ptr;
179199767fToomas Soome
180199767fToomas Soome	if (zfs_temp_ptr + size > zfs_temp_end) {
181b5b5d63Toomas Soome		panic("ZFS: out of temporary buffer space");
182199767fToomas Soome	}
183199767fToomas Soome	ptr = zfs_temp_ptr;
184199767fToomas Soome	zfs_temp_ptr += size;
185199767fToomas Soome
186199767fToomas Soome	return (ptr);
187199767fToomas Soome}
188199767fToomas Soome
189199767fToomas Soomestatic void
190199767fToomas Soomezfs_free(void *ptr, size_t size)
191199767fToomas Soome{
192199767fToomas Soome
193199767fToomas Soome	zfs_temp_ptr -= size;
194199767fToomas Soome	if (zfs_temp_ptr != ptr) {
195b5b5d63Toomas Soome		panic("ZFS: zfs_alloc()/zfs_free() mismatch");
196199767fToomas Soome	}
197199767fToomas Soome}
198199767fToomas Soome
199199767fToomas Soomestatic int
200199767fToomas Soomexdr_int(const unsigned char **xdr, int *ip)
201199767fToomas Soome{
20213a6e30Toomas Soome	*ip = be32dec(*xdr);
203199767fToomas Soome	(*xdr) += 4;
204199767fToomas Soome	return (0);
205199767fToomas Soome}
206199767fToomas Soome
207199767fToomas Soomestatic int
208199767fToomas Soomexdr_u_int(const unsigned char **xdr, u_int *ip)
209199767fToomas Soome{
21013a6e30Toomas Soome	*ip = be32dec(*xdr);
211199767fToomas Soome	(*xdr) += 4;
212199767fToomas Soome	return (0);
213199767fToomas Soome}
214199767fToomas Soome
215199767fToomas Soomestatic int
216199767fToomas Soomexdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
217199767fToomas Soome{
218199767fToomas Soome	u_int hi, lo;
219199767fToomas Soome
220199767fToomas Soome	xdr_u_int(xdr, &hi);
221199767fToomas Soome	xdr_u_int(xdr, &lo);
222199767fToomas Soome	*lp = (((uint64_t) hi) << 32) | lo;
223199767fToomas Soome	return (0);
224199767fToomas Soome}
225199767fToomas Soome
226199767fToomas Soomestatic int
227199767fToomas Soomenvlist_find(const unsigned char *nvlist, const char *name, int type,
228c023f65Toomas Soome	    int *elementsp, void *valuep)
229199767fToomas Soome{
230199767fToomas Soome	const unsigned char *p, *pair;
231199767fToomas Soome	int junk;
232199767fToomas Soome	int encoded_size, decoded_size;
233199767fToomas Soome
234199767fToomas Soome	p = nvlist;
235199767fToomas Soome	xdr_int(&p, &junk);
236199767fToomas Soome	xdr_int(&p, &junk);
237199767fToomas Soome
238199767fToomas Soome	pair = p;
239199767fToomas Soome	xdr_int(&p, &encoded_size);
240199767fToomas Soome	xdr_int(&p, &decoded_size);
241199767fToomas Soome	while (encoded_size && decoded_size) {
242199767fToomas Soome		int namelen, pairtype, elements;
243199767fToomas Soome		const char *pairname;
244199767fToomas Soome
245199767fToomas Soome		xdr_int(&p, &namelen);
246199767fToomas Soome		pairname = (const char*) p;
247199767fToomas Soome		p += roundup(namelen, 4);
248199767fToomas Soome		xdr_int(&p, &pairtype);
249199767fToomas Soome
250199767fToomas Soome		if (!memcmp(name, pairname, namelen) && type == pairtype) {
251199767fToomas Soome			xdr_int(&p, &elements);
252199767fToomas Soome			if (elementsp)
253199767fToomas Soome				*elementsp = elements;
254199767fToomas Soome			if (type == DATA_TYPE_UINT64) {
255199767fToomas Soome				xdr_uint64_t(&p, (uint64_t *) valuep);
256199767fToomas Soome				return (0);
257199767fToomas Soome			} else if (type == DATA_TYPE_STRING) {
258199767fToomas Soome				int len;
259199767fToomas Soome				xdr_int(&p, &len);
260199767fToomas Soome				(*(const char**) valuep) = (const char*) p;
261199767fToomas Soome				return (0);
262199767fToomas Soome			} else if (type == DATA_TYPE_NVLIST
263199767fToomas Soome				   || type == DATA_TYPE_NVLIST_ARRAY) {
264199767fToomas Soome				(*(const unsigned char**) valuep) =
265199767fToomas Soome					 (const unsigned char*) p;
266199767fToomas Soome				return (0);
267199767fToomas Soome			} else {
268199767fToomas Soome				return (EIO);
269199767fToomas Soome			}
270199767fToomas Soome		} else {
271199767fToomas Soome			/*
272199767fToomas Soome			 * Not the pair we are looking for, skip to the next one.
273199767fToomas Soome			 */
274199767fToomas Soome			p = pair + encoded_size;
275199767fToomas Soome		}
276199767fToomas Soome
277199767fToomas Soome		pair = p;
278199767fToomas Soome		xdr_int(&p, &encoded_size);
279199767fToomas Soome		xdr_int(&p, &decoded_size);
280199767fToomas Soome	}
281199767fToomas Soome
282199767fToomas Soome	return (EIO);
283199767fToomas Soome}
284199767fToomas Soome
285199767fToomas Soomestatic int
286199767fToomas Soomenvlist_check_features_for_read(const unsigned char *nvlist)
287199767fToomas Soome{
288199767fToomas Soome	const unsigned char *p, *pair;
289199767fToomas Soome	int junk;
290199767fToomas Soome	int encoded_size, decoded_size;
291199767fToomas Soome	int rc;
292199767fToomas Soome
293199767fToomas Soome	rc = 0;
294199767fToomas Soome
295199767fToomas Soome	p = nvlist;
296199767fToomas Soome	xdr_int(&p, &junk);
297199767fToomas Soome	xdr_int(&p, &junk);
298199767fToomas Soome
299199767fToomas Soome	pair = p;
300199767fToomas Soome	xdr_int(&p, &encoded_size);
301199767fToomas Soome	xdr_int(&p, &decoded_size);
302199767fToomas Soome	while (encoded_size && decoded_size) {
303199767fToomas Soome		int namelen, pairtype;
304199767fToomas Soome		const char *pairname;
305199767fToomas Soome		int i, found;
306199767fToomas Soome
307199767fToomas Soome		found = 0;
308199767fToomas Soome
309199767fToomas Soome		xdr_int(&p, &namelen);
310199767fToomas Soome		pairname = (const char*) p;
311199767fToomas Soome		p += roundup(namelen, 4);
312199767fToomas Soome		xdr_int(&p, &pairtype);
313199767fToomas Soome
314199767fToomas Soome		for (i = 0; features_for_read[i] != NULL; i++) {
315199767fToomas Soome			if (!memcmp(pairname, features_for_read[i], namelen)) {
316199767fToomas Soome				found = 1;
317199767fToomas Soome				break;
318199767fToomas Soome			}
319199767fToomas Soome		}
320199767fToomas Soome
321199767fToomas Soome		if (!found) {
322199767fToomas Soome			printf("ZFS: unsupported feature: %s\n", pairname);
323199767fToomas Soome			rc = EIO;
324199767fToomas Soome		}
325199767fToomas Soome
326199767fToomas Soome		p = pair + encoded_size;
327199767fToomas Soome
328199767fToomas Soome		pair = p;
329199767fToomas Soome		xdr_int(&p, &encoded_size);
330199767fToomas Soome		xdr_int(&p, &decoded_size);
331199767fToomas Soome	}
332199767fToomas Soome
333199767fToomas Soome	return (rc);
334199767fToomas Soome}
335199767fToomas Soome
336199767fToomas Soome/*
337199767fToomas Soome * Return the next nvlist in an nvlist array.
338199767fToomas Soome */
339199767fToomas Soomestatic const unsigned char *
340199767fToomas Soomenvlist_next(const unsigned char *nvlist)
341199767fToomas Soome{
342199767fToomas Soome	const unsigned char *p, *pair;
343199767fToomas Soome	int junk;
344199767fToomas Soome	int encoded_size, decoded_size;
345199767fToomas Soome
346199767fToomas Soome	p = nvlist;
347199767fToomas Soome	xdr_int(&p, &junk);
348199767fToomas Soome	xdr_int(&p, &junk);
349199767fToomas Soome
350199767fToomas Soome	pair = p;
351199767fToomas Soome	xdr_int(&p, &encoded_size);
352199767fToomas Soome	xdr_int(&p, &decoded_size);
353199767fToomas Soome	while (encoded_size && decoded_size) {
354199767fToomas Soome		p = pair + encoded_size;
355199767fToomas Soome
356199767fToomas Soome		pair = p;
357199767fToomas Soome		xdr_int(&p, &encoded_size);
358199767fToomas Soome		xdr_int(&p, &decoded_size);
359199767fToomas Soome	}
360199767fToomas Soome
361199767fToomas Soome	return p;
362199767fToomas Soome}
363199767fToomas Soome
364199767fToomas Soome#ifdef TEST
365199767fToomas Soome
366199767fToomas Soomestatic const unsigned char *
367199767fToomas Soomenvlist_print(const unsigned char *nvlist, unsigned int indent)
368199767fToomas Soome{
369199767fToomas Soome	static const char* typenames[] = {
370199767fToomas Soome		"DATA_TYPE_UNKNOWN",
371199767fToomas Soome		"DATA_TYPE_BOOLEAN",
372199767fToomas Soome		"DATA_TYPE_BYTE",
373199767fToomas Soome		"DATA_TYPE_INT16",
374199767fToomas Soome		"DATA_TYPE_UINT16",
375199767fToomas Soome		"DATA_TYPE_INT32",
376199767fToomas Soome		"DATA_TYPE_UINT32",
377199767fToomas Soome		"DATA_TYPE_INT64",
378199767fToomas Soome		"DATA_TYPE_UINT64",
379199767fToomas Soome		"DATA_TYPE_STRING",
380199767fToomas Soome		"DATA_TYPE_BYTE_ARRAY",
381199767fToomas Soome		"DATA_TYPE_INT16_ARRAY",
382199767fToomas Soome		"DATA_TYPE_UINT16_ARRAY",
383199767fToomas Soome		"DATA_TYPE_INT32_ARRAY",
384199767fToomas Soome		"DATA_TYPE_UINT32_ARRAY",
385199767fToomas Soome		"DATA_TYPE_INT64_ARRAY",
386199767fToomas Soome		"DATA_TYPE_UINT64_ARRAY",
387199767fToomas Soome		"DATA_TYPE_STRING_ARRAY",
388199767fToomas Soome		"DATA_TYPE_HRTIME",
389199767fToomas Soome		"DATA_TYPE_NVLIST",
390199767fToomas Soome		"DATA_TYPE_NVLIST_ARRAY",
391199767fToomas Soome		"DATA_TYPE_BOOLEAN_VALUE",
392199767fToomas Soome		"DATA_TYPE_INT8",
393199767fToomas Soome		"DATA_TYPE_UINT8",
394199767fToomas Soome		"DATA_TYPE_BOOLEAN_ARRAY",
395199767fToomas Soome		"DATA_TYPE_INT8_ARRAY",
396199767fToomas Soome		"DATA_TYPE_UINT8_ARRAY"
397199767fToomas Soome	};
398199767fToomas Soome
399199767fToomas Soome	unsigned int i, j;
400199767fToomas Soome	const unsigned char *p, *pair;
401199767fToomas Soome	int junk;
402199767fToomas Soome	int encoded_size, decoded_size;
403199767fToomas Soome
404199767fToomas Soome	p = nvlist;
405199767fToomas Soome	xdr_int(&p, &junk);
406199767fToomas Soome	xdr_int(&p, &junk);
407199767fToomas Soome
408199767fToomas Soome	pair = p;
409199767fToomas Soome	xdr_int(&p, &encoded_size);
410199767fToomas Soome	xdr_int(&p, &decoded_size);
411199767fToomas Soome	while (encoded_size && decoded_size) {
412199767fToomas Soome		int namelen, pairtype, elements;
413199767fToomas Soome		const char *pairname;
414199767fToomas Soome
415199767fToomas Soome		xdr_int(&p, &namelen);
416199767fToomas Soome		pairname = (const char*) p;
417199767fToomas Soome		p += roundup(namelen, 4);
418199767fToomas Soome		xdr_int(&p, &pairtype);
419199767fToomas Soome
420199767fToomas Soome		for (i = 0; i < indent; i++)
421199767fToomas Soome			printf(" ");
422199767fToomas Soome		printf("%s %s", typenames[pairtype], pairname);
423199767fToomas Soome
424199767fToomas Soome		xdr_int(&p, &elements);
425199767fToomas Soome		switch (pairtype) {
426199767fToomas Soome		case DATA_TYPE_UINT64: {
427199767fToomas Soome			uint64_t val;
428199767fToomas Soome			xdr_uint64_t(&p, &val);
429199767fToomas Soome			printf(" = 0x%jx\n", (uintmax_t)val);
430199767fToomas Soome			break;
431199767fToomas Soome		}
432199767fToomas Soome
433199767fToomas Soome		case DATA_TYPE_STRING: {
434199767fToomas Soome			int len;
435199767fToomas Soome			xdr_int(&p, &len);
436199767fToomas Soome			printf(" = \"%s\"\n", p);
437199767fToomas Soome			break;
438199767fToomas Soome		}
439199767fToomas Soome
440199767fToomas Soome		case DATA_TYPE_NVLIST:
441199767fToomas Soome			printf("\n");
442199767fToomas Soome			nvlist_print(p, indent + 1);
443199767fToomas Soome			break;
444199767fToomas Soome
445199767fToomas Soome		case DATA_TYPE_NVLIST_ARRAY:
446199767fToomas Soome			for (j = 0; j < elements; j++) {
447199767fToomas Soome				printf("[%d]\n", j);
448199767fToomas Soome				p = nvlist_print(p, indent + 1);
449199767fToomas Soome				if (j != elements - 1) {
450199767fToomas Soome					for (i = 0; i < indent; i++)
451199767fToomas Soome						printf(" ");
452199767fToomas Soome					printf("%s %s", typenames[pairtype], pairname);
453199767fToomas Soome				}
454199767fToomas Soome			}
455199767fToomas Soome			break;
456199767fToomas Soome
457199767fToomas Soome		default:
458199767fToomas Soome			printf("\n");
459199767fToomas Soome		}
460199767fToomas Soome
461199767fToomas Soome		p = pair + encoded_size;
462199767fToomas Soome
463199767fToomas Soome		pair = p;
464199767fToomas Soome		xdr_int(&p, &encoded_size);
465199767fToomas Soome		xdr_int(&p, &decoded_size);
466199767fToomas Soome	}
467199767fToomas Soome
468199767fToomas Soome	return p;
469199767fToomas Soome}
470199767fToomas Soome
471199767fToomas Soome#endif
472199767fToomas Soome
473199767fToomas Soomestatic int
474199767fToomas Soomevdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
475199767fToomas Soome    off_t offset, size_t size)
476199767fToomas Soome{
477199767fToomas Soome	size_t psize;
478199767fToomas Soome	int rc;
479199767fToomas Soome
480199767fToomas Soome	if (!vdev->v_phys_read)
481199767fToomas Soome		return (EIO);
482199767fToomas Soome
483199767fToomas Soome	if (bp) {
484199767fToomas Soome		psize = BP_GET_PSIZE(bp);
485199767fToomas Soome	} else {
486199767fToomas Soome		psize = size;
487199767fToomas Soome	}
488199767fToomas Soome
489f905073Toomas Soome	/*printf("ZFS: reading %zu bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
490199767fToomas Soome	rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
491199767fToomas Soome	if (rc)
492199767fToomas Soome		return (rc);
493c023f65Toomas Soome	if (bp != NULL)
494c023f65Toomas Soome		return (zio_checksum_verify(vdev->spa, bp, buf));
495199767fToomas Soome
496199767fToomas Soome	return (0);
497199767fToomas Soome}
498199767fToomas Soome
499c023f65Toomas Soometypedef struct remap_segment {
500c023f65Toomas Soome	vdev_t *rs_vd;
501c023f65Toomas Soome	uint64_t rs_offset;
502c023f65Toomas Soome	uint64_t rs_asize;
503c023f65Toomas Soome	uint64_t rs_split_offset;
504c023f65Toomas Soome	list_node_t rs_node;
505c023f65Toomas Soome} remap_segment_t;
506c023f65Toomas Soome
507c023f65Toomas Soomestatic remap_segment_t *
508c023f65Toomas Soomers_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset)
509c023f65Toomas Soome{
510c023f65Toomas Soome	remap_segment_t *rs = malloc(sizeof (remap_segment_t));
511c023f65Toomas Soome
512c023f65Toomas Soome	if (rs != NULL) {
513c023f65Toomas Soome		rs->rs_vd = vd;
514c023f65Toomas Soome		rs->rs_offset = offset;
515c023f65Toomas Soome		rs->rs_asize = asize;
516c023f65Toomas Soome		rs->rs_split_offset = split_offset;
517c023f65Toomas Soome	}
518c023f65Toomas Soome
519c023f65Toomas Soome	return (rs);
520c023f65Toomas Soome}
521c023f65Toomas Soome
522c023f65Toomas Soomevdev_indirect_mapping_t *
523c023f65Toomas Soomevdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os,
524c023f65Toomas Soome    uint64_t mapping_object)
525c023f65Toomas Soome{
526c023f65Toomas Soome	vdev_indirect_mapping_t *vim;
527c023f65Toomas Soome	vdev_indirect_mapping_phys_t *vim_phys;
528c023f65Toomas Soome	int rc;
529c023f65Toomas Soome
530c023f65Toomas Soome	vim = calloc(1, sizeof (*vim));
531c023f65Toomas Soome	if (vim == NULL)
532c023f65Toomas Soome		return (NULL);
533c023f65Toomas Soome
534c023f65Toomas Soome	vim->vim_dn = calloc(1, sizeof (*vim->vim_dn));
535c023f65Toomas Soome	if (vim->vim_dn == NULL) {
536c023f65Toomas Soome		free(vim);
537c023f65Toomas Soome		return (NULL);
538c023f65Toomas Soome	}
539c023f65Toomas Soome
540c023f65Toomas Soome	rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn);
541c023f65Toomas Soome	if (rc != 0) {
542c023f65Toomas Soome		free(vim->vim_dn);
543c023f65Toomas Soome		free(vim);
544c023f65Toomas Soome		return (NULL);
545c023f65Toomas Soome	}
546c023f65Toomas Soome
547c023f65Toomas Soome	vim->vim_spa = spa;
548c023f65Toomas Soome	vim->vim_phys = malloc(sizeof (*vim->vim_phys));
549c023f65Toomas Soome	if (vim->vim_phys == NULL) {
550c023f65Toomas Soome		free(vim->vim_dn);
551c023f65Toomas Soome		free(vim);
552c023f65Toomas Soome		return (NULL);
553c023f65Toomas Soome	}
554c023f65Toomas Soome
555c023f65Toomas Soome	vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn);
556c023f65Toomas Soome	*vim->vim_phys = *vim_phys;
557c023f65Toomas Soome
558c023f65Toomas Soome	vim->vim_objset = os;
559c023f65Toomas Soome	vim->vim_object = mapping_object;
560c023f65Toomas Soome	vim->vim_entries = NULL;
561c023f65Toomas Soome
562c023f65Toomas Soome	vim->vim_havecounts =
563c023f65Toomas Soome	    (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0);
564c023f65Toomas Soome
565c023f65Toomas Soome	return (vim);
566c023f65Toomas Soome}
567c023f65Toomas Soome
568c023f65Toomas Soome/*
569c023f65Toomas Soome * Compare an offset with an indirect mapping entry; there are three
570c023f65Toomas Soome * possible scenarios:
571c023f65Toomas Soome *
572c023f65Toomas Soome *     1. The offset is "less than" the mapping entry; meaning the
573c023f65Toomas Soome *        offset is less than the source offset of the mapping entry. In
574c023f65Toomas Soome *        this case, there is no overlap between the offset and the
575c023f65Toomas Soome *        mapping entry and -1 will be returned.
576c023f65Toomas Soome *
577c023f65Toomas Soome *     2. The offset is "greater than" the mapping entry; meaning the
578c023f65Toomas Soome *        offset is greater than the mapping entry's source offset plus
579c023f65Toomas Soome *        the entry's size. In this case, there is no overlap between
580c023f65Toomas Soome *        the offset and the mapping entry and 1 will be returned.
581c023f65Toomas Soome *
582c023f65Toomas Soome *        NOTE: If the offset is actually equal to the entry's offset
583c023f65Toomas Soome *        plus size, this is considered to be "greater" than the entry,
584c023f65Toomas Soome *        and this case applies (i.e. 1 will be returned). Thus, the
585c023f65Toomas Soome *        entry's "range" can be considered to be inclusive at its
586c023f65Toomas Soome *        start, but exclusive at its end: e.g. [src, src + size).
587c023f65Toomas Soome *
588c023f65Toomas Soome *     3. The last case to consider is if the offset actually falls
589c023f65Toomas Soome *        within the mapping entry's range. If this is the case, the
590c023f65Toomas Soome *        offset is considered to be "equal to" the mapping entry and
591c023f65Toomas Soome *        0 will be returned.
592c023f65Toomas Soome *
593c023f65Toomas Soome *        NOTE: If the offset is equal to the entry's source offset,
594c023f65Toomas Soome *        this case applies and 0 will be returned. If the offset is
595c023f65Toomas Soome *        equal to the entry's source plus its size, this case does
596c023f65Toomas Soome *        *not* apply (see "NOTE" above for scenario 2), and 1 will be
597c023f65Toomas Soome *        returned.
598c023f65Toomas Soome */
599c023f65Toomas Soomestatic int
600c023f65Toomas Soomedva_mapping_overlap_compare(const void *v_key, const void *v_array_elem)
601c023f65Toomas Soome{
602c023f65Toomas Soome	const uint64_t *key = v_key;
603c023f65Toomas Soome	const vdev_indirect_mapping_entry_phys_t *array_elem =
604c023f65Toomas Soome	    v_array_elem;
605c023f65Toomas Soome	uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem);
606c023f65Toomas Soome
607c023f65Toomas Soome	if (*key < src_offset) {
608c023f65Toomas Soome		return (-1);
609c023f65Toomas Soome	} else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) {
610c023f65Toomas Soome		return (0);
611c023f65Toomas Soome	} else {
612c023f65Toomas Soome		return (1);
613c023f65Toomas Soome	}
614c023f65Toomas Soome}
615c023f65Toomas Soome
616c023f65Toomas Soome/*
617c023f65Toomas Soome * Return array entry.
618c023f65Toomas Soome */
619c023f65Toomas Soomestatic vdev_indirect_mapping_entry_phys_t *
620c023f65Toomas Soomevdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index)
621c023f65Toomas Soome{
622c023f65Toomas Soome	uint64_t size;
623c023f65Toomas Soome	off_t offset = 0;
624c023f65Toomas Soome	int rc;
625c023f65Toomas Soome
626c023f65Toomas Soome	if (vim->vim_phys->vimp_num_entries == 0)
627c023f65Toomas Soome		return (NULL);
628c023f65Toomas Soome
629c023f65Toomas Soome	if (vim->vim_entries == NULL) {
630c023f65Toomas Soome		uint64_t bsize;
631c023f65Toomas Soome
632c023f65Toomas Soome		bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
633c023f65Toomas Soome		size = vim->vim_phys->vimp_num_entries *
634c023f65Toomas Soome		    sizeof (*vim->vim_entries);
635c023f65Toomas Soome		if (size > bsize) {
636c023f65Toomas Soome			size = bsize / sizeof (*vim->vim_entries);
637c023f65Toomas Soome			size *= sizeof (*vim->vim_entries);
638c023f65Toomas Soome		}
639c023f65Toomas Soome		vim->vim_entries = malloc(size);
640c023f65Toomas Soome		if (vim->vim_entries == NULL)
641c023f65Toomas Soome			return (NULL);
642c023f65Toomas Soome		vim->vim_num_entries = size / sizeof (*vim->vim_entries);
643c023f65Toomas Soome		offset = index * sizeof (*vim->vim_entries);
644c023f65Toomas Soome	}
645c023f65Toomas Soome
646c023f65Toomas Soome	/* We have data in vim_entries */
647c023f65Toomas Soome	if (offset == 0) {
648c023f65Toomas Soome		if (index >= vim->vim_entry_offset &&
649c023f65Toomas Soome		    index <= vim->vim_entry_offset + vim->vim_num_entries) {
650c023f65Toomas Soome			index -= vim->vim_entry_offset;
651c023f65Toomas Soome			return (&vim->vim_entries[index]);
652c023f65Toomas Soome		}
653c023f65Toomas Soome		offset = index * sizeof (*vim->vim_entries);
654c023f65Toomas Soome	}
655c023f65Toomas Soome
656c023f65Toomas Soome	vim->vim_entry_offset = index;
657c023f65Toomas Soome	size = vim->vim_num_entries * sizeof (*vim->vim_entries);
658c023f65Toomas Soome	rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries,
659c023f65Toomas Soome	    size);
660c023f65Toomas Soome	if (rc != 0) {
661c023f65Toomas Soome		/* Read error, invalidate vim_entries. */
662c023f65Toomas Soome		free(vim->vim_entries);
663c023f65Toomas Soome		vim->vim_entries = NULL;
664c023f65Toomas Soome		return (NULL);
665c023f65Toomas Soome	}
666c023f65Toomas Soome	index -= vim->vim_entry_offset;
667c023f65Toomas Soome	return (&vim->vim_entries[index]);
668c023f65Toomas Soome}
669c023f65Toomas Soome
670c023f65Toomas Soome/*
671c023f65Toomas Soome * Returns the mapping entry for the given offset.
672c023f65Toomas Soome *
673c023f65Toomas Soome * It's possible that the given offset will not be in the mapping table
674c023f65Toomas Soome * (i.e. no mapping entries contain this offset), in which case, the
675c023f65Toomas Soome * return value value depends on the "next_if_missing" parameter.
676c023f65Toomas Soome *
677c023f65Toomas Soome * If the offset is not found in the table and "next_if_missing" is
678c023f65Toomas Soome * B_FALSE, then NULL will always be returned. The behavior is intended
679c023f65Toomas Soome * to allow consumers to get the entry corresponding to the offset
680c023f65Toomas Soome * parameter, iff the offset overlaps with an entry in the table.
681c023f65Toomas Soome *
682c023f65Toomas Soome * If the offset is not found in the table and "next_if_missing" is
683c023f65Toomas Soome * B_TRUE, then the entry nearest to the given offset will be returned,
684c023f65Toomas Soome * such that the entry's source offset is greater than the offset
685c023f65Toomas Soome * passed in (i.e. the "next" mapping entry in the table is returned, if
686c023f65Toomas Soome * the offset is missing from the table). If there are no entries whose
687c023f65Toomas Soome * source offset is greater than the passed in offset, NULL is returned.
688c023f65Toomas Soome */
689c023f65Toomas Soomestatic vdev_indirect_mapping_entry_phys_t *
690c023f65Toomas Soomevdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim,
691c023f65Toomas Soome    uint64_t offset)
692c023f65Toomas Soome{
693c023f65Toomas Soome	ASSERT(vim->vim_phys->vimp_num_entries > 0);
694c023f65Toomas Soome
695c023f65Toomas Soome	vdev_indirect_mapping_entry_phys_t *entry;
696c023f65Toomas Soome
697c023f65Toomas Soome	uint64_t last = vim->vim_phys->vimp_num_entries - 1;
698c023f65Toomas Soome	uint64_t base = 0;
699c023f65Toomas Soome
700c023f65Toomas Soome	/*
701c023f65Toomas Soome	 * We don't define these inside of the while loop because we use
702c023f65Toomas Soome	 * their value in the case that offset isn't in the mapping.
703c023f65Toomas Soome	 */
704c023f65Toomas Soome	uint64_t mid;
705c023f65Toomas Soome	int result;
706c023f65Toomas Soome
707c023f65Toomas Soome	while (last >= base) {
708c023f65Toomas Soome		mid = base + ((last - base) >> 1);
709c023f65Toomas Soome
710c023f65Toomas Soome		entry = vdev_indirect_mapping_entry(vim, mid);
711c023f65Toomas Soome		if (entry == NULL)
712c023f65Toomas Soome			break;
713c023f65Toomas Soome		result = dva_mapping_overlap_compare(&offset, entry);
714c023f65Toomas Soome
715c023f65Toomas Soome		if (result == 0) {
716c023f65Toomas Soome			break;
717c023f65Toomas Soome		} else if (result < 0) {
718c023f65Toomas Soome			last = mid - 1;
719c023f65Toomas Soome		} else {
720c023f65Toomas Soome			base = mid + 1;
721c023f65Toomas Soome		}
722c023f65Toomas Soome	}
723c023f65Toomas Soome	return (entry);
724c023f65Toomas Soome}
725c023f65Toomas Soome
726c023f65Toomas Soome/*
727c023f65Toomas Soome * Given an indirect vdev and an extent on that vdev, it duplicates the
728c023f65Toomas Soome * physical entries of the indirect mapping that correspond to the extent
729c023f65Toomas Soome * to a new array and returns a pointer to it. In addition, copied_entries
730c023f65Toomas Soome * is populated with the number of mapping entries that were duplicated.
731c023f65Toomas Soome *
732c023f65Toomas Soome * Finally, since we are doing an allocation, it is up to the caller to
733c023f65Toomas Soome * free the array allocated in this function.
734c023f65Toomas Soome */
735c023f65Toomas Soomevdev_indirect_mapping_entry_phys_t *
736c023f65Toomas Soomevdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset,
737c023f65Toomas Soome    uint64_t asize, uint64_t *copied_entries)
738c023f65Toomas Soome{
739c023f65Toomas Soome	vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL;
740c023f65Toomas Soome	vdev_indirect_mapping_t *vim = vd->v_mapping;
741c023f65Toomas Soome	uint64_t entries = 0;
742c023f65Toomas Soome
743c023f65Toomas Soome	vdev_indirect_mapping_entry_phys_t *first_mapping =
744c023f65Toomas Soome	    vdev_indirect_mapping_entry_for_offset(vim, offset);
745c023f65Toomas Soome	ASSERT3P(first_mapping, !=, NULL);
746c023f65Toomas Soome
747c023f65Toomas Soome	vdev_indirect_mapping_entry_phys_t *m = first_mapping;
748c023f65Toomas Soome	while (asize > 0) {
749c023f65Toomas Soome		uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
750c023f65Toomas Soome		uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m);
751c023f65Toomas Soome		uint64_t inner_size = MIN(asize, size - inner_offset);
752c023f65Toomas Soome
753c023f65Toomas Soome		offset += inner_size;
754c023f65Toomas Soome		asize -= inner_size;
755c023f65Toomas Soome		entries++;
756c023f65Toomas Soome		m++;
757c023f65Toomas Soome	}
758c023f65Toomas Soome
759c023f65Toomas Soome	size_t copy_length = entries * sizeof (*first_mapping);
760c023f65Toomas Soome	duplicate_mappings = malloc(copy_length);
761c023f65Toomas Soome	if (duplicate_mappings != NULL)
762c023f65Toomas Soome		bcopy(first_mapping, duplicate_mappings, copy_length);
763c023f65Toomas Soome	else
764c023f65Toomas Soome		entries = 0;
765c023f65Toomas Soome
766c023f65Toomas Soome	*copied_entries = entries;
767c023f65Toomas Soome
768c023f65Toomas Soome	return (duplicate_mappings);
769c023f65Toomas Soome}
770c023f65Toomas Soome
771c023f65Toomas Soomestatic vdev_t *
772c023f65Toomas Soomevdev_lookup_top(spa_t *spa, uint64_t vdev)
773c023f65Toomas Soome{
774c023f65Toomas Soome	vdev_t *rvd;
775c023f65Toomas Soome
776c023f65Toomas Soome	STAILQ_FOREACH(rvd, &spa->spa_vdevs, v_childlink)
777c023f65Toomas Soome		if (rvd->v_id == vdev)
778c023f65Toomas Soome			break;
779c023f65Toomas Soome
780c023f65Toomas Soome	return (rvd);
781c023f65Toomas Soome}
782c023f65Toomas Soome
783c023f65Toomas Soome/*
784c023f65Toomas Soome * This is a callback for vdev_indirect_remap() which allocates an
785c023f65Toomas Soome * indirect_split_t for each split segment and adds it to iv_splits.
786c023f65Toomas Soome */
787c023f65Toomas Soomestatic void
788c023f65Toomas Soomevdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset,
789c023f65Toomas Soome    uint64_t size, void *arg)
790c023f65Toomas Soome{
791c023f65Toomas Soome	int n = 1;
792c023f65Toomas Soome	zio_t *zio = arg;
793c023f65Toomas Soome	indirect_vsd_t *iv = zio->io_vsd;
794c023f65Toomas Soome
795c023f65Toomas Soome	if (vd->v_read == vdev_indirect_read)
796c023f65Toomas Soome		return;
797c023f65Toomas Soome
798c023f65Toomas Soome	if (vd->v_read == vdev_mirror_read)
799c023f65Toomas Soome		n = vd->v_nchildren;
800c023f65Toomas Soome
801c023f65Toomas Soome	indirect_split_t *is =
802c023f65Toomas Soome	    malloc(offsetof(indirect_split_t, is_child[n]));
803c023f65Toomas Soome	if (is == NULL) {
804c023f65Toomas Soome		zio->io_error = ENOMEM;
805c023f65Toomas Soome		return;
806c023f65Toomas Soome	}
807c023f65Toomas Soome	bzero(is, offsetof(indirect_split_t, is_child[n]));
808c023f65Toomas Soome
809c023f65Toomas Soome	is->is_children = n;
810c023f65Toomas Soome	is->is_size = size;
811c023f65Toomas Soome	is->is_split_offset = split_offset;
812c023f65Toomas Soome	is->is_target_offset = offset;
813c023f65Toomas Soome	is->is_vdev = vd;
814c023f65Toomas Soome
815c023f65Toomas Soome	/*
816c023f65Toomas Soome	 * Note that we only consider multiple copies of the data for
817c023f65Toomas Soome	 * *mirror* vdevs.  We don't for "replacing" or "spare" vdevs, even
818c023f65Toomas Soome	 * though they use the same ops as mirror, because there's only one
819c023f65Toomas Soome	 * "good" copy under the replacing/spare.
820c023f65Toomas Soome	 */
821c023f65Toomas Soome	if (vd->v_read == vdev_mirror_read) {
822c023f65Toomas Soome		int i = 0;
823c023f65Toomas Soome		vdev_t *kid;
824c023f65Toomas Soome
825c023f65Toomas Soome		STAILQ_FOREACH(kid, &vd->v_children, v_childlink) {
826c023f65Toomas Soome			is->is_child[i++].ic_vdev = kid;
827c023f65Toomas Soome		}
828c023f65Toomas Soome	} else {
829c023f65Toomas Soome		is->is_child[0].ic_vdev = vd;
830c023f65Toomas Soome	}
831c023f65Toomas Soome
832c023f65Toomas Soome	list_insert_tail(&iv->iv_splits, is);
833c023f65Toomas Soome}
834c023f65Toomas Soome
835c023f65Toomas Soomestatic void
836c023f65Toomas Soomevdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg)
837c023f65Toomas Soome{
838c023f65Toomas Soome	list_t stack;
839c023f65Toomas Soome	spa_t *spa = vd->spa;
840c023f65Toomas Soome	zio_t *zio = arg;
841042b560Toomas Soome	remap_segment_t *rs;
842c023f65Toomas Soome
843c023f65Toomas Soome	list_create(&stack, sizeof (remap_segment_t),
844c023f65Toomas Soome	    offsetof(remap_segment_t, rs_node));
845c023f65Toomas Soome
846042b560Toomas Soome	rs = rs_alloc(vd, offset, asize, 0);
847042b560Toomas Soome	if (rs == NULL) {
848042b560Toomas Soome		printf("vdev_indirect_remap: out of memory.\n");
849042b560Toomas Soome		zio->io_error = ENOMEM;
850042b560Toomas Soome	}
851042b560Toomas Soome	for ( ; rs != NULL; rs = list_remove_head(&stack)) {
852c023f65Toomas Soome		vdev_t *v = rs->rs_vd;
853c023f65Toomas Soome		uint64_t num_entries = 0;
854c023f65Toomas Soome		/* vdev_indirect_mapping_t *vim = v->v_mapping; */
855c023f65Toomas Soome		vdev_indirect_mapping_entry_phys_t *mapping =
856c023f65Toomas Soome		    vdev_indirect_mapping_duplicate_adjacent_entries(v,
857c023f65Toomas Soome		    rs->rs_offset, rs->rs_asize, &num_entries);
858c023f65Toomas Soome
859042b560Toomas Soome		if (num_entries == 0)
860042b560Toomas Soome			zio->io_error = ENOMEM;
861042b560Toomas Soome
862c023f65Toomas Soome		for (uint64_t i = 0; i < num_entries; i++) {
863c023f65Toomas Soome			vdev_indirect_mapping_entry_phys_t *m = &mapping[i];
864c023f65Toomas Soome			uint64_t size = DVA_GET_ASIZE(&m->vimep_dst);
865c023f65Toomas Soome			uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst);
866c023f65Toomas Soome			uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst);
867c023f65Toomas Soome			uint64_t inner_offset = rs->rs_offset -
868c023f65Toomas Soome			    DVA_MAPPING_GET_SRC_OFFSET(m);
869c023f65Toomas Soome			uint64_t inner_size =
870c023f65Toomas Soome			    MIN(rs->rs_asize, size - inner_offset);
871c023f65Toomas Soome			vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev);
872c023f65Toomas Soome
873c023f65Toomas Soome			if (dst_v->v_read == vdev_indirect_read) {
874042b560Toomas Soome				remap_segment_t *o;
875042b560Toomas Soome
876042b560Toomas Soome				o = rs_alloc(dst_v, dst_offset + inner_offset,
877042b560Toomas Soome				    inner_size, rs->rs_split_offset);
878042b560Toomas Soome				if (o == NULL) {
879042b560Toomas Soome					printf("vdev_indirect_remap: "
880042b560Toomas Soome					    "out of memory.\n");
881042b560Toomas Soome					zio->io_error = ENOMEM;
882042b560Toomas Soome					break;
883042b560Toomas Soome				}
884042b560Toomas Soome
885042b560Toomas Soome				list_insert_head(&stack, o);
886c023f65Toomas Soome			}
887c023f65Toomas Soome			vdev_indirect_gather_splits(rs->rs_split_offset, dst_v,
888c023f65Toomas Soome			    dst_offset + inner_offset,
889c023f65Toomas Soome			    inner_size, arg);
890c023f65Toomas Soome
891c023f65Toomas Soome			/*
892c023f65Toomas Soome			 * vdev_indirect_gather_splits can have memory
893c023f65Toomas Soome			 * allocation error, we can not recover from it.
894c023f65Toomas Soome			 */
895c023f65Toomas Soome			if (zio->io_error != 0)
896c023f65Toomas Soome				break;
897c023f65Toomas Soome			rs->rs_offset += inner_size;
898c023f65Toomas Soome			rs->rs_asize -= inner_size;
899c023f65Toomas Soome			rs->rs_split_offset += inner_size;
900c023f65Toomas Soome		}
901c023f65Toomas Soome
902c023f65Toomas Soome		free(mapping);
903c023f65Toomas Soome		free(rs);
904c023f65Toomas Soome		if (zio->io_error != 0)
905c023f65Toomas Soome			break;
906c023f65Toomas Soome	}
907c023f65Toomas Soome
908c023f65Toomas Soome	list_destroy(&stack);
909c023f65Toomas Soome}
910c023f65Toomas Soome
911c023f65Toomas Soomestatic void
912c023f65Toomas Soomevdev_indirect_map_free(zio_t *zio)
913c023f65Toomas Soome{
914c023f65Toomas Soome	indirect_vsd_t *iv = zio->io_vsd;
915c023f65Toomas Soome	indirect_split_t *is;
916c023f65Toomas Soome
917c023f65Toomas Soome	while ((is = list_head(&iv->iv_splits)) != NULL) {
918c023f65Toomas Soome		for (int c = 0; c < is->is_children; c++) {
919c023f65Toomas Soome			indirect_child_t *ic = &is->is_child[c];
920c023f65Toomas Soome			free(ic->ic_data);
921c023f65Toomas Soome		}
922c023f65Toomas Soome		list_remove(&iv->iv_splits, is);
923c023f65Toomas Soome		free(is);
924c023f65Toomas Soome	}
925c023f65Toomas Soome	free(iv);
926c023f65Toomas Soome}
927c023f65Toomas Soome
928c023f65Toomas Soomestatic int
929c023f65Toomas Soomevdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
930c023f65Toomas Soome    off_t offset, size_t bytes)
931c023f65Toomas Soome{
932c023f65Toomas Soome	zio_t zio = { 0 };
933c023f65Toomas Soome	spa_t *spa = vdev->spa;
934c023f65Toomas Soome	indirect_vsd_t *iv = malloc(sizeof (*iv));
935c023f65Toomas Soome	indirect_split_t *first;
936c023f65Toomas Soome	int rc = EIO;
937c023f65Toomas Soome
938c023f65Toomas Soome	if (iv == NULL)
939c023f65Toomas Soome		return (ENOMEM);
940c023f65Toomas Soome	bzero(iv, sizeof (*iv));
941c023f65Toomas Soome
942c023f65Toomas Soome	list_create(&iv->iv_splits,
943c023f65Toomas Soome	    sizeof (indirect_split_t), offsetof(indirect_split_t, is_node));
944c023f65Toomas Soome
945c023f65Toomas Soome	zio.io_spa = spa;
946c023f65Toomas Soome	zio.io_bp = (blkptr_t *)bp;
947c023f65Toomas Soome	zio.io_data = buf;
948c023f65Toomas Soome	zio.io_size = bytes;
949c023f65Toomas Soome	zio.io_offset = offset;
950c023f65Toomas Soome	zio.io_vd = vdev;
951c023f65Toomas Soome	zio.io_vsd = iv;
952c023f65Toomas Soome
953c023f65Toomas Soome	if (vdev->v_mapping == NULL) {
954c023f65Toomas Soome		vdev_indirect_config_t *vic;
955c023f65Toomas Soome
956c023f65Toomas Soome		vic = &vdev->vdev_indirect_config;
957c023f65Toomas Soome		vdev->v_mapping = vdev_indirect_mapping_open(spa,
958c023f65Toomas Soome		    &spa->spa_mos, vic->vic_mapping_object);
959c023f65Toomas Soome	}
960c023f65Toomas Soome
961c023f65Toomas Soome	vdev_indirect_remap(vdev, offset, bytes, &zio);
962c023f65Toomas Soome	if (zio.io_error != 0)
963c023f65Toomas Soome		return (zio.io_error);
964c023f65Toomas Soome
965c023f65Toomas Soome	first = list_head(&iv->iv_splits);
966c023f65Toomas Soome	if (first->is_size == zio.io_size) {
967c023f65Toomas Soome		/*
968c023f65Toomas Soome		 * This is not a split block; we are pointing to the entire
969c023f65Toomas Soome		 * data, which will checksum the same as the original data.
970c023f65Toomas Soome		 * Pass the BP down so that the child i/o can verify the
971c023f65Toomas Soome		 * checksum, and try a different location if available
972c023f65Toomas Soome		 * (e.g. on a mirror).
973c023f65Toomas Soome		 *
974c023f65Toomas Soome		 * While this special case could be handled the same as the
975c023f65Toomas Soome		 * general (split block) case, doing it this way ensures
976c023f65Toomas Soome		 * that the vast majority of blocks on indirect vdevs
977c023f65Toomas Soome		 * (which are not split) are handled identically to blocks
978c023f65Toomas Soome		 * on non-indirect vdevs.  This allows us to be less strict
979c023f65Toomas Soome		 * about performance in the general (but rare) case.
980c023f65Toomas Soome		 */
981c023f65Toomas Soome		rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp,
982c023f65Toomas Soome		    zio.io_data, first->is_target_offset, bytes);
983c023f65Toomas Soome	} else {
984c023f65Toomas Soome		iv->iv_split_block = B_TRUE;
985c023f65Toomas Soome		/*
986c023f65Toomas Soome		 * Read one copy of each split segment, from the
987c023f65Toomas Soome		 * top-level vdev.  Since we don't know the
988c023f65Toomas Soome		 * checksum of each split individually, the child
989c023f65Toomas Soome		 * zio can't ensure that we get the right data.
990c023f65Toomas Soome		 * E.g. if it's a mirror, it will just read from a
991c023f65Toomas Soome		 * random (healthy) leaf vdev.  We have to verify
992c023f65Toomas Soome		 * the checksum in vdev_indirect_io_done().
993c023f65Toomas Soome		 */
994c023f65Toomas Soome		for (indirect_split_t *is = list_head(&iv->iv_splits);
995c023f65Toomas Soome		    is != NULL; is = list_next(&iv->iv_splits, is)) {
996c023f65Toomas Soome			char *ptr = zio.io_data;
997c023f65Toomas Soome
998c023f65Toomas Soome			rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp,
999c023f65Toomas Soome			    ptr + is->is_split_offset, is->is_target_offset,
1000c023f65Toomas Soome			    is->is_size);
1001c023f65Toomas Soome		}
1002c023f65Toomas Soome		if (zio_checksum_verify(spa, zio.io_bp, zio.io_data))
1003c023f65Toomas Soome			rc = ECKSUM;
1004c023f65Toomas Soome		else
1005c023f65Toomas Soome			rc = 0;
1006c023f65Toomas Soome	}
1007c023f65Toomas Soome
1008c023f65Toomas Soome	vdev_indirect_map_free(&zio);
1009c023f65Toomas Soome	if (rc == 0)
1010c023f65Toomas Soome		rc = zio.io_error;
1011c023f65Toomas Soome
1012c023f65Toomas Soome	return (rc);
1013c023f65Toomas Soome}
1014c023f65Toomas Soome
1015199767fToomas Soomestatic int
1016199767fToomas Soomevdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1017199767fToomas Soome    off_t offset, size_t bytes)
1018199767fToomas Soome{
1019199767fToomas Soome
1020199767fToomas Soome	return (vdev_read_phys(vdev, bp, buf,
1021199767fToomas Soome		offset + VDEV_LABEL_START_SIZE, bytes));
1022199767fToomas Soome}
1023199767fToomas Soome
1024199767fToomas Soome
1025199767fToomas Soomestatic int
1026199767fToomas Soomevdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1027199767fToomas Soome    off_t offset, size_t bytes)
1028199767fToomas Soome{
1029199767fToomas Soome	vdev_t *kid;
1030199767fToomas Soome	int rc;
1031199767fToomas Soome
1032199767fToomas Soome	rc = EIO;
1033199767fToomas Soome	STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1034199767fToomas Soome		if (kid->v_state != VDEV_STATE_HEALTHY)
1035199767fToomas Soome			continue;
1036199767fToomas Soome		rc = kid->v_read(kid, bp, buf, offset, bytes);
1037199767fToomas Soome		if (!rc)
1038199767fToomas Soome			return (0);
1039199767fToomas Soome	}
1040199767fToomas Soome
1041199767fToomas Soome	return (rc);
1042199767fToomas Soome}
1043199767fToomas Soome
1044199767fToomas Soomestatic int
1045199767fToomas Soomevdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
1046199767fToomas Soome    off_t offset, size_t bytes)
1047199767fToomas Soome{
1048199767fToomas Soome	vdev_t *kid;
1049199767fToomas Soome
1050199767fToomas Soome	/*
1051199767fToomas Soome	 * Here we should have two kids:
1052199767fToomas Soome	 * First one which is the one we are replacing and we can trust
1053199767fToomas Soome	 * only this one to have valid data, but it might not be present.
1054199767fToomas Soome	 * Second one is that one we are replacing with. It is most likely
1055199767fToomas Soome	 * healthy, but we can't trust it has needed data, so we won't use it.
1056199767fToomas Soome	 */
1057199767fToomas Soome	kid = STAILQ_FIRST(&vdev->v_children);
1058199767fToomas Soome	if (kid == NULL)
1059199767fToomas Soome		return (EIO);
1060199767fToomas Soome	if (kid->v_state != VDEV_STATE_HEALTHY)
1061199767fToomas Soome		return (EIO);
1062199767fToomas Soome	return (kid->v_read(kid, bp, buf, offset, bytes));
1063199767fToomas Soome}
1064199767fToomas Soome
1065199767fToomas Soomestatic vdev_t *
1066199767fToomas Soomevdev_find(uint64_t guid)
1067199767fToomas Soome{
1068199767fToomas Soome	vdev_t *vdev;
1069199767fToomas Soome
1070199767fToomas Soome	STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
1071199767fToomas Soome		if (vdev->v_guid == guid)
1072199767fToomas Soome			return (vdev);
1073199767fToomas Soome
1074199767fToomas Soome	return (0);
1075199767fToomas Soome}
1076199767fToomas Soome
1077199767fToomas Soomestatic vdev_t *
1078199767fToomas Soomevdev_create(uint64_t guid, vdev_read_t *vdev_read)
1079199767fToomas Soome{
1080199767fToomas Soome	vdev_t *vdev;
1081c023f65Toomas Soome	vdev_indirect_config_t *vic;
1082199767fToomas Soome
1083199767fToomas Soome	vdev = malloc(sizeof(vdev_t));
1084199767fToomas Soome	memset(vdev, 0, sizeof(vdev_t));
1085199767fToomas Soome	STAILQ_INIT(&vdev->v_children);
1086199767fToomas Soome	vdev->v_guid = guid;
1087199767fToomas Soome	vdev->v_state = VDEV_STATE_OFFLINE;
1088199767fToomas Soome	vdev->v_read = vdev_read;
1089c023f65Toomas Soome
1090c023f65Toomas Soome	vic = &vdev->vdev_indirect_config;
1091c023f65Toomas Soome	vic->vic_prev_indirect_vdev = UINT64_MAX;
1092199767fToomas Soome	STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
1093199767fToomas Soome
1094199767fToomas Soome	return (vdev);
1095199767fToomas Soome}
1096199767fToomas Soome
1097199767fToomas Soomestatic int
1098199767fToomas Soomevdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
1099199767fToomas Soome    vdev_t **vdevp, int is_newer)
1100199767fToomas Soome{
1101199767fToomas Soome	int rc;
11024c2b14fToomas Soome	uint64_t guid, id, ashift, asize, nparity;
1103199767fToomas Soome	const char *type;
1104199767fToomas Soome	const char *path;
1105199767fToomas Soome	vdev_t *vdev, *kid;
1106199767fToomas Soome	const unsigned char *kids;
1107199767fToomas Soome	int nkids, i, is_new;
1108199767fToomas Soome	uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
110967806cdToomas Soome	uint64_t is_log;
1110199767fToomas Soome
1111edb3504Toomas Soome	if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64,
1112edb3504Toomas Soome	    NULL, &guid) ||
1113edb3504Toomas Soome	    nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id) ||
1114edb3504Toomas Soome	    nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING,
1115edb3504Toomas Soome	    NULL, &type)) {
1116199767fToomas Soome		printf("ZFS: can't find vdev details\n");
1117199767fToomas Soome		return (ENOENT);
1118199767fToomas Soome	}
1119199767fToomas Soome
1120199767fToomas Soome	if (strcmp(type, VDEV_TYPE_MIRROR)
1121199767fToomas Soome	    && strcmp(type, VDEV_TYPE_DISK)
1122199767fToomas Soome#ifdef ZFS_TEST
1123199767fToomas Soome	    && strcmp(type, VDEV_TYPE_FILE)
1124199767fToomas Soome#endif
1125199767fToomas Soome	    && strcmp(type, VDEV_TYPE_RAIDZ)
1126c023f65Toomas Soome	    && strcmp(type, VDEV_TYPE_INDIRECT)
1127199767fToomas Soome	    && strcmp(type, VDEV_TYPE_REPLACING)) {
1128199767fToomas Soome		printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
1129199767fToomas Soome		return (EIO);
1130199767fToomas Soome	}
1131199767fToomas Soome
1132199767fToomas Soome	is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
113367806cdToomas Soome	is_log = 0;
1134199767fToomas Soome
1135edb3504Toomas Soome	nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL,
1136edb3504Toomas Soome	    &is_offline);
1137edb3504Toomas Soome	nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL,
1138edb3504Toomas Soome	    &is_removed);
1139edb3504Toomas Soome	nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL,
1140edb3504Toomas Soome	    &is_faulted);
1141edb3504Toomas Soome	nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, NULL,
1142edb3504Toomas Soome	    &is_degraded);
1143edb3504Toomas Soome	nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, NULL,
1144edb3504Toomas Soome	    &isnt_present);
114567806cdToomas Soome	nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL,
114667806cdToomas Soome	    &is_log);
1147199767fToomas Soome
1148199767fToomas Soome	vdev = vdev_find(guid);
1149199767fToomas Soome	if (!vdev) {
1150199767fToomas Soome		is_new = 1;
1151199767fToomas Soome
1152199767fToomas Soome		if (!strcmp(type, VDEV_TYPE_MIRROR))
1153199767fToomas Soome			vdev = vdev_create(guid, vdev_mirror_read);
1154199767fToomas Soome		else if (!strcmp(type, VDEV_TYPE_RAIDZ))
1155199767fToomas Soome			vdev = vdev_create(guid, vdev_raidz_read);
1156199767fToomas Soome		else if (!strcmp(type, VDEV_TYPE_REPLACING))
1157199767fToomas Soome			vdev = vdev_create(guid, vdev_replacing_read);
1158c023f65Toomas Soome		else if (!strcmp(type, VDEV_TYPE_INDIRECT)) {
1159c023f65Toomas Soome			vdev_indirect_config_t *vic;
1160c023f65Toomas Soome
1161c023f65Toomas Soome			vdev = vdev_create(guid, vdev_indirect_read);
1162c023f65Toomas Soome			vdev->v_state = VDEV_STATE_HEALTHY;
1163c023f65Toomas Soome			vic = &vdev->vdev_indirect_config;
1164c023f65Toomas Soome
1165c023f65Toomas Soome			nvlist_find(nvlist,
1166c023f65Toomas Soome			    ZPOOL_CONFIG_INDIRECT_OBJECT, DATA_TYPE_UINT64,
1167c023f65Toomas Soome			    NULL, &vic->vic_mapping_object);
1168c023f65Toomas Soome			nvlist_find(nvlist,
1169c023f65Toomas Soome			    ZPOOL_CONFIG_INDIRECT_BIRTHS, DATA_TYPE_UINT64,
1170c023f65Toomas Soome			    NULL, &vic->vic_births_object);
1171c023f65Toomas Soome			nvlist_find(nvlist,
1172c023f65Toomas Soome			    ZPOOL_CONFIG_PREV_INDIRECT_VDEV, DATA_TYPE_UINT64,
1173c023f65Toomas Soome			    NULL, &vic->vic_prev_indirect_vdev);
1174c023f65Toomas Soome		} else
1175199767fToomas Soome			vdev = vdev_create(guid, vdev_disk_read);
1176199767fToomas Soome
1177199767fToomas Soome		vdev->v_id = id;
1178199767fToomas Soome		vdev->v_top = pvdev != NULL ? pvdev : vdev;
1179199767fToomas Soome		if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
1180edb3504Toomas Soome		    DATA_TYPE_UINT64, NULL, &ashift) == 0) {
1181199767fToomas Soome			vdev->v_ashift = ashift;
1182edb3504Toomas Soome		} else {
1183199767fToomas Soome			vdev->v_ashift = 0;
1184edb3504Toomas Soome		}
11854c2b14fToomas Soome		if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
11864c2b14fToomas Soome		    DATA_TYPE_UINT64, NULL, &asize) == 0) {
11874c2b14fToomas Soome			vdev->v_psize = asize +
11884c2b14fToomas Soome			    VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
11894c2b14fToomas Soome		}
1190199767fToomas Soome		if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
1191edb3504Toomas Soome		    DATA_TYPE_UINT64, NULL, &nparity) == 0) {
1192199767fToomas Soome			vdev->v_nparity = nparity;
1193edb3504Toomas Soome		} else {
1194199767fToomas Soome			vdev->v_nparity = 0;
1195edb3504Toomas Soome		}
1196199767fToomas Soome		if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
1197edb3504Toomas Soome		    DATA_TYPE_STRING, NULL, &path) == 0) {
1198199767fToomas Soome			if (strncmp(path, "/dev/dsk/", 9) == 0)
1199199767fToomas Soome				path += 9;
1200199767fToomas Soome			vdev->v_name = strdup(path);
1201199767fToomas Soome			if (nvlist_find(nvlist, ZPOOL_CONFIG_PHYS_PATH,
1202edb3504Toomas Soome			    DATA_TYPE_STRING, NULL, &path) == 0) {
1203199767fToomas Soome				vdev->v_phys_path = strdup(path);
1204edb3504Toomas Soome			} else {
1205199767fToomas Soome				vdev->v_phys_path = NULL;
1206edb3504Toomas Soome			}
1207199767fToomas Soome			if (nvlist_find(nvlist, ZPOOL_CONFIG_DEVID,
1208edb3504Toomas Soome			    DATA_TYPE_STRING, NULL, &path) == 0) {
1209199767fToomas Soome				vdev->v_devid = strdup(path);
1210edb3504Toomas Soome			} else {
1211199767fToomas Soome				vdev->v_devid = NULL;
1212edb3504Toomas Soome			}
1213199767fToomas Soome		} else {
1214c023f65Toomas Soome			char *name;
1215c023f65Toomas Soome
1216199767fToomas Soome			if (!strcmp(type, "raidz")) {
1217c023f65Toomas Soome				if (vdev->v_nparity < 1 ||
1218c023f65Toomas Soome				    vdev->v_nparity > 3) {
1219c023f65Toomas Soome					printf("ZFS: can only boot from disk, "
1220c023f65Toomas Soome					    "mirror, raidz1, raidz2 and raidz3 "
1221c023f65Toomas Soome					    "vdevs\n");
1222199767fToomas Soome					return (EIO);
1223199767fToomas Soome				}
1224c023f65Toomas Soome				asprintf(&name, "%s%d-%" PRIu64, type,
1225c023f65Toomas Soome				    vdev->v_nparity, id);
1226199767fToomas Soome			} else {
1227c023f65Toomas Soome				asprintf(&name, "%s-%" PRIu64, type, id);
1228199767fToomas Soome			}
1229c023f65Toomas Soome			if (name == NULL)
1230c023f65Toomas Soome				return (ENOMEM);
1231c023f65Toomas Soome			vdev->v_name = name;
1232199767fToomas Soome		}
123367806cdToomas Soome		vdev->v_islog = is_log == 1;
1234199767fToomas Soome	} else {
1235199767fToomas Soome		is_new = 0;
1236199767fToomas Soome	}
1237199767fToomas Soome
1238199767fToomas Soome	if (is_new || is_newer) {
1239199767fToomas Soome		/*
1240199767fToomas Soome		 * This is either new vdev or we've already seen this vdev,
1241199767fToomas Soome		 * but from an older vdev label, so let's refresh its state
1242199767fToomas Soome		 * from the newer label.
1243199767fToomas Soome		 */
1244199767fToomas Soome		if (is_offline)
1245199767fToomas Soome			vdev->v_state = VDEV_STATE_OFFLINE;
1246199767fToomas Soome		else if (is_removed)
1247199767fToomas Soome			vdev->v_state = VDEV_STATE_REMOVED;
1248199767fToomas Soome		else if (is_faulted)
1249199767fToomas Soome			vdev->v_state = VDEV_STATE_FAULTED;
1250199767fToomas Soome		else if (is_degraded)
1251199767fToomas Soome			vdev->v_state = VDEV_STATE_DEGRADED;
1252199767fToomas Soome		else if (isnt_present)
1253199767fToomas Soome			vdev->v_state = VDEV_STATE_CANT_OPEN;
1254199767fToomas Soome	}
1255199767fToomas Soome
1256edb3504Toomas Soome	rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY,
1257edb3504Toomas Soome	    &nkids, &kids);
1258199767fToomas Soome	/*
1259199767fToomas Soome	 * Its ok if we don't have any kids.
1260199767fToomas Soome	 */
1261199767fToomas Soome	if (rc == 0) {
1262199767fToomas Soome		vdev->v_nchildren = nkids;
1263199767fToomas Soome		for (i = 0; i < nkids; i++) {
1264199767fToomas Soome			rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
1265199767fToomas Soome			if (rc)
1266199767fToomas Soome				return (rc);
1267199767fToomas Soome			if (is_new)
1268199767fToomas Soome				STAILQ_INSERT_TAIL(&vdev->v_children, kid,
1269199767fToomas Soome						   v_childlink);
1270199767fToomas Soome			kids = nvlist_next(kids);
1271199767fToomas Soome		}
1272199767fToomas Soome	} else {
1273199767fToomas Soome		vdev->v_nchildren = 0;
1274199767fToomas Soome	}
1275199767fToomas Soome
1276199767fToomas Soome	if (vdevp)
1277199767fToomas Soome		*vdevp = vdev;
1278199767fToomas Soome	return (0);
1279199767fToomas Soome}
1280199767fToomas Soome
1281199767fToomas Soomestatic void
1282199767fToomas Soomevdev_set_state(vdev_t *vdev)
1283199767fToomas Soome{
1284199767fToomas Soome	vdev_t *kid;
1285199767fToomas Soome	int good_kids;
1286199767fToomas Soome	int bad_kids;
1287199767fToomas Soome
1288199767fToomas Soome	/*
1289199767fToomas Soome	 * A mirror or raidz is healthy if all its kids are healthy. A
1290199767fToomas Soome	 * mirror is degraded if any of its kids is healthy; a raidz
1291199767fToomas Soome	 * is degraded if at most nparity kids are offline.
1292199767fToomas Soome	 */
1293199767fToomas Soome	if (STAILQ_FIRST(&vdev->v_children)) {
1294199767fToomas Soome		good_kids = 0;
1295199767fToomas Soome		bad_kids = 0;
1296199767fToomas Soome		STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1297199767fToomas Soome			if (kid->v_state == VDEV_STATE_HEALTHY)
1298199767fToomas Soome				good_kids++;
1299199767fToomas Soome			else
1300199767fToomas Soome				bad_kids++;
1301199767fToomas Soome		}
1302199767fToomas Soome		if (bad_kids == 0) {
1303199767fToomas Soome			vdev->v_state = VDEV_STATE_HEALTHY;
1304199767fToomas Soome		} else {
1305199767fToomas Soome			if (vdev->v_read == vdev_mirror_read) {
1306199767fToomas Soome				if (good_kids) {
1307199767fToomas Soome					vdev->v_state = VDEV_STATE_DEGRADED;
1308199767fToomas Soome				} else {
1309199767fToomas Soome					vdev->v_state = VDEV_STATE_OFFLINE;
1310199767fToomas Soome				}
1311199767fToomas Soome			} else if (vdev->v_read == vdev_raidz_read) {
1312199767fToomas Soome				if (bad_kids > vdev->v_nparity) {
1313199767fToomas Soome					vdev->v_state = VDEV_STATE_OFFLINE;
1314199767fToomas Soome				} else {
1315199767fToomas Soome					vdev->v_state = VDEV_STATE_DEGRADED;
1316199767fToomas Soome				}
1317199767fToomas Soome			}
1318199767fToomas Soome		}
1319199767fToomas Soome	}
1320199767fToomas Soome}
1321199767fToomas Soome
1322199767fToomas Soomestatic spa_t *
1323199767fToomas Soomespa_find_by_guid(uint64_t guid)
1324199767fToomas Soome{
1325199767fToomas Soome	spa_t *spa;
1326199767fToomas Soome
1327199767fToomas Soome	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1328199767fToomas Soome		if (spa->spa_guid == guid)
1329199767fToomas Soome			return (spa);
1330199767fToomas Soome
1331199767fToomas Soome	return (0);
1332199767fToomas Soome}
1333199767fToomas Soome
1334199767fToomas Soomestatic spa_t *
1335199767fToomas Soomespa_find_by_name(const char *name)
1336199767fToomas Soome{
1337199767fToomas Soome	spa_t *spa;
1338199767fToomas Soome
1339199767fToomas Soome	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
1340199767fToomas Soome		if (!strcmp(spa->spa_name, name))
1341199767fToomas Soome			return (spa);
1342199767fToomas Soome
1343199767fToomas Soome	return (0);
1344199767fToomas Soome}
1345199767fToomas Soome
1346199767fToomas Soomespa_t *
1347199767fToomas Soomespa_get_primary(void)
1348199767fToomas Soome{
1349199767fToomas Soome	return (STAILQ_FIRST(&zfs_pools));
1350199767fToomas Soome}
1351199767fToomas Soome
1352199767fToomas Soomevdev_t *
1353199767fToomas Soomespa_get_primary_vdev(const spa_t *spa)
1354199767fToomas Soome{
1355199767fToomas Soome	vdev_t *vdev;
1356199767fToomas Soome	vdev_t *kid;
1357199767fToomas Soome
1358199767fToomas Soome	if (spa == NULL)
1359199767fToomas Soome		spa = spa_get_primary();
1360199767fToomas Soome	if (spa == NULL)
1361199767fToomas Soome		return (NULL);
1362199767fToomas Soome	vdev = STAILQ_FIRST(&spa->spa_vdevs);
1363199767fToomas Soome	if (vdev == NULL)
1364199767fToomas Soome		return (NULL);
1365199767fToomas Soome	for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
1366199767fToomas Soome	     kid = STAILQ_FIRST(&vdev->v_children))
1367199767fToomas Soome		vdev = kid;
1368199767fToomas Soome	return (vdev);
1369199767fToomas Soome}
1370199767fToomas Soome
1371199767fToomas Soomestatic spa_t *
1372edb3504Toomas Soomespa_create(uint64_t guid, const char *name)
1373199767fToomas Soome{
1374199767fToomas Soome	spa_t *spa;
1375199767fToomas Soome
137602f1166Toomas Soome	if ((spa = calloc(1, sizeof (spa_t))) == NULL)
1377edb3504Toomas Soome		return (NULL);
1378edb3504Toomas Soome	if ((spa->spa_name = strdup(name)) == NULL) {
1379edb3504Toomas Soome		free(spa);
1380edb3504Toomas Soome		return (NULL);
1381edb3504Toomas Soome	}
1382199767fToomas Soome	STAILQ_INIT(&spa->spa_vdevs);
1383199767fToomas Soome	spa->spa_guid = guid;
1384199767fToomas Soome	STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
1385199767fToomas Soome
1386199767fToomas Soome	return (spa);
1387199767fToomas Soome}
1388199767fToomas Soome
1389199767fToomas Soomestatic const char *
1390199767fToomas Soomestate_name(vdev_state_t state)
1391199767fToomas Soome{
1392199767fToomas Soome	static const char* names[] = {
1393199767fToomas Soome		"UNKNOWN",
1394199767fToomas Soome		"CLOSED",
1395199767fToomas Soome		"OFFLINE",
1396199767fToomas Soome		"REMOVED",
1397199767fToomas Soome		"CANT_OPEN",
1398199767fToomas Soome		"FAULTED",
1399199767fToomas Soome		"DEGRADED",
1400199767fToomas Soome		"ONLINE"
1401199767fToomas Soome	};
1402199767fToomas Soome	return names[state];
1403199767fToomas Soome}
1404199767fToomas Soome
1405199767fToomas Soomestatic int
1406199767fToomas Soomepager_printf(const char *fmt, ...)
1407199767fToomas Soome{
1408199767fToomas Soome	char line[80];
1409199767fToomas Soome	va_list args;
1410199767fToomas Soome
1411199767fToomas Soome	va_start(args, fmt);
1412199767fToomas Soome	vsnprintf(line, sizeof (line), fmt, args);
1413199767fToomas Soome	va_end(args);
1414199767fToomas Soome	return (pager_output(line));
1415199767fToomas Soome}
1416199767fToomas Soome
1417199767fToomas Soome#define STATUS_FORMAT	"        %s %s\n"
1418199767fToomas Soome
1419199767fToomas Soomestatic int
1420199767fToomas Soomeprint_state(int indent, const char *name, vdev_state_t state)
1421199767fToomas Soome{
1422199767fToomas Soome	int i;
1423199767fToomas Soome	char buf[512];
1424199767fToomas Soome
1425199767fToomas Soome	buf[0] = 0;
1426199767fToomas Soome	for (i = 0; i < indent; i++)
1427199767fToomas Soome		strcat(buf, "  ");
1428199767fToomas Soome	strcat(buf, name);
1429199767fToomas Soome	return (pager_printf(STATUS_FORMAT, buf, state_name(state)));
1430199767fToomas Soome}
1431199767fToomas Soome
1432199767fToomas Soomestatic int
1433199767fToomas Soomevdev_status(vdev_t *vdev, int indent)
1434199767fToomas Soome{
1435199767fToomas Soome	vdev_t *kid;
1436199767fToomas Soome	int ret;
143767806cdToomas Soome
143867806cdToomas Soome	if (vdev->v_islog) {
143967806cdToomas Soome		(void)pager_output("        logs\n");
144067806cdToomas Soome		indent++;
144167806cdToomas Soome	}
144267806cdToomas Soome
1443199767fToomas Soome	ret = print_state(indent, vdev->v_name, vdev->v_state);
1444199767fToomas Soome	if (ret != 0)
1445199767fToomas Soome		return (ret);
1446199767fToomas Soome
1447199767fToomas Soome	STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
1448199767fToomas Soome		ret = vdev_status(kid, indent + 1);
1449199767fToomas Soome		if (ret != 0)
1450199767fToomas Soome			return (ret);
1451199767fToomas Soome	}
1452199767fToomas Soome	return (ret);
1453199767fToomas Soome}
1454199767fToomas Soome
1455199767fToomas Soomestatic int
1456199767fToomas Soomespa_status(spa_t *spa)
1457199767fToomas Soome{
1458199767fToomas Soome	static char bootfs[ZFS_MAXNAMELEN];
1459199767fToomas Soome	uint64_t rootid;
1460199767fToomas Soome	vdev_t *vdev;
1461199767fToomas Soome	int good_kids, bad_kids, degraded_kids, ret;
1462199767fToomas Soome	vdev_state_t state;
1463199767fToomas Soome
1464199767fToomas Soome	ret = pager_printf("  pool: %s\n", spa->spa_name);
1465199767fToomas Soome	if (ret != 0)
1466199767fToomas Soome		return (ret);
1467199767fToomas Soome
1468199767fToomas Soome	if (zfs_get_root(spa, &rootid) == 0 &&
1469199767fToomas Soome	    zfs_rlookup(spa, rootid, bootfs) == 0) {
1470199767fToomas Soome		if (bootfs[0] == '\0')
1471199767fToomas Soome			ret = pager_printf("bootfs: %s\n", spa->spa_name);
1472199767fToomas Soome		else
1473199767fToomas Soome			ret = pager_printf("bootfs: %s/%s\n", spa->spa_name,
1474199767fToomas Soome			    bootfs);
1475199767fToomas Soome		if (ret != 0)
1476199767fToomas Soome			return (ret);
1477199767fToomas Soome	}
1478199767fToomas Soome	ret = pager_printf("config:\n\n");
1479199767fToomas Soome	if (ret != 0)
1480199767fToomas Soome		return (ret);
1481199767fToomas Soome	ret = pager_printf(STATUS_FORMAT, "NAME", "STATE");
1482199767fToomas Soome	if (ret != 0)
1483199767fToomas Soome		return (ret);
1484199767fToomas Soome
1485199767fToomas Soome	good_kids = 0;
1486199767fToomas Soome	degraded_kids = 0;
1487199767fToomas Soome	bad_kids = 0;
1488199767fToomas Soome	STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1489199767fToomas Soome		if (vdev->v_state == VDEV_STATE_HEALTHY)
1490199767fToomas Soome			good_kids++;
1491199767fToomas Soome		else if (vdev->v_state == VDEV_STATE_DEGRADED)
1492199767fToomas Soome			degraded_kids++;
1493199767fToomas Soome		else
1494199767fToomas Soome			bad_kids++;
1495199767fToomas Soome	}
1496199767fToomas Soome
1497199767fToomas Soome	state = VDEV_STATE_CLOSED;
1498199767fToomas Soome	if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
1499199767fToomas Soome		state = VDEV_STATE_HEALTHY;
1500199767fToomas Soome	else if ((good_kids + degraded_kids) > 0)
1501199767fToomas Soome		state = VDEV_STATE_DEGRADED;
1502199767fToomas Soome
1503199767fToomas Soome	ret = print_state(0, spa->spa_name, state);
1504199767fToomas Soome	if (ret != 0)
1505199767fToomas Soome		return (ret);
1506199767fToomas Soome	STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1507199767fToomas Soome		ret = vdev_status(vdev, 1);
1508199767fToomas Soome		if (ret != 0)
1509199767fToomas Soome			return (ret);
1510199767fToomas Soome	}
1511199767fToomas Soome	return (ret);
1512199767fToomas Soome}
1513199767fToomas Soome
1514199767fToomas Soomeint
1515199767fToomas Soomespa_all_status(void)
1516199767fToomas Soome{
1517199767fToomas Soome	spa_t *spa;
1518199767fToomas Soome	int first = 1, ret = 0;
1519199767fToomas Soome
1520199767fToomas Soome	STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
1521199767fToomas Soome		if (!first) {
1522199767fToomas Soome			ret = pager_printf("\n");
1523199767fToomas Soome			if (ret != 0)
1524199767fToomas Soome				return (ret);
1525199767fToomas Soome		}
1526199767fToomas Soome		first = 0;
1527199767fToomas Soome		ret = spa_status(spa);
1528199767fToomas Soome		if (ret != 0)
1529199767fToomas Soome			return (ret);
1530199767fToomas Soome	}
1531199767fToomas Soome	return (ret);
1532199767fToomas Soome}
1533199767fToomas Soome
1534edb3504Toomas Soomeuint64_t
1535edb3504Toomas Soomevdev_label_offset(uint64_t psize, int l, uint64_t offset)
1536edb3504Toomas Soome{
1537edb3504Toomas Soome	uint64_t label_offset;
1538edb3504Toomas Soome
1539edb3504Toomas Soome	if (l < VDEV_LABELS / 2)
1540edb3504Toomas Soome		label_offset = 0;
1541edb3504Toomas Soome	else
1542edb3504Toomas Soome		label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t);
1543edb3504Toomas Soome
1544edb3504Toomas Soome	return (offset + l * sizeof (vdev_label_t) + label_offset);
1545edb3504Toomas Soome}
1546edb3504Toomas Soome
1547199767fToomas Soomestatic int
15487dcf02bToomas Soomevdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
15497dcf02bToomas Soome{
15507dcf02bToomas Soome	unsigned int seq1 = 0;
15517dcf02bToomas Soome	unsigned int seq2 = 0;
15527dcf02bToomas Soome	int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
15537dcf02bToomas Soome
15547dcf02bToomas Soome	if (cmp != 0)
15557dcf02bToomas Soome		return (cmp);
15567dcf02bToomas Soome
15577dcf02bToomas Soome	cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
15587dcf02bToomas Soome	if (cmp != 0)
15597dcf02bToomas Soome		return (cmp);
15607dcf02bToomas Soome
15617dcf02bToomas Soome	if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
15627dcf02bToomas Soome		seq1 = MMP_SEQ(ub1);
15637dcf02bToomas Soome
15647dcf02bToomas Soome	if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
15657dcf02bToomas Soome		seq2 = MMP_SEQ(ub2);
15667dcf02bToomas Soome
15677dcf02bToomas Soome	return (AVL_CMP(seq1, seq2));
15687dcf02bToomas Soome}
15697dcf02bToomas Soome
15707dcf02bToomas Soomestatic int
15717dcf02bToomas Soomeuberblock_verify(uberblock_t *ub)
15727dcf02bToomas Soome{
15737dcf02bToomas Soome	if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) {
15747dcf02bToomas Soome		byteswap_uint64_array(ub, sizeof (uberblock_t));
15757dcf02bToomas Soome	}
15767dcf02bToomas Soome
15777dcf02bToomas Soome	if (ub->ub_magic != UBERBLOCK_MAGIC ||
15787dcf02bToomas Soome	    !SPA_VERSION_IS_SUPPORTED(ub->ub_version))
15797dcf02bToomas Soome		return (EINVAL);
15807dcf02bToomas Soome
15817dcf02bToomas Soome	return (0);
15827dcf02bToomas Soome}
15837dcf02bToomas Soome
15847dcf02bToomas Soomestatic int
15857dcf02bToomas Soomevdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset,
15867dcf02bToomas Soome    size_t size)
1587199767fToomas Soome{
1588199767fToomas Soome	blkptr_t bp;
15897dcf02bToomas Soome	off_t off;
1590199767fToomas Soome
15917dcf02bToomas Soome	off = vdev_label_offset(vd->v_psize, l, offset);
1592199767fToomas Soome
15937dcf02bToomas Soome	BP_ZERO(&bp);
15947dcf02bToomas Soome	BP_SET_LSIZE(&bp, size);
15957dcf02bToomas Soome	BP_SET_PSIZE(&bp, size);
15967dcf02bToomas Soome	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
15977dcf02bToomas Soome	BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
15987dcf02bToomas Soome	DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
15997dcf02bToomas Soome	ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
16006617bbfToomas Soome
16017dcf02bToomas Soome	return (vdev_read_phys(vd, &bp, buf, off, size));
16027dcf02bToomas Soome}
16036617bbfToomas Soome
16047dcf02bToomas Soomestatic unsigned char *
16057dcf02bToomas Soomevdev_label_read_config(vdev_t *vd, uint64_t txg)
16067dcf02bToomas Soome{
16077dcf02bToomas Soome	vdev_phys_t *label;
16087dcf02bToomas Soome	uint64_t best_txg = 0;
16097dcf02bToomas Soome	uint64_t label_txg = 0;
16107dcf02bToomas Soome	uint64_t asize;
16117dcf02bToomas Soome	unsigned char *nvl;
16127dcf02bToomas Soome	size_t nvl_size;
16137dcf02bToomas Soome	int error;
16147dcf02bToomas Soome
16157dcf02bToomas Soome	label = malloc(sizeof (vdev_phys_t));
16167dcf02bToomas Soome	if (label == NULL)
16177dcf02bToomas Soome		return (NULL);
1618edb3504Toomas Soome
16197dcf02bToomas Soome	nvl_size = VDEV_PHYS_SIZE - sizeof (zio_eck_t) - 4;
16207dcf02bToomas Soome	nvl = malloc(nvl_size);
16217dcf02bToomas Soome	if (nvl == NULL)
16227dcf02bToomas Soome		goto done;
1623edb3504Toomas Soome
16247dcf02bToomas Soome	for (int l = 0; l < VDEV_LABELS; l++) {
16257dcf02bToomas Soome		const unsigned char *nvlist;
1626edb3504Toomas Soome
16277dcf02bToomas Soome		if (vdev_label_read(vd, l, label,
16287dcf02bToomas Soome		    offsetof(vdev_label_t, vl_vdev_phys),
16297dcf02bToomas Soome		    sizeof (vdev_phys_t)))
1630edb3504Toomas Soome			continue;
1631edb3504Toomas Soome
16327dcf02bToomas Soome		if (label->vp_nvlist[0] != NV_ENCODE_XDR)
1633edb3504Toomas Soome			continue;
1634edb3504Toomas Soome
16357dcf02bToomas Soome		nvlist = (const unsigned char *) label->vp_nvlist + 4;
16367dcf02bToomas Soome		error = nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG,
16377dcf02bToomas Soome		    DATA_TYPE_UINT64, NULL, &label_txg);
16387dcf02bToomas Soome		if (error != 0 || label_txg == 0) {
16397dcf02bToomas Soome			memcpy(nvl, nvlist, nvl_size);
16407dcf02bToomas Soome			goto done;
16417dcf02bToomas Soome		}
16424c2b14fToomas Soome
16437dcf02bToomas Soome		if (label_txg <= txg && label_txg > best_txg) {
16447dcf02bToomas Soome			best_txg = label_txg;
16457dcf02bToomas Soome			memcpy(nvl, nvlist, nvl_size);
16464c2b14fToomas Soome
16474c2b14fToomas Soome			/*
16484c2b14fToomas Soome			 * Use asize from pool config. We need this
16494c2b14fToomas Soome			 * because we can get bad value from BIOS.
16504c2b14fToomas Soome			 */
16514c2b14fToomas Soome			if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE,
16524c2b14fToomas Soome			    DATA_TYPE_UINT64, NULL, &asize) == 0) {
16537dcf02bToomas Soome				vd->v_psize = asize +
16544c2b14fToomas Soome				    VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
16554c2b14fToomas Soome			}
1656edb3504Toomas Soome		}
1657199767fToomas Soome	}
1658199767fToomas Soome
16597dcf02bToomas Soome	if (best_txg == 0) {
16607dcf02bToomas Soome		free(nvl);
16617dcf02bToomas Soome		nvl = NULL;
16627dcf02bToomas Soome	}
16637dcf02bToomas Soomedone:
16647dcf02bToomas Soome	free(label);
16657dcf02bToomas Soome	return (nvl);
16667dcf02bToomas Soome}
16677dcf02bToomas Soome
16687dcf02bToomas Soomestatic void
16697dcf02bToomas Soomevdev_uberblock_load(vdev_t *vd, uberblock_t *ub)
16707dcf02bToomas Soome{
16717dcf02bToomas Soome	uberblock_t *buf;
16727dcf02bToomas Soome
16737dcf02bToomas Soome	buf = malloc(VDEV_UBERBLOCK_SIZE(vd));
16747dcf02bToomas Soome	if (buf == NULL)
16757dcf02bToomas Soome		return;
16767dcf02bToomas Soome
16777dcf02bToomas Soome	for (int l = 0; l < VDEV_LABELS; l++) {
16787dcf02bToomas Soome		for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1679