xref: /netbsd-src/usr.sbin/fstyp/zfs.c (revision b985414b8f8688f3bfa6718c583260b26b0bf1d1)
1 /*	$NetBSD: zfs.c,v 1.1 2018/01/09 03:31:15 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2017 The NetBSD Foundation, Inc.
5  * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
6  * Copyright (c) 2015 Xin LI <delphij@FreeBSD.org>
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: zfs.c,v 1.1 2018/01/09 03:31:15 christos Exp $");
35 
36 #include <sys/types.h>
37 #include <cddl/osnet/sys/sys/types.h>
38 #include <sys/time.h>
39 #include <cddl/osnet/sys/sys/time.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include <libnvpair.h>
47 #include <sys/vdev_impl.h>
48 
49 #include "fstyp.h"
50 
51 int
fstyp_zfs(FILE * fp,char * label,size_t labelsize)52 fstyp_zfs(FILE *fp, char *label, size_t labelsize)
53 {
54 	vdev_label_t *vdev_label = NULL;
55 	vdev_phys_t *vdev_phys;
56 	char *zpool_name = NULL;
57 	nvlist_t *config = NULL;
58 	int err = 0;
59 
60 	/*
61 	 * Read in the first ZFS vdev label ("L0"), located at the beginning
62 	 * of the vdev and extract the pool name from it.
63 	 *
64 	 * TODO: the checksum of label should be validated.
65 	 */
66 	vdev_label = (vdev_label_t *)read_buf(fp, 0, sizeof(*vdev_label));
67 	if (vdev_label == NULL)
68 		return 1;
69 
70 	vdev_phys = &(vdev_label->vl_vdev_phys);
71 
72 	if ((nvlist_unpack(vdev_phys->vp_nvlist, sizeof(vdev_phys->vp_nvlist),
73 	    &config, 0)) == 0 &&
74 	    (nvlist_lookup_string(config, "name", &zpool_name) == 0)) {
75 		strlcpy(label, zpool_name, labelsize);
76 	} else
77 		err = 1;
78 
79 	nvlist_free(config);
80 	free(vdev_label);
81 
82 	return err;
83 }
84