1279377Simp/*-
2279377Simp * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org>
3279377Simp * Copyright (c) 2015 Xin LI <delphij@FreeBSD.org>
4279377Simp * All rights reserved.
5279377Simp *
6279377Simp * Redistribution and use in source and binary forms, with or without
7279377Simp * modification, are permitted provided that the following conditions
8279377Simp * are met:
9279377Simp * 1. Redistributions of source code must retain the above copyright
10279377Simp *    notice, this list of conditions and the following disclaimer.
11279377Simp * 2. Redistributions in binary form must reproduce the above copyright
12279377Simp *    notice, this list of conditions and the following disclaimer in the
13279377Simp *    documentation and/or other materials provided with the distribution.
14279377Simp *
15279377Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16279377Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17295436Sandrew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18279377Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19279377Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20279377Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21279377Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22279377Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23279377Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24279377Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25279377Simp * SUCH DAMAGE.
26279377Simp */
27279377Simp
28279377Simp#include <sys/cdefs.h>
29279377Simp__FBSDID("$FreeBSD: releng/11.0/usr.sbin/fstyp/zfs.c 292757 2015-12-26 19:48:36Z allanjude $");
30279377Simp
31279377Simp#include <sys/types.h>
32279377Simp#include <cddl/compat/opensolaris/sys/types.h>
33279377Simp#include <sys/time.h>
34279377Simp#include <cddl/compat/opensolaris/sys/time.h>
35295436Sandrew#include <stdint.h>
36295436Sandrew#include <stdio.h>
37295436Sandrew#include <stdlib.h>
38295436Sandrew#include <string.h>
39295436Sandrew#include <unistd.h>
40295436Sandrew
41295436Sandrew#include <libnvpair.h>
42295436Sandrew#include <sys/vdev_impl.h>
43279377Simp
44279377Simp#include "fstyp.h"
45279377Simp
46279377Simpint
47279377Simpfstyp_zfs(FILE *fp, char *label, size_t labelsize)
48279377Simp{
49279377Simp	vdev_label_t *vdev_label = NULL;
50279377Simp	vdev_phys_t *vdev_phys;
51279377Simp	char *zpool_name = NULL;
52279377Simp	nvlist_t *config = NULL;
53279377Simp	int err = 0;
54279377Simp
55279377Simp	/*
56279377Simp	 * Read in the first ZFS vdev label ("L0"), located at the beginning
57279377Simp	 * of the vdev and extract the pool name from it.
58279377Simp	 *
59279377Simp	 * TODO: the checksum of label should be validated.
60279377Simp	 */
61279377Simp	vdev_label = (vdev_label_t *)read_buf(fp, 0, sizeof(*vdev_label));
62279377Simp	if (vdev_label == NULL)
63279377Simp		return (1);
64279377Simp
65279377Simp	vdev_phys = &(vdev_label->vl_vdev_phys);
66279377Simp
67279377Simp	if ((nvlist_unpack(vdev_phys->vp_nvlist, sizeof(vdev_phys->vp_nvlist),
68279377Simp	    &config, 0)) == 0 &&
69279377Simp	    (nvlist_lookup_string(config, "name", &zpool_name) == 0)) {
70295436Sandrew		strlcpy(label, zpool_name, labelsize);
71295436Sandrew	} else
72295436Sandrew		err = 1;
73295436Sandrew
74295436Sandrew	nvlist_free(config);
75295436Sandrew	free(vdev_label);
76295436Sandrew
77295436Sandrew	return (err);
78295436Sandrew}
79295436Sandrew