1 /*-
2  * Copyright (c) 2007 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: stable/10/sys/boot/zfs/zfsimpl.c 316323 2017-03-31 04:51:08Z ngie $");
29 
30 /*
31  *	Stand-alone ZFS file reader.
32  */
33 
34 #include <sys/stat.h>
35 #include <sys/stdint.h>
36 
37 #include "zfsimpl.h"
38 #include "zfssubr.c"
39 
40 
41 struct zfsmount {
42 	const spa_t	*spa;
43 	objset_phys_t	objset;
44 	uint64_t	rootobj;
45 };
46 
47 /*
48  * List of all vdevs, chained through v_alllink.
49  */
50 static vdev_list_t zfs_vdevs;
51 
52  /*
53  * List of ZFS features supported for read
54  */
55 static const char *features_for_read[] = {
56 	"org.illumos:lz4_compress",
57 	"com.delphix:hole_birth",
58 	"com.delphix:extensible_dataset",
59 	"com.delphix:embedded_data",
60 	"org.open-zfs:large_blocks",
61 	NULL
62 };
63 
64 /*
65  * List of all pools, chained through spa_link.
66  */
67 static spa_list_t zfs_pools;
68 
69 static const dnode_phys_t *dnode_cache_obj = 0;
70 static uint64_t dnode_cache_bn;
71 static char *dnode_cache_buf;
72 static char *zap_scratch;
73 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
74 
75 #define TEMP_SIZE	(1024 * 1024)
76 
77 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
78 static int zfs_get_root(const spa_t *spa, uint64_t *objid);
79 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
80 
81 static void
zfs_init(void)82 zfs_init(void)
83 {
84 	STAILQ_INIT(&zfs_vdevs);
85 	STAILQ_INIT(&zfs_pools);
86 
87 	zfs_temp_buf = malloc(TEMP_SIZE);
88 	zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
89 	zfs_temp_ptr = zfs_temp_buf;
90 	dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
91 	zap_scratch = malloc(SPA_MAXBLOCKSIZE);
92 
93 	zfs_init_crc();
94 }
95 
96 static void *
zfs_alloc(size_t size)97 zfs_alloc(size_t size)
98 {
99 	char *ptr;
100 
101 	if (zfs_temp_ptr + size > zfs_temp_end) {
102 		printf("ZFS: out of temporary buffer space\n");
103 		for (;;) ;
104 	}
105 	ptr = zfs_temp_ptr;
106 	zfs_temp_ptr += size;
107 
108 	return (ptr);
109 }
110 
111 static void
zfs_free(void * ptr,size_t size)112 zfs_free(void *ptr, size_t size)
113 {
114 
115 	zfs_temp_ptr -= size;
116 	if (zfs_temp_ptr != ptr) {
117 		printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
118 		for (;;) ;
119 	}
120 }
121 
122 static int
xdr_int(const unsigned char ** xdr,int * ip)123 xdr_int(const unsigned char **xdr, int *ip)
124 {
125 	*ip = ((*xdr)[0] << 24)
126 		| ((*xdr)[1] << 16)
127 		| ((*xdr)[2] << 8)
128 		| ((*xdr)[3] << 0);
129 	(*xdr) += 4;
130 	return (0);
131 }
132 
133 static int
xdr_u_int(const unsigned char ** xdr,u_int * ip)134 xdr_u_int(const unsigned char **xdr, u_int *ip)
135 {
136 	*ip = ((*xdr)[0] << 24)
137 		| ((*xdr)[1] << 16)
138 		| ((*xdr)[2] << 8)
139 		| ((*xdr)[3] << 0);
140 	(*xdr) += 4;
141 	return (0);
142 }
143 
144 static int
xdr_uint64_t(const unsigned char ** xdr,uint64_t * lp)145 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
146 {
147 	u_int hi, lo;
148 
149 	xdr_u_int(xdr, &hi);
150 	xdr_u_int(xdr, &lo);
151 	*lp = (((uint64_t) hi) << 32) | lo;
152 	return (0);
153 }
154 
155 static int
nvlist_find(const unsigned char * nvlist,const char * name,int type,int * elementsp,void * valuep)156 nvlist_find(const unsigned char *nvlist, const char *name, int type,
157 	    int* elementsp, void *valuep)
158 {
159 	const unsigned char *p, *pair;
160 	int junk;
161 	int encoded_size, decoded_size;
162 
163 	p = nvlist;
164 	xdr_int(&p, &junk);
165 	xdr_int(&p, &junk);
166 
167 	pair = p;
168 	xdr_int(&p, &encoded_size);
169 	xdr_int(&p, &decoded_size);
170 	while (encoded_size && decoded_size) {
171 		int namelen, pairtype, elements;
172 		const char *pairname;
173 
174 		xdr_int(&p, &namelen);
175 		pairname = (const char*) p;
176 		p += roundup(namelen, 4);
177 		xdr_int(&p, &pairtype);
178 
179 		if (!memcmp(name, pairname, namelen) && type == pairtype) {
180 			xdr_int(&p, &elements);
181 			if (elementsp)
182 				*elementsp = elements;
183 			if (type == DATA_TYPE_UINT64) {
184 				xdr_uint64_t(&p, (uint64_t *) valuep);
185 				return (0);
186 			} else if (type == DATA_TYPE_STRING) {
187 				int len;
188 				xdr_int(&p, &len);
189 				(*(const char**) valuep) = (const char*) p;
190 				return (0);
191 			} else if (type == DATA_TYPE_NVLIST
192 				   || type == DATA_TYPE_NVLIST_ARRAY) {
193 				(*(const unsigned char**) valuep) =
194 					 (const unsigned char*) p;
195 				return (0);
196 			} else {
197 				return (EIO);
198 			}
199 		} else {
200 			/*
201 			 * Not the pair we are looking for, skip to the next one.
202 			 */
203 			p = pair + encoded_size;
204 		}
205 
206 		pair = p;
207 		xdr_int(&p, &encoded_size);
208 		xdr_int(&p, &decoded_size);
209 	}
210 
211 	return (EIO);
212 }
213 
214 static int
nvlist_check_features_for_read(const unsigned char * nvlist)215 nvlist_check_features_for_read(const unsigned char *nvlist)
216 {
217 	const unsigned char *p, *pair;
218 	int junk;
219 	int encoded_size, decoded_size;
220 	int rc;
221 
222 	rc = 0;
223 
224 	p = nvlist;
225 	xdr_int(&p, &junk);
226 	xdr_int(&p, &junk);
227 
228 	pair = p;
229 	xdr_int(&p, &encoded_size);
230 	xdr_int(&p, &decoded_size);
231 	while (encoded_size && decoded_size) {
232 		int namelen, pairtype;
233 		const char *pairname;
234 		int i, found;
235 
236 		found = 0;
237 
238 		xdr_int(&p, &namelen);
239 		pairname = (const char*) p;
240 		p += roundup(namelen, 4);
241 		xdr_int(&p, &pairtype);
242 
243 		for (i = 0; features_for_read[i] != NULL; i++) {
244 			if (!memcmp(pairname, features_for_read[i], namelen)) {
245 				found = 1;
246 				break;
247 			}
248 		}
249 
250 		if (!found) {
251 			printf("ZFS: unsupported feature: %s\n", pairname);
252 			rc = EIO;
253 		}
254 
255 		p = pair + encoded_size;
256 
257 		pair = p;
258 		xdr_int(&p, &encoded_size);
259 		xdr_int(&p, &decoded_size);
260 	}
261 
262 	return (rc);
263 }
264 
265 /*
266  * Return the next nvlist in an nvlist array.
267  */
268 static const unsigned char *
nvlist_next(const unsigned char * nvlist)269 nvlist_next(const unsigned char *nvlist)
270 {
271 	const unsigned char *p, *pair;
272 	int junk;
273 	int encoded_size, decoded_size;
274 
275 	p = nvlist;
276 	xdr_int(&p, &junk);
277 	xdr_int(&p, &junk);
278 
279 	pair = p;
280 	xdr_int(&p, &encoded_size);
281 	xdr_int(&p, &decoded_size);
282 	while (encoded_size && decoded_size) {
283 		p = pair + encoded_size;
284 
285 		pair = p;
286 		xdr_int(&p, &encoded_size);
287 		xdr_int(&p, &decoded_size);
288 	}
289 
290 	return p;
291 }
292 
293 #ifdef TEST
294 
295 static const unsigned char *
nvlist_print(const unsigned char * nvlist,unsigned int indent)296 nvlist_print(const unsigned char *nvlist, unsigned int indent)
297 {
298 	static const char* typenames[] = {
299 		"DATA_TYPE_UNKNOWN",
300 		"DATA_TYPE_BOOLEAN",
301 		"DATA_TYPE_BYTE",
302 		"DATA_TYPE_INT16",
303 		"DATA_TYPE_UINT16",
304 		"DATA_TYPE_INT32",
305 		"DATA_TYPE_UINT32",
306 		"DATA_TYPE_INT64",
307 		"DATA_TYPE_UINT64",
308 		"DATA_TYPE_STRING",
309 		"DATA_TYPE_BYTE_ARRAY",
310 		"DATA_TYPE_INT16_ARRAY",
311 		"DATA_TYPE_UINT16_ARRAY",
312 		"DATA_TYPE_INT32_ARRAY",
313 		"DATA_TYPE_UINT32_ARRAY",
314 		"DATA_TYPE_INT64_ARRAY",
315 		"DATA_TYPE_UINT64_ARRAY",
316 		"DATA_TYPE_STRING_ARRAY",
317 		"DATA_TYPE_HRTIME",
318 		"DATA_TYPE_NVLIST",
319 		"DATA_TYPE_NVLIST_ARRAY",
320 		"DATA_TYPE_BOOLEAN_VALUE",
321 		"DATA_TYPE_INT8",
322 		"DATA_TYPE_UINT8",
323 		"DATA_TYPE_BOOLEAN_ARRAY",
324 		"DATA_TYPE_INT8_ARRAY",
325 		"DATA_TYPE_UINT8_ARRAY"
326 	};
327 
328 	unsigned int i, j;
329 	const unsigned char *p, *pair;
330 	int junk;
331 	int encoded_size, decoded_size;
332 
333 	p = nvlist;
334 	xdr_int(&p, &junk);
335 	xdr_int(&p, &junk);
336 
337 	pair = p;
338 	xdr_int(&p, &encoded_size);
339 	xdr_int(&p, &decoded_size);
340 	while (encoded_size && decoded_size) {
341 		int namelen, pairtype, elements;
342 		const char *pairname;
343 
344 		xdr_int(&p, &namelen);
345 		pairname = (const char*) p;
346 		p += roundup(namelen, 4);
347 		xdr_int(&p, &pairtype);
348 
349 		for (i = 0; i < indent; i++)
350 			printf(" ");
351 		printf("%s %s", typenames[pairtype], pairname);
352 
353 		xdr_int(&p, &elements);
354 		switch (pairtype) {
355 		case DATA_TYPE_UINT64: {
356 			uint64_t val;
357 			xdr_uint64_t(&p, &val);
358 			printf(" = 0x%jx\n", (uintmax_t)val);
359 			break;
360 		}
361 
362 		case DATA_TYPE_STRING: {
363 			int len;
364 			xdr_int(&p, &len);
365 			printf(" = \"%s\"\n", p);
366 			break;
367 		}
368 
369 		case DATA_TYPE_NVLIST:
370 			printf("\n");
371 			nvlist_print(p, indent + 1);
372 			break;
373 
374 		case DATA_TYPE_NVLIST_ARRAY:
375 			for (j = 0; j < elements; j++) {
376 				printf("[%d]\n", j);
377 				p = nvlist_print(p, indent + 1);
378 				if (j != elements - 1) {
379 					for (i = 0; i < indent; i++)
380 						printf(" ");
381 					printf("%s %s", typenames[pairtype], pairname);
382 				}
383 			}
384 			break;
385 
386 		default:
387 			printf("\n");
388 		}
389 
390 		p = pair + encoded_size;
391 
392 		pair = p;
393 		xdr_int(&p, &encoded_size);
394 		xdr_int(&p, &decoded_size);
395 	}
396 
397 	return p;
398 }
399 
400 #endif
401 
402 static int
vdev_read_phys(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t size)403 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
404     off_t offset, size_t size)
405 {
406 	size_t psize;
407 	int rc;
408 
409 	if (!vdev->v_phys_read)
410 		return (EIO);
411 
412 	if (bp) {
413 		psize = BP_GET_PSIZE(bp);
414 	} else {
415 		psize = size;
416 	}
417 
418 	/*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
419 	rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
420 	if (rc)
421 		return (rc);
422 	if (bp && zio_checksum_verify(bp, buf))
423 		return (EIO);
424 
425 	return (0);
426 }
427 
428 static int
vdev_disk_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)429 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
430     off_t offset, size_t bytes)
431 {
432 
433 	return (vdev_read_phys(vdev, bp, buf,
434 		offset + VDEV_LABEL_START_SIZE, bytes));
435 }
436 
437 
438 static int
vdev_mirror_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)439 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
440     off_t offset, size_t bytes)
441 {
442 	vdev_t *kid;
443 	int rc;
444 
445 	rc = EIO;
446 	STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
447 		if (kid->v_state != VDEV_STATE_HEALTHY)
448 			continue;
449 		rc = kid->v_read(kid, bp, buf, offset, bytes);
450 		if (!rc)
451 			return (0);
452 	}
453 
454 	return (rc);
455 }
456 
457 static int
vdev_replacing_read(vdev_t * vdev,const blkptr_t * bp,void * buf,off_t offset,size_t bytes)458 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
459     off_t offset, size_t bytes)
460 {
461 	vdev_t *kid;
462 
463 	/*
464 	 * Here we should have two kids:
465 	 * First one which is the one we are replacing and we can trust
466 	 * only this one to have valid data, but it might not be present.
467 	 * Second one is that one we are replacing with. It is most likely
468 	 * healthy, but we can't trust it has needed data, so we won't use it.
469 	 */
470 	kid = STAILQ_FIRST(&vdev->v_children);
471 	if (kid == NULL)
472 		return (EIO);
473 	if (kid->v_state != VDEV_STATE_HEALTHY)
474 		return (EIO);
475 	return (kid->v_read(kid, bp, buf, offset, bytes));
476 }
477 
478 static vdev_t *
vdev_find(uint64_t guid)479 vdev_find(uint64_t guid)
480 {
481 	vdev_t *vdev;
482 
483 	STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
484 		if (vdev->v_guid == guid)
485 			return (vdev);
486 
487 	return (0);
488 }
489 
490 static vdev_t *
vdev_create(uint64_t guid,vdev_read_t * _read)491 vdev_create(uint64_t guid, vdev_read_t *_read)
492 {
493 	vdev_t *vdev;
494 
495 	vdev = malloc(sizeof(vdev_t));
496 	memset(vdev, 0, sizeof(vdev_t));
497 	STAILQ_INIT(&vdev->v_children);
498 	vdev->v_guid = guid;
499 	vdev->v_state = VDEV_STATE_OFFLINE;
500 	vdev->v_read = _read;
501 	vdev->v_phys_read = 0;
502 	vdev->v_read_priv = 0;
503 	STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
504 
505 	return (vdev);
506 }
507 
508 static int
vdev_init_from_nvlist(const unsigned char * nvlist,vdev_t * pvdev,vdev_t ** vdevp,int is_newer)509 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
510     vdev_t **vdevp, int is_newer)
511 {
512 	int rc;
513 	uint64_t guid, id, ashift, nparity;
514 	const char *type;
515 	const char *path;
516 	vdev_t *vdev, *kid;
517 	const unsigned char *kids;
518 	int nkids, i, is_new;
519 	uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
520 
521 	if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
522 			DATA_TYPE_UINT64, 0, &guid)
523 	    || nvlist_find(nvlist, ZPOOL_CONFIG_ID,
524 			   DATA_TYPE_UINT64, 0, &id)
525 	    || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
526 			   DATA_TYPE_STRING, 0, &type)) {
527 		printf("ZFS: can't find vdev details\n");
528 		return (ENOENT);
529 	}
530 
531 	if (strcmp(type, VDEV_TYPE_MIRROR)
532 	    && strcmp(type, VDEV_TYPE_DISK)
533 #ifdef ZFS_TEST
534 	    && strcmp(type, VDEV_TYPE_FILE)
535 #endif
536 	    && strcmp(type, VDEV_TYPE_RAIDZ)
537 	    && strcmp(type, VDEV_TYPE_REPLACING)) {
538 		printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
539 		return (EIO);
540 	}
541 
542 	is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
543 
544 	nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
545 			&is_offline);
546 	nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
547 			&is_removed);
548 	nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
549 			&is_faulted);
550 	nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
551 			&is_degraded);
552 	nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
553 			&isnt_present);
554 
555 	vdev = vdev_find(guid);
556 	if (!vdev) {
557 		is_new = 1;
558 
559 		if (!strcmp(type, VDEV_TYPE_MIRROR))
560 			vdev = vdev_create(guid, vdev_mirror_read);
561 		else if (!strcmp(type, VDEV_TYPE_RAIDZ))
562 			vdev = vdev_create(guid, vdev_raidz_read);
563 		else if (!strcmp(type, VDEV_TYPE_REPLACING))
564 			vdev = vdev_create(guid, vdev_replacing_read);
565 		else
566 			vdev = vdev_create(guid, vdev_disk_read);
567 
568 		vdev->v_id = id;
569 		vdev->v_top = pvdev != NULL ? pvdev : vdev;
570 		if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
571 			DATA_TYPE_UINT64, 0, &ashift) == 0)
572 			vdev->v_ashift = ashift;
573 		else
574 			vdev->v_ashift = 0;
575 		if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
576 			DATA_TYPE_UINT64, 0, &nparity) == 0)
577 			vdev->v_nparity = nparity;
578 		else
579 			vdev->v_nparity = 0;
580 		if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
581 				DATA_TYPE_STRING, 0, &path) == 0) {
582 			if (strncmp(path, "/dev/", 5) == 0)
583 				path += 5;
584 			vdev->v_name = strdup(path);
585 		} else {
586 			if (!strcmp(type, "raidz")) {
587 				if (vdev->v_nparity == 1)
588 					vdev->v_name = "raidz1";
589 				else if (vdev->v_nparity == 2)
590 					vdev->v_name = "raidz2";
591 				else if (vdev->v_nparity == 3)
592 					vdev->v_name = "raidz3";
593 				else {
594 					printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
595 					return (EIO);
596 				}
597 			} else {
598 				vdev->v_name = strdup(type);
599 			}
600 		}
601 	} else {
602 		is_new = 0;
603 	}
604 
605 	if (is_new || is_newer) {
606 		/*
607 		 * This is either new vdev or we've already seen this vdev,
608 		 * but from an older vdev label, so let's refresh its state
609 		 * from the newer label.
610 		 */
611 		if (is_offline)
612 			vdev->v_state = VDEV_STATE_OFFLINE;
613 		else if (is_removed)
614 			vdev->v_state = VDEV_STATE_REMOVED;
615 		else if (is_faulted)
616 			vdev->v_state = VDEV_STATE_FAULTED;
617 		else if (is_degraded)
618 			vdev->v_state = VDEV_STATE_DEGRADED;
619 		else if (isnt_present)
620 			vdev->v_state = VDEV_STATE_CANT_OPEN;
621 	}
622 
623 	rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
624 			 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
625 	/*
626 	 * Its ok if we don't have any kids.
627 	 */
628 	if (rc == 0) {
629 		vdev->v_nchildren = nkids;
630 		for (i = 0; i < nkids; i++) {
631 			rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
632 			if (rc)
633 				return (rc);
634 			if (is_new)
635 				STAILQ_INSERT_TAIL(&vdev->v_children, kid,
636 						   v_childlink);
637 			kids = nvlist_next(kids);
638 		}
639 	} else {
640 		vdev->v_nchildren = 0;
641 	}
642 
643 	if (vdevp)
644 		*vdevp = vdev;
645 	return (0);
646 }
647 
648 static void
vdev_set_state(vdev_t * vdev)649 vdev_set_state(vdev_t *vdev)
650 {
651 	vdev_t *kid;
652 	int good_kids;
653 	int bad_kids;
654 
655 	/*
656 	 * A mirror or raidz is healthy if all its kids are healthy. A
657 	 * mirror is degraded if any of its kids is healthy; a raidz
658 	 * is degraded if at most nparity kids are offline.
659 	 */
660 	if (STAILQ_FIRST(&vdev->v_children)) {
661 		good_kids = 0;
662 		bad_kids = 0;
663 		STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
664 			if (kid->v_state == VDEV_STATE_HEALTHY)
665 				good_kids++;
666 			else
667 				bad_kids++;
668 		}
669 		if (bad_kids == 0) {
670 			vdev->v_state = VDEV_STATE_HEALTHY;
671 		} else {
672 			if (vdev->v_read == vdev_mirror_read) {
673 				if (good_kids) {
674 					vdev->v_state = VDEV_STATE_DEGRADED;
675 				} else {
676 					vdev->v_state = VDEV_STATE_OFFLINE;
677 				}
678 			} else if (vdev->v_read == vdev_raidz_read) {
679 				if (bad_kids > vdev->v_nparity) {
680 					vdev->v_state = VDEV_STATE_OFFLINE;
681 				} else {
682 					vdev->v_state = VDEV_STATE_DEGRADED;
683 				}
684 			}
685 		}
686 	}
687 }
688 
689 static spa_t *
spa_find_by_guid(uint64_t guid)690 spa_find_by_guid(uint64_t guid)
691 {
692 	spa_t *spa;
693 
694 	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
695 		if (spa->spa_guid == guid)
696 			return (spa);
697 
698 	return (0);
699 }
700 
701 static spa_t *
spa_find_by_name(const char * name)702 spa_find_by_name(const char *name)
703 {
704 	spa_t *spa;
705 
706 	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
707 		if (!strcmp(spa->spa_name, name))
708 			return (spa);
709 
710 	return (0);
711 }
712 
713 #ifdef BOOT2
714 static spa_t *
spa_get_primary(void)715 spa_get_primary(void)
716 {
717 
718 	return (STAILQ_FIRST(&zfs_pools));
719 }
720 
721 static vdev_t *
spa_get_primary_vdev(const spa_t * spa)722 spa_get_primary_vdev(const spa_t *spa)
723 {
724 	vdev_t *vdev;
725 	vdev_t *kid;
726 
727 	if (spa == NULL)
728 		spa = spa_get_primary();
729 	if (spa == NULL)
730 		return (NULL);
731 	vdev = STAILQ_FIRST(&spa->spa_vdevs);
732 	if (vdev == NULL)
733 		return (NULL);
734 	for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
735 	     kid = STAILQ_FIRST(&vdev->v_children))
736 		vdev = kid;
737 	return (vdev);
738 }
739 #endif
740 
741 static spa_t *
spa_create(uint64_t guid)742 spa_create(uint64_t guid)
743 {
744 	spa_t *spa;
745 
746 	spa = malloc(sizeof(spa_t));
747 	memset(spa, 0, sizeof(spa_t));
748 	STAILQ_INIT(&spa->spa_vdevs);
749 	spa->spa_guid = guid;
750 	STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
751 
752 	return (spa);
753 }
754 
755 static const char *
state_name(vdev_state_t state)756 state_name(vdev_state_t state)
757 {
758 	static const char* names[] = {
759 		"UNKNOWN",
760 		"CLOSED",
761 		"OFFLINE",
762 		"REMOVED",
763 		"CANT_OPEN",
764 		"FAULTED",
765 		"DEGRADED",
766 		"ONLINE"
767 	};
768 	return names[state];
769 }
770 
771 #ifdef BOOT2
772 
773 #define pager_printf printf
774 
775 #else
776 
777 static void
pager_printf(const char * fmt,...)778 pager_printf(const char *fmt, ...)
779 {
780 	char line[80];
781 	va_list args;
782 
783 	va_start(args, fmt);
784 	vsprintf(line, fmt, args);
785 	va_end(args);
786 	pager_output(line);
787 }
788 
789 #endif
790 
791 #define STATUS_FORMAT	"        %s %s\n"
792 
793 static void
print_state(int indent,const char * name,vdev_state_t state)794 print_state(int indent, const char *name, vdev_state_t state)
795 {
796 	int i;
797 	char buf[512];
798 
799 	buf[0] = 0;
800 	for (i = 0; i < indent; i++)
801 		strcat(buf, "  ");
802 	strcat(buf, name);
803 	pager_printf(STATUS_FORMAT, buf, state_name(state));
804 
805 }
806 
807 static void
vdev_status(vdev_t * vdev,int indent)808 vdev_status(vdev_t *vdev, int indent)
809 {
810 	vdev_t *kid;
811 	print_state(indent, vdev->v_name, vdev->v_state);
812 
813 	STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
814 		vdev_status(kid, indent + 1);
815 	}
816 }
817 
818 static void
spa_status(spa_t * spa)819 spa_status(spa_t *spa)
820 {
821 	static char bootfs[ZFS_MAXNAMELEN];
822 	uint64_t rootid;
823 	vdev_t *vdev;
824 	int good_kids, bad_kids, degraded_kids;
825 	vdev_state_t state;
826 
827 	pager_printf("  pool: %s\n", spa->spa_name);
828 	if (zfs_get_root(spa, &rootid) == 0 &&
829 	    zfs_rlookup(spa, rootid, bootfs) == 0) {
830 		if (bootfs[0] == '\0')
831 			pager_printf("bootfs: %s\n", spa->spa_name);
832 		else
833 			pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
834 	}
835 	pager_printf("config:\n\n");
836 	pager_printf(STATUS_FORMAT, "NAME", "STATE");
837 
838 	good_kids = 0;
839 	degraded_kids = 0;
840 	bad_kids = 0;
841 	STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
842 		if (vdev->v_state == VDEV_STATE_HEALTHY)
843 			good_kids++;
844 		else if (vdev->v_state == VDEV_STATE_DEGRADED)
845 			degraded_kids++;
846 		else
847 			bad_kids++;
848 	}
849 
850 	state = VDEV_STATE_CLOSED;
851 	if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
852 		state = VDEV_STATE_HEALTHY;
853 	else if ((good_kids + degraded_kids) > 0)
854 		state = VDEV_STATE_DEGRADED;
855 
856 	print_state(0, spa->spa_name, state);
857 	STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
858 		vdev_status(vdev, 1);
859 	}
860 }
861 
862 static void
spa_all_status(void)863 spa_all_status(void)
864 {
865 	spa_t *spa;
866 	int first = 1;
867 
868 	STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
869 		if (!first)
870 			pager_printf("\n");
871 		first = 0;
872 		spa_status(spa);
873 	}
874 }
875 
876 static int
vdev_probe(vdev_phys_read_t * _read,void * read_priv,spa_t ** spap)877 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap)
878 {
879 	vdev_t vtmp;
880 	vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
881 	spa_t *spa;
882 	vdev_t *vdev, *top_vdev, *pool_vdev;
883 	off_t off;
884 	blkptr_t bp;
885 	const unsigned char *nvlist;
886 	uint64_t val;
887 	uint64_t guid;
888 	uint64_t pool_txg, pool_guid;
889 	uint64_t is_log;
890 	const char *pool_name;
891 	const unsigned char *vdevs;
892 	const unsigned char *features;
893 	int i, rc, is_newer;
894 	char *upbuf;
895 	const struct uberblock *up;
896 
897 	/*
898 	 * Load the vdev label and figure out which
899 	 * uberblock is most current.
900 	 */
901 	memset(&vtmp, 0, sizeof(vtmp));
902 	vtmp.v_phys_read = _read;
903 	vtmp.v_read_priv = read_priv;
904 	off = offsetof(vdev_label_t, vl_vdev_phys);
905 	BP_ZERO(&bp);
906 	BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
907 	BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
908 	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
909 	BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
910 	DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
911 	ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
912 	if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
913 		return (EIO);
914 
915 	if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
916 		return (EIO);
917 	}
918 
919 	nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
920 
921 	if (nvlist_find(nvlist,
922 			ZPOOL_CONFIG_VERSION,
923 			DATA_TYPE_UINT64, 0, &val)) {
924 		return (EIO);
925 	}
926 
927 	if (!SPA_VERSION_IS_SUPPORTED(val)) {
928 		printf("ZFS: unsupported ZFS version %u (should be %u)\n",
929 		    (unsigned) val, (unsigned) SPA_VERSION);
930 		return (EIO);
931 	}
932 
933 	/* Check ZFS features for read */
934 	if (nvlist_find(nvlist,
935 			ZPOOL_CONFIG_FEATURES_FOR_READ,
936 			DATA_TYPE_NVLIST, 0, &features) == 0
937 	    && nvlist_check_features_for_read(features) != 0)
938 		return (EIO);
939 
940 	if (nvlist_find(nvlist,
941 			ZPOOL_CONFIG_POOL_STATE,
942 			DATA_TYPE_UINT64, 0, &val)) {
943 		return (EIO);
944 	}
945 
946 	if (val == POOL_STATE_DESTROYED) {
947 		/* We don't boot only from destroyed pools. */
948 		return (EIO);
949 	}
950 
951 	if (nvlist_find(nvlist,
952 			ZPOOL_CONFIG_POOL_TXG,
953 			DATA_TYPE_UINT64, 0, &pool_txg)
954 	    || nvlist_find(nvlist,
955 			   ZPOOL_CONFIG_POOL_GUID,
956 			   DATA_TYPE_UINT64, 0, &pool_guid)
957 	    || nvlist_find(nvlist,
958 			   ZPOOL_CONFIG_POOL_NAME,
959 			   DATA_TYPE_STRING, 0, &pool_name)) {
960 		/*
961 		 * Cache and spare devices end up here - just ignore
962 		 * them.
963 		 */
964 		/*printf("ZFS: can't find pool details\n");*/
965 		return (EIO);
966 	}
967 
968 	is_log = 0;
969 	(void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
970 	    &is_log);
971 	if (is_log)
972 		return (EIO);
973 
974 	/*
975 	 * Create the pool if this is the first time we've seen it.
976 	 */
977 	spa = spa_find_by_guid(pool_guid);
978 	if (!spa) {
979 		spa = spa_create(pool_guid);
980 		spa->spa_name = strdup(pool_name);
981 	}
982 	if (pool_txg > spa->spa_txg) {
983 		spa->spa_txg = pool_txg;
984 		is_newer = 1;
985 	} else
986 		is_newer = 0;
987 
988 	/*
989 	 * Get the vdev tree and create our in-core copy of it.
990 	 * If we already have a vdev with this guid, this must
991 	 * be some kind of alias (overlapping slices, dangerously dedicated
992 	 * disks etc).
993 	 */
994 	if (nvlist_find(nvlist,
995 			ZPOOL_CONFIG_GUID,
996 			DATA_TYPE_UINT64, 0, &guid)) {
997 		return (EIO);
998 	}
999 	vdev = vdev_find(guid);
1000 	if (vdev && vdev->v_phys_read)	/* Has this vdev already been inited? */
1001 		return (EIO);
1002 
1003 	if (nvlist_find(nvlist,
1004 			ZPOOL_CONFIG_VDEV_TREE,
1005 			DATA_TYPE_NVLIST, 0, &vdevs)) {
1006 		return (EIO);
1007 	}
1008 
1009 	rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer);
1010 	if (rc)
1011 		return (rc);
1012 
1013 	/*
1014 	 * Add the toplevel vdev to the pool if its not already there.
1015 	 */
1016 	STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink)
1017 		if (top_vdev == pool_vdev)
1018 			break;
1019 	if (!pool_vdev && top_vdev)
1020 		STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink);
1021 
1022 	/*
1023 	 * We should already have created an incomplete vdev for this
1024 	 * vdev. Find it and initialise it with our read proc.
1025 	 */
1026 	vdev = vdev_find(guid);
1027 	if (vdev) {
1028 		vdev->v_phys_read = _read;
1029 		vdev->v_read_priv = read_priv;
1030 		vdev->v_state = VDEV_STATE_HEALTHY;
1031 	} else {
1032 		printf("ZFS: inconsistent nvlist contents\n");
1033 		return (EIO);
1034 	}
1035 
1036 	/*
1037 	 * Re-evaluate top-level vdev state.
1038 	 */
1039 	vdev_set_state(top_vdev);
1040 
1041 	/*
1042 	 * Ok, we are happy with the pool so far. Lets find
1043 	 * the best uberblock and then we can actually access
1044 	 * the contents of the pool.
1045 	 */
1046 	upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev));
1047 	up = (const struct uberblock *)upbuf;
1048 	for (i = 0;
1049 	     i < VDEV_UBERBLOCK_COUNT(vdev);
1050 	     i++) {
1051 		off = VDEV_UBERBLOCK_OFFSET(vdev, i);
1052 		BP_ZERO(&bp);
1053 		DVA_SET_OFFSET(&bp.blk_dva[0], off);
1054 		BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1055 		BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev));
1056 		BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1057 		BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
1058 		ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
1059 
1060 		if (vdev_read_phys(vdev, &bp, upbuf, off, 0))
1061 			continue;
1062 
1063 		if (up->ub_magic != UBERBLOCK_MAGIC)
1064 			continue;
1065 		if (up->ub_txg < spa->spa_txg)
1066 			continue;
1067 		if (up->ub_txg > spa->spa_uberblock.ub_txg) {
1068 			spa->spa_uberblock = *up;
1069 		} else if (up->ub_txg == spa->spa_uberblock.ub_txg) {
1070 			if (up->ub_timestamp > spa->spa_uberblock.ub_timestamp)
1071 				spa->spa_uberblock = *up;
1072 		}
1073 	}
1074 	zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev));
1075 
1076 	if (spap)
1077 		*spap = spa;
1078 	return (0);
1079 }
1080 
1081 static int
ilog2(int n)1082 ilog2(int n)
1083 {
1084 	int v;
1085 
1086 	for (v = 0; v < 32; v++)
1087 		if (n == (1 << v))
1088 			return v;
1089 	return -1;
1090 }
1091 
1092 static int
zio_read_gang(const spa_t * spa,const blkptr_t * bp,void * buf)1093 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf)
1094 {
1095 	blkptr_t gbh_bp;
1096 	zio_gbh_phys_t zio_gb;
1097 	char *pbuf;
1098 	int i;
1099 
1100 	/* Artificial BP for gang block header. */
1101 	gbh_bp = *bp;
1102 	BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1103 	BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE);
1104 	BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER);
1105 	BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF);
1106 	for (i = 0; i < SPA_DVAS_PER_BP; i++)
1107 		DVA_SET_GANG(&gbh_bp.blk_dva[i], 0);
1108 
1109 	/* Read gang header block using the artificial BP. */
1110 	if (zio_read(spa, &gbh_bp, &zio_gb))
1111 		return (EIO);
1112 
1113 	pbuf = buf;
1114 	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1115 		blkptr_t *gbp = &zio_gb.zg_blkptr[i];
1116 
1117 		if (BP_IS_HOLE(gbp))
1118 			continue;
1119 		if (zio_read(spa, gbp, pbuf))
1120 			return (EIO);
1121 		pbuf += BP_GET_PSIZE(gbp);
1122 	}
1123 
1124 	if (zio_checksum_verify(bp, buf))
1125 		return (EIO);
1126 	return (0);
1127 }
1128 
1129 static int
zio_read(const spa_t * spa,const blkptr_t * bp,void * buf)1130 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf)
1131 {
1132 	int cpfunc = BP_GET_COMPRESS(bp);
1133 	uint64_t align, size;
1134 	void *pbuf;
1135 	int i, error;
1136 
1137 	/*
1138 	 * Process data embedded in block pointer
1139 	 */
1140 	if (BP_IS_EMBEDDED(bp)) {
1141 		ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
1142 
1143 		size = BPE_GET_PSIZE(bp);
1144 		ASSERT(size <= BPE_PAYLOAD_SIZE);
1145 
1146 		if (cpfunc != ZIO_COMPRESS_OFF)
1147 			pbuf = zfs_alloc(size);
1148 		else
1149 			pbuf = buf;
1150 
1151 		decode_embedded_bp_compressed(bp, pbuf);
1152 		error = 0;
1153 
1154 		if (cpfunc != ZIO_COMPRESS_OFF) {
1155 			error = zio_decompress_data(cpfunc, pbuf,
1156 			    size, buf, BP_GET_LSIZE(bp));
1157 			zfs_free(pbuf, size);
1158 		}
1159 		if (error != 0)
1160 			printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n",
1161 			    error);
1162 		return (error);
1163 	}
1164 
1165 	error = EIO;
1166 
1167 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
1168 		const dva_t *dva = &bp->blk_dva[i];
1169 		vdev_t *vdev;
1170 		int vdevid;
1171 		off_t offset;
1172 
1173 		if (!dva->dva_word[0] && !dva->dva_word[1])
1174 			continue;
1175 
1176 		vdevid = DVA_GET_VDEV(dva);
1177 		offset = DVA_GET_OFFSET(dva);
1178 		STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
1179 			if (vdev->v_id == vdevid)
1180 				break;
1181 		}
1182 		if (!vdev || !vdev->v_read)
1183 			continue;
1184 
1185 		size = BP_GET_PSIZE(bp);
1186 		if (vdev->v_read == vdev_raidz_read) {
1187 			align = 1ULL << vdev->v_top->v_ashift;
1188 			if (P2PHASE(size, align) != 0)
1189 				size = P2ROUNDUP(size, align);
1190 		}
1191 		if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF)
1192 			pbuf = zfs_alloc(size);
1193 		else
1194 			pbuf = buf;
1195 
1196 		if (DVA_GET_GANG(dva))
1197 			error = zio_read_gang(spa, bp, pbuf);
1198 		else
1199 			error = vdev->v_read(vdev, bp, pbuf, offset, size);
1200 		if (error == 0) {
1201 			if (cpfunc != ZIO_COMPRESS_OFF)
1202 				error = zio_decompress_data(cpfunc, pbuf,
1203 				    BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp));
1204 			else if (size != BP_GET_PSIZE(bp))
1205 				bcopy(pbuf, buf, BP_GET_PSIZE(bp));
1206 		}
1207 		if (buf != pbuf)
1208 			zfs_free(pbuf, size);
1209 		if (error == 0)
1210 			break;
1211 	}
1212 	if (error != 0)
1213 		printf("ZFS: i/o error - all block copies unavailable\n");
1214 	return (error);
1215 }
1216 
1217 static int
dnode_read(const spa_t * spa,const dnode_phys_t * dnode,off_t offset,void * buf,size_t buflen)1218 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen)
1219 {
1220 	int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
1221 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1222 	int nlevels = dnode->dn_nlevels;
1223 	int i, rc;
1224 
1225 	if (bsize > SPA_MAXBLOCKSIZE) {
1226 		printf("ZFS: I/O error - blocks larger than 128K are not supported\n");
1227 		return (EIO);
1228 	}
1229 
1230 	/*
1231 	 * Note: bsize may not be a power of two here so we need to do an
1232 	 * actual divide rather than a bitshift.
1233 	 */
1234 	while (buflen > 0) {
1235 		uint64_t bn = offset / bsize;
1236 		int boff = offset % bsize;
1237 		int ibn;
1238 		const blkptr_t *indbp;
1239 		blkptr_t bp;
1240 
1241 		if (bn > dnode->dn_maxblkid)
1242 			return (EIO);
1243 
1244 		if (dnode == dnode_cache_obj && bn == dnode_cache_bn)
1245 			goto cached;
1246 
1247 		indbp = dnode->dn_blkptr;
1248 		for (i = 0; i < nlevels; i++) {
1249 			/*
1250 			 * Copy the bp from the indirect array so that
1251 			 * we can re-use the scratch buffer for multi-level
1252 			 * objects.
1253 			 */
1254 			ibn = bn >> ((nlevels - i - 1) * ibshift);
1255 			ibn &= ((1 << ibshift) - 1);
1256 			bp = indbp[ibn];
1257 			if (BP_IS_HOLE(&bp)) {
1258 				memset(dnode_cache_buf, 0, bsize);
1259 				break;
1260 			}
1261 			rc = zio_read(spa, &bp, dnode_cache_buf);
1262 			if (rc)
1263 				return (rc);
1264 			indbp = (const blkptr_t *) dnode_cache_buf;
1265 		}
1266 		dnode_cache_obj = dnode;
1267 		dnode_cache_bn = bn;
1268 	cached:
1269 
1270 		/*
1271 		 * The buffer contains our data block. Copy what we
1272 		 * need from it and loop.
1273 		 */
1274 		i = bsize - boff;
1275 		if (i > buflen) i = buflen;
1276 		memcpy(buf, &dnode_cache_buf[boff], i);
1277 		buf = ((char*) buf) + i;
1278 		offset += i;
1279 		buflen -= i;
1280 	}
1281 
1282 	return (0);
1283 }
1284 
1285 /*
1286  * Lookup a value in a microzap directory. Assumes that the zap
1287  * scratch buffer contains the directory contents.
1288  */
1289 static int
mzap_lookup(const dnode_phys_t * dnode,const char * name,uint64_t * value)1290 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value)
1291 {
1292 	const mzap_phys_t *mz;
1293 	const mzap_ent_phys_t *mze;
1294 	size_t size;
1295 	int chunks, i;
1296 
1297 	/*
1298 	 * Microzap objects use exactly one block. Read the whole
1299 	 * thing.
1300 	 */
1301 	size = dnode->dn_datablkszsec * 512;
1302 
1303 	mz = (const mzap_phys_t *) zap_scratch;
1304 	chunks = size / MZAP_ENT_LEN - 1;
1305 
1306 	for (i = 0; i < chunks; i++) {
1307 		mze = &mz->mz_chunk[i];
1308 		if (!strcmp(mze->mze_name, name)) {
1309 			*value = mze->mze_value;
1310 			return (0);
1311 		}
1312 	}
1313 
1314 	return (ENOENT);
1315 }
1316 
1317 /*
1318  * Compare a name with a zap leaf entry. Return non-zero if the name
1319  * matches.
1320  */
1321 static int
fzap_name_equal(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,const char * name)1322 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name)
1323 {
1324 	size_t namelen;
1325 	const zap_leaf_chunk_t *nc;
1326 	const char *p;
1327 
1328 	namelen = zc->l_entry.le_name_numints;
1329 
1330 	nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1331 	p = name;
1332 	while (namelen > 0) {
1333 		size_t len;
1334 		len = namelen;
1335 		if (len > ZAP_LEAF_ARRAY_BYTES)
1336 			len = ZAP_LEAF_ARRAY_BYTES;
1337 		if (memcmp(p, nc->l_array.la_array, len))
1338 			return (0);
1339 		p += len;
1340 		namelen -= len;
1341 		nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1342 	}
1343 
1344 	return 1;
1345 }
1346 
1347 /*
1348  * Extract a uint64_t value from a zap leaf entry.
1349  */
1350 static uint64_t
fzap_leaf_value(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc)1351 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc)
1352 {
1353 	const zap_leaf_chunk_t *vc;
1354 	int i;
1355 	uint64_t value;
1356 	const uint8_t *p;
1357 
1358 	vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk);
1359 	for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) {
1360 		value = (value << 8) | p[i];
1361 	}
1362 
1363 	return value;
1364 }
1365 
1366 /*
1367  * Lookup a value in a fatzap directory. Assumes that the zap scratch
1368  * buffer contains the directory header.
1369  */
1370 static int
fzap_lookup(const spa_t * spa,const dnode_phys_t * dnode,const char * name,uint64_t * value)1371 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1372 {
1373 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1374 	zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1375 	fat_zap_t z;
1376 	uint64_t *ptrtbl;
1377 	uint64_t hash;
1378 	int rc;
1379 
1380 	if (zh.zap_magic != ZAP_MAGIC)
1381 		return (EIO);
1382 
1383 	z.zap_block_shift = ilog2(bsize);
1384 	z.zap_phys = (zap_phys_t *) zap_scratch;
1385 
1386 	/*
1387 	 * Figure out where the pointer table is and read it in if necessary.
1388 	 */
1389 	if (zh.zap_ptrtbl.zt_blk) {
1390 		rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize,
1391 			       zap_scratch, bsize);
1392 		if (rc)
1393 			return (rc);
1394 		ptrtbl = (uint64_t *) zap_scratch;
1395 	} else {
1396 		ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0);
1397 	}
1398 
1399 	hash = zap_hash(zh.zap_salt, name);
1400 
1401 	zap_leaf_t zl;
1402 	zl.l_bs = z.zap_block_shift;
1403 
1404 	off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs;
1405 	zap_leaf_chunk_t *zc;
1406 
1407 	rc = dnode_read(spa, dnode, off, zap_scratch, bsize);
1408 	if (rc)
1409 		return (rc);
1410 
1411 	zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1412 
1413 	/*
1414 	 * Make sure this chunk matches our hash.
1415 	 */
1416 	if (zl.l_phys->l_hdr.lh_prefix_len > 0
1417 	    && zl.l_phys->l_hdr.lh_prefix
1418 	    != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len))
1419 		return (ENOENT);
1420 
1421 	/*
1422 	 * Hash within the chunk to find our entry.
1423 	 */
1424 	int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len);
1425 	int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1);
1426 	h = zl.l_phys->l_hash[h];
1427 	if (h == 0xffff)
1428 		return (ENOENT);
1429 	zc = &ZAP_LEAF_CHUNK(&zl, h);
1430 	while (zc->l_entry.le_hash != hash) {
1431 		if (zc->l_entry.le_next == 0xffff) {
1432 			zc = 0;
1433 			break;
1434 		}
1435 		zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next);
1436 	}
1437 	if (fzap_name_equal(&zl, zc, name)) {
1438 		if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 8)
1439 			return (E2BIG);
1440 		*value = fzap_leaf_value(&zl, zc);
1441 		return (0);
1442 	}
1443 
1444 	return (ENOENT);
1445 }
1446 
1447 /*
1448  * Lookup a name in a zap object and return its value as a uint64_t.
1449  */
1450 static int
zap_lookup(const spa_t * spa,const dnode_phys_t * dnode,const char * name,uint64_t * value)1451 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, uint64_t *value)
1452 {
1453 	int rc;
1454 	uint64_t zap_type;
1455 	size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1456 
1457 	rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1458 	if (rc)
1459 		return (rc);
1460 
1461 	zap_type = *(uint64_t *) zap_scratch;
1462 	if (zap_type == ZBT_MICRO)
1463 		return mzap_lookup(dnode, name, value);
1464 	else if (zap_type == ZBT_HEADER)
1465 		return fzap_lookup(spa, dnode, name, value);
1466 	printf("ZFS: invalid zap_type=%d\n", (int)zap_type);
1467 	return (EIO);
1468 }
1469 
1470 /*
1471  * List a microzap directory. Assumes that the zap scratch buffer contains
1472  * the directory contents.
1473  */
1474 static int
mzap_list(const dnode_phys_t * dnode,int (* callback)(const char *))1475 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *))
1476 {
1477 	const mzap_phys_t *mz;
1478 	const mzap_ent_phys_t *mze;
1479 	size_t size;
1480 	int chunks, i;
1481 
1482 	/*
1483 	 * Microzap objects use exactly one block. Read the whole
1484 	 * thing.
1485 	 */
1486 	size = dnode->dn_datablkszsec * 512;
1487 	mz = (const mzap_phys_t *) zap_scratch;
1488 	chunks = size / MZAP_ENT_LEN - 1;
1489 
1490 	for (i = 0; i < chunks; i++) {
1491 		mze = &mz->mz_chunk[i];
1492 		if (mze->mze_name[0])
1493 			//printf("%-32s 0x%jx\n", mze->mze_name, (uintmax_t)mze->mze_value);
1494 			callback(mze->mze_name);
1495 	}
1496 
1497 	return (0);
1498 }
1499 
1500 /*
1501  * List a fatzap directory. Assumes that the zap scratch buffer contains
1502  * the directory header.
1503  */
1504 static int
fzap_list(const spa_t * spa,const dnode_phys_t * dnode,int (* callback)(const char *))1505 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *))
1506 {
1507 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1508 	zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1509 	fat_zap_t z;
1510 	int i, j;
1511 
1512 	if (zh.zap_magic != ZAP_MAGIC)
1513 		return (EIO);
1514 
1515 	z.zap_block_shift = ilog2(bsize);
1516 	z.zap_phys = (zap_phys_t *) zap_scratch;
1517 
1518 	/*
1519 	 * This assumes that the leaf blocks start at block 1. The
1520 	 * documentation isn't exactly clear on this.
1521 	 */
1522 	zap_leaf_t zl;
1523 	zl.l_bs = z.zap_block_shift;
1524 	for (i = 0; i < zh.zap_num_leafs; i++) {
1525 		off_t off = (i + 1) << zl.l_bs;
1526 		char name[256], *p;
1527 		uint64_t value;
1528 
1529 		if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1530 			return (EIO);
1531 
1532 		zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1533 
1534 		for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1535 			zap_leaf_chunk_t *zc, *nc;
1536 			int namelen;
1537 
1538 			zc = &ZAP_LEAF_CHUNK(&zl, j);
1539 			if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1540 				continue;
1541 			namelen = zc->l_entry.le_name_numints;
1542 			if (namelen > sizeof(name))
1543 				namelen = sizeof(name);
1544 
1545 			/*
1546 			 * Paste the name back together.
1547 			 */
1548 			nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
1549 			p = name;
1550 			while (namelen > 0) {
1551 				int len;
1552 				len = namelen;
1553 				if (len > ZAP_LEAF_ARRAY_BYTES)
1554 					len = ZAP_LEAF_ARRAY_BYTES;
1555 				memcpy(p, nc->l_array.la_array, len);
1556 				p += len;
1557 				namelen -= len;
1558 				nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
1559 			}
1560 
1561 			/*
1562 			 * Assume the first eight bytes of the value are
1563 			 * a uint64_t.
1564 			 */
1565 			value = fzap_leaf_value(&zl, zc);
1566 
1567 			//printf("%s 0x%jx\n", name, (uintmax_t)value);
1568 			callback((const char *)name);
1569 		}
1570 	}
1571 
1572 	return (0);
1573 }
1574 
zfs_printf(const char * name)1575 static int zfs_printf(const char *name)
1576 {
1577 
1578 	printf("%s\n", name);
1579 
1580 	return (0);
1581 }
1582 
1583 /*
1584  * List a zap directory.
1585  */
1586 static int
zap_list(const spa_t * spa,const dnode_phys_t * dnode)1587 zap_list(const spa_t *spa, const dnode_phys_t *dnode)
1588 {
1589 	uint64_t zap_type;
1590 	size_t size = dnode->dn_datablkszsec * 512;
1591 
1592 	if (dnode_read(spa, dnode, 0, zap_scratch, size))
1593 		return (EIO);
1594 
1595 	zap_type = *(uint64_t *) zap_scratch;
1596 	if (zap_type == ZBT_MICRO)
1597 		return mzap_list(dnode, zfs_printf);
1598 	else
1599 		return fzap_list(spa, dnode, zfs_printf);
1600 }
1601 
1602 static int
objset_get_dnode(const spa_t * spa,const objset_phys_t * os,uint64_t objnum,dnode_phys_t * dnode)1603 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode)
1604 {
1605 	off_t offset;
1606 
1607 	offset = objnum * sizeof(dnode_phys_t);
1608 	return dnode_read(spa, &os->os_meta_dnode, offset,
1609 		dnode, sizeof(dnode_phys_t));
1610 }
1611 
1612 static int
mzap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,char * name,uint64_t value)1613 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1614 {
1615 	const mzap_phys_t *mz;
1616 	const mzap_ent_phys_t *mze;
1617 	size_t size;
1618 	int chunks, i;
1619 
1620 	/*
1621 	 * Microzap objects use exactly one block. Read the whole
1622 	 * thing.
1623 	 */
1624 	size = dnode->dn_datablkszsec * 512;
1625 
1626 	mz = (const mzap_phys_t *) zap_scratch;
1627 	chunks = size / MZAP_ENT_LEN - 1;
1628 
1629 	for (i = 0; i < chunks; i++) {
1630 		mze = &mz->mz_chunk[i];
1631 		if (value == mze->mze_value) {
1632 			strcpy(name, mze->mze_name);
1633 			return (0);
1634 		}
1635 	}
1636 
1637 	return (ENOENT);
1638 }
1639 
1640 static void
fzap_name_copy(const zap_leaf_t * zl,const zap_leaf_chunk_t * zc,char * name)1641 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name)
1642 {
1643 	size_t namelen;
1644 	const zap_leaf_chunk_t *nc;
1645 	char *p;
1646 
1647 	namelen = zc->l_entry.le_name_numints;
1648 
1649 	nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk);
1650 	p = name;
1651 	while (namelen > 0) {
1652 		size_t len;
1653 		len = namelen;
1654 		if (len > ZAP_LEAF_ARRAY_BYTES)
1655 			len = ZAP_LEAF_ARRAY_BYTES;
1656 		memcpy(p, nc->l_array.la_array, len);
1657 		p += len;
1658 		namelen -= len;
1659 		nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next);
1660 	}
1661 
1662 	*p = '\0';
1663 }
1664 
1665 static int
fzap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,char * name,uint64_t value)1666 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1667 {
1668 	int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1669 	zap_phys_t zh = *(zap_phys_t *) zap_scratch;
1670 	fat_zap_t z;
1671 	int i, j;
1672 
1673 	if (zh.zap_magic != ZAP_MAGIC)
1674 		return (EIO);
1675 
1676 	z.zap_block_shift = ilog2(bsize);
1677 	z.zap_phys = (zap_phys_t *) zap_scratch;
1678 
1679 	/*
1680 	 * This assumes that the leaf blocks start at block 1. The
1681 	 * documentation isn't exactly clear on this.
1682 	 */
1683 	zap_leaf_t zl;
1684 	zl.l_bs = z.zap_block_shift;
1685 	for (i = 0; i < zh.zap_num_leafs; i++) {
1686 		off_t off = (i + 1) << zl.l_bs;
1687 
1688 		if (dnode_read(spa, dnode, off, zap_scratch, bsize))
1689 			return (EIO);
1690 
1691 		zl.l_phys = (zap_leaf_phys_t *) zap_scratch;
1692 
1693 		for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) {
1694 			zap_leaf_chunk_t *zc;
1695 
1696 			zc = &ZAP_LEAF_CHUNK(&zl, j);
1697 			if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
1698 				continue;
1699 			if (zc->l_entry.le_value_intlen != 8 ||
1700 			    zc->l_entry.le_value_numints != 1)
1701 				continue;
1702 
1703 			if (fzap_leaf_value(&zl, zc) == value) {
1704 				fzap_name_copy(&zl, zc, name);
1705 				return (0);
1706 			}
1707 		}
1708 	}
1709 
1710 	return (ENOENT);
1711 }
1712 
1713 static int
zap_rlookup(const spa_t * spa,const dnode_phys_t * dnode,char * name,uint64_t value)1714 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value)
1715 {
1716 	int rc;
1717 	uint64_t zap_type;
1718 	size_t size = dnode->dn_datablkszsec * 512;
1719 
1720 	rc = dnode_read(spa, dnode, 0, zap_scratch, size);
1721 	if (rc)
1722 		return (rc);
1723 
1724 	zap_type = *(uint64_t *) zap_scratch;
1725 	if (zap_type == ZBT_MICRO)
1726 		return mzap_rlookup(spa, dnode, name, value);
1727 	else
1728 		return fzap_rlookup(spa, dnode, name, value);
1729 }
1730 
1731 static int
zfs_rlookup(const spa_t * spa,uint64_t objnum,char * result)1732 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result)
1733 {
1734 	char name[256];
1735 	char component[256];
1736 	uint64_t dir_obj, parent_obj, child_dir_zapobj;
1737 	dnode_phys_t child_dir_zap, dataset, dir, parent;
1738 	dsl_dir_phys_t *dd;
1739 	dsl_dataset_phys_t *ds;
1740 	char *p;
1741 	int len;
1742 
1743 	p = &name[sizeof(name) - 1];
1744 	*p = '\0';
1745 
1746 	if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1747 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1748 		return (EIO);
1749 	}
1750 	ds = (dsl_dataset_phys_t *)&dataset.dn_bonus;
1751 	dir_obj = ds->ds_dir_obj;
1752 
1753 	for (;;) {
1754 		if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0)
1755 			return (EIO);
1756 		dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1757 
1758 		/* Actual loop condition. */
1759 		parent_obj  = dd->dd_parent_obj;
1760 		if (parent_obj == 0)
1761 			break;
1762 
1763 		if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0)
1764 			return (EIO);
1765 		dd = (dsl_dir_phys_t *)&parent.dn_bonus;
1766 		child_dir_zapobj = dd->dd_child_dir_zapobj;
1767 		if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1768 			return (EIO);
1769 		if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0)
1770 			return (EIO);
1771 
1772 		len = strlen(component);
1773 		p -= len;
1774 		memcpy(p, component, len);
1775 		--p;
1776 		*p = '/';
1777 
1778 		/* Actual loop iteration. */
1779 		dir_obj = parent_obj;
1780 	}
1781 
1782 	if (*p != '\0')
1783 		++p;
1784 	strcpy(result, p);
1785 
1786 	return (0);
1787 }
1788 
1789 static int
zfs_lookup_dataset(const spa_t * spa,const char * name,uint64_t * objnum)1790 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum)
1791 {
1792 	char element[256];
1793 	uint64_t dir_obj, child_dir_zapobj;
1794 	dnode_phys_t child_dir_zap, dir;
1795 	dsl_dir_phys_t *dd;
1796 	const char *p, *q;
1797 
1798 	if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir))
1799 		return (EIO);
1800 	if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &dir_obj))
1801 		return (EIO);
1802 
1803 	p = name;
1804 	for (;;) {
1805 		if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir))
1806 			return (EIO);
1807 		dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1808 
1809 		while (*p == '/')
1810 			p++;
1811 		/* Actual loop condition #1. */
1812 		if (*p == '\0')
1813 			break;
1814 
1815 		q = strchr(p, '/');
1816 		if (q) {
1817 			memcpy(element, p, q - p);
1818 			element[q - p] = '\0';
1819 			p = q + 1;
1820 		} else {
1821 			strcpy(element, p);
1822 			p += strlen(p);
1823 		}
1824 
1825 		child_dir_zapobj = dd->dd_child_dir_zapobj;
1826 		if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0)
1827 			return (EIO);
1828 
1829 		/* Actual loop condition #2. */
1830 		if (zap_lookup(spa, &child_dir_zap, element, &dir_obj) != 0)
1831 			return (ENOENT);
1832 	}
1833 
1834 	*objnum = dd->dd_head_dataset_obj;
1835 	return (0);
1836 }
1837 
1838 #ifndef BOOT2
1839 static int
zfs_list_dataset(const spa_t * spa,uint64_t objnum)1840 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/)
1841 {
1842 	uint64_t dir_obj, child_dir_zapobj;
1843 	dnode_phys_t child_dir_zap, dir, dataset;
1844 	dsl_dataset_phys_t *ds;
1845 	dsl_dir_phys_t *dd;
1846 
1847 	if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1848 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1849 		return (EIO);
1850 	}
1851 	ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1852 	dir_obj = ds->ds_dir_obj;
1853 
1854 	if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) {
1855 		printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1856 		return (EIO);
1857 	}
1858 	dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1859 
1860 	child_dir_zapobj = dd->dd_child_dir_zapobj;
1861 	if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) {
1862 		printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1863 		return (EIO);
1864 	}
1865 
1866 	return (zap_list(spa, &child_dir_zap) != 0);
1867 }
1868 
1869 int
zfs_callback_dataset(const spa_t * spa,uint64_t objnum,int (* callback)(const char * name))1870 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *name))
1871 {
1872 	uint64_t dir_obj, child_dir_zapobj, zap_type;
1873 	dnode_phys_t child_dir_zap, dir, dataset;
1874 	dsl_dataset_phys_t *ds;
1875 	dsl_dir_phys_t *dd;
1876 	int err;
1877 
1878 	err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset);
1879 	if (err != 0) {
1880 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1881 		return (err);
1882 	}
1883 	ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1884 	dir_obj = ds->ds_dir_obj;
1885 
1886 	err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir);
1887 	if (err != 0) {
1888 		printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj);
1889 		return (err);
1890 	}
1891 	dd = (dsl_dir_phys_t *)&dir.dn_bonus;
1892 
1893 	child_dir_zapobj = dd->dd_child_dir_zapobj;
1894 	err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap);
1895 	if (err != 0) {
1896 		printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj);
1897 		return (err);
1898 	}
1899 
1900 	err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512);
1901 	if (err != 0)
1902 		return (err);
1903 
1904 	zap_type = *(uint64_t *) zap_scratch;
1905 	if (zap_type == ZBT_MICRO)
1906 		return mzap_list(&child_dir_zap, callback);
1907 	else
1908 		return fzap_list(spa, &child_dir_zap, callback);
1909 }
1910 #endif
1911 
1912 /*
1913  * Find the object set given the object number of its dataset object
1914  * and return its details in *objset
1915  */
1916 static int
zfs_mount_dataset(const spa_t * spa,uint64_t objnum,objset_phys_t * objset)1917 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset)
1918 {
1919 	dnode_phys_t dataset;
1920 	dsl_dataset_phys_t *ds;
1921 
1922 	if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) {
1923 		printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum);
1924 		return (EIO);
1925 	}
1926 
1927 	ds = (dsl_dataset_phys_t *) &dataset.dn_bonus;
1928 	if (zio_read(spa, &ds->ds_bp, objset)) {
1929 		printf("ZFS: can't read object set for dataset %ju\n",
1930 		    (uintmax_t)objnum);
1931 		return (EIO);
1932 	}
1933 
1934 	return (0);
1935 }
1936 
1937 /*
1938  * Find the object set pointed to by the BOOTFS property or the root
1939  * dataset if there is none and return its details in *objset
1940  */
1941 static int
zfs_get_root(const spa_t * spa,uint64_t * objid)1942 zfs_get_root(const spa_t *spa, uint64_t *objid)
1943 {
1944 	dnode_phys_t dir, propdir;
1945 	uint64_t props, bootfs, root;
1946 
1947 	*objid = 0;
1948 
1949 	/*
1950 	 * Start with the MOS directory object.
1951 	 */
1952 	if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) {
1953 		printf("ZFS: can't read MOS object directory\n");
1954 		return (EIO);
1955 	}
1956 
1957 	/*
1958 	 * Lookup the pool_props and see if we can find a bootfs.
1959 	 */
1960 	if (zap_lookup(spa, &dir, DMU_POOL_PROPS, &props) == 0
1961 	     && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0
1962 	     && zap_lookup(spa, &propdir, "bootfs", &bootfs) == 0
1963 	     && bootfs != 0)
1964 	{
1965 		*objid = bootfs;
1966 		return (0);
1967 	}
1968 	/*
1969 	 * Lookup the root dataset directory
1970 	 */
1971 	if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, &root)
1972 	    || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) {
1973 		printf("ZFS: can't find root dsl_dir\n");
1974 		return (EIO);
1975 	}
1976 
1977 	/*
1978 	 * Use the information from the dataset directory's bonus buffer
1979 	 * to find the dataset object and from that the object set itself.
1980 	 */
1981 	dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus;
1982 	*objid = dd->dd_head_dataset_obj;
1983 	return (0);
1984 }
1985 
1986 static int
zfs_mount(const spa_t * spa,uint64_t rootobj,struct zfsmount * mount)1987 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount)
1988 {
1989 
1990 	mount->spa = spa;
1991 
1992 	/*
1993 	 * Find the root object set if not explicitly provided
1994 	 */
1995 	if (rootobj == 0 && zfs_get_root(spa, &rootobj)) {
1996 		printf("ZFS: can't find root filesystem\n");
1997 		return (EIO);
1998 	}
1999 
2000 	if (zfs_mount_dataset(spa, rootobj, &mount->objset)) {
2001 		printf("ZFS: can't open root filesystem\n");
2002 		return (EIO);
2003 	}
2004 
2005 	mount->rootobj = rootobj;
2006 
2007 	return (0);
2008 }
2009 
2010 static int
zfs_spa_init(spa_t * spa)2011 zfs_spa_init(spa_t *spa)
2012 {
2013 
2014 	if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) {
2015 		printf("ZFS: can't read MOS of pool %s\n", spa->spa_name);
2016 		return (EIO);
2017 	}
2018 	if (spa->spa_mos.os_type != DMU_OST_META) {
2019 		printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name);
2020 		return (EIO);
2021 	}
2022 	return (0);
2023 }
2024 
2025 static int
zfs_dnode_stat(const spa_t * spa,dnode_phys_t * dn,struct stat * sb)2026 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb)
2027 {
2028 
2029 	if (dn->dn_bonustype != DMU_OT_SA) {
2030 		znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus;
2031 
2032 		sb->st_mode = zp->zp_mode;
2033 		sb->st_uid = zp->zp_uid;
2034 		sb->st_gid = zp->zp_gid;
2035 		sb->st_size = zp->zp_size;
2036 	} else {
2037 		sa_hdr_phys_t *sahdrp;
2038 		int hdrsize;
2039 		size_t size = 0;
2040 		void *buf = NULL;
2041 
2042 		if (dn->dn_bonuslen != 0)
2043 			sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn);
2044 		else {
2045 			if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) {
2046 				blkptr_t *bp = &dn->dn_spill;
2047 				int error;
2048 
2049 				size = BP_GET_LSIZE(bp);
2050 				buf = zfs_alloc(size);
2051 				error = zio_read(spa, bp, buf);
2052 				if (error != 0) {
2053 					zfs_free(buf, size);
2054 					return (error);
2055 				}
2056 				sahdrp = buf;
2057 			} else {
2058 				return (EIO);
2059 			}
2060 		}
2061 		hdrsize = SA_HDR_SIZE(sahdrp);
2062 		sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize +
2063 		    SA_MODE_OFFSET);
2064 		sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize +
2065 		    SA_UID_OFFSET);
2066 		sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize +
2067 		    SA_GID_OFFSET);
2068 		sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize +
2069 		    SA_SIZE_OFFSET);
2070 		if (buf != NULL)
2071 			zfs_free(buf, size);
2072 	}
2073 
2074 	return (0);
2075 }
2076 
2077 /*
2078  * Lookup a file and return its dnode.
2079  */
2080 static int
zfs_lookup(const struct zfsmount * mount,const char * upath,dnode_phys_t * dnode)2081 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode)
2082 {
2083 	int rc;
2084 	uint64_t objnum, rootnum, parentnum;
2085 	const spa_t *spa;
2086 	dnode_phys_t dn;
2087 	const char *p, *q;
2088 	char element[256];
2089 	char path[1024];
2090 	int symlinks_followed = 0;
2091 	struct stat sb;
2092 
2093 	spa = mount->spa;
2094 	if (mount->objset.os_type != DMU_OST_ZFS) {
2095 		printf("ZFS: unexpected object set type %ju\n",
2096 		    (uintmax_t)mount->objset.os_type);
2097 		return (EIO);
2098 	}
2099 
2100 	/*
2101 	 * Get the root directory dnode.
2102 	 */
2103 	rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn);
2104 	if (rc)
2105 		return (rc);
2106 
2107 	rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, &rootnum);
2108 	if (rc)
2109 		return (rc);
2110 
2111 	rc = objset_get_dnode(spa, &mount->objset, rootnum, &dn);
2112 	if (rc)
2113 		return (rc);
2114 
2115 	objnum = rootnum;
2116 	p = upath;
2117 	while (p && *p) {
2118 		while (*p == '/')
2119 			p++;
2120 		if (!*p)
2121 			break;
2122 		q = strchr(p, '/');
2123 		if (q) {
2124 			memcpy(element, p, q - p);
2125 			element[q - p] = 0;
2126 			p = q;
2127 		} else {
2128 			strcpy(element, p);
2129 			p = 0;
2130 		}
2131 
2132 		rc = zfs_dnode_stat(spa, &dn, &sb);
2133 		if (rc)
2134 			return (rc);
2135 		if (!S_ISDIR(sb.st_mode))
2136 			return (ENOTDIR);
2137 
2138 		parentnum = objnum;
2139 		rc = zap_lookup(spa, &dn, element, &objnum);
2140 		if (rc)
2141 			return (rc);
2142 		objnum = ZFS_DIRENT_OBJ(objnum);
2143 
2144 		rc = objset_get_dnode(spa, &mount->objset, objnum, &dn);
2145 		if (rc)
2146 			return (rc);
2147 
2148 		/*
2149 		 * Check for symlink.
2150 		 */
2151 		rc = zfs_dnode_stat(spa, &dn, &sb);
2152 		if (rc)
2153 			return (rc);
2154 		if (S_ISLNK(sb.st_mode)) {
2155 			if (symlinks_followed > 10)
2156 				return (EMLINK);
2157 			symlinks_followed++;
2158 
2159 			/*
2160 			 * Read the link value and copy the tail of our
2161 			 * current path onto the end.
2162 			 */
2163 			if (p)
2164 				strcpy(&path[sb.st_size], p);
2165 			else
2166 				path[sb.st_size] = 0;
2167 			/*
2168 			 * Second test is purely to silence bogus compiler
2169 			 * warning about accessing past the end of dn_bonus.
2170 			 */
2171 			if (sb.st_size + sizeof(znode_phys_t) <=
2172 			    dn.dn_bonuslen && sizeof(znode_phys_t) <=
2173 			    sizeof(dn.dn_bonus)) {
2174 				memcpy(path, &dn.dn_bonus[sizeof(znode_phys_t)],
2175 					sb.st_size);
2176 			} else {
2177 				rc = dnode_read(spa, &dn, 0, path, sb.st_size);
2178 				if (rc)
2179 					return (rc);
2180 			}
2181 
2182 			/*
2183 			 * Restart with the new path, starting either at
2184 			 * the root or at the parent depending whether or
2185 			 * not the link is relative.
2186 			 */
2187 			p = path;
2188 			if (*p == '/')
2189 				objnum = rootnum;
2190 			else
2191 				objnum = parentnum;
2192 			objset_get_dnode(spa, &mount->objset, objnum, &dn);
2193 		}
2194 	}
2195 
2196 	*dnode = dn;
2197 	return (0);
2198 }
2199