xref: /freebsd/usr.sbin/fstyp/zfs.c (revision 298022457a9a016cbdda4e22d751abb5cd91c919)
1 /*-
2  * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/types.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <libnvpair.h>
41 #include <sys/vdev_impl.h>
42 
43 #include "fstyp.h"
44 
45 int
46 fstyp_zfs(FILE *fp, char *label, size_t labelsize)
47 {
48 	vdev_label_t *zpool_ptr = NULL;
49 	vdev_label_t zpool_label;
50 	char *buf = zpool_label.vl_vdev_phys.vp_nvlist;
51 	char *zpool_name = NULL;
52 	size_t buflen = sizeof (zpool_label.vl_vdev_phys.vp_nvlist);
53 	nvlist_t *config = NULL;
54 
55 	zpool_ptr = (vdev_label_t *)read_buf(fp, 0, sizeof(zpool_label));
56 	if (zpool_ptr == NULL)
57 		return (1);
58 	zpool_label = *zpool_ptr;
59 	if (nvlist_unpack(buf, buflen, &config, 0) != 0)
60 		goto zfserr;
61 	if (nvlist_lookup_string(config, "name", &zpool_name) != 0)
62 		goto zfserr;
63 	strlcpy(label, zpool_name, labelsize);
64 	nvlist_free(config);
65 	free(zpool_ptr);
66 	return (0);
67 
68 zfserr:
69 	nvlist_free(config);
70 	free(zpool_ptr);
71 
72 	return (1);
73 }
74