xref: /illumos-gate/usr/src/cmd/fs.d/zfs/fstyp/fstyp.c (revision 97b5374547d500fded52d886ceba8a9962af0527)
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/types.h>
35 #include <unistd.h>
36 #include <libintl.h>
37 #include <locale.h>
38 #include <string.h>
39 #include <libnvpair.h>
40 #include <libzfs.h>
41 #include <libzutil.h>
42 #include <libfstyp_module.h>
43 #include <errno.h>
44 
45 struct fstyp_zfs {
46 	int		fd;
47 	nvlist_t	*config;
48 };
49 
50 int	fstyp_mod_init(int fd, off_t offset, fstyp_mod_handle_t *handle);
51 void	fstyp_mod_fini(fstyp_mod_handle_t handle);
52 int	fstyp_mod_ident(fstyp_mod_handle_t handle);
53 int	fstyp_mod_get_attr(fstyp_mod_handle_t handle, nvlist_t **attrp);
54 
55 int
56 fstyp_mod_init(int fd, off_t offset, fstyp_mod_handle_t *handle)
57 {
58 	struct fstyp_zfs *h;
59 
60 	if (offset != 0) {
61 		return (FSTYP_ERR_OFFSET);
62 	}
63 
64 	if ((h = calloc(1, sizeof (struct fstyp_zfs))) == NULL) {
65 		return (FSTYP_ERR_NOMEM);
66 	}
67 	h->fd = fd;
68 
69 	*handle = (fstyp_mod_handle_t)h;
70 	return (0);
71 }
72 
73 void
74 fstyp_mod_fini(fstyp_mod_handle_t handle)
75 {
76 	struct fstyp_zfs *h = (struct fstyp_zfs *)handle;
77 
78 	if (h->config != NULL) {
79 		nvlist_free(h->config);
80 	}
81 	free(h);
82 }
83 
84 int
85 fstyp_mod_ident(fstyp_mod_handle_t handle)
86 {
87 	struct fstyp_zfs *h = (struct fstyp_zfs *)handle;
88 	uint64_t state;
89 	char	*str;
90 	uint64_t u64;
91 	char	buf[64];
92 
93 	if (zpool_read_label(h->fd, &h->config, NULL) != 0) {
94 		return (FSTYP_ERR_NO_MATCH);
95 	}
96 
97 	if (nvlist_lookup_uint64(h->config, ZPOOL_CONFIG_POOL_STATE,
98 	    &state) != 0 || state == POOL_STATE_DESTROYED) {
99 		nvlist_free(h->config);
100 		h->config = NULL;
101 		return (FSTYP_ERR_NO_MATCH);
102 	}
103 
104 	/* add generic attributes */
105 	(void) nvlist_add_boolean_value(h->config, "gen_clean", B_TRUE);
106 	if (nvlist_lookup_uint64(h->config, "guid", &u64) == 0) {
107 		(void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)u64);
108 		(void) nvlist_add_string(h->config, "gen_guid", buf);
109 	}
110 	if (nvlist_lookup_uint64(h->config, "version", &u64) == 0) {
111 		(void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)u64);
112 		(void) nvlist_add_string(h->config, "gen_version", buf);
113 	}
114 	if (nvlist_lookup_string(h->config, "name", &str) == 0) {
115 		(void) nvlist_add_string(h->config, "gen_volume_label", str);
116 	}
117 
118 	return (0);
119 }
120 
121 int
122 fstyp_mod_get_attr(fstyp_mod_handle_t handle, nvlist_t **attrp)
123 {
124 	struct fstyp_zfs *h = (struct fstyp_zfs *)handle;
125 
126 	*attrp = h->config;
127 	return (0);
128 }
129