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