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