xref: /illumos-gate/usr/src/cmd/fs.d/zfs/fstyp/fstyp.c (revision 9b664393d4fdda96221e6ea9ea95790d3c15be70)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2020 Joyent, Inc.
26  */
27 
28 /*
29  * libfstyp module for zfs
30  */
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/debug.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <libintl.h>
38 #include <locale.h>
39 #include <string.h>
40 #include <libnvpair.h>
41 #include <libzfs.h>
42 #include <libzutil.h>
43 #include <libfstyp_module.h>
44 #include <errno.h>
45 
46 struct fstyp_zfs {
47 	int		fd;
48 	nvlist_t	*config;
49 };
50 
51 int	fstyp_mod_init(int fd, off_t offset, fstyp_mod_handle_t *handle);
52 void	fstyp_mod_fini(fstyp_mod_handle_t handle);
53 int	fstyp_mod_ident(fstyp_mod_handle_t handle);
54 int	fstyp_mod_get_attr(fstyp_mod_handle_t handle, nvlist_t **attrp);
55 
56 int
57 fstyp_mod_init(int fd, off_t offset, fstyp_mod_handle_t *handle)
58 {
59 	struct fstyp_zfs *h;
60 
61 	if (offset != 0) {
62 		return (FSTYP_ERR_OFFSET);
63 	}
64 
65 	if ((h = calloc(1, sizeof (struct fstyp_zfs))) == NULL) {
66 		return (FSTYP_ERR_NOMEM);
67 	}
68 	h->fd = fd;
69 
70 	*handle = (fstyp_mod_handle_t)h;
71 	return (0);
72 }
73 
74 void
75 fstyp_mod_fini(fstyp_mod_handle_t handle)
76 {
77 	struct fstyp_zfs *h = (struct fstyp_zfs *)handle;
78 
79 	if (h->config != NULL) {
80 		nvlist_free(h->config);
81 	}
82 	free(h);
83 }
84 
85 int
86 fstyp_mod_ident(fstyp_mod_handle_t handle)
87 {
88 	struct fstyp_zfs *h = (struct fstyp_zfs *)handle;
89 	uint64_t state;
90 	char	*str;
91 	uint64_t u64;
92 	char	buf[64];
93 	int	num_labels = 0;
94 
95 	if (zpool_read_label(h->fd, &h->config, &num_labels) != 0) {
96 		/* This is the only reason zpool_read_label() can fail */
97 		VERIFY3S(errno, ==, ENOMEM);
98 		return (FSTYP_ERR_NOMEM);
99 	}
100 
101 	if (num_labels == 0)
102 		return (FSTYP_ERR_NO_MATCH);
103 
104 	if (nvlist_lookup_uint64(h->config, ZPOOL_CONFIG_POOL_STATE,
105 	    &state) != 0 || state == POOL_STATE_DESTROYED) {
106 		nvlist_free(h->config);
107 		h->config = NULL;
108 		return (FSTYP_ERR_NO_MATCH);
109 	}
110 
111 	/* add generic attributes */
112 	(void) nvlist_add_boolean_value(h->config, "gen_clean", B_TRUE);
113 	if (nvlist_lookup_uint64(h->config, "guid", &u64) == 0) {
114 		(void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)u64);
115 		(void) nvlist_add_string(h->config, "gen_guid", buf);
116 	}
117 	if (nvlist_lookup_uint64(h->config, "version", &u64) == 0) {
118 		(void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)u64);
119 		(void) nvlist_add_string(h->config, "gen_version", buf);
120 	}
121 	if (nvlist_lookup_string(h->config, "name", &str) == 0) {
122 		(void) nvlist_add_string(h->config, "gen_volume_label", str);
123 	}
124 
125 	return (0);
126 }
127 
128 int
129 fstyp_mod_get_attr(fstyp_mod_handle_t handle, nvlist_t **attrp)
130 {
131 	struct fstyp_zfs *h = (struct fstyp_zfs *)handle;
132 
133 	*attrp = h->config;
134 	return (0);
135 }
136